forked from NIVANorge/Mobius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobius_data_set.cpp
1427 lines (1181 loc) · 55.8 KB
/
mobius_data_set.cpp
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
static mobius_data_set *
GenerateDataSet(mobius_model *Model)
{
if(!Model->Finalized)
{
std::cout << "ERROR: Attempted to generate a data set before the model was finalized using an EndModelDefinition call." << std::endl;
return 0;
}
mobius_data_set *DataSet = new mobius_data_set {}; //NOTE: The {} ensures that all pointers are set to 0. This is important.
DataSet->Model = Model;
DataSet->IndexCounts = AllocClearedArray(index_t, Model->FirstUnusedIndexSetHandle);
DataSet->IndexCounts[0] = index_t({0}, 1);
DataSet->IndexNames = AllocClearedArray(const char **, Model->FirstUnusedIndexSetHandle);
DataSet->IndexNamesToHandle.resize(Model->FirstUnusedIndexSetHandle);
DataSet->BranchInputs = AllocClearedArray(branch_inputs *, Model->FirstUnusedIndexSetHandle);
if(Model->FirstUnusedIndexSetHandle == 1) // NOTE: In case there are no index sets, all index sets have had their indexes set.
{
DataSet->AllIndexesHaveBeenSet = true;
}
DataSet->ParameterStorageStructure.Model = Model;
DataSet->ParameterStorageStructure.Type = EntityType_Parameter;
DataSet->InputStorageStructure.Model = Model;
DataSet->InputStorageStructure.Type = EntityType_Input;
DataSet->ResultStorageStructure.Model = Model;
DataSet->ResultStorageStructure.Type = EntityType_Equation;
DataSet->TimestepsLastRun = 0;
return DataSet;
}
mobius_data_set::~mobius_data_set()
{
//NOTE: This has not been properly tested yet..
if(ParameterData) free(ParameterData);
if(InputData) free(InputData);
if(ResultData) free(ResultData);
if(InputTimeseriesWasProvided) free(InputTimeseriesWasProvided);
if(IndexCounts)
{
if(IndexNames)
{
for(entity_handle IndexSetHandle = 1; IndexSetHandle < Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
if(IndexNames[IndexSetHandle])
{
for(index_t Index = {IndexSetHandle, 0}; Index < IndexCounts[IndexSetHandle]; ++Index)
{
if(IndexNames[IndexSetHandle][Index]) free((void *)IndexNames[IndexSetHandle][Index]); //NOTE: We free this const char * because we know that it was set using SetIndexes(), which always allocates copies of the index names it receives.
}
free(IndexNames[IndexSetHandle]);
}
}
free(IndexNames);
}
if(BranchInputs)
{
for(entity_handle IndexSetHandle = 1; IndexSetHandle < Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
if(BranchInputs[IndexSetHandle])
{
for(index_t Index = {IndexSetHandle, 0}; Index < IndexCounts[IndexSetHandle]; ++Index)
{
if(BranchInputs[IndexSetHandle][Index].Inputs) free(BranchInputs[IndexSetHandle][Index].Inputs);
}
free(BranchInputs[IndexSetHandle]);
}
}
free(BranchInputs);
}
free(IndexCounts);
}
if(x0) free(x0);
if(wk) free(wk);
}
static void
CopyStorageStructure(storage_structure &Source, storage_structure &Dest, size_t FirstUnusedHandle)
{
Dest.Units = Source.Units; //NOTE: Nested vector copy.
size_t UnitCount = Dest.Units.size();
//TODO: It may be ok to have these as std::vector<size_t> instead of size_t*, in which case we would not have to do all this work doing explicit copies.
if(Source.TotalCountForUnit) Dest.TotalCountForUnit = CopyArray(size_t, UnitCount, Source.TotalCountForUnit);
if(Source.OffsetForUnit) Dest.OffsetForUnit = CopyArray(size_t, UnitCount, Source.OffsetForUnit);
if(Source.UnitForHandle) Dest.UnitForHandle = CopyArray(size_t, FirstUnusedHandle, Source.UnitForHandle);
if(Source.LocationOfHandleInUnit) Dest.LocationOfHandleInUnit = CopyArray(size_t, FirstUnusedHandle, Source.LocationOfHandleInUnit);
Dest.TotalCount = Source.TotalCount;
Dest.HasBeenSetUp = Source.HasBeenSetUp;
Dest.Model = Source.Model;
}
static mobius_data_set *
CopyDataSet(mobius_data_set *DataSet, bool CopyResults = false)
{
const mobius_model *Model = DataSet->Model;
mobius_data_set *Copy = new mobius_data_set {};
Copy->Model = Model;
if(DataSet->ParameterData) Copy->ParameterData = CopyArray(parameter_value, DataSet->ParameterStorageStructure.TotalCount, DataSet->ParameterData);
CopyStorageStructure(DataSet->ParameterStorageStructure, Copy->ParameterStorageStructure, Model->FirstUnusedParameterHandle);
if(DataSet->InputData) Copy->InputData = CopyArray(double, DataSet->InputStorageStructure.TotalCount * DataSet->InputDataTimesteps, DataSet->InputData);
CopyStorageStructure(DataSet->InputStorageStructure, Copy->InputStorageStructure, Model->FirstUnusedInputHandle);
Copy->InputDataStartDate = DataSet->InputDataStartDate;
Copy->InputDataHasSeparateStartDate = DataSet->InputDataHasSeparateStartDate;
Copy->InputDataTimesteps = DataSet->InputDataTimesteps;
if(DataSet->InputTimeseriesWasProvided) Copy->InputTimeseriesWasProvided = CopyArray(bool, DataSet->InputStorageStructure.TotalCount, DataSet->InputTimeseriesWasProvided);
if(CopyResults)
{
if(DataSet->ResultData) Copy->ResultData = CopyArray(double, DataSet->ResultStorageStructure.TotalCount * (DataSet->TimestepsLastRun + 1), DataSet->ResultData);
CopyStorageStructure(DataSet->ResultStorageStructure, Copy->ResultStorageStructure, Model->FirstUnusedEquationHandle);
Copy->TimestepsLastRun = DataSet->TimestepsLastRun;
Copy->StartDateLastRun = DataSet->StartDateLastRun;
Copy->HasBeenRun = DataSet->HasBeenRun;
}
else
{
Copy->HasBeenRun = false;
}
if(DataSet->IndexCounts) Copy->IndexCounts = CopyArray(index_t, Model->FirstUnusedIndexSetHandle, DataSet->IndexCounts);
//TODO: This could probably be a std::vector<std::vector<std::string>>
if(DataSet->IndexNames)
{
Copy->IndexNames = AllocClearedArray(const char **, Model->FirstUnusedIndexSetHandle);
for(entity_handle IndexSetHandle = 0; IndexSetHandle < Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
if(DataSet->IndexNames[IndexSetHandle])
{
Copy->IndexNames[IndexSetHandle] = AllocClearedArray(const char *, DataSet->IndexCounts[IndexSetHandle]);
for(index_t Index = {IndexSetHandle, 0}; Index < DataSet->IndexCounts[IndexSetHandle]; ++Index)
{
Copy->IndexNames[IndexSetHandle][Index] = CopyString(DataSet->IndexNames[IndexSetHandle][Index]);
}
}
}
}
Copy->IndexNamesToHandle = DataSet->IndexNamesToHandle;
Copy->AllIndexesHaveBeenSet = DataSet->AllIndexesHaveBeenSet;
if(DataSet->BranchInputs)
{
Copy->BranchInputs = AllocClearedArray(branch_inputs *, Model->FirstUnusedIndexSetHandle);
for(entity_handle IndexSetHandle = 0; IndexSetHandle < Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
if(DataSet->BranchInputs[IndexSetHandle])
{
Copy->BranchInputs[IndexSetHandle] = AllocClearedArray(branch_inputs, DataSet->IndexCounts[IndexSetHandle]);
for(index_t Index = {IndexSetHandle, 0}; Index < DataSet->IndexCounts[IndexSetHandle]; ++Index)
{
//yeahh.. this could also be a std::vector
branch_inputs &Inputs = DataSet->BranchInputs[IndexSetHandle][Index];
size_t Count = Inputs.Count;
Copy->BranchInputs[IndexSetHandle][Index].Count = Count;
Copy->BranchInputs[IndexSetHandle][Index].Inputs = CopyArray(index_t, Count, Inputs.Inputs);
}
}
}
}
//NOTE: We don't copy any of the following as they will be generated when you try to run the DataSet.
//std::vector<parameter_value> FastParameterLookup;
//std::vector<size_t> FastInputLookup;
//std::vector<size_t> FastResultLookup;
//std::vector<size_t> FastLastResultLookup;
//double *x0; //NOTE: Temporary storage for use by solvers
//double *wk; //NOTE: Temporary storage for use by solvers
//u64 TimestepsLastRun;
return Copy;
}
static void
SetupStorageStructureSpecifer(storage_structure &Structure, index_t *IndexCounts, size_t FirstUnusedHandle)
{
size_t UnitCount = Structure.Units.size();
Structure.TotalCountForUnit = AllocClearedArray(size_t, UnitCount);
Structure.OffsetForUnit = AllocClearedArray(size_t, UnitCount);
Structure.UnitForHandle = AllocClearedArray(size_t, FirstUnusedHandle);
Structure.LocationOfHandleInUnit = AllocClearedArray(size_t, FirstUnusedHandle);
Structure.TotalCount = 0;
size_t UnitIndex = 0;
size_t OffsetForUnitSoFar = 0;
for(storage_unit_specifier &Unit : Structure.Units)
{
Structure.TotalCountForUnit[UnitIndex] = Unit.Handles.size();
for(index_set_h IndexSet : Unit.IndexSets)
{
Structure.TotalCountForUnit[UnitIndex] *= IndexCounts[IndexSet.Handle];
}
size_t HandleIdx = 0;
for(entity_handle Handle : Unit.Handles)
{
Structure.UnitForHandle[Handle] = UnitIndex;
Structure.LocationOfHandleInUnit[Handle] = HandleIdx;
++HandleIdx;
}
Structure.OffsetForUnit[UnitIndex] = OffsetForUnitSoFar;
OffsetForUnitSoFar += Structure.TotalCountForUnit[UnitIndex];
Structure.TotalCount += Structure.TotalCountForUnit[UnitIndex];
++UnitIndex;
}
Structure.HasBeenSetUp = true;
}
storage_structure::~storage_structure()
{
if(HasBeenSetUp)
{
free(TotalCountForUnit);
free(OffsetForUnit);
free(UnitForHandle);
free(LocationOfHandleInUnit);
}
}
//NOTE: The following functions are very similar, but it would incur a performance penalty to try to merge them.
//NOTE: The OffsetForHandle functions are meant to be internal functions that can be used by other wrappers that do more error checking.
// NOTE: Returns the storage index of the first instance of a value corresponding to this Handle, i.e where all indexes that this handle depends on are the first index of their index set.
inline size_t
OffsetForHandle(storage_structure &Structure, entity_handle Handle)
{
size_t UnitIndex = Structure.UnitForHandle[Handle];
size_t OffsetForUnit = Structure.OffsetForUnit[UnitIndex];
size_t LocationOfHandleInUnit = Structure.LocationOfHandleInUnit[Handle];
return OffsetForUnit + LocationOfHandleInUnit;
}
// NOTE: Returns the storage index of a value corresponding to this Handle with the given index set indexes.
// CurrentIndexes must be set up so that for any index set with handle IndexSetHandle, CurrentIndexes[IndexSetHandle] is the current index of that index set. (Typically ValueSet->CurrentIndexes)
// IndexCounts must be set up so that for any index set with handle IndexSetHandle, IndexCounts[IndexSetHandle] is the index count of that index set. (Typically DataSet->IndexCounts)
inline size_t
OffsetForHandle(storage_structure &Structure, const index_t *CurrentIndexes, const index_t *IndexCounts, entity_handle Handle)
{
std::vector<storage_unit_specifier> &Units = Structure.Units;
size_t UnitIndex = Structure.UnitForHandle[Handle];
storage_unit_specifier &Specifier = Units[UnitIndex];
size_t NumHandlesInUnitInstance = Specifier.Handles.size();
size_t OffsetForUnit = Structure.OffsetForUnit[UnitIndex];
size_t LocationOfHandleInUnit = Structure.LocationOfHandleInUnit[Handle];
size_t InstanceOffset = 0;
for(index_set_h IndexSet : Specifier.IndexSets)
{
size_t Count = IndexCounts[IndexSet.Handle];
index_t Index = CurrentIndexes[IndexSet.Handle];
#if MOBIUS_INDEX_BOUNDS_TESTS
if(Index >= Count)
{
const char *EntityName = GetName(Structure.Model, Structure.Type, Handle);
const char *TypeName = GetEntityTypeName(Structure.Type);
MOBIUS_PARTIAL_ERROR("ERROR: Index out of bounds for index set \"" << GetName(Structure.Model, IndexSet) << "\", got index " << Index << ", count was " << Count << std::endl);
MOBIUS_FATAL_ERROR("This happened while looking up the value of the " << TypeName << " \"" << EntityName << "\"." << std::endl);
}
if(Index.IndexSetHandle != IndexSet.Handle)
{
const char *EntityName = GetName(Structure.Model, Structure.Type, Handle);
const char *TypeName = GetEntityTypeName(Structure.Type);
MOBIUS_PARTIAL_ERROR("ERROR: Used an index addressed to the index set \"" << GetName(Structure.Model, index_set_h {Index.IndexSetHandle}) << "\" for indexing the index set \"" << GetName(Structure.Model, IndexSet) << "\"." << std::endl);
MOBIUS_FATAL_ERROR("This happened while looking up the value of the " << TypeName << " \"" << EntityName << "\"." << std::endl);
}
#endif
InstanceOffset = InstanceOffset * Count + Index;
}
return OffsetForUnit + InstanceOffset * NumHandlesInUnitInstance + LocationOfHandleInUnit;
}
#if !defined(MOBIUS_INDEX_BOUNDS_TESTS)
#define MOBIUS_INDEX_BOUNDS_TESTS 0
#endif
inline void
CheckIndexErrors(const mobius_model *Model, index_set_h IndexSet, index_t Index, size_t Count, entity_type Type, entity_handle Handle)
{
bool CountError = (Index >= Count);
bool HandleError = (Index.IndexSetHandle != IndexSet.Handle);
if(CountError || HandleError)
{
const char *EntityName = GetName(Model, Type, Handle);
const char *TypeName = GetEntityTypeName(Type);
if(CountError)
{
MOBIUS_PARTIAL_ERROR("ERROR: Index out of bounds for index set \"" << GetName(Model, IndexSet) << "\", got index " << Index << ", count was " << Count << std::endl);
}
if(HandleError)
{
MOBIUS_PARTIAL_ERROR("ERROR: Used an index addressed to the index set \"" << GetName(Model, index_set_h {Index.IndexSetHandle}) << "\" for indexing the index set \"" << GetName(Model, IndexSet) << "\"." << std::endl);
}
MOBIUS_FATAL_ERROR("This happened while looking up the value of the " << TypeName << " \"" << EntityName << "\"." << std::endl);
}
}
// NOTE: Returns the storage index of a value corresponding to this Handle with the given index set indexes.
// Indexes must be set up so that Indexes[I] is the index of the I'th index set that the entity one wishes to look up depends on.
// IndexesCount is the number of index sets the entity depends on (and so the length of the array Indexes).
// IndexCounts must be set up so that for any index set with handle IndexSetHandle, IndexCounts[IndexSetHandle] is the index count of that index set. (Typically DataSet->IndexCounts)
inline size_t
OffsetForHandle(storage_structure &Structure, const index_t *Indexes, size_t IndexesCount, const index_t *IndexCounts, entity_handle Handle)
{
std::vector<storage_unit_specifier> &Units = Structure.Units;
size_t UnitIndex = Structure.UnitForHandle[Handle];
storage_unit_specifier &Specifier = Units[UnitIndex];
size_t NumHandlesInUnitInstance = Specifier.Handles.size();
size_t OffsetForUnit = Structure.OffsetForUnit[UnitIndex];
size_t LocationOfHandleInUnit = Structure.LocationOfHandleInUnit[Handle];
size_t InstanceOffset = 0;
size_t Level = 0;
for(index_set_h IndexSet : Specifier.IndexSets)
{
size_t Count = IndexCounts[IndexSet.Handle];
index_t Index = Indexes[Level];
#if MOBIUS_INDEX_BOUNDS_TESTS
CheckIndexErrors(Structure.Model, IndexSet, Index, Count, Structure.Type, Handle);
#endif
InstanceOffset = InstanceOffset * Count + Index;
++Level;
}
return OffsetForUnit + InstanceOffset * NumHandlesInUnitInstance + LocationOfHandleInUnit;
}
// NOTE: Returns the storage index of a value corresponding to this Handle with the given index set indexes.
// CurrentIndexes must be set up so that for any index set with handle IndexSetHandle, CurrentIndexes[IndexSetHandle] is the current index of that index set. (Typically ValueSet->CurrentIndexes)
// IndexCounts must be set up so that for any index set with handle IndexSetHandle, IndexCounts[IndexSetHandle] is the index count of that index set. (Typically DataSet->IndexCounts)
// OverrideCount specifies how many of the last index sets one wants to override the indexes of
// OverrideIndexes provides indexes for the overridden index sets.
//
// Example. If Handle is a parameter handle to a parameter depending on the index sets IndexSetA, IndexSetB, IndexSetC, IndexSetD in that order, then if
// CurrentIndexes[IndexSetA.Handle]=0, CurrentIndexes[IndexSetB.Handle]=1, CurrentIndexes[IndexSetC.Handle]=2, CurrentIndexes[IndexSetD.Handle]=3,
// OverrideCount = 2, OverrideIndexes = {5, 6},
// then this function returns the storage index of the parameter value corresponding to this Handle, and with indexes [0, 1, 5, 6]
//
// This function is designed to be used by the system for explicit indexing of lookup values, such as when one uses access macros like "PARAMETER(MyParameter, Index1, Index2)" etc. inside equations.
inline size_t
OffsetForHandle(storage_structure &Structure, const index_t* CurrentIndexes, const index_t *IndexCounts, const index_t *OverrideIndexes, size_t OverrideCount, entity_handle Handle)
{
std::vector<storage_unit_specifier> &Units = Structure.Units;
size_t UnitIndex = Structure.UnitForHandle[Handle];
storage_unit_specifier &Specifier = Units[UnitIndex];
size_t NumHandlesInUnitInstance = Specifier.Handles.size();
size_t OffsetForUnit = Structure.OffsetForUnit[UnitIndex];
size_t LocationOfHandleInUnit = Structure.LocationOfHandleInUnit[Handle];
size_t IndexSetCount = Specifier.IndexSets.size();
size_t InstanceOffset = 0;
size_t IndexSetLevel = 0;
for(index_set_h IndexSet : Specifier.IndexSets)
{
size_t Count = IndexCounts[IndexSet.Handle];
index_t Index;
if(IndexSetLevel < (IndexSetCount - OverrideCount))
{
Index = CurrentIndexes[IndexSet.Handle];
}
else
{
Index = OverrideIndexes[IndexSetLevel + (OverrideCount - IndexSetCount)];
}
#if MOBIUS_INDEX_BOUNDS_TESTS
CheckIndexErrors(Structure.Model, IndexSet, Index, Count, Structure.Type, Handle);
#endif
InstanceOffset = InstanceOffset * Count + Index;
++IndexSetLevel;
}
return OffsetForUnit + InstanceOffset * NumHandlesInUnitInstance + LocationOfHandleInUnit;
}
//NOTE: Returns the storage index of a value corresponding to this Handle with the given index set indexes.
// CurrentIndexes must be set up so that for any index set with handle IndexSetHandle, CurrentIndexes[IndexSetHandle] is the current index of that index set. (Typically ValueSet->CurrentIndexes)
// IndexCounts must be set up so that for any index set with handle IndexSetHandle, IndexCounts[IndexSetHandle] is the index count of that index set. (Typically DataSet->IndexCounts)
// Skip is an index set that one wants to be set to its first index.
// SubsequentOffset is a second output value, which will contain the distance between each instance of this value if one were to change the index in Skip.
//
// Example: MyParameter depends on IndexSetA, IndexSetB
// IndexCount[IndexSetA.Handle] = 3, IndexCount[IndexSetB.Handle] = 2. CurrentIndexes[IndexSetA.Handle] = 2, CurrentIndexes[IndexSetB.Handle] = 1.
// size_t SubsequentOffset;
// size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, ValueSet->CurrentIndexes, DataSet->IndexCounts, IndexSetA, SubsequentOffset, MyParameter.Handle);
// DataSet->ParameterData[Offset] is the value of MyParameter with indexes {0, 1};
// DataSet->ParameterData[Offset] + Idx*SubsequentOffset is the value of MyParameter with indexes {Idx, 1};
//
// This function is designed to be used with the system that evaluates cumulation equations.
static size_t
OffsetForHandle(storage_structure &Structure, index_t *CurrentIndexes, index_t *IndexCounts, index_set_h Skip, size_t& SubsequentOffset, entity_handle Handle)
{
std::vector<storage_unit_specifier> &Units = Structure.Units;
size_t UnitIndex = Structure.UnitForHandle[Handle];
storage_unit_specifier &Specifier = Units[UnitIndex];
size_t NumHandlesInUnitInstance = Specifier.Handles.size();
size_t OffsetForUnit = Structure.OffsetForUnit[UnitIndex];
size_t LocationOfHandleInUnit = Structure.LocationOfHandleInUnit[Handle];
size_t InstanceOffset = 0;
SubsequentOffset = 1;
bool Skipped = false;
for(index_set_h IndexSet : Specifier.IndexSets)
{
size_t Count = IndexCounts[IndexSet.Handle];
index_t Index = CurrentIndexes[IndexSet.Handle];
if(Skipped)
{
SubsequentOffset *= Count;
}
if(IndexSet == Skip)
{
Index = {IndexSet, 0};
Skipped = true;
}
#if MOBIUS_INDEX_BOUNDS_TESTS
CheckIndexErrors(Structure.Model, IndexSet, Index, Count, Structure.Type, Handle);
#endif
InstanceOffset = InstanceOffset * Count + Index;
}
SubsequentOffset *= NumHandlesInUnitInstance;
return OffsetForUnit + InstanceOffset * NumHandlesInUnitInstance + LocationOfHandleInUnit;
}
static void
SetMultipleValuesForParameter(mobius_data_set *DataSet, entity_handle ParameterHandle, parameter_value *Values, size_t Count)
{
//NOTE: There are almost no safety checks in this function. The caller of the function is responsible for the checks!
// It was designed to be used with the default text input for parameters. If you want to use it for anything else, you should make sure you understand how it works.
size_t UnitIndex = DataSet->ParameterStorageStructure.UnitForHandle[ParameterHandle];
size_t Stride = DataSet->ParameterStorageStructure.Units[UnitIndex].Handles.size();
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, ParameterHandle);
parameter_value *Base = DataSet->ParameterData + Offset;
for(size_t Idx = 0; Idx < Count; ++Idx)
{
*Base = Values[Idx];
Base += Stride;
}
}
static datetime
GetStartDate(mobius_data_set *DataSet)
{
const mobius_model *Model = DataSet->Model;
auto FindTime = Model->ParameterNameToHandle.find("Start date");
if(FindTime != Model->ParameterNameToHandle.end())
{
entity_handle StartTimeHandle = FindTime->second;
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, StartTimeHandle);
return DataSet->ParameterData[Offset].ValTime; //TODO: Check that it was actually registered with the correct type and that it does not have any index set dependencies.
}
return datetime(); //I.e. 1970-1-1
}
inline datetime
GetInputStartDate(mobius_data_set *DataSet)
{
if(DataSet->InputDataHasSeparateStartDate)
{
return DataSet->InputDataStartDate;
}
return GetStartDate(DataSet);
}
static u64
GetTimesteps(mobius_data_set *DataSet)
{
const mobius_model *Model = DataSet->Model;
auto FindTimestep = Model->ParameterNameToHandle.find("Timesteps");
if(FindTimestep != Model->ParameterNameToHandle.end())
{
entity_handle TimestepHandle = FindTimestep->second;
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, TimestepHandle);
return DataSet->ParameterData[Offset].ValUInt; //TODO: Check that it was actually registered with the correct type and that it does not have any index set dependencies.
}
return 100;
}
static void
SetIndexes(mobius_data_set *DataSet, token_string IndexSetName, const std::vector<token_string>& IndexNames)
{
const mobius_model *Model = DataSet->Model;
entity_handle IndexSetHandle = GetIndexSetHandle(DataSet->Model, IndexSetName).Handle;
const index_set_spec &Spec = Model->IndexSetSpecs[IndexSetHandle];
if(Spec.Type != IndexSetType_Basic)
{
MOBIUS_FATAL_ERROR("ERROR: Can not use the method SetIndexes for the index set " << Spec.Name << ", use a method that is specific to the type of that index set instead." << std::endl);
}
if(DataSet->IndexNames[IndexSetHandle] != 0)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to set the indexes for the index set " << Spec.Name << " more than once." << std::endl);
}
if(IndexNames.empty())
{
MOBIUS_FATAL_ERROR("ERROR: Tried to set indexes for the index set " << Spec.Name << ", but no indexes were provided" << std::endl);
}
if(!Spec.RequiredIndexes.empty())
{
bool Correct = true;
if(Spec.RequiredIndexes.size() > IndexNames.size()) Correct = false;
else
{
for(size_t IdxIdx = 0; IdxIdx < Spec.RequiredIndexes.size(); ++IdxIdx)
{
if(!IndexNames[IdxIdx].Equals(Spec.RequiredIndexes[IdxIdx]))
{
Correct = false;
break;
}
}
}
if(!Correct)
{
MOBIUS_PARTIAL_ERROR("ERROR: The model requires the following indexes to be the first indexes for the index set " << Spec.Name << ":" << std::endl);
for(const char *IndexName : Spec.RequiredIndexes)
{
MOBIUS_PARTIAL_ERROR("\"" << IndexName << "\" ");
}
MOBIUS_PARTIAL_ERROR(std::endl << "in that order. We got the indexes: " << std::endl);
for(token_string IndexName : IndexNames)
{
MOBIUS_PARTIAL_ERROR("\"" << IndexName << "\" ");
}
MOBIUS_FATAL_ERROR(std::endl);
}
}
DataSet->IndexCounts[IndexSetHandle] = {IndexSetHandle, (u32)IndexNames.size()};
DataSet->IndexNames[IndexSetHandle] = AllocClearedArray(const char *, IndexNames.size());
for(size_t IndexIndex = 0; IndexIndex < IndexNames.size(); ++IndexIndex)
{
const char *IndexName = IndexNames[IndexIndex].Copy().Data; //NOTE: Leaks unless we free it.
DataSet->IndexNames[IndexSetHandle][IndexIndex] = IndexName;
DataSet->IndexNamesToHandle[IndexSetHandle][IndexName] = IndexIndex;
}
if(DataSet->IndexNamesToHandle[IndexSetHandle].size() != IndexNames.size())
{
MOBIUS_FATAL_ERROR("ERROR: Got duplicate indexes for index set " << Spec.Name << std::endl);
}
bool AllSet = true;
for(size_t IndexSetHandle = 1; IndexSetHandle < Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
if(DataSet->IndexCounts[IndexSetHandle] == 0)
{
AllSet = false;
break;
}
}
DataSet->AllIndexesHaveBeenSet = AllSet;
}
static void
SetBranchIndexes(mobius_data_set *DataSet, token_string IndexSetName, const std::vector<std::pair<token_string, std::vector<token_string>>>& Inputs)
{
const mobius_model *Model = DataSet->Model;
entity_handle IndexSetHandle = GetIndexSetHandle(Model, IndexSetName).Handle;
const index_set_spec &Spec = Model->IndexSetSpecs[IndexSetHandle];
if(Spec.Type != IndexSetType_Branched)
{
MOBIUS_FATAL_ERROR("ERROR: Can not use the method SetBranchIndexes for the index set " << IndexSetName << ", use a method that is specific to the type of that index set instead." << std::endl);
}
if(DataSet->IndexNames[IndexSetHandle] != 0)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to set the indexes for the index set " << Spec.Name << " more than once." << std::endl);
}
if(Inputs.empty())
{
MOBIUS_FATAL_ERROR("ERROR: Tried to set indexes for the index set " << Spec.Name << ", but no indexes were provided" << std::endl);
}
DataSet->IndexCounts[IndexSetHandle] = {IndexSetHandle, (u32)Inputs.size()};
DataSet->IndexNames[IndexSetHandle] = AllocClearedArray(const char *, Inputs.size());
DataSet->BranchInputs[IndexSetHandle] = AllocClearedArray(branch_inputs, Inputs.size());
index_t IndexIndex = {IndexSetHandle, 0};
for(const auto &InputData : Inputs)
{
const char *IndexName = InputData.first.Copy().Data; //NOTE: Leaks unless we free it.
const std::vector<token_string> &InputNames = InputData.second;
if(DataSet->IndexNamesToHandle[IndexSetHandle].find(IndexName) != DataSet->IndexNamesToHandle[IndexSetHandle].end())
{
MOBIUS_FATAL_ERROR("ERROR: Got duplicate indexes for index set " << IndexSetName << std::endl);
}
DataSet->IndexNamesToHandle[IndexSetHandle][IndexName] = IndexIndex;
DataSet->IndexNames[IndexSetHandle][IndexIndex] = IndexName;
DataSet->BranchInputs[IndexSetHandle][IndexIndex].Count = InputNames.size();
DataSet->BranchInputs[IndexSetHandle][IndexIndex].Inputs = AllocClearedArray(index_t, InputNames.size());
index_t InputIdxIdx = {IndexSetHandle, 0};
for(token_string InputName : InputNames)
{
auto Find = DataSet->IndexNamesToHandle[IndexSetHandle].find(InputName);
if(Find == DataSet->IndexNamesToHandle[IndexSetHandle].end())
{
MOBIUS_FATAL_ERROR("ERROR: The index \"" << InputName << "\" appears an input to the index \"" << IndexName << "\", in the index set " << IndexSetName << ", before it itself is declared." << std::endl);
}
index_t InputIndex = {IndexSetHandle, Find->second};
DataSet->BranchInputs[IndexSetHandle][IndexIndex].Inputs[InputIdxIdx] = InputIndex;
++InputIdxIdx;
}
++IndexIndex;
}
bool AllSet = true;
for(size_t IndexSetHandle = 1; IndexSetHandle < DataSet->Model->FirstUnusedIndexSetHandle; ++IndexSetHandle)
{
if(DataSet->IndexCounts[IndexSetHandle] == 0)
{
AllSet = false;
break;
}
}
DataSet->AllIndexesHaveBeenSet = AllSet;
}
static void
AllocateParameterStorage(mobius_data_set *DataSet)
{
const mobius_model *Model = DataSet->Model;
if(!DataSet->AllIndexesHaveBeenSet)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to allocate parameter storage before all index sets were filled." << std::endl);
}
if(DataSet->ParameterData)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to allocate parameter storage twice." << std::endl);
}
std::map<std::vector<index_set_h>, std::vector<entity_handle>> TransposedParameterDependencies;
for(entity_handle ParameterHandle = 1; ParameterHandle < Model->FirstUnusedParameterHandle; ++ParameterHandle)
{
std::vector<index_set_h> Dependencies = Model->ParameterSpecs[ParameterHandle].IndexSetDependencies;
TransposedParameterDependencies[Dependencies].push_back(ParameterHandle);
}
size_t ParameterStorageUnitCount = TransposedParameterDependencies.size();
std::vector<storage_unit_specifier> &Units = DataSet->ParameterStorageStructure.Units;
Units.resize(ParameterStorageUnitCount);
size_t UnitIndex = 0;
for(auto& Structure : TransposedParameterDependencies)
{
Units[UnitIndex].IndexSets = Structure.first; //NOTE: vector copy.
Units[UnitIndex].Handles = Structure.second; //NOTE: vector copy.
++UnitIndex;
}
SetupStorageStructureSpecifer(DataSet->ParameterStorageStructure, DataSet->IndexCounts, Model->FirstUnusedParameterHandle);
DataSet->ParameterData = AllocClearedArray(parameter_value, DataSet->ParameterStorageStructure.TotalCount);
//NOTE: Setting up default values.
UnitIndex = 0;
for(storage_unit_specifier& Unit : Units)
{
size_t HandlesInInstance = Unit.Handles.size();
size_t TotalHandlesForUnit = DataSet->ParameterStorageStructure.TotalCountForUnit[UnitIndex];
size_t ParameterIndex = 0;
for(entity_handle ParameterHandle : Unit.Handles)
{
parameter_value DefaultValue = Model->ParameterSpecs[ParameterHandle].Default;
size_t At = OffsetForHandle(DataSet->ParameterStorageStructure, ParameterHandle);
for(size_t Instance = 0; Instance < TotalHandlesForUnit; Instance += HandlesInInstance)
{
DataSet->ParameterData[At] = DefaultValue;
At += HandlesInInstance;
}
++ParameterIndex;
}
++UnitIndex;
}
}
static void
AllocateInputStorage(mobius_data_set *DataSet, u64 Timesteps)
{
const mobius_model *Model = DataSet->Model;
if(!DataSet->AllIndexesHaveBeenSet)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to allocate input storage before all index sets were filled." << std::endl);
}
if(DataSet->InputData)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to allocate input storage twice." << std::endl);
}
std::map<std::vector<index_set_h>, std::vector<entity_handle>> TransposedInputDependencies;
for(entity_handle InputHandle = 1; InputHandle < Model->FirstUnusedInputHandle; ++InputHandle)
{
const input_spec &Spec = Model->InputSpecs[InputHandle];
TransposedInputDependencies[Spec.IndexSetDependencies].push_back(InputHandle);
}
size_t InputStorageUnitCount = TransposedInputDependencies.size();
std::vector<storage_unit_specifier> &Units = DataSet->InputStorageStructure.Units;
Units.resize(InputStorageUnitCount);
size_t UnitIndex = 0;
for(auto& Structure : TransposedInputDependencies)
{
Units[UnitIndex].IndexSets = Structure.first; //NOTE: vector copy.
Units[UnitIndex].Handles = Structure.second; //NOTE: vector copy.
++UnitIndex;
}
SetupStorageStructureSpecifer(DataSet->InputStorageStructure, DataSet->IndexCounts, Model->FirstUnusedInputHandle);
DataSet->InputData = AllocClearedArray(double, DataSet->InputStorageStructure.TotalCount * Timesteps);
DataSet->InputDataTimesteps = Timesteps;
DataSet->InputTimeseriesWasProvided = AllocClearedArray(bool, DataSet->InputStorageStructure.TotalCount);
}
static void
AllocateResultStorage(mobius_data_set *DataSet, u64 Timesteps)
{
const mobius_model *Model = DataSet->Model;
if(!DataSet->AllIndexesHaveBeenSet)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to allocate result storage before all index sets were filled." << std::endl);
}
if(DataSet->ResultData)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to allocate result storage twice." << std::endl);
}
//NOTE: We set up a storage structure for results that mirrors the equation batch group structure. This simplifies things a lot in other code.
if(!DataSet->HasBeenRun) //If it was run once before we don't need to set up the storage structure again. //TODO: This should be a flag on the storage structure instead, and it should be the same for all AllocateXStorage functions.
{
size_t ResultStorageUnitCount = Model->BatchGroups.size();
std::vector<storage_unit_specifier> &Units = DataSet->ResultStorageStructure.Units;
Units.resize(ResultStorageUnitCount);
for(size_t UnitIndex = 0; UnitIndex < ResultStorageUnitCount; ++UnitIndex)
{
const equation_batch_group &BatchGroup = Model->BatchGroups[UnitIndex];
Units[UnitIndex].IndexSets = BatchGroup.IndexSets;
for(size_t BatchIdx = BatchGroup.FirstBatch; BatchIdx <= BatchGroup.LastBatch; ++BatchIdx)
{
const equation_batch &Batch = Model->EquationBatches[BatchIdx];
FOR_ALL_BATCH_EQUATIONS(Batch,
Units[UnitIndex].Handles.push_back(Equation.Handle);
)
}
}
SetupStorageStructureSpecifer(DataSet->ResultStorageStructure, DataSet->IndexCounts, Model->FirstUnusedEquationHandle);
}
DataSet->ResultData = AllocClearedArray(double, DataSet->ResultStorageStructure.TotalCount * (Timesteps + 1)); //NOTE: We add one to timesteps since we also need space for the initial values.
}
//NOTE: Returns the numeric index corresponding to an index name and an index_set.
inline index_t
GetIndex(mobius_data_set *DataSet, index_set_h IndexSet, token_string IndexName)
{
auto &IndexMap = DataSet->IndexNamesToHandle[IndexSet.Handle];
auto Find = IndexMap.find(IndexName);
if(Find != IndexMap.end())
{
return {IndexSet, Find->second};
}
else
{
MOBIUS_FATAL_ERROR("ERROR: Tried the index name " << IndexName << " with the index set " << GetName(DataSet->Model, IndexSet) << ", but that index set does not contain that index." << std::endl);
}
return {IndexSet, 0};
}
static void
SetParameterValue(mobius_data_set *DataSet, const char *Name, const char * const *Indexes, size_t IndexCount, parameter_value Value, parameter_type Type)
{
if(!DataSet->AllIndexesHaveBeenSet)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to set a parameter value before all index sets have been filled with indexes" << std::endl);
}
if(DataSet->ParameterData == 0)
{
AllocateParameterStorage(DataSet);
}
const mobius_model *Model = DataSet->Model;
entity_handle ParameterHandle = GetParameterHandle(Model, Name);
if(Model->ParameterSpecs[ParameterHandle].Type != Type)
{
std::cout << "WARNING: Tried to set the value of the parameter " << Name << " with a value that was of the wrong type. This can lead to undefined behaviour." << std::endl;
}
//TODO: Check that the value is in the Min-Max range. (issue warning only)
size_t StorageUnitIndex = DataSet->ParameterStorageStructure.UnitForHandle[ParameterHandle];
std::vector<storage_unit_specifier> &Units = DataSet->ParameterStorageStructure.Units;
std::vector<index_set_h> &IndexSetDependencies = Units[StorageUnitIndex].IndexSets;
if(IndexCount != IndexSetDependencies.size())
{
MOBIUS_FATAL_ERROR("ERROR; Tried to set the value of the parameter " << Name << ", but an incorrect number of indexes were provided. Got " << IndexCount << ", expected " << IndexSetDependencies.size() << std::endl);
}
//TODO: This crashes if somebody have more than 256 index sets for a parameter, but that is highly unlikely. Still, this is not clean code...
index_t IndexValues[256];
for(size_t Level = 0; Level < IndexCount; ++Level)
{
IndexValues[Level] = GetIndex(DataSet, IndexSetDependencies[Level], Indexes[Level]);
}
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, IndexValues, IndexCount, DataSet->IndexCounts, ParameterHandle);
DataSet->ParameterData[Offset] = Value;
}
inline void
SetParameterValue(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes, double Value)
{
parameter_value Val;
Val.ValDouble = Value;
SetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), Val, ParameterType_Double);
}
inline void
SetParameterValue(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes, u64 Value)
{
parameter_value Val;
Val.ValUInt = Value;
SetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), Val, ParameterType_UInt);
}
inline void
SetParameterValue(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes, bool Value)
{
parameter_value Val;
Val.ValBool = Value;
SetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), Val, ParameterType_Bool);
}
inline void
SetParameterValue(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes, const char *TimeValue)
{
parameter_value Val;
bool ParseSuccess;
Val.ValTime = datetime(TimeValue, &ParseSuccess);
if(!ParseSuccess)
{
MOBIUS_FATAL_ERROR("ERROR: Unrecognized date format when setting the value of the parameter " << Name << std::endl);
}
SetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), Val, ParameterType_Time);
}
static parameter_value
GetParameterValue(mobius_data_set *DataSet, const char *Name, const char * const *Indexes, size_t IndexCount, parameter_type Type)
{
if(DataSet->ParameterData == 0)
{
MOBIUS_FATAL_ERROR("ERROR: Tried to get a parameter value before parameter storage was allocated." << std::endl);
}
const mobius_model *Model = DataSet->Model;
entity_handle ParameterHandle = GetParameterHandle(Model, Name);
if(Model->ParameterSpecs[ParameterHandle].Type != Type)
{
std::cout << "WARNING: Tried to get the value of the parameter " << Name << " specifying the wrong type for the parameter. This can lead to undefined behaviour." << std::endl;
}
size_t StorageUnitIndex = DataSet->ParameterStorageStructure.UnitForHandle[ParameterHandle];
std::vector<storage_unit_specifier> &Units = DataSet->ParameterStorageStructure.Units;
std::vector<index_set_h> &IndexSetDependencies = Units[StorageUnitIndex].IndexSets;
if(IndexCount != IndexSetDependencies.size())
{
MOBIUS_FATAL_ERROR("ERROR; Tried to get the value of the parameter " << Name << ", but an incorrect number of indexes were provided. Got " << IndexCount << ", expected " << IndexSetDependencies.size() << std::endl);
}
//TODO: This crashes if somebody have more than 256 index sets for a parameter, but that is highly unlikely. Still, this is not clean code...
index_t IndexValues[256];
for(size_t Level = 0; Level < IndexCount; ++Level)
{
IndexValues[Level] = GetIndex(DataSet, IndexSetDependencies[Level], Indexes[Level]);
}
size_t Offset = OffsetForHandle(DataSet->ParameterStorageStructure, IndexValues, IndexCount, DataSet->IndexCounts, ParameterHandle);
return DataSet->ParameterData[Offset];
}
inline double
GetParameterDouble(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes)
{
return GetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), ParameterType_Double).ValDouble;
}
inline u64
GetParameterUInt(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes)
{
return GetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), ParameterType_UInt).ValUInt;
}
inline bool
GetParameterBool(mobius_data_set *DataSet, const char *Name, const std::vector<const char *>& Indexes)
{
return GetParameterValue(DataSet, Name, Indexes.data(), Indexes.size(), ParameterType_Bool).ValBool;
}
//TODO: GetParameterTime? But what format should it use?
//NOTE: Is used by cumulation equations when they evaluate.
static double
CumulateResult(mobius_data_set *DataSet, equation_h Equation, index_set_h CumulateOverIndexSet, index_t *CurrentIndexes, double *LookupBase)
{
//NOTE: LookupBase should always be DataSet->ResultData + N*DataSet->ResultStorageStructure.TotalCount. I.e. it should point to the beginning of one timestep.
double Total = 0.0;
size_t SubsequentOffset;
size_t Offset = OffsetForHandle(DataSet->ResultStorageStructure, CurrentIndexes, DataSet->IndexCounts, CumulateOverIndexSet, SubsequentOffset, Equation.Handle);
double *Lookup = LookupBase + Offset;
for(index_t Index = {CumulateOverIndexSet, 0}; Index < DataSet->IndexCounts[CumulateOverIndexSet.Handle]; ++Index)
{
Total += *Lookup;
Lookup += SubsequentOffset;
}
return Total;
}
static double
CumulateResult(mobius_data_set *DataSet, equation_h Equation, index_set_h CumulateOverIndexSet, index_t *CurrentIndexes, double *LookupBase, parameter_double_h Weight)