@@ -372,33 +372,10 @@ bool CSTRModel::configure(IParameterProvider& paramProvider)
372
372
373
373
if (_qsReactionBulk != nullptr )
374
374
{
375
- _dynReactionBulk->fillConservedMoietiesBulk (_MconvMoityBulk, _QsCompBulk); // fill conserved moities matrix
376
- _dynReactionBulk->fillConservedMoietiesBulk2 (_MconvMoityBulk2, _nConservedQuants);
377
- int nMoitiesBulk = _MconvMoityBulk.rows ();
378
- if (nMoitiesBulk != 0 )
379
- {
380
- int mIdx = 0 ;
381
- for (int state = 0 ; state < _nComp; state++)
382
- {
383
- std::bitset<3 > c;
384
- if (_QsCompBulk[state] == 0 ) // state is dynamic
385
- {
386
- c.set (0 );
387
- _stateMap.push_back (c);
388
- }
389
- else if (mIdx < nMoitiesBulk) // state is dynamic and calculated with conserved moities
390
- {
391
- c.set (1 );
392
- _stateMap.push_back (c);
393
- mIdx ++;
394
- }
395
- else if (mIdx >= nMoitiesBulk) // state algebraic
396
- {
397
- c.set (2 );
398
- _stateMap.push_back (c);
399
- }
400
- }
401
- }
375
+ // _dynReactionBulk->fillConservedMoietiesBulk(_MconvMoityBulk, _QsCompBulk); // fill conserved moities matrix
376
+ _QsCompBulk.resize (_nComp);
377
+ _dynReactionBulk->fillConservedMoietiesBulk2 (_MconvMoityBulk2, _nConservedQuants, _QsCompBulk);
378
+
402
379
403
380
bool hasQSBinding = false ;
404
381
for (int i = 0 ; i < _nParType; i++)
@@ -732,7 +709,7 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
732
709
continue ;
733
710
double dotProduct = 0.0 ;
734
711
for (unsigned int j = 0 ; j < _MconvMoityBulk2.cols (); ++j)
735
- dotProduct += _MconvMoityBulk2 (i, j) * c[j];
712
+ dotProduct += static_cast < double >( _MconvMoityBulk2 (i, j) ) * c[j];
736
713
737
714
conservedQuants[mIdx ] = dotProduct;
738
715
mIdx ++;
@@ -795,14 +772,19 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
795
772
int mIdx = 0 ;
796
773
for (unsigned int i = 0 ; i < _nComp; ++i)
797
774
{
798
- if (_QsCompBulk[i]== 0 )
775
+ if (_QsCompBulk[i] == 0 )
799
776
continue ;
800
777
if (mIdx >= _nConservedQuants)
801
778
continue ;
802
- for (unsigned int j = 0 ; j < _MconvMoityBulk2.cols (); ++i)
779
+
780
+ int jIdx = 0 ;
781
+ for (unsigned int j = 0 ; j < _MconvMoityBulk2.cols (); ++j)
803
782
{
804
- mat.native (mIdx , j) = _MconvMoityBulk2 (i, j);
805
- j++;
783
+ if (_QsCompBulk[j] == 0 )
784
+ continue ;
785
+
786
+ mat.native (mIdx , jIdx) = static_cast <double >(_MconvMoityBulk2 (i, j));
787
+ jIdx++;
806
788
}
807
789
mIdx ++;
808
790
}
@@ -840,7 +822,7 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
840
822
if (_QsCompBulk[j] == 0 )
841
823
continue ;
842
824
843
- mat.native (mIdx , jIdx) = _MconvMoityBulk2 (i, j);
825
+ mat.native (mIdx , jIdx) = static_cast < double >( _MconvMoityBulk2 (i, j) );
844
826
jIdx++;
845
827
}
846
828
mIdx ++;
@@ -877,7 +859,7 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
877
859
{
878
860
if (_QsCompBulk[j] == 0 )
879
861
continue ;
880
- dotProduct += _MconvMoityBulk2 (i, j) * x[jIdx];
862
+ dotProduct += static_cast < double >( _MconvMoityBulk2 (i, j) ) * x[jIdx];
881
863
jIdx++;
882
864
}
883
865
r[mIdx ] = dotProduct - conservedQuants[mIdx ];
@@ -1734,17 +1716,18 @@ void CSTRModel::applyConservedMoitiesBulk2(double t, unsigned int secIdx, const
1734
1716
1735
1717
// calculate conserved moities
1736
1718
Eigen::Matrix<ResidualType, Eigen::Dynamic, Eigen::Dynamic> M (_nComp, _nComp);
1737
- _dynReactionBulk->fillConservedMoietiesBulk2 (M, _nConservedQuants); // fill conserved moities matrix (alternative method)
1719
+ _dynReactionBulk->fillConservedMoietiesBulk2 (M, _nConservedQuants, _QsCompBulk ); // fill conserved moities matrix (alternative method)
1738
1720
1739
1721
1740
1722
// buffer memory for transformed residual
1741
1723
BufferedArray<ResidualType> temp = subAlloc.array <ResidualType>(_nComp);
1742
1724
Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> resCWithMoities (static_cast <ResidualType*>(temp), _nComp);
1743
1725
resCWithMoities.setZero ();
1744
1726
1727
+ Eigen::Matrix<ResidualType, Eigen::Dynamic, Eigen::Dynamic> MconvMoityBulk2Cast = _MconvMoityBulk2.template cast <ResidualType>();
1745
1728
1746
1729
// multiply conserved moities matrix with residual
1747
- resCWithMoities = M * mapResC;
1730
+ resCWithMoities = MconvMoityBulk2Cast * mapResC;
1748
1731
1749
1732
// add quasi stationary reaction to residium
1750
1733
const int nQsReac = _dynReactionBulk->numReactionQuasiStationary ();
@@ -1768,7 +1751,7 @@ void CSTRModel::applyConservedMoitiesBulk2(double t, unsigned int secIdx, const
1768
1751
1769
1752
if (wantJac)
1770
1753
{
1771
- EigenMatrixTimesDemseMatrix (M , _jac);
1754
+ EigenMatrixTimesDemseMatrix (MconvMoityBulk2Cast , _jac);
1772
1755
int mIdx = 0 ;
1773
1756
int rIdx = 0 ;
1774
1757
for (int i = 0 ; i < _nComp; i++)
@@ -1787,52 +1770,9 @@ void CSTRModel::applyConservedMoitiesBulk2(double t, unsigned int secIdx, const
1787
1770
1788
1771
}
1789
1772
1790
- // std::cout << "Jacobian with conserved moities" << std::endl;
1791
- // std::cout << "Jacobian before" << std::endl;
1792
- // for (int i = 0; i < _nComp + 1; i++)
1793
- // {
1794
- // for (int j = 0; j < _nComp+ 1; j++)
1795
- // {
1796
- // std::cout << _jac.native(i, j) << " ";
1797
- // }
1798
- // std::cout << std::endl;
1799
- // }
1800
- // multiply conserved moities matrix with jacobian
1801
- // if (wantJac)
1802
- // {
1803
- // EigenMatrixTimesDemseMatrix(M, _jac);
1804
-
1805
- // int state = _nComp - _dynReactionBulk->numReactionQuasiStationary();
1806
- // for (int qsReac = 0; qsReac < nQsReac; ++qsReac) // todo this in a function
1807
- // {
1808
- // if (state < _nComp)
1809
- // {
1810
- // _dynReactionBulk->analyticJacobianQuasiStationaryReaction(t, secIdx, colPos, reinterpret_cast<double const*>(c), state, qsReac, _jac.row(state), subAlloc);
1811
- // _jac.native(state, _nComp + _totalBound) = 0.0; // dF_{ci}/dvliquid = 0
1812
- // state++;
1813
- // }
1814
- // else
1815
- // throw InvalidParameterException(
1816
- // "Jacobian implementation with conserved moities: Too many quasi stationary reactions detected. "
1817
- // "Please check the implementation of the model."
1818
- // );
1819
- // }
1820
-
1821
- // std::cout << "Jacobian with conserved moities" << std::endl;
1822
- // std::cout << "Jacobian after" << std::endl;
1823
- // for (int i = 0; i < _nComp+ 1; i++)
1824
- // {
1825
- // for (int j = 0; j < _nComp+1; j++)
1826
- // {
1827
- // std::cout << _jac.native(i, j) << " ";
1828
- // }
1829
- // std::cout << std::endl;
1830
- // }
1831
-
1832
1773
}
1833
1774
1834
1775
1835
-
1836
1776
template <typename StateType, typename ResidualType, typename ParamType, bool wantJac>
1837
1777
int CSTRModel::residualImpl (double t, unsigned int secIdx, StateType const * const y, double const * const yDot, ResidualType* const res, LinearBufferAllocator tlmAlloc)
1838
1778
{
0 commit comments