-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmobius_dll.h
1318 lines (960 loc) · 33.1 KB
/
mobius_dll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <sstream>
std::stringstream Dll_GlobalErrstream;
std::stringstream Dll_GlobalWarningStream;
void
ErrorPrint() {}
template<typename t, typename... v>
void
ErrorPrint(t Value, v... Tail)
{
Dll_GlobalErrstream << Value;
ErrorPrint(Tail...);
}
template<typename... v>
void
FatalError(v... Tail)
{
ErrorPrint(Tail...);
throw 1;
}
void
WarningPrint() {}
template<typename t, typename... v>
void
WarningPrint(t Value, v... Tail)
{
Dll_GlobalWarningStream << Value;
WarningPrint(Tail...);
}
#define MOBIUS_ERROR_OVERRIDE
#include "mobius.h"
#define CHECK_ERROR_BEGIN \
try {
#define CHECK_ERROR_END \
} catch(int Errcode) { \
}
#if (defined(_WIN32) || defined(_WIN64))
#define DLLEXPORT extern "C" __declspec(dllexport)
#elif (defined(__unix__) || defined(__linux__) || defined(__unix) || defined(unix))
#define DLLEXPORT extern "C" __attribute((visibility("default")))
#endif
DLLEXPORT int
DllEncounteredError(char *ErrmsgOut, u64 BufferLen)
{
Dll_GlobalErrstream.getline(ErrmsgOut, BufferLen, 0);
Dll_GlobalErrstream.clear(); // If the stream is shorter than the BufferLen, the eofbit flag is set. We have to clear it here to be able to reuse the buffer
return strlen(ErrmsgOut);
}
DLLEXPORT int
DllEncounteredWarning(char *WarningmsgOut, u64 BufferLen)
{
Dll_GlobalWarningStream.getline(WarningmsgOut, BufferLen, 0);
Dll_GlobalWarningStream.clear(); // If the stream is shorter than the BufferLen, the eofbit flag is set. We have to clear it here to be able to reuse the buffer
return strlen(WarningmsgOut);
}
//This one has to be provided in each separate application
mobius_model *
DllBuildModel();
DLLEXPORT void *
DllSetupModel(char *ParameterFilename, char *InputFilename)
{
CHECK_ERROR_BEGIN
mobius_model *Model = DllBuildModel();
ReadInputDependenciesFromFile(Model, InputFilename);
EndModelDefinition(Model);
mobius_data_set *DataSet = GenerateDataSet(Model);
ReadParametersFromFile(DataSet, ParameterFilename);
ReadInputsFromFile(DataSet, InputFilename);
//NOTE: This makes some functionality in MobiView more convenient. Without this it can't read the storage structure of the results before the model is run.
SetupResultStorageStructure(DataSet);
return (void *)DataSet;
CHECK_ERROR_END
return nullptr;
}
DLLEXPORT void *
DllSetupModelBlankIndexSets(char *InputFilename)
{
CHECK_ERROR_BEGIN
mobius_model *Model = DllBuildModel();
ReadInputDependenciesFromFile(Model, InputFilename);
EndModelDefinition(Model);
mobius_data_set *DataSet = GenerateDataSet(Model);
return (void *)DataSet;
CHECK_ERROR_END
return nullptr;
}
DLLEXPORT void *
DllSetupModelBlank(char **InputNames, u64 NInputs, char ***InputIndexSets, u64 *IndexSetCounts)
{
CHECK_ERROR_BEGIN
mobius_model *Model = DllBuildModel();
for(int In = 0; In < NInputs; ++In)
{
const char *Name = InputNames[In];
if(!Model->Inputs.Has(Name))
RegisterInput(Model, Name, {}, true, true);
if(IndexSetCounts && InputIndexSets)
{
input_h Input = GetInputHandle(Model, Name);
for(int Ix = 0; Ix < IndexSetCounts[In]; ++Ix)
{
const char *Idx = InputIndexSets[In][Ix];
index_set_h IndexSet = GetIndexSetHandle(Model, Idx);
AddInputIndexSetDependency(Model, Input, IndexSet);
}
}
}
EndModelDefinition(Model);
mobius_data_set *DataSet = GenerateDataSet(Model);
return (void *)DataSet;
CHECK_ERROR_END
return nullptr;
}
DLLEXPORT void
DllSetInputSpan(void *DataSetPtr, char *StartDate, char *EndDate)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
if(DataSet->InputData)
FatalError("Tried to set date range for inputs after input data was allocated.\n");
bool Success;
datetime Start(StartDate, &Success);
if(!Success)
FatalError("The input start date \"", StartDate, "\" is not on the right format.\n");
datetime End(EndDate, &Success);
if(!Success)
FatalError("The input end date \"", EndDate, "\" is not on the right format.\n");
DataSet->InputDataStartDate = Start;
DataSet->InputDataHasSeparateStartDate = true;
//TODO: This is a bit of code duplication from mobius_io.h
s64 Step = FindTimestep(DataSet->InputDataStartDate, End, DataSet->Model->TimestepSize);
Step += 1; //NOTE: Because the end date is inclusive.
if(Step <= 0)
FatalError("The input data end date was set to be earlier than the input data start date.\n");
AllocateInputStorage(DataSet, (u64)Step);
CHECK_ERROR_END
}
DLLEXPORT void
DllReadInputs(void *DataSetPtr, char *InputFilename)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
ReadInputsFromFile(DataSet, InputFilename);
CHECK_ERROR_END
}
DLLEXPORT void
DllReadParameters(void *DataSetPtr, char *ParameterFilename, bool IgnoreUnknown)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
ReadParametersFromFile(DataSet, ParameterFilename, IgnoreUnknown);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetIndexes(void *DataSetPtr, char *IndexSetName, u64 IndexCount, char **IndexNames)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
std::vector<token_string> IndexNames2;
IndexNames2.reserve(IndexCount);
for(size_t Idx = 0; Idx < IndexCount; ++Idx) IndexNames2.push_back(token_string(IndexNames[Idx]));
SetIndexes(DataSet, token_string(IndexSetName), IndexNames2);
if(DataSet->AllIndexesHaveBeenSet)
{
AllocateParameterStorage(DataSet);
}
CHECK_ERROR_END
}
struct dll_branch_index
{
char *IndexName;
u64 BranchCount;
char **BranchNames;
};
DLLEXPORT void
DllSetBranchIndexes(void *DataSetPtr, char *IndexSetName, u64 IndexCount, dll_branch_index *Indexes)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
std::vector<std::pair<token_string, std::vector<token_string>>> Inputs;
Inputs.reserve(IndexCount);
for(size_t Idx = 0; Idx < IndexCount; ++Idx)
{
dll_branch_index &Data = Indexes[Idx];
std::pair<token_string, std::vector<token_string>> Branches;
Branches.first = token_string(Data.IndexName);
Branches.second.reserve(Data.BranchCount);
for(size_t Bch = 0; Bch < Data.BranchCount; ++Bch) Branches.second.push_back(token_string(Data.BranchNames[Bch]));
Inputs.push_back(Branches);
}
SetBranchIndexes(DataSet, token_string(IndexSetName), Inputs);
if(DataSet->AllIndexesHaveBeenSet)
AllocateParameterStorage(DataSet);
CHECK_ERROR_END
}
DLLEXPORT const char *
DllGetModelName(void *DataSetPtr)
{
return ((mobius_data_set *)DataSetPtr)->Model->Name;
}
DLLEXPORT bool
DllRunModel(void *DataSetPtr, s64 MillisecondTimeout)
{
CHECK_ERROR_BEGIN
return RunModel((mobius_data_set *)DataSetPtr, MillisecondTimeout);
CHECK_ERROR_END
return false;
}
DLLEXPORT void *
DllCopyDataSet(void *DataSetPtr, bool CopyResults, bool BorrowInputs)
{
CHECK_ERROR_BEGIN
return (void *)CopyDataSet((mobius_data_set *)DataSetPtr, CopyResults, BorrowInputs);
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllDeleteDataSet(void *DataSetPtr)
{
CHECK_ERROR_BEGIN
delete (mobius_data_set *)DataSetPtr;
CHECK_ERROR_END
}
DLLEXPORT void
DllDeleteModelAndDataSet(void *DataSetPtr)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
mobius_model *Model = (mobius_model *)DataSet->Model;
//NOTE: Unfortunately it seems like the dataset destructor has to look up stuff in the model, so the dataset must always be destroyed first.
delete DataSet;
delete Model;
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetTimesteps(void *DataSetPtr)
{
//This returns the amount of timesteps the last time the model was run.
CHECK_ERROR_BEGIN
return ((mobius_data_set *)DataSetPtr)->TimestepsLastRun;
CHECK_ERROR_END
return 0;
}
DLLEXPORT u64
DllGetNextTimesteps(void *DataSetPtr)
{
//This returns the amount of timesteps set for the next run
CHECK_ERROR_BEGIN
return GetTimesteps((mobius_data_set *)DataSetPtr);
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetStartDate(void *DataSetPtr, char *WriteTo)
{
CHECK_ERROR_BEGIN
//IMPORTANT: This assumes that WriteTo is a char* buffer that is long enough (i.e. at least 30-ish bytes)
//IMPORTANT: This is NOT thread safe since TimeString is not thread safe.
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const char *TimeStr = DataSet->StartDateLastRun.ToString();
strcpy(WriteTo, TimeStr);
CHECK_ERROR_END
}
DLLEXPORT void
DllGetNextStartDate(void *DataSetPtr, char *WriteTo)
{
CHECK_ERROR_BEGIN
//IMPORTANT: This assumes that WriteTo is a char* buffer that is long enough (i.e. at least 30-ish bytes)
//IMPORTANT: This is NOT thread safe since TimeString is not thread safe.
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
datetime StartDate = GetStartDate(DataSet);
const char *TimeStr = StartDate.ToString();
strcpy(WriteTo, TimeStr);
CHECK_ERROR_END
}
DLLEXPORT timestep_size
DllGetTimestepSize(void *DataSetPtr)
{
CHECK_ERROR_BEGIN
return ((mobius_data_set *) DataSetPtr)->Model->TimestepSize;
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetInputTimesteps(void *DataSetPtr)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
return DataSet->InputDataTimesteps;
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetInputStartDate(void *DataSetPtr, char *WriteTo)
{
CHECK_ERROR_BEGIN
//IMPORTANT: This assumes that WriteTo is a char* buffer that is long enough (i.e. at least 30-ish bytes)
//IMPORTANT: This is NOT thread safe since TimeString is not thread safe.
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const char *TimeStr = GetInputStartDate(DataSet).ToString();
strcpy(WriteTo, TimeStr);
CHECK_ERROR_END
}
DLLEXPORT void
DllGetResultSeries(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, double *WriteTo)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
size_t Timesteps = DataSet->TimestepsLastRun;
GetResultSeries(DataSet, Name, IndexNames, (size_t)IndexCount, WriteTo, Timesteps);
CHECK_ERROR_END
}
DLLEXPORT double
DllGetResultInitialValue(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
double Initial;
GetResultSeries(DataSet, Name, IndexNames, (size_t)IndexCount, &Initial, 1, true);
return Initial;
CHECK_ERROR_END
return 0.0;
}
DLLEXPORT void
DllGetInputSeries(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, double *WriteTo, bool AlignWithResults)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
size_t Timesteps = GetTimesteps(DataSet);
if(!AlignWithResults)
Timesteps = DataSet->InputDataTimesteps;
GetInputSeries(DataSet, Name, IndexNames, (size_t)IndexCount, WriteTo, Timesteps, AlignWithResults);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetParameterDouble(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, double Val)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
SetParameterValue(DataSet, Name, IndexNames, (size_t)IndexCount, Val);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetParameterUInt(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, u64 Val)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
SetParameterValue(DataSet, Name, IndexNames, (size_t)IndexCount, Val);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetParameterBool(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, bool Val)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
SetParameterValue(DataSet, Name, IndexNames, (size_t)IndexCount, Val);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetParameterTime(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, char *Val)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
SetParameterValue(DataSet, Name, IndexNames, (size_t)IndexCount, Val);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetParameterEnum(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, char *Val)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
SetParameterValue(DataSet, Name, IndexNames, IndexCount, Val);
CHECK_ERROR_END
}
DLLEXPORT double
DllGetParameterDouble(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount)
{
CHECK_ERROR_BEGIN
return GetParameterValue((mobius_data_set *)DataSetPtr, Name, IndexNames, (size_t)IndexCount, ParameterType_Double).ValDouble;
CHECK_ERROR_END
return 0.0;
}
DLLEXPORT u64
DllGetParameterUInt(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount)
{
CHECK_ERROR_BEGIN
return GetParameterValue((mobius_data_set *)DataSetPtr, Name, IndexNames, (size_t)IndexCount, ParameterType_UInt).ValUInt;
CHECK_ERROR_END
return 0;
}
DLLEXPORT bool
DllGetParameterBool(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount)
{
CHECK_ERROR_BEGIN
return GetParameterValue((mobius_data_set *)DataSetPtr, Name, IndexNames, (size_t)IndexCount, ParameterType_Bool).ValBool;
CHECK_ERROR_END
return false;
}
DLLEXPORT void
DllGetParameterTime(void *DataSetPtr, const char *Name, char **IndexNames, u64 IndexCount, char *WriteTo)
{
//IMPORTANT: This assumes that WriteTo is a char* buffer that is long enough (i.e. at least 30-ish bytes)
//IMPORTANT: This is NOT thread safe since datetime::ToString is not thread safe.
CHECK_ERROR_BEGIN
datetime DateTime = GetParameterValue((mobius_data_set *)DataSetPtr, Name, IndexNames, (size_t)IndexCount, ParameterType_Time).ValTime;
const char *TimeStr = DateTime.ToString();
strcpy(WriteTo, TimeStr);
CHECK_ERROR_END
}
DLLEXPORT const char *
DllGetParameterEnum(void *DataSetPtr, const char *Name, char **IndexNames, u64 IndexCount)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
u64 Value = GetParameterValue(DataSet, Name, IndexNames, IndexCount, ParameterType_Enum).ValUInt;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
return Spec.EnumNames[Value];
CHECK_ERROR_END
return nullptr;
}
DLLEXPORT u64
DllGetEnumValuesCount(void *DataSetPtr, const char *Name)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
return Spec.EnumNames.size();
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetEnumValues(void *DataSetPtr, const char *Name, const char **NamesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
int Idx = 0;
for(const char *EnumName : Spec.EnumNames)
NamesOut[Idx++] = EnumName;
CHECK_ERROR_END
}
DLLEXPORT void
DllGetParameterDoubleMinMax(void *DataSetPtr, char *Name, double *MinOut, double *MaxOut)
{
CHECK_ERROR_BEGIN
//TODO: We don't check that the requested parameter was of type double!
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
if(Spec.Type != ParameterType_Double)
FatalError("ERROR: Requested the min and max values of the parameter \"", Name, "\" using DllGetParameterDoubleMinMax, but it is not of type double.\n");
*MinOut = Spec.Min.ValDouble;
*MaxOut = Spec.Max.ValDouble;
CHECK_ERROR_END
}
DLLEXPORT void
DllGetParameterUIntMinMax(void *DataSetPtr, char *Name, u64 *MinOut, u64 *MaxOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
if(Spec.Type != ParameterType_UInt)
FatalError("ERROR: Requested the min and max values of the parameter \"", Name, "\" using DllGetParameterUIntMinMax, but it is not of type uint.\n");
*MinOut = Spec.Min.ValUInt;
*MaxOut = Spec.Max.ValUInt;
CHECK_ERROR_END
}
DLLEXPORT const char *
DllGetParameterDescription(void *DataSetPtr, char *Name)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
return Spec.Description;
CHECK_ERROR_END
return 0;
}
DLLEXPORT const char *
DllGetParameterShortName(void *DataSetPtr, char *Name)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
return Spec.ShortName;
CHECK_ERROR_END
return 0;
}
DLLEXPORT const char *
DllGetParameterUnit(void *DataSetPtr, char *Name)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const parameter_spec &Spec = DataSet->Model->Parameters[GetParameterHandle(DataSet->Model, Name)];
return GetName(DataSet->Model, Spec.Unit);
CHECK_ERROR_END
return 0;
}
DLLEXPORT const char *
DllGetResultUnit(void *DataSetPtr, char *Name)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const equation_spec &Spec = DataSet->Model->Equations[GetEquationHandle(DataSet->Model, Name)];
return GetName(DataSet->Model, Spec.Unit);
CHECK_ERROR_END
return 0;
}
DLLEXPORT const char *
DllGetInputUnit(void *DataSetPtr, char *Name)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const input_spec &Spec = DataSet->Model->Inputs[GetInputHandle(DataSet->Model, Name)];
if(IsValid(Spec.Unit))
return GetName(DataSet->Model, Spec.Unit);
else
return "";
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllWriteParametersToFile(void *DataSetPtr, char *Filename)
{
CHECK_ERROR_BEGIN
WriteParametersToFile((mobius_data_set *)DataSetPtr, (const char *)Filename);
CHECK_ERROR_END
}
DLLEXPORT void
DllWriteInputsToFile(void *DataSetPtr, char *Filename)
{
CHECK_ERROR_BEGIN
WriteInputsToFile((mobius_data_set *)DataSetPtr, (const char *)Filename);
CHECK_ERROR_END
}
DLLEXPORT void
DllSetInputSeries(void *DataSetPtr, char *Name, char **IndexNames, u64 IndexCount, double *InputData, u64 InputDataLength, bool AlignWithResults)
{
CHECK_ERROR_BEGIN
SetInputSeries((mobius_data_set *)DataSetPtr, Name, IndexNames, (size_t)IndexCount, InputData, (size_t)InputDataLength, AlignWithResults);
CHECK_ERROR_END
}
//TODO: Some of the following should be wrapped into accessors in mobius_data_set.h, because we are creating too many dependencies on implementation details here:
DLLEXPORT u64
DllGetIndexSetsCount(void *DataSetPtr)
{
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
return DataSet->Model->IndexSets.Count() - 1;
}
DLLEXPORT void
DllGetIndexSets(void *DataSetPtr, const char **NamesOut, const char **TypesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const mobius_model *Model = DataSet->Model;
for(index_set_h IndexSet : Model->IndexSets)
{
const index_set_spec &Spec = Model->IndexSets[IndexSet];
NamesOut[IndexSet.Handle - 1] = Spec.Name;
TypesOut[IndexSet.Handle - 1] = Spec.Type == IndexSetType_Basic ? "basic" : "branched";
}
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetIndexCount(void *DataSetPtr, char *IndexSetName)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
index_set_h IndexSet = GetIndexSetHandle(DataSet->Model, IndexSetName);
return DataSet->IndexCounts[IndexSet.Handle];
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetIndexes(void *DataSetPtr, char *IndexSetName, const char **NamesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
index_set_h IndexSet = GetIndexSetHandle(DataSet->Model, IndexSetName);
for(size_t IdxIdx = 0; IdxIdx < DataSet->IndexCounts[IndexSet.Handle]; ++IdxIdx)
{
NamesOut[IdxIdx] = DataSet->IndexNames[IndexSet.Handle][IdxIdx];
}
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetParameterIndexSetsCount(void *DataSetPtr, char *ParameterName)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
parameter_h Parameter = GetParameterHandle(DataSet->Model, ParameterName);
return GetIndexSetCount(DataSet->ParameterStorageStructure, Parameter);
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetParameterIndexSets(void *DataSetPtr, char *ParameterName, const char **NamesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
parameter_h Parameter = GetParameterHandle(DataSet->Model, ParameterName);
GetIndexSets(DataSet->ParameterStorageStructure, Parameter, NamesOut);
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetResultIndexSetsCount(void *DataSetPtr, char *ResultName)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
if(!DataSet->ResultStorageStructure.HasBeenSetUp)
FatalError("ERROR: Tried to look up result indexes before result storage structure was set up.\n");
equation_h Equation = GetEquationHandle(DataSet->Model, ResultName);
return GetIndexSetCount(DataSet->ResultStorageStructure, Equation);
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetResultIndexSets(void *DataSetPtr, char *ResultName, const char **NamesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
if(!DataSet->ResultStorageStructure.HasBeenSetUp)
FatalError("ERROR: Tried to look up result indexes before result storage structure was set up.\n");
equation_h Equation = GetEquationHandle(DataSet->Model, ResultName);
GetIndexSets(DataSet->ResultStorageStructure, Equation, NamesOut);
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetInputIndexSetsCount(void *DataSetPtr, char *InputName)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
input_h Input = GetInputHandle(DataSet->Model, InputName);
return GetIndexSetCount(DataSet->InputStorageStructure, Input);
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetInputIndexSets(void *DataSetPtr, char *InputName, const char **NamesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
input_h Input = GetInputHandle(DataSet->Model, InputName);
GetIndexSets(DataSet->InputStorageStructure, Input, NamesOut);
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetAllModulesCount(void *DataSetPtr)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
return DataSet->Model->Modules.Count()-1;
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetAllModules(void *DataSetPtr, const char **NamesOut, const char **VersionsOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
for(module_h Module : DataSet->Model->Modules)
{
NamesOut[Module.Handle-1] = DataSet->Model->Modules[Module].Name;
VersionsOut[Module.Handle-1] = DataSet->Model->Modules[Module].Version;
}
CHECK_ERROR_END
}
DLLEXPORT const char *
DllGetModuleDescription(void *DataSetPtr, const char *ModuleName)
{
CHECK_ERROR_BEGIN
const mobius_model *Model = ((mobius_data_set *)DataSetPtr)->Model;
return Model->Modules[GetModuleHandle(Model, ModuleName)].Description;
CHECK_ERROR_END
return nullptr;
}
DLLEXPORT bool
DllIsParameterGroupName(void *DataSetPtr, char *Name)
{
CHECK_ERROR_BEGIN
return ((mobius_data_set *)DataSetPtr)->Model->ParameterGroups.Has(Name);
CHECK_ERROR_END
return false;
}
DLLEXPORT u64
DllGetParameterGroupIndexSetsCount(void *DataSetPtr, char *ParameterGroupName)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
parameter_group_h ParameterGroup = GetParameterGroupHandle(DataSet->Model, ParameterGroupName);
return (u64)DataSet->Model->ParameterGroups[ParameterGroup].IndexSets.size();
CHECK_ERROR_END
return 0;
}
DLLEXPORT void
DllGetParameterGroupIndexSets(void *DataSetPtr, char *ParameterGroupName, const char **NamesOut)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
parameter_group_h ParameterGroup = GetParameterGroupHandle(DataSet->Model, ParameterGroupName);
const std::vector<index_set_h> &IndexSets = DataSet->Model->ParameterGroups[ParameterGroup].IndexSets;
for(size_t Idx = 0; Idx < IndexSets.size(); ++Idx)
NamesOut[Idx] = GetName(DataSet->Model, IndexSets[Idx]);
CHECK_ERROR_END
}
DLLEXPORT u64
DllGetAllParameterGroupsCount(void *DataSetPtr, const char *ModuleName)
{
CHECK_ERROR_BEGIN
mobius_data_set *DataSet = (mobius_data_set *)DataSetPtr;
const mobius_model *Model = DataSet->Model;
bool All = ModuleName && strcmp(ModuleName, "__all!!__") == 0; //NOTE: Kind of stupid way to do it. Basically there are two default cases. Either we want groups not belonging to the top level or we want all parameter groups. Can't handle both using ModuleName==0
module_h Module = {0};
if(ModuleName && strlen(ModuleName) > 0 && !All)
Module = GetModuleHandle(Model, ModuleName);
u64 Count = 0;
for(parameter_group_h ParameterGroup : Model->ParameterGroups)
{
const parameter_group_spec &Spec = Model->ParameterGroups[ParameterGroup];
if(All || Module == Spec.Module) ++Count;
}
return Count;
CHECK_ERROR_END
return 0;
}