forked from BhallaLab/moose-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStoich.cpp
2015 lines (1812 loc) · 64.1 KB
/
Stoich.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
/**********************************************************************
** This program is part of 'MOOSE', the
** Messaging Object Oriented Simulation Environment.
** Copyright (C) 2003-2010 Upinder S. Bhalla. and NCBS
** It is made available under the terms of the
** GNU Lesser General Public License version 2.1
** See the file COPYING.LIB for the full notice.
**********************************************************************/
class KsolveBase;
#include "../basecode/header.h"
#include "../basecode/ElementValueFinfo.h"
#include "../kinetics/PoolBase.h"
#include "../kinetics/EnzBase.h"
#include "FuncTerm.h"
#include "RateTerm.h"
#include "FuncRateTerm.h"
#include "../basecode/SparseMatrix.h"
#include "KinSparseMatrix.h"
#include "VoxelPoolsBase.h"
#include "../mesh/VoxelJunction.h"
#include "XferInfo.h"
#include "KsolveBase.h"
#include "../builtins/Variable.h"
#include "../builtins/Function.h"
#include "Stoich.h"
#include "../kinetics/Reac.h"
#include "../kinetics/lookupVolumeFromMesh.h"
#include "../scheduling/Clock.h"
#include "../shell/Shell.h"
#include "../shell/Wildcard.h"
#include "../utility/testing_macros.hpp"
const Cinfo* Stoich::initCinfo()
{
//////////////////////////////////////////////////////////////
// Field Definitions
//////////////////////////////////////////////////////////////
/*
static ElementValueFinfo<Stoich, string> path(
"path", "Wildcard path for reaction system handled by Stoich",
&Stoich::setPath, &Stoich::getPath);
*/
static ElementValueFinfo<Stoich, string> reacSystemPath(
"reacSystemPath", "Wildcard path for reaction system handled by Stoich",
&Stoich::setReacSystemPath, &Stoich::getReacSystemPath);
static ValueFinfo<Stoich, Id> ksolve(
"ksolve",
"Id of Kinetic reaction solver class that works with this "
"Stoich. "
" Must be of class Ksolve, or Gsolve (at present) "
" Must be assigned before the path is set.",
&Stoich::setKsolve, &Stoich::getKsolve);
static ValueFinfo<Stoich, Id> dsolve(
"dsolve",
"Id of Diffusion solver class that works with this Stoich."
" Must be of class Dsolve "
" If left unset then the system will be assumed to work in a"
" non-diffusive, well-stirred cell. If it is going to be "
" used it must be assigned before the path is set.",
&Stoich::setDsolve, &Stoich::getDsolve);
static ValueFinfo<Stoich, Id> compartment(
"compartment",
"Id of chemical compartment class that works with this Stoich."
" Must be derived from class ChemCompt."
" If left unset then the system will be assumed to work in a"
" non-diffusive, well-stirred cell. If it is going to be "
" used it must be assigned before the path is set.",
&Stoich::setCompartment, &Stoich::getCompartment);
static ValueFinfo<Stoich, bool> allowNegative(
"allowNegative",
"Flag: allow negative values if true. Default is false."
" This is used to protect the chemical system from going unstable"
" in cases where the numerical integration gives a negative value."
" Typically it is a small negative value but is obviously"
" physically impossible. In some cases we want to use the "
" solvers to handle general systems of equations (not purely "
" chemical ones), so we have this flag to allow it.",
&Stoich::setAllowNegative, &Stoich::getAllowNegative);
static ReadOnlyValueFinfo<Stoich, unsigned int> numVarPools(
"numVarPools",
"Number of time-varying pools to be computed by the "
"numerical engine",
&Stoich::getNumVarPools);
static ReadOnlyValueFinfo<Stoich, unsigned int> numBufPools(
"numBufPools",
"Number of buffered pools to be computed by the "
"numerical engine.",
&Stoich::getNumBufPools);
static ReadOnlyValueFinfo<Stoich, unsigned int> numFuncPools(
"numFuncPools",
"Number of pools controlled by functions computed by the "
"numerical engine.",
&Stoich::getNumFuncPools);
static ReadOnlyValueFinfo<Stoich, unsigned int> numAllPools(
"numAllPools",
"Total number of pools handled by the numerical engine. "
"This includes variable ones, buffered ones, and functions. "
"It includes local pools as well as cross-solver proxy pools.",
&Stoich::getNumAllPools);
static ReadOnlyValueFinfo<Stoich, unsigned int> numProxyPools(
"numProxyPools",
"Number of pools here by proxy as substrates of a cross-"
"compartment reaction.",
&Stoich::getNumProxyPools);
static ReadOnlyValueFinfo<Stoich, vector<unsigned int>> poolIdMap(
"poolIdMap",
"Map to look up the index of the pool from its Id."
"poolIndex = poolIdMap[ Id::value() - poolOffset ] "
"where the poolOffset is the smallest Id::value. "
"poolOffset is passed back as the last entry of this vector."
" Any Ids that are not pools return EMPTY=~0. ",
&Stoich::getPoolIdMap);
static ReadOnlyValueFinfo<Stoich, unsigned int> numRates(
"numRates", "Total number of rate terms in the reaction system.",
&Stoich::getNumRates);
// Stuff here for getting Stoichiometry matrix to manipulate in
// Python.
static ReadOnlyValueFinfo<Stoich, vector<int>> matrixEntry(
"matrixEntry",
"The non-zero matrix entries in the sparse matrix. Their"
"column indices are in a separate vector and the row"
"information in a third",
&Stoich::getMatrixEntry);
// Stuff here for getting Stoichiometry matrix to manipulate in
// Python.
static ReadOnlyValueFinfo<Stoich, vector<unsigned int>> columnIndex(
"columnIndex", "Column Index of each matrix entry",
&Stoich::getColIndex);
// Stuff here for getting Stoichiometry matrix to manipulate in
// Python.
static ReadOnlyValueFinfo<Stoich, vector<unsigned int>> rowStart(
"rowStart", "Row start for each block of entries and column indices",
&Stoich::getRowStart);
// Proxy pool information
static ReadOnlyLookupValueFinfo<Stoich, Id, vector<Id>> proxyPools(
"proxyPools",
"Return vector of proxy pools for X-compt reactions between "
"current stoich, and the argument, which is a StoichId. "
"The returned pools belong to the compartment handling the "
"Stoich specified in the argument. "
"If no pools are found, return an empty vector.",
&Stoich::getProxyPools);
static ReadOnlyValueFinfo<Stoich, int> status(
"status",
"Status of Stoich in the model building process. Values are: "
"-1: Reaction path not yet assigned.\n "
"0: Successfully built stoichiometry matrix.\n "
"1: Warning: Missing a reactant in a Reac or Enz.\n "
"2: Warning: Missing a substrate in an MMenz.\n "
"3: Warning: Missing substrates as well as reactants.\n "
"4: Warning: Compartment not defined.\n "
"8: Warning: Neither Ksolve nor Dsolve defined.\n "
"16: Warning: No objects found on path.\n "
"",
&Stoich::getStatus);
//////////////////////////////////////////////////////////////
// MsgDest Definitions
//////////////////////////////////////////////////////////////
static DestFinfo unzombify("unzombify",
"Restore all zombies to their native state",
new OpFunc0<Stoich>(&Stoich::unZombifyModel));
static DestFinfo scaleBufsAndRates(
"scaleBufsAndRates",
"Args: voxel#, volRatio\n"
"Handles requests for runtime volume changes in the specified "
"voxel#, Used in adaptors changing spine vols.",
new OpFunc2<Stoich, unsigned int, double>(&Stoich::scaleBufsAndRates));
//////////////////////////////////////////////////////////////
// SrcFinfo Definitions
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// SharedMsg Definitions
//////////////////////////////////////////////////////////////
static Finfo* stoichFinfos[] = {
// &path, // ElementValue, deprecated.
&reacSystemPath, // ElementValue
&ksolve, // Value
&dsolve, // Value
&compartment, // Value
&allowNegative, // Value
&numVarPools, // ReadOnlyValue
&numBufPools, // ReadOnlyValue
&numFuncPools, // ReadOnlyValue
&numAllPools, // ReadOnlyValue
&numProxyPools, // ReadOnlyValue
&poolIdMap, // ReadOnlyValue
&numRates, // ReadOnlyValue
&matrixEntry, // ReadOnlyValue
&columnIndex, // ReadOnlyValue
&rowStart, // ReadOnlyValue
&proxyPools, // ReadOnlyLookupValue
&status, // ReadOnlyLookupValue
&unzombify, // DestFinfo
&scaleBufsAndRates, // DestFinfo
};
static Dinfo<Stoich> dinfo;
static Cinfo stoichCinfo("Stoich", Neutral::initCinfo(), stoichFinfos,
sizeof(stoichFinfos) / sizeof(Finfo*), &dinfo);
return &stoichCinfo;
}
//////////////////////////////////////////////////////////////
// Class definitions
//////////////////////////////////////////////////////////////
static const Cinfo* stoichCinfo = Stoich::initCinfo();
//////////////////////////////////////////////////////////////
Stoich::Stoich()
: useOneWay_(false),
allowNegative_(false),
wildcard_(""),
ksolve_(), // Must be reassigned to build stoich system.
dsolve_(), // Must be assigned if diffusion is planned.
compartment_(), // Must be assigned if diffusion is planned.
kinterface_(nullptr),
dinterface_(nullptr),
rates_(0), // No RateTerms yet.
// uniqueVols_( 1, 1.0 ),
numVoxels_(1),
status_(-1)
{
;
}
Stoich::~Stoich()
{
unZombifyModel();
// Note that we cannot do the unZombify here, because it is too
// prone to problems with the ordering of the delete operations
// relative to the zombies.
for(vector<RateTerm*>::iterator j = rates_.begin(); j != rates_.end(); ++j)
delete *j;
for(vector<FuncTerm*>::iterator j = funcs_.begin(); j != funcs_.end(); ++j)
delete *j;
}
//////////////////////////////////////////////////////////////
// Field Definitions
//////////////////////////////////////////////////////////////
void Stoich::setOneWay(bool v)
{
useOneWay_ = v;
}
bool Stoich::getOneWay() const
{
return useOneWay_;
}
void Stoich::setAllowNegative(bool v)
{
allowNegative_ = v;
}
bool Stoich::getAllowNegative() const
{
return allowNegative_;
}
void Stoich::setPath(const Eref& e, string v)
{
cout << "DeprecationWarning:: Use Soitch::readSystemPath instead. In "
"the future, it will be an error."
<< endl;
setReacSystemPath(e, v);
}
void Stoich::setReacSystemPath(const Eref& e, string v)
{
if(wildcard_ != "" && wildcard_ != v) {
// unzombify( wildcard_ );
cerr << __func__ << ":: need to clear old reacSystemPath." << endl;
status_ = -1;
return;
}
if(ksolve_ == Id()) {
cout << "Stoich::setReacSystemPath: need to first set ksolve.\n";
status_ = -1;
return;
}
vector<ObjId> elist;
wildcard_ = v;
wildcardFind(wildcard_, elist);
setElist(e, elist);
}
void convWildcards(vector<Id>& ret, const vector<ObjId>& elist)
{
ret.resize(elist.size());
for(unsigned int i = 0; i < elist.size(); ++i)
ret[i] = elist[i].id;
}
void filterWildcards(vector<Id>& ret, const vector<ObjId>& elist)
{
ret.clear();
ret.reserve(elist.size());
for(vector<ObjId>::const_iterator i = elist.begin(); i != elist.end();
++i) {
if(i->element()->cinfo()->isA("PoolBase") ||
i->element()->cinfo()->isA("Reac") ||
i->element()->cinfo()->isA("EnzBase") ||
i->element()->cinfo()->isA("Function"))
ret.push_back(i->id);
}
}
void Stoich::setElist(const Eref& e, const vector<ObjId>& elist)
{
if(compartment_ == Id()) {
cerr << "Warning: Stoich::setElist/setReacSystemPath: Compartment not "
"set. Aborting." << endl;
status_ = 4;
return;
}
if(!(kinterface_ || dinterface_)) {
cerr << "Warning: Stoich::setElist/setReacSystemPath: Neither solver "
"has been set. Aborting." << endl;
status_ = 8;
return;
}
status_ = 0;
if(kinterface_)
kinterface_->setCompartment(compartment_);
if(dinterface_)
dinterface_->setCompartment(compartment_);
vector<Id> temp;
filterWildcards(temp, elist);
if(temp.size() == 0) {
cerr << "Warning: Stoich::setElist/setReacSystemPath: No kinetics objects "
"found on path. Aborting." << endl;
status_ = 16;
return;
}
// allocateObjMap( temp );
deAllocateModel();
allocateModel(temp);
if(kinterface_) {
// kinterface_->setNumPools( n );
kinterface_->setStoich(e.id());
Shell* shell = reinterpret_cast<Shell*>(Id().eref().data());
shell->doAddMsg("Single", compartment_, "voxelVolOut", ksolve_,
"voxelVol");
}
if(dinterface_) {
// dinterface_->setNumPools( n );
dinterface_->setStoich(e.id());
}
zombifyModel(e, temp);
if(kinterface_) {
kinterface_->setDsolve(dsolve_);
kinterface_->updateRateTerms();
}
}
//////////////////////////////////////////////////////////////////////
string Stoich::getPath(const Eref& e) const
{
cout << "DeprecationWarning:: Use Soitch::reacSystemPath instead. In "
"the future, it will be an error."
<< endl;
return getReacSystemPath(e);
}
string Stoich::getReacSystemPath(const Eref& e) const
{
return wildcard_;
}
void Stoich::setKsolve(Id ksolve)
{
ksolve_ = Id();
kinterface_ = nullptr;
if(!(ksolve.element()->cinfo()->isA("Ksolve") ||
ksolve.element()->cinfo()->isA("Gsolve"))) {
cout << "Error: Stoich::setKsolve: invalid class assigned,"
" should be either Ksolve or Gsolve\n";
return;
}
ksolve_ = ksolve;
kinterface_ = reinterpret_cast<KsolveBase*>(ksolve.eref().data());
if(ksolve.element()->cinfo()->isA("Gsolve"))
setOneWay(true);
else
setOneWay(false);
}
Id Stoich::getKsolve() const
{
return ksolve_;
}
void Stoich::setDsolve(Id dsolve)
{
dsolve_ = Id();
dinterface_ = 0;
if(not(dsolve.element()->cinfo()->isA("Dsolve"))) {
cout << "Error: Stoich::setDsolve: invalid class assigned,"
" should be Dsolve\n";
return;
}
dsolve_ = dsolve;
dinterface_ = reinterpret_cast<KsolveBase*>(dsolve.eref().data());
}
Id Stoich::getDsolve() const
{
return dsolve_;
}
void Stoich::setCompartment(Id compartment)
{
if(!(compartment.element()->cinfo()->isA("ChemCompt"))) {
cerr << "Error: Stoich::setCompartment: invalid class assigned,"
" should be ChemCompt or derived class\n";
return;
}
compartment_ = compartment;
vector<double> temp;
vector<double> vols =
Field<vector<double>>::get(compartment, "voxelVolume");
if(vols.size() > 0) {
numVoxels_ = vols.size();
sort(vols.begin(), vols.end());
double bigVol = vols.back();
assert(bigVol > 0.0);
temp.push_back(vols[0] / bigVol);
for(auto i = vols.begin(); i != vols.end(); ++i) {
if(!doubleEq(temp.back(), *i / bigVol))
temp.push_back(*i / bigVol);
}
}
}
Id Stoich::getCompartment() const
{
return compartment_;
}
unsigned int Stoich::getNumVarPools() const
{
return varPoolVec_.size();
}
unsigned int Stoich::getNumBufPools() const
{
return bufPoolVec_.size();
}
unsigned int Stoich::getNumFuncPools() const
{
return funcTargetPoolVec_.size();
}
unsigned int Stoich::getNumAllPools() const
{
return varPoolVec_.size() + offSolverPoolVec_.size() + funcTargetPoolVec_.size() + bufPoolVec_.size();
}
unsigned int Stoich::getNumProxyPools() const
{
return offSolverPoolVec_.size();
}
vector<unsigned int> Stoich::getPoolIdMap() const
{
if(poolLookup_.size() == 0)
return vector<unsigned int>(1, 0);
unsigned int minId = 1000000;
unsigned int maxId = 0;
unsigned int maxIndex = 0;
for(auto i = poolLookup_.cbegin(); i != poolLookup_.end(); ++i) {
unsigned int j = i->first.value();
if(j < minId)
minId = j;
if(j > maxId)
maxId = j;
if(maxIndex < i->second)
maxIndex = i->second;
}
vector<unsigned int> ret(maxId - minId + 2, ~0U);
for(auto i = poolLookup_.cbegin(); i != poolLookup_.end(); ++i) {
unsigned int j = i->first.value() - minId;
ret[j] = i->second;
}
ret[ret.size() - 1] = minId;
return ret;
}
Id Stoich::getPoolByIndex(unsigned int index) const
{
map<Id, unsigned int>::const_iterator i;
for(i = poolLookup_.begin(); i != poolLookup_.end(); ++i) {
if(i->second == index)
return i->first;
}
return Id();
}
unsigned int Stoich::getNumRates() const
{
return rates_.size();
}
/// Number of rate terms for reactions purely on this compartment.
unsigned int Stoich::getNumCoreRates() const
{
return reacVec_.size() * (1 + useOneWay_) +
enzVec_.size() * (2 + useOneWay_) + mmEnzVec_.size() +
incrementFuncVec_.size();
}
const RateTerm* Stoich::rates(unsigned int i) const
{
assert(i < rates_.size());
return rates_[i];
}
const vector<RateTerm*>& Stoich::getRateTerms() const
{
return rates_;
}
unsigned int Stoich::getNumFuncs() const
{
return funcs_.size();
}
const FuncTerm* Stoich::funcs(unsigned int i) const
{
assert(i < funcs_.size());
assert(funcs_[i]);
return funcs_[i];
}
bool Stoich::isFuncTarget(unsigned int poolIndex) const
{
assert(poolIndex < funcTarget_.size());
return (funcTarget_[poolIndex] != ~0U);
}
vector<int> Stoich::getMatrixEntry() const
{
return N_.matrixEntry();
}
vector<unsigned int> Stoich::getColIndex() const
{
return N_.colIndex();
}
vector<unsigned int> Stoich::getRowStart() const
{
return N_.rowStart();
}
vector<Id> Stoich::getProxyPools(Id i) const
{
static vector<Id> dummy;
if(!i.element()->cinfo()->isA("Stoich")) {
cout << "Warning: Stoich::getProxyPools: argument " << i
<< " is not a Stoich\n";
return dummy;
}
Id compt = Field<Id>::get(i, "compartment");
map<Id, vector<Id>>::const_iterator j = offSolverPoolMap_.find(compt);
if(j != offSolverPoolMap_.end())
return j->second;
return dummy;
}
int Stoich::getStatus() const
{
return status_;
}
//////////////////////////////////////////////////////////////
// Model setup functions
//////////////////////////////////////////////////////////////
/**
* Checks if specified reac is off solver. As side-effect it compiles
* a vector of the pools that are off-solver, and the corresponding
* compartments for those pools
*/
static bool isOffSolverReac(const Element* e, Id myCompt,
// vector< Id >& offSolverPools,
vector<Id>& poolCompts, map<Id, Id>& poolComptMap)
{
assert(myCompt != Id());
assert(myCompt.element()->cinfo()->isA("ChemCompt"));
bool ret = false;
vector<Id> neighbors;
e->getNeighbors(neighbors, e->cinfo()->findFinfo("subOut"));
vector<Id> n2;
e->getNeighbors(n2, e->cinfo()->findFinfo("prdOut"));
neighbors.insert(neighbors.end(), n2.begin(), n2.end());
for(vector<Id>::const_iterator j = neighbors.begin(); j != neighbors.end();
++j) {
Id otherCompt = getCompt(*j);
if(myCompt != otherCompt) {
// offSolverPools.push_back( *j );
poolCompts.push_back(otherCompt);
poolComptMap[*j] = otherCompt; // Avoids duplication of pools
ret = true;
if(j->element()->cinfo()->isA("BufPool")) {
cout << "Warning: Avoid BufPool: " << j->path()
<< "\n as reactant in cross-compartment reactions\n";
}
}
}
return ret;
}
/**
* Extracts and orders the compartments associated with a given reac.
*/
pair<Id, Id> extractCompts(const vector<Id>& compts)
{
pair<Id, Id> ret;
for(vector<Id>::const_iterator i = compts.begin(); i != compts.end(); ++i) {
if(ret.first == Id()) {
ret.first = *i;
}
else if(ret.first != *i) {
if(ret.second == Id())
ret.second = *i;
else {
cout << "Error: Stoich::extractCompts: more than 2 "
"compartments\n";
assert(0);
}
}
}
if((ret.second != Id()) && ret.second < ret.first) {
Id temp = ret.first;
ret.first = ret.second;
ret.second = ret.first;
}
return ret;
}
////// Is this used at all?
void Stoich::locateOffSolverReacs(Id myCompt, vector<Id>& elist)
{
map<Id, Id> poolComptMap; // < pool, compt >
vector<Id> temp;
temp.reserve(elist.size());
for(vector<Id>::const_iterator i = elist.begin(); i != elist.end(); ++i) {
const Element* e = i->element();
if(e->cinfo()->isA("Reac") || e->cinfo()->isA("EnzBase")) {
vector<Id> compts;
if(isOffSolverReac(e, myCompt, compts, poolComptMap)) {
if(e->cinfo()->isA("Reac")) {
offSolverReacVec_.push_back(*i);
offSolverReacCompts_.push_back(extractCompts(compts));
}
else if(e->cinfo()->isA("Enz")) {
offSolverEnzVec_.push_back(*i);
offSolverEnzCompts_.push_back(extractCompts(compts));
}
else if(e->cinfo()->isA("MMEnz")) {
offSolverMMenzVec_.push_back(*i);
offSolverMMenzCompts_.push_back(extractCompts(compts));
}
}
else {
temp.push_back(*i);
}
}
else {
temp.push_back(*i);
}
}
offSolverPoolMap_.clear();
for(map<Id, Id>::iterator i = poolComptMap.begin(); i != poolComptMap.end();
++i) {
// fill in the map for activeOffSolverPools.
offSolverPoolMap_[i->second].push_back(i->first);
}
// Ensure we don't have repeats, and the pools are ordered by compt
offSolverPoolVec_.clear();
for(map<Id, vector<Id>>::iterator i = offSolverPoolMap_.begin();
i != offSolverPoolMap_.end(); ++i) {
if(i->first != myCompt) {
offSolverPoolVec_.insert(offSolverPoolVec_.end(), i->second.begin(),
i->second.end());
}
}
elist = temp;
}
///////////////////////////////////////////////////////////////////
// Model allocation stuff here
///////////////////////////////////////////////////////////////////
/// Identifies and allocates objects in the Stoich.
void Stoich::allocateModelObject(Id id)
{
static const Cinfo* poolCinfo = Cinfo::find("Pool");
static const Cinfo* bufPoolCinfo = Cinfo::find("BufPool");
static const Cinfo* reacCinfo = Cinfo::find("Reac");
static const Cinfo* enzCinfo = Cinfo::find("Enz");
static const Cinfo* mmEnzCinfo = Cinfo::find("MMenz");
static const Cinfo* functionCinfo = Cinfo::find("Function");
static const Finfo* f1 = functionCinfo->findFinfo("valueOut");
static const SrcFinfo* sf = dynamic_cast<const SrcFinfo*>(f1);
assert(sf);
Element* ei = id.element();
if(ei->cinfo() == poolCinfo) {
// objMap_[ id.value() - objMapStart_ ] = numVarPools_;
varPoolVec_.push_back(id);
// ++numVarPools_;
}
else if(ei->cinfo() == bufPoolCinfo) {
bufPoolVec_.push_back(id);
}
else if(ei->cinfo() == mmEnzCinfo) {
mmEnzVec_.push_back(ei->id());
// objMap_[ id.value() - objMapStart_ ] = numReac_;
// ++numReac_;
}
else if(ei->cinfo() == reacCinfo) {
reacVec_.push_back(ei->id());
/*
if ( useOneWay_ ) {
objMap_[ id.value() - objMapStart_ ] = numReac_;
numReac_ += 2;
} else {
objMap_[ id.value() - objMapStart_ ] = numReac_;
++numReac_;
}
*/
}
else if(ei->cinfo() == enzCinfo) {
enzVec_.push_back(ei->id());
/*
if ( useOneWay_ ) {
objMap_[ id.value() - objMapStart_ ] = numReac_;
numReac_ += 3;
} else {
objMap_[ id.value() - objMapStart_ ] = numReac_;
numReac_ += 2;
}
*/
}
else if(ei->cinfo() == functionCinfo) {
vector<ObjId> tgt;
vector<string> func;
ei->getMsgTargetAndFunctions(0, sf, tgt, func);
if(func.size() > 0 && func[0] == "increment") {
incrementFuncVec_.push_back(ei->id());
// objMap_[ id.value() - objMapStart_ ] = numReac_;
// numReac_++;
}
else if(func.size() > 0 && func[0] == "setNumKf") {
reacFuncVec_.push_back(ei->id());
}
else // Assume it controls the N of a pool.
{
poolFuncVec_.push_back(ei->id());
// objMap_[ id.value() - objMapStart_ ] = numFunctions_;
// ++numFunctions_;
assert( tgt.size() == 1 );
funcTargetPoolVec_.push_back( tgt[0] );
}
}
}
// Sorts, unique, erase any extras.
void myUnique(vector<Id>& v)
{
sort(v.begin(), v.end());
vector<Id>::iterator last = unique(v.begin(), v.end());
v.erase(last, v.end());
}
void Stoich::clearFuncTargetPools()
{
vector< Id > temp;
for ( auto pid : varPoolVec_ ) {
if ( std::find( funcTargetPoolVec_.begin(), funcTargetPoolVec_.end(), pid ) == funcTargetPoolVec_.end() )
temp.push_back( pid );
}
varPoolVec_ = temp;
temp.clear();
for ( auto pid : bufPoolVec_ ) {
if ( std::find( funcTargetPoolVec_.begin(), funcTargetPoolVec_.end(), pid ) == funcTargetPoolVec_.end() )
temp.push_back( pid );
}
bufPoolVec_ = temp;
}
/// Using the computed array sizes, now allocate space for them.
void Stoich::resizeArrays()
{
myUnique(funcTargetPoolVec_);
myUnique(varPoolVec_);
myUnique(bufPoolVec_);
myUnique(offSolverPoolVec_);
myUnique(reacVec_);
myUnique(offSolverReacVec_);
myUnique(enzVec_);
myUnique(offSolverEnzVec_);
myUnique(mmEnzVec_);
myUnique(offSolverMMenzVec_);
clearFuncTargetPools();
unsigned int totNumPools =
varPoolVec_.size() + bufPoolVec_.size() + funcTargetPoolVec_.size() + offSolverPoolVec_.size();
species_.resize(totNumPools, 0);
funcTarget_.clear();
// Only the pools controlled by a func (targets) have positive indices.
funcTarget_.resize(totNumPools, ~0);
unsigned int totNumRates =
(reacVec_.size() + offSolverReacVec_.size()) * (1 + useOneWay_) +
(enzVec_.size() + offSolverEnzVec_.size()) * (2 + useOneWay_) +
mmEnzVec_.size() + offSolverMMenzVec_.size() + incrementFuncVec_.size();
rates_.resize(totNumRates, 0);
funcs_.resize(poolFuncVec_.size(), 0);
N_.setSize(totNumPools, totNumRates);
if(kinterface_)
kinterface_->setNumPools(totNumPools);
if(dinterface_) // Need to set both the numVar and numTot
dinterface_->setNumVarTotPools(varPoolVec_.size(), totNumPools);
}
/// Clear out any existing model data
void Stoich::deAllocateModel()
{
varPoolVec_.clear();
bufPoolVec_.clear();
funcTargetPoolVec_.clear();
reacVec_.clear();
enzVec_.clear();
mmEnzVec_.clear();
poolFuncVec_.clear();
incrementFuncVec_.clear();
reacFuncVec_.clear();
// Unclear where these are set up and used. The locateOffSolverReacs
// function doesn't seem to be called anywhere.
offSolverPoolVec_.clear();
offSolverReacVec_.clear();
offSolverEnzVec_.clear();
offSolverMMenzVec_.clear();
offSolverReacCompts_.clear();
offSolverEnzCompts_.clear();
offSolverMMenzCompts_.clear();
offSolverPoolMap_.clear();
// Clear out the rate terms
for ( auto j = rates_.begin(); j != rates_.end(); ++j ) {
delete *j;
}
rates_.clear();
// Clear out the funcs
for ( auto j = funcs_.begin(); j != funcs_.end(); ++j) {
delete *j;
}
funcs_.clear();
}
/// Calculate sizes of all arrays, and allocate them.
void Stoich::allocateModel(const vector<Id>& elist)
{
for ( auto i = elist.begin(); i != elist.end(); ++i ) {
allocateModelObject(*i);
}
resizeArrays();
buildPoolLookup();
buildRateTermLookup();
buildFuncLookup();
}
///////////////////////////////////////////////////////////////////
// Build the lookup maps for conversion of Ids to internal indices.
///////////////////////////////////////////////////////////////////
void Stoich::buildPoolLookup()
{
// The order of pools is: varPools, offSolverVarPools, funcTargetPools, bufPools.
poolLookup_.clear();
int poolNum = 0;
vector<Id>::iterator i;
for(i = varPoolVec_.begin(); i != varPoolVec_.end(); ++i)
poolLookup_[*i] = poolNum++;
for(i = offSolverPoolVec_.begin(); i != offSolverPoolVec_.end(); ++i)
poolLookup_[*i] = poolNum++;
for(i = funcTargetPoolVec_.begin(); i != funcTargetPoolVec_.end(); ++i)
poolLookup_[*i] = poolNum++;
for(i = bufPoolVec_.begin(); i != bufPoolVec_.end(); ++i)
poolLookup_[*i] = poolNum++;
}
void Stoich::buildRateTermLookup()
{
// The order of pools is: varPools, offSolverVarPools, bufPools.
rateTermLookup_.clear();
int termNum = 0;
vector<Id>::iterator i;
for(i = reacVec_.begin(); i != reacVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 1 + useOneWay_;
}
for(i = enzVec_.begin(); i != enzVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 2 + useOneWay_;
}
for(i = mmEnzVec_.begin(); i != mmEnzVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 1;
}
for(i = incrementFuncVec_.begin(); i != incrementFuncVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 1;
}
for(i = offSolverReacVec_.begin(); i != offSolverReacVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 1 + useOneWay_;
}
for(i = offSolverEnzVec_.begin(); i != offSolverEnzVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 2 + useOneWay_;
}
for(i = offSolverMMenzVec_.begin(); i != offSolverMMenzVec_.end(); ++i) {
rateTermLookup_[*i] = termNum;
termNum += 1;
}
}
void Stoich::buildFuncLookup()
{
funcLookup_.clear();
int funcNum = 0;
vector<Id>::iterator i;
for(i = poolFuncVec_.begin(); i != poolFuncVec_.end(); ++i)
funcLookup_[*i] = funcNum++;
}
///////////////////////////////////////////////////////////////////
// Stoich building stuff here for installing model components.
///////////////////////////////////////////////////////////////////
void Stoich::installAndUnschedFunc(Id func, Id pool, double volScale)
{
static const Cinfo* varCinfo = Cinfo::find("Variable");
static const Finfo* funcInputFinfo = varCinfo->findFinfo("input");
static const DestFinfo* df = dynamic_cast<const DestFinfo*>(funcInputFinfo);
assert(df);
// Unsched Func
func.element()->setTick(-2); // Disable with option to resurrect.