/
datastoreinterface.cpp
1090 lines (766 loc) · 27.3 KB
/
datastoreinterface.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
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// Data store interface
//==============================================================================
#ifdef OpenCOR_MAIN
#include "cliutils.h"
#else
#include "corecliutils.h"
#endif
#include "datastoreinterface.h"
#include "solverinterface.h"
//==============================================================================
#include <QThread>
//==============================================================================
namespace OpenCOR {
//==============================================================================
extern "C" Q_DECL_EXPORT int dataStoreInterfaceVersion()
{
// Version of the data store interface
return 5;
}
//==============================================================================
namespace DataStore {
//==============================================================================
DataStoreArray::DataStoreArray(quint64 pSize) :
mSize(pSize)
{
// Allocate our data
mData = new double[pSize] {};
}
//==============================================================================
quint64 DataStoreArray::size() const
{
// Return our size
return mSize;
}
//==============================================================================
double * DataStoreArray::data() const
{
// Return our data
return mData;
}
//==============================================================================
double DataStoreArray::data(quint64 pPosition) const
{
// Return the data value at the given position
if ((pPosition < mSize) && (mData != nullptr)) {
return mData[pPosition];
}
return qQNaN();
}
//==============================================================================
void DataStoreArray::reset()
{
// Reset our data
memset(mData, 0, mSize*Solver::SizeOfDouble);
}
//==============================================================================
void DataStoreArray::hold()
{
// Increment our reference counter
++mReferenceCounter;
}
//==============================================================================
void DataStoreArray::release()
{
// Decrement our reference counter, and delete our data and ourselves, if
// needed
if (--mReferenceCounter == 0) {
delete[] mData;
delete this;
}
}
//==============================================================================
DataStoreValue::DataStoreValue(double *pValue) :
mValue(pValue)
{
}
//==============================================================================
QString DataStoreValue::uri() const
{
// Return our URI
return mUri;
}
//==============================================================================
void DataStoreValue::setUri(const QString &pUri)
{
// Set our URI
mUri = pUri;
}
//==============================================================================
double DataStoreValue::value() const
{
// Return our value
if (mValue != nullptr) {
return *mValue;
}
return qQNaN();
}
//==============================================================================
void DataStoreValue::setValue(double pValue)
{
// Set our value
if (mValue != nullptr) {
*mValue = pValue;
}
}
//==============================================================================
DataStoreValues::DataStoreValues(DataStoreArray *pDataStoreArray)
{
// Create a list of DataStoreValue objects
double *data = pDataStoreArray->data();
for (quint64 i = 0, iMax = pDataStoreArray->size(); i < iMax; ++i) {
*this << new DataStoreValue(data);
++data;
}
}
//==============================================================================
DataStoreValues::~DataStoreValues()
{
// Delete our DataStoreValue objects
for (DataStoreValue *dataStoreValue : *this) {
delete dataStoreValue;
}
clear();
}
//==============================================================================
DataStoreVariableRun::DataStoreVariableRun(quint64 pCapacity, double *pValue) :
mCapacity(pCapacity),
mValue(pValue)
{
// Create our array of values
mArray = new DataStoreArray(mCapacity);
}
//==============================================================================
DataStoreVariableRun::~DataStoreVariableRun()
{
// Delete some internal objects
mArray->release();
}
//==============================================================================
quint64 DataStoreVariableRun::size() const
{
// Return our size
return mSize;
}
//==============================================================================
void DataStoreVariableRun::addValue()
{
// Set the value of the variable at the given position
if ((mSize < mCapacity) && (mValue != nullptr)) {
mArray->data()[mSize] = *mValue;
++mSize;
}
}
//==============================================================================
void DataStoreVariableRun::addValue(double pValue)
{
// Set the value of the variable at the given position using the given value
if (mSize < mCapacity) {
mArray->data()[mSize] = pValue;
++mSize;
}
}
//==============================================================================
DataStoreArray * DataStoreVariableRun::array() const
{
// Return our array
return mArray;
}
//==============================================================================
double DataStoreVariableRun::value(quint64 pPosition) const
{
// Return the value at the given position
return (pPosition < mSize)?
mArray->data()[pPosition]:
qQNaN();
}
//==============================================================================
double * DataStoreVariableRun::values() const
{
// Return our values
return mArray->data();
}
//==============================================================================
DataStoreVariable::DataStoreVariable(double *pValue) :
mValue(pValue)
{
}
//==============================================================================
DataStoreVariable::~DataStoreVariable()
{
// Delete some internal objects
for (auto run : mRuns) {
delete run;
}
}
//==============================================================================
bool DataStoreVariable::compare(DataStoreVariable *pVariable1,
DataStoreVariable *pVariable2)
{
// Determine which of the two variables should be first based on their URI
// Note: the comparison is case insensitive, so that it's easier for people
// to find a variable...
return pVariable1->uri().compare(pVariable2->uri(), Qt::CaseInsensitive) < 0;
}
//==============================================================================
bool DataStoreVariable::isVisible() const
{
// Return whether we are visible, i.e. we have a non-empty URI
// Note: this applies to CellML 1.1 file where we keep track of all the
// model parameters (including imported ones), but should only export
// those that the user can see in the GUI (e.g. in the Simulation
// Experiment view). See issues #568 and #808 for some background on
// this...
return !mUri.isEmpty();
}
//==============================================================================
int DataStoreVariable::runsCount() const
{
// Return our number of runs
return mRuns.count();
}
//==============================================================================
bool DataStoreVariable::addRun(quint64 pCapacity)
{
// Try to add a run of the given capacity
try {
mRuns << new DataStoreVariableRun(pCapacity, mValue);
} catch (...) {
return false;
}
return true;
}
//==============================================================================
void DataStoreVariable::keepRuns(int pRunsCount)
{
// Keep the given number of runs
while (mRuns.count() > pRunsCount) {
delete mRuns.last();
mRuns.removeLast();
}
}
//==============================================================================
int DataStoreVariable::type() const
{
// Return our type
return mType;
}
//==============================================================================
void DataStoreVariable::setType(int pType)
{
// Set our type
mType = pType;
}
//==============================================================================
QString DataStoreVariable::uri() const
{
// Return our URI
return mUri;
}
//==============================================================================
void DataStoreVariable::setUri(const QString &pUri)
{
// Set our URI
mUri = pUri;
}
//==============================================================================
QString DataStoreVariable::name() const
{
// Return our name
return mName;
}
//==============================================================================
void DataStoreVariable::setName(const QString &pName)
{
// Set our name
mName = pName;
}
//==============================================================================
QString DataStoreVariable::unit() const
{
// Return our unit
return mUnit;
}
//==============================================================================
void DataStoreVariable::setUnit(const QString &pUnit)
{
// Set our unit
mUnit = pUnit;
}
//==============================================================================
quint64 DataStoreVariable::size(int pRun) const
{
// Return our size for the given run
if (mRuns.isEmpty()) {
return 0;
}
if (pRun == -1) {
return mRuns.last()->size();
}
return ((pRun >= 0) && (pRun < mRuns.count()))?
mRuns[pRun]->size():
0;
}
//==============================================================================
DataStoreArray * DataStoreVariable::array(int pRun) const
{
// Return the array for the given run, if any
if (mRuns.isEmpty()) {
return nullptr;
}
if (pRun == -1) {
return mRuns.last()->array();
}
return ((pRun >= 0) && (pRun < mRuns.count()))?
mRuns[pRun]->array():
nullptr;
}
//==============================================================================
void DataStoreVariable::addValue()
{
// Add a value to our current (i.e. last) run
if (!mRuns.isEmpty()) {
mRuns.last()->addValue();
}
}
//==============================================================================
void DataStoreVariable::addValue(double pValue, int pRun)
{
// Add the given value to our current (i.e. last) run
if (!mRuns.isEmpty()) {
if (pRun == -1) {
mRuns.last()->addValue(pValue);
} else if ((pRun >= 0) && (pRun < mRuns.count())) {
mRuns[pRun]->addValue(pValue);
}
}
}
//==============================================================================
double DataStoreVariable::value(quint64 pPosition, int pRun) const
{
// Return the value at the given position and this for the given run
if (mRuns.isEmpty()) {
return qQNaN();
}
if (pRun == -1) {
return mRuns.last()->value(pPosition);
}
return ((pRun >= 0) && (pRun < mRuns.count()))?
mRuns[pRun]->value(pPosition):
qQNaN();
}
//==============================================================================
double * DataStoreVariable::values(int pRun) const
{
// Return the values for the given run, if any
if (mRuns.isEmpty()) {
return nullptr;
}
if (pRun == -1) {
return mRuns.last()->values();
}
return ((pRun >= 0) && (pRun < mRuns.count()))?
mRuns[pRun]->values():
nullptr;
}
//==============================================================================
double DataStoreVariable::value() const
{
// Return the value to be added next
if (mValue != nullptr) {
return *mValue;
}
return qQNaN();
}
//==============================================================================
void DataStoreVariable::setValue(double pValue)
{
// Set the value to be added next
if (mValue != nullptr) {
*mValue = pValue;
}
}
//==============================================================================
DataStoreData::DataStoreData(const QString &pFileName) :
mFileName(pFileName)
{
}
//==============================================================================
QString DataStoreData::fileName() const
{
// Return our file name
return mFileName;
}
//==============================================================================
DataStoreImportData::DataStoreImportData(const QString &pFileName,
DataStore *pImportDataStore,
DataStore *pResultsDataStore,
int pNbOfVariables,
quint64 pNbOfDataPoints,
const QList<quint64> &pRunSizes) :
DataStoreData(pFileName),
mImportDataStore(pImportDataStore),
mResultsDataStore(pResultsDataStore),
mNbOfVariables(pNbOfVariables),
mNbOfDataPoints(pNbOfDataPoints),
mRunSizes(pRunSizes),
mOneOverTotalProgress(1.0/pNbOfDataPoints)
{
// Initialise our hierarchy
static int importNb = 0;
mHierarchy = QStringList() << "imports" << QString("import_%1").arg(++importNb);
// Allocate space for our import/results values, as well as add the required
// number of variables for our import/results data store and a run that will
// contain all of our raw/computed imported data in our import/results data
// store
// Note: we make several calls to DataStore::addVariable() rather than one
// big one to DataStore::addVariables() in case we can't allocate
// enough memory, in which case we will need to remove the variables
// we have added, and a failing call to DataStore::addVariables()
// wouldn't allow us to do this since that call wouldn't actually
// return...
try {
mImportValues = new double[pNbOfVariables] {};
mResultsValues = new double[pNbOfVariables] {};
for (int i = 0; i < pNbOfVariables; ++i) {
mImportVariables << pImportDataStore->addVariable(mImportValues+i);
mResultsVariables << pResultsDataStore->addVariable(mResultsValues+i);
}
if (!pImportDataStore->addRun(pNbOfDataPoints)) {
throw std::exception();
}
if (!pRunSizes.isEmpty()) {
for (auto runSize : pRunSizes) {
for (auto resultsVariables : mResultsVariables) {
if (!resultsVariables->addRun(runSize)) {
throw std::exception();
}
}
}
}
} catch (...) {
// Something went wrong, so release the memory that was directly or
// indirectly allocated
mValid = false;
delete mImportValues;
delete mResultsValues;
mImportValues = nullptr;
mResultsValues = nullptr;
pImportDataStore->removeVariables(mImportVariables);
pResultsDataStore->removeVariables(mResultsVariables);
}
}
//==============================================================================
DataStoreImportData::~DataStoreImportData()
{
// Delete some internal objects
// Note: we must not delete mResultsValues since it is to be deleted by
// whoever used us (e.g. a Simulation object)...
delete mImportValues;
}
//==============================================================================
bool DataStoreImportData::valid() const
{
// Return whether we are valid
return mValid;
}
//==============================================================================
DataStore * DataStoreImportData::importDataStore() const
{
// Return our import data store
return mImportDataStore;
}
//==============================================================================
DataStore * DataStoreImportData::resultsDataStore() const
{
// Return our results data store
return mResultsDataStore;
}
//==============================================================================
QStringList DataStoreImportData::hierarchy() const
{
// Return our hierarchy
return mHierarchy;
}
//==============================================================================
int DataStoreImportData::nbOfVariables() const
{
// Return our number of variables
return mNbOfVariables;
}
//==============================================================================
quint64 DataStoreImportData::nbOfDataPoints() const
{
// Return our number of data points
return mNbOfDataPoints;
}
//==============================================================================
double * DataStoreImportData::importValues() const
{
// Return our import values
return mImportValues;
}
//==============================================================================
double * DataStoreImportData::resultsValues() const
{
// Return our results values
return mResultsValues;
}
//==============================================================================
DataStoreVariables DataStoreImportData::importVariables() const
{
// Return our import variables
return mImportVariables;
}
//==============================================================================
DataStoreVariables DataStoreImportData::resultsVariables() const
{
// Return our results variables
return mResultsVariables;
}
//==============================================================================
QList<quint64> DataStoreImportData::runSizes() const
{
// Return our run sizes
return mRunSizes;
}
//==============================================================================
double DataStoreImportData::progress()
{
// Increase and return our normalised progress
return (++mProgress)*mOneOverTotalProgress;
}
//==============================================================================
DataStoreExportData::DataStoreExportData(const QString &pFileName, DataStore *pDataStore,
const DataStoreVariables &pVariables) :
DataStoreData(pFileName),
mDataStore(pDataStore),
mVariables(pVariables)
{
}
//==============================================================================
DataStore * DataStoreExportData::dataStore() const
{
// Return our data store
return mDataStore;
}
//==============================================================================
DataStoreVariables DataStoreExportData::variables() const
{
// Return our variables
return mVariables;
}
//==============================================================================
DataStore::DataStore(const QString &pUri) :
mUri(pUri),
mVoi(new DataStoreVariable())
{
}
//==============================================================================
DataStore::~DataStore()
{
// Delete some internal objects
delete mVoi;
for (auto variable : mVariables) {
delete variable;
}
}
//==============================================================================
QString DataStore::uri() const
{
// Return our URI
return mUri;
}
//==============================================================================
int DataStore::runsCount() const
{
// Return our number of runs, i.e. the number of runs for our VOI, for
// example
return mVoi->runsCount();
}
//==============================================================================
bool DataStore::addRun(quint64 pCapacity)
{
// Try to add a run to our VOI and all our variables
int oldRunsCount = mVoi->runsCount();
try {
if (!mVoi->addRun(pCapacity)) {
throw std::exception();
}
for (auto variable : mVariables) {
if (!variable->addRun(pCapacity)) {
throw std::exception();
}
}
} catch (...) {
// We couldn't add a run to our VOI and all our variables, so only keep
// the number of runs we used to have
mVoi->keepRuns(oldRunsCount);
for (auto variable : mVariables) {
variable->keepRuns(oldRunsCount);
}
return false;
}
return true;
}
//==============================================================================
quint64 DataStore::size(int pRun) const
{
// Return our size, i.e. the size of our VOI, for example
return mVoi->size(pRun);
}
//==============================================================================
DataStoreVariable * DataStore::voi() const
{
// Return our VOI
return mVoi;
}
//==============================================================================
DataStoreVariables DataStore::variables()
{
// Return all our variables, after making sure that they are sorted
std::sort(mVariables.begin(), mVariables.end(), DataStoreVariable::compare);
return mVariables;
}
//==============================================================================
DataStoreVariables DataStore::voiAndVariables()
{
// Return our VOI, if any, and all our variables, after making sure that
// they are sorted
DataStoreVariables variables;
variables << mVoi << mVariables;
std::sort(variables.begin(), variables.end(), DataStoreVariable::compare);
return variables;
}
//==============================================================================
DataStoreVariables DataStore::addVariables(double *pValues, int pCount)
{
// Add some variables to our data store
DataStoreVariables variables;
for (int i = 0; i < pCount; ++i, ++pValues) {
variables << new DataStoreVariable(pValues);
}
mVariables << variables;
return variables;
}
//==============================================================================
DataStoreVariable * DataStore::addVariable(double *pValue)
{
// Add a variable to our data store
auto variable = new DataStoreVariable(pValue);
mVariables << variable;
return variable;
}
//==============================================================================
void DataStore::removeVariables(const DataStoreVariables &pVariables)
{
// Remove the given variables from our data store
for (auto variable : pVariables) {
delete variable;
mVariables.removeOne(variable);
}
}
//==============================================================================
void DataStore::removeVariable(DataStoreVariable *pVariable)
{
// Remove the given variable from our data store
delete pVariable;
mVariables.removeOne(pVariable);
}
//==============================================================================
void DataStore::addValues(double pVoiValue)
{
// Set the value at the mSize position of all our variables including our
// VOI, which value is directly given to us
// Note: it is very important to add the VOI value last since our size()
// method relies on it to determine our size. So, if we were to add
// the VOI value first, we might in some cases (see issue #1579 for
// example) end up with the wrong size...
for (auto variable : mVariables) {
variable->addValue();
}
mVoi->addValue(pVoiValue);
}
//==============================================================================
DataStoreImporterWorker::DataStoreImporterWorker(DataStoreImportData *pImportData) :
mImportData(pImportData)
{
}
//==============================================================================