34
34
#include < Eigen/Dense>
35
35
#include < Eigen/Sparse>
36
36
37
- #include < iostream>
37
+ #include < iostream> // todo delete
38
38
39
39
namespace cadet
40
40
{
@@ -82,8 +82,7 @@ CSTRModel::~CSTRModel() CADET_NOEXCEPT
82
82
delete[] _offsetParType;
83
83
84
84
delete[] _dynReactionBulk;
85
- // delete[] _temp;
86
- // delete[] _temp2;
85
+
87
86
}
88
87
89
88
unsigned int CSTRModel::numDofs () const CADET_NOEXCEPT
@@ -372,9 +371,8 @@ bool CSTRModel::configure(IParameterProvider& paramProvider)
372
371
373
372
if (_qsReactionBulk != nullptr )
374
373
{
375
- // _dynReactionBulk->fillConservedMoietiesBulk(_MconvMoityBulk, _QsCompBulk); // fill conserved moities matrix
376
374
_QsCompBulk.resize (_nComp);
377
- _dynReactionBulk->fillConservedMoietiesBulk2 (_MconvMoityBulk2 , _nConservedQuants, _QsCompBulk);
375
+ _dynReactionBulk->fillConservedMoietiesBulk (_MconvMoityBulk , _nConservedQuants, _QsCompBulk);
378
376
379
377
380
378
bool hasQSBinding = false ;
@@ -395,8 +393,10 @@ bool CSTRModel::configure(IParameterProvider& paramProvider)
395
393
}
396
394
else
397
395
{
398
- _MconvMoityBulk = Eigen::MatrixXd ::Zero (0 , 0 ); // matrix for conserved moities
396
+ _MconvMoityBulk = Eigen::Matrix<active, Eigen::Dynamic, Eigen::Dynamic> ::Zero (0 , 0 ); // matrix for conserved moities
399
397
_QsCompBulk.clear ();
398
+ _qsReactionBulk = nullptr ;
399
+ _nConservedQuants = 0 ;
400
400
}
401
401
402
402
for (unsigned int type = 0 ; type < _nParType; ++type)
@@ -708,8 +708,8 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
708
708
if (mIdx >= _nConservedQuants)
709
709
continue ;
710
710
double dotProduct = 0.0 ;
711
- for (unsigned int j = 0 ; j < _MconvMoityBulk2 .cols (); ++j)
712
- dotProduct += static_cast <double >(_MconvMoityBulk2 (i, j)) * c[j];
711
+ for (unsigned int j = 0 ; j < _MconvMoityBulk .cols (); ++j)
712
+ dotProduct += static_cast <double >(_MconvMoityBulk (i, j)) * c[j];
713
713
714
714
conservedQuants[mIdx ] = dotProduct;
715
715
mIdx ++;
@@ -778,12 +778,12 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
778
778
continue ;
779
779
780
780
int jIdx = 0 ;
781
- for (unsigned int j = 0 ; j < _MconvMoityBulk2 .cols (); ++j)
781
+ for (unsigned int j = 0 ; j < _MconvMoityBulk .cols (); ++j)
782
782
{
783
783
if (_QsCompBulk[j] == 0 )
784
784
continue ;
785
785
786
- mat.native (mIdx , jIdx) = static_cast <double >(_MconvMoityBulk2 (i, j));
786
+ mat.native (mIdx , jIdx) = static_cast <double >(_MconvMoityBulk (i, j));
787
787
jIdx++;
788
788
}
789
789
mIdx ++;
@@ -817,12 +817,12 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
817
817
continue ;
818
818
819
819
int jIdx = 0 ;
820
- for (unsigned int j = 0 ; j < _MconvMoityBulk2 .cols (); ++j)
820
+ for (unsigned int j = 0 ; j < _MconvMoityBulk .cols (); ++j)
821
821
{
822
822
if (_QsCompBulk[j] == 0 )
823
823
continue ;
824
824
825
- mat.native (mIdx , jIdx) = static_cast <double >(_MconvMoityBulk2 (i, j));
825
+ mat.native (mIdx , jIdx) = static_cast <double >(_MconvMoityBulk (i, j));
826
826
jIdx++;
827
827
}
828
828
mIdx ++;
@@ -855,25 +855,17 @@ void CSTRModel::consistentInitialState(const SimulationTime& simTime, double* co
855
855
continue ;
856
856
int jIdx = 0 ;
857
857
double dotProduct = 0.0 ;
858
- for (unsigned int j = 0 ; j < _MconvMoityBulk2 .cols (); ++j)
858
+ for (unsigned int j = 0 ; j < _MconvMoityBulk .cols (); ++j)
859
859
{
860
860
if (_QsCompBulk[j] == 0 )
861
861
continue ;
862
- dotProduct += static_cast <double >(_MconvMoityBulk2 (i, j)) * x[jIdx];
862
+ dotProduct += static_cast <double >(_MconvMoityBulk (i, j)) * x[jIdx];
863
863
jIdx++;
864
864
}
865
865
r[mIdx ] = dotProduct - conservedQuants[mIdx ];
866
866
mIdx ++;
867
867
}
868
868
869
- std::cout << " Residual: " << std::endl;
870
- for (unsigned int i = 0 ; i < probSize; ++i) {
871
- std::cout << r[i] << std::endl;
872
- }
873
- std::cout << " Solution: " << std::endl;
874
- for (unsigned int i = 0 ; i < probSize; ++i)
875
- std::cout << x[i] << std::endl;
876
-
877
869
return true ;
878
870
},
879
871
jacFunc, errorTol, static_cast <double *>(solution), nonlinMem, jacobianMatrix, probSize);
@@ -1566,88 +1558,7 @@ int CSTRModel::residual(const SimulationTime& simTime, const ConstSimulationStat
1566
1558
{
1567
1559
return residualImpl<double , double , double , false >(simTime.t , simTime.secIdx , simState.vecStateY , simState.vecStateYdot , res, threadLocalMem.get ());
1568
1560
}
1569
- template <typename StateType, typename ResidualType, typename ParamType, bool wantJac>
1570
- void CSTRModel::applyConservedMoitiesBulk (double t, unsigned int secIdx, const ColumnPosition& colPos, StateType const * const c, double const * const yDot, ResidualType* const resC, LinearBufferAllocator tlmAlloc)
1571
- {
1572
- const ParamType flowIn = static_cast <ParamType>(_flowRateIn);
1573
- const ParamType flowOut = static_cast <ParamType>(_flowRateOut);
1574
- double const * const cDot = yDot ? yDot + _nComp : nullptr ;
1575
- const double vDot = yDot ? yDot[2 * _nComp + _totalBound] : 0.0 ;
1576
-
1577
- LinearBufferAllocator subAlloc = tlmAlloc.manageRemainingMemory ();
1578
-
1579
- BufferedArray<ResidualType> temp = subAlloc.array <ResidualType>(_nComp);
1580
- Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> resCWithMoities (static_cast <ResidualType*>(temp), _nComp);
1581
- resCWithMoities.setZero ();
1582
-
1583
- BufferedArray<ResidualType> temp2 = subAlloc.array <ResidualType>(_nComp);
1584
- Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> qsFlux (static_cast <ResidualType*>(temp2), _dynReactionBulk->numReactionsLiquid ());
1585
- qsFlux.setZero ();
1586
-
1587
- _dynReactionBulk->computeQuasiStationaryReactionFlux (t, secIdx, colPos, c, qsFlux, _qsReactionBulk, subAlloc);
1588
- Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> mapResC (resC, _nComp);
1589
-
1590
- int MoityIdx = 0 ;
1591
- int qsreac = 0 ;
1592
- int comp = 0 ;
1593
-
1594
- for (unsigned int state = 0 ; state < _nComp; state++)
1595
- {
1596
- if (_stateMap[state].test (0 )) // dynamic
1597
- {
1598
- resCWithMoities[state] = resC[state];
1599
- if (wantJac)
1600
- {
1601
- _jac.native (state, state) += (static_cast <double >(vDot) + static_cast <double >(flowOut)); // dF_{ci}/dcj = v_liquidDot + F_out
1602
- _dynReactionBulk->analyticJacobianLiquidSingleFluxAdd (t, secIdx, colPos, reinterpret_cast <double const *>(c), state, 0 , _jac.row (state), subAlloc);
1603
- }
1604
- comp++;
1605
- }
1606
- else if (_stateMap[state].test (1 )) // conserved
1607
- {
1608
- // todo test of matrix times vector isfaster
1609
- ResidualType dotProduct = 0.0 ;
1610
- for (unsigned int i = 0 ; i < _MconvMoityBulk.cols (); ++i)
1611
- {
1612
- dotProduct += _MconvMoityBulk (MoityIdx, i) * (mapResC[i]);
1613
- if (wantJac)
1614
- {
1615
- _jac.native (state, i) += (static_cast <double >(vDot) + static_cast <double >(flowOut)) * _MconvMoityBulk (MoityIdx, i); // dF_{ci}/dcj = v_liquidDot + F_out
1616
- if (cadet_likely (yDot))
1617
- _jac.native (i, _nComp + _totalBound) += _MconvMoityBulk (MoityIdx, i) * cDot[i] ; // dF/dvliquid = cDot
1618
- }
1619
- }
1620
- if (wantJac)
1621
- _dynReactionBulk->analyticJacobianLiquidSingleFluxAdd (t, secIdx, colPos, reinterpret_cast <double const *>(c), state, 0 , _jac.row (state), subAlloc);
1622
- resCWithMoities[state] = dotProduct;
1623
- MoityIdx++;
1624
- comp++;
1625
- }
1626
- else if (_stateMap[state].test (2 )) // algebraic
1627
- {
1628
- resCWithMoities[state] = qsFlux[qsreac];
1629
1561
1630
- if (wantJac)
1631
- {
1632
- _dynReactionBulk->analyticJacobianQuasiStationaryReaction (t, secIdx, colPos, reinterpret_cast <double const *>(c), state, qsreac, _jac.row (state), subAlloc);
1633
- _jac.native (state, _nComp + _totalBound) = 0.0 ; // dF_{ci}/dvliquid = 0
1634
- }
1635
- qsreac++;
1636
- }
1637
- }
1638
- mapResC = resCWithMoities;
1639
-
1640
- std::cout << " Jacobian with conserved moities" << std::endl;
1641
- std::cout << " Jacobian after" << std::endl;
1642
- for (int i = 0 ; i < _nComp + 1 ; i++)
1643
- {
1644
- for (int j = 0 ; j < _nComp + 1 ; j++)
1645
- {
1646
- std::cout << _jac.native (i, j) << " " ;
1647
- }
1648
- std::cout << std::endl;
1649
- }
1650
- }
1651
1562
template <typename ResidualType>
1652
1563
void CSTRModel::EigenMatrixTimesDemseMatrix (Eigen::Matrix<ResidualType, Eigen::Dynamic, Eigen::Dynamic> A, linalg::DenseMatrix& B)
1653
1564
{
@@ -1699,7 +1610,7 @@ void CSTRModel::EigenMatrixTimesDemseMatrix(Eigen::Matrix<ResidualType, Eigen::D
1699
1610
}
1700
1611
1701
1612
template <typename StateType, typename ResidualType, typename ParamType, bool wantJac>
1702
- void CSTRModel::applyConservedMoitiesBulk2 (double t, unsigned int secIdx, const ColumnPosition& colPos, StateType const * const c, double const * const yDot, ResidualType* const resC, LinearBufferAllocator tlmAlloc)
1613
+ void CSTRModel::applyConservedMoitiesBulk (double t, unsigned int secIdx, const ColumnPosition& colPos, StateType const * const c, double const * const yDot, ResidualType* const resC, LinearBufferAllocator tlmAlloc)
1703
1614
{
1704
1615
// prepare memory
1705
1616
LinearBufferAllocator subAlloc = tlmAlloc.manageRemainingMemory ();
@@ -1714,17 +1625,12 @@ void CSTRModel::applyConservedMoitiesBulk2(double t, unsigned int secIdx, const
1714
1625
1715
1626
_dynReactionBulk->computeQuasiStationaryReactionFlux (t, secIdx, colPos, c, qsFlux, _qsReactionBulk, subAlloc);
1716
1627
1717
- // calculate conserved moities
1718
- Eigen::Matrix<ResidualType, Eigen::Dynamic, Eigen::Dynamic> M (_nComp, _nComp);
1719
- _dynReactionBulk->fillConservedMoietiesBulk2 (M, _nConservedQuants, _QsCompBulk); // fill conserved moities matrix (alternative method)
1720
-
1721
-
1722
1628
// buffer memory for transformed residual
1723
1629
BufferedArray<ResidualType> temp = subAlloc.array <ResidualType>(_nComp);
1724
1630
Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> resCWithMoities (static_cast <ResidualType*>(temp), _nComp);
1725
1631
resCWithMoities.setZero ();
1726
1632
1727
- Eigen::Matrix<ResidualType, Eigen::Dynamic, Eigen::Dynamic> MconvMoityBulk2Cast = _MconvMoityBulk2 .template cast <ResidualType>();
1633
+ Eigen::Matrix<ResidualType, Eigen::Dynamic, Eigen::Dynamic> MconvMoityBulk2Cast = _MconvMoityBulk .template cast <ResidualType>();
1728
1634
1729
1635
// multiply conserved moities matrix with residual
1730
1636
resCWithMoities = MconvMoityBulk2Cast * mapResC;
@@ -1867,7 +1773,7 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
1867
1773
1868
1774
if (_hasQuasiStationaryReactionBulk)
1869
1775
{
1870
- applyConservedMoitiesBulk2 <StateType, ResidualType, ParamType, wantJac>(t, secIdx, colPos, c, yDot, resC, subAlloc);
1776
+ applyConservedMoitiesBulk <StateType, ResidualType, ParamType, wantJac>(t, secIdx, colPos, c, yDot, resC, subAlloc);
1871
1777
1872
1778
}
1873
1779
}
@@ -2401,11 +2307,10 @@ void CSTRModel::addTimeDerivativeJacobian(double t, double alpha, const ConstSim
2401
2307
continue ;
2402
2308
else
2403
2309
{
2404
- // mat.native(state, state) += timeV * (static_cast<double>(_MconvMoityBulk(MoityIdx, MoityIdx))); // dRes / dcDot
2405
2310
for (int j = 0 ; j < _nComp; j++)
2406
2311
{
2407
- mat.native (i, j) += timeV * static_cast <double >(_MconvMoityBulk2 (i, j)); // dRes / dcDot
2408
- mat.native (i, _nComp + _totalBound) += alpha * static_cast <double >(_MconvMoityBulk2 (i, j)) * c[j]; // dRes / dVlDot
2312
+ mat.native (i, j) += timeV * static_cast <double >(_MconvMoityBulk (i, j)); // dRes / dcDot
2313
+ mat.native (i, _nComp + _totalBound) += alpha * static_cast <double >(_MconvMoityBulk (i, j)) * c[j]; // dRes / dVlDot
2409
2314
}
2410
2315
MoityIdx++;
2411
2316
}
@@ -2475,7 +2380,7 @@ void CSTRModel::addTimeDerivativeJacobian(double t, double alpha, const ConstSim
2475
2380
2476
2381
/* if (_hasQuasiStationaryReactionBulk)
2477
2382
{
2478
- EigenMatrixTimesDemseMatrix(_MconvMoityBulk2 , mat);
2383
+ EigenMatrixTimesDemseMatrix(_MconvMoityBulk , mat);
2479
2384
}*/
2480
2385
2481
2386
std::cout << " Jacobian Derivative: " << std::endl;
0 commit comments