Skip to content

Commit 6a4367e

Browse files
committed
Restart implementation
1 parent 5333e26 commit 6a4367e

9 files changed

+303
-18
lines changed

src/libcadet/model/ReactionModel.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ class IDynamicReactionModel
213213
virtual int residualLiquidAdd(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y,
214214
double* res, double factor, LinearBufferAllocator workSpace) const = 0;
215215

216+
virtual int quasiStationaryFlux(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y,
217+
active* fluxes, std::vector<int>& mapQSReac, LinearBufferAllocator workSpace) const = 0;
218+
219+
216220
/**
217221
* @brief Adds the analytical Jacobian of the reaction terms for one liquid phase cell
218222
* @details Adds the Jacobian of the dynamic reaction terms for the given liquid phase
@@ -237,9 +241,15 @@ class IDynamicReactionModel
237241
virtual void analyticJacobianLiquidAdd(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y, double factor, linalg::BandMatrix::RowIterator jac, LinearBufferAllocator workSpace) const = 0;
238242
virtual void analyticJacobianLiquidAdd(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y, double factor, linalg::DenseBandedRowIterator jac, LinearBufferAllocator workSpace) const = 0;
239243
virtual void analyticJacobianLiquidAdd(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y, double factor, linalg::BandedSparseRowIterator jac, LinearBufferAllocator workSpace) const = 0;
240-
#ifdef ENABLE_DG
244+
245+
246+
virtual void fillConservedMoietiesBulk(Eigen::MatrixXd& M, std::vector<int>& QSReaction, std::vector<int>& _QsCompBulk) = 0;
247+
248+
249+
#ifdef ENABLE_DG
241250
virtual void analyticJacobianLiquidAdd(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y, double factor, linalg::BandedEigenSparseRowIterator jac, LinearBufferAllocator workSpace) const = 0;
242-
#endif
251+
252+
#endif
243253

244254
/**
245255
* @brief Evaluates the residual for one combined phase cell
@@ -314,6 +324,11 @@ class IDynamicReactionModel
314324
* @return Number of reactions
315325
*/
316326
virtual unsigned int numReactionsCombined() const CADET_NOEXCEPT = 0;
327+
328+
/**
329+
* @brief Returns wheather liquid reactions are in quasi-stationary state
330+
* @return boolean
331+
*/
317332

318333
protected:
319334
};

src/libcadet/model/StirredTankModel.cpp

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include <algorithm>
3232
#include <functional>
3333

34+
#include <Eigen/Dense>
35+
36+
#include<iostream>
37+
3438
namespace cadet
3539
{
3640

@@ -76,7 +80,8 @@ CSTRModel::~CSTRModel() CADET_NOEXCEPT
7680
delete[] _strideBound;
7781
delete[] _offsetParType;
7882

79-
delete _dynReactionBulk;
83+
delete[] _dynReactionBulk;
84+
//delete[] _temp; -> Jan warum funktioniert das nicht?
8085
}
8186

8287
unsigned int CSTRModel::numDofs() const CADET_NOEXCEPT
@@ -235,6 +240,26 @@ bool CSTRModel::configureModelDiscretization(IParameterProvider& paramProvider,
235240

236241
if (_dynReactionBulk->usesParamProviderInDiscretizationConfig())
237242
paramProvider.popScope();
243+
244+
if (true) //paramProvider.exists("QUASI_STATIONARY_REACTION_BULK")
245+
{
246+
//_qsReacBulk = paramProvider.getIntArray("QUASI_STATIONARY_REACTION_BULK");
247+
_qsReacBulk = {1};
248+
_nQsReacBulk = _qsReacBulk.size();
249+
//_temp = new active[_nComp];
250+
251+
_nMoitiesBulk = (_nComp + _totalBound) - _nQsReacBulk;
252+
_MconvMoityBulk = Eigen::MatrixXd::Zero(_nMoitiesBulk, _nComp); // matrix for conserved moities
253+
}
254+
else
255+
{
256+
_QsCompBulk.clear();
257+
_qsReacBulk.clear();
258+
_nMoitiesBulk = 0;
259+
_nQsReacBulk = 0;
260+
_MconvMoityBulk = Eigen::MatrixXd::Zero(0, 0);
261+
262+
}
238263
}
239264

240265
_dynReaction = std::vector<IDynamicReactionModel*>(_nParType, nullptr);
@@ -356,6 +381,12 @@ bool CSTRModel::configure(IParameterProvider& paramProvider)
356381
paramProvider.pushScope("reaction_bulk");
357382
dynReactionConfSuccess = _dynReactionBulk->configure(paramProvider, _unitOpIdx, ParTypeIndep);
358383
paramProvider.popScope();
384+
385+
if (true) //paramProvider.exists("QUASI_STATIONARY_REACTION_BULK")
386+
{
387+
_dynReactionBulk->fillConservedMoietiesBulk(_MconvMoityBulk, _qsReacBulk, _QsCompBulk); // fill conserved moities matrix
388+
int a = 0;
389+
}
359390
}
360391

361392
for (unsigned int type = 0; type < _nParType; ++type)
@@ -422,9 +453,7 @@ unsigned int CSTRModel::threadLocalMemorySize() const CADET_NOEXCEPT
422453
return lms.bufferSize();
423454
}
424455

425-
void CSTRModel::setSectionTimes(double const* secTimes, bool const* secContinuity, unsigned int nSections)
426-
{
427-
}
456+
void CSTRModel::setSectionTimes(double const* secTimes, bool const* secContinuity, unsigned int nSections){}
428457

429458
void CSTRModel::useAnalyticJacobian(const bool analyticJac)
430459
{
@@ -1247,7 +1276,7 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
12471276

12481277
const ParamType flowIn = static_cast<ParamType>(_flowRateIn);
12491278
const ParamType flowOut = static_cast<ParamType>(_flowRateOut);
1250-
1279+
12511280
// Inlet DOF
12521281
for (unsigned int i = 0; i < _nComp; ++i)
12531282
{
@@ -1256,7 +1285,7 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
12561285

12571286
// Concentrations: \dot{V^l} * c + V^l * \dot{c} + V^s * sum_j sum_m \dot{q}_{j,m}] = c_in * F_in - c * F_out
12581287
const ParamType vsolid = static_cast<ParamType>(_constSolidVolume);
1259-
ResidualType* const resC = res + _nComp;
1288+
ResidualType* resC = res + _nComp;
12601289
for (unsigned int i = 0; i < _nComp; ++i)
12611290
{
12621291
resC[i] = 0.0;
@@ -1312,14 +1341,73 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
13121341
// Reactions in liquid phase
13131342
const ColumnPosition colPos{0.0, 0.0, 0.0};
13141343

1315-
if (_dynReactionBulk && (_dynReactionBulk->numReactionsLiquid() > 0))
1344+
if (_dynReactionBulk && (_nQsReacBulk > 0))
13161345
{
1317-
LinearBufferAllocator subAlloc = tlmAlloc.manageRemainingMemory();
1318-
BufferedArray<ResidualType> flux = subAlloc.array<ResidualType>(_nComp);
1346+
LinearBufferAllocator subAlloc = tlmAlloc.manageRemainingMemory(); // todo nachgucken ob das wirklich so geht
13191347

1348+
BufferedArray<ResidualType> flux = subAlloc.array<ResidualType>(_nComp);
13201349
std::fill_n(static_cast<ResidualType*>(flux), _nComp, 0.0);
1350+
1351+
BufferedArray<ResidualType> qsflux = subAlloc.array<ResidualType>(_nQsReacBulk);
1352+
std::fill_n(static_cast<ResidualType*>(qsflux), _nQsReacBulk, 0.0);
1353+
1354+
BufferedArray<ResidualType> temp = subAlloc.array<ResidualType>(_nComp);
1355+
std::fill_n(static_cast<ResidualType*>(temp), _nComp, 0.0);
1356+
13211357
_dynReactionBulk->residualLiquidAdd(t, secIdx, colPos, c, static_cast<ResidualType*>(flux), -1.0, subAlloc);
1358+
_dynReactionBulk->quasiStationaryFlux(t, secIdx, colPos, c, static_cast<ResidualType*>(qsflux), _qsReacBulk, subAlloc);
1359+
1360+
1361+
1362+
Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> resCMoities(static_cast<ResidualType*>(temp), _nComp);
1363+
resCMoities.setZero();
1364+
1365+
Eigen::Map<Eigen::Vector<ResidualType, Eigen::Dynamic>> mapResC(resC, _nComp);
1366+
std::vector<int> visitedQSComp(_nComp, 0);
13221367

1368+
int MoityIdx = 0;
1369+
1370+
for (unsigned int state = 0; state < (_nComp - _nQsReacBulk); ++state)
1371+
{
1372+
if (_QsCompBulk[state] == 1)
1373+
{
1374+
ResidualType dotProduct = 0.0;
1375+
for (unsigned int i; i < _MconvMoityBulk.cols(); i++) // hier Optimierung durch Vermeidung von 0 Zeilen in MconvMoityBulk
1376+
{
1377+
dotProduct += static_cast<ResidualType>(_MconvMoityBulk(MoityIdx, i)) * (mapResC[i]);
1378+
1379+
if (wantJac)
1380+
_jac.native(i, state) = vDotTimeFactor + static_cast<double>(flowOut); // dF_{ci}/dcj = v_liquidDot + F_out
1381+
}
1382+
resCMoities[state] = dotProduct;
1383+
1384+
1385+
MoityIdx++;
1386+
}
1387+
else if (_QsCompBulk[state] == 0)
1388+
{
1389+
resCMoities[state] += v * flux[state]; // hier sicher stellen, was in flux steht entweder res + flux oder nur flux
1390+
1391+
if (wantJac)
1392+
_jac.native(state, _nComp + _totalBound) += static_cast<double>(flux[state]); dF/dvliquid = flux
1393+
int a = 0; // add function that adds the jacobian for one state or change analyticJacobianLiquidAdd
1394+
}
1395+
}
1396+
1397+
int state = (_nComp - _nQsReacBulk);
1398+
for (unsigned int qsreac = 0; qsreac < _nQsReacBulk; ++qsreac)
1399+
{
1400+
resCMoities[state++] = v * qsflux[qsreac];
1401+
1402+
if(wantJac)
1403+
int a = 0; // add function that adds the jacobian for single reactions -> maybe der klammer
1404+
}
1405+
1406+
mapResC = resCMoities;
1407+
1408+
}
1409+
else
1410+
{
13231411
for (unsigned int comp = 0; comp < _nComp; ++comp)
13241412
resC[comp] += v * flux[comp];
13251413

@@ -1331,7 +1419,7 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
13311419
_dynReactionBulk->analyticJacobianLiquidAdd(t, secIdx, colPos, reinterpret_cast<double const*>(c), -static_cast<double>(v), _jac.row(0), subAlloc);
13321420
}
13331421
}
1334-
1422+
13351423
// Bound states
13361424
for (unsigned int type = 0; type < _nParType; ++type)
13371425
{
@@ -1401,7 +1489,6 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
14011489
}
14021490
}
14031491
}
1404-
14051492
if (wantJac)
14061493
{
14071494
// Assemble Jacobian: Reaction
@@ -1440,7 +1527,6 @@ int CSTRModel::residualImpl(double t, unsigned int secIdx, StateType const* cons
14401527

14411528
// Volume: \dot{V} = F_{in} - F_{out} - F_{filter}
14421529
res[2 * _nComp + _totalBound] = vDot - flowIn + flowOut + static_cast<ParamType>(_curFlowRateFilter);
1443-
14441530
return 0;
14451531
}
14461532

src/libcadet/model/StirredTankModel.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ class CSTRModel : public UnitOperationBase
171171

172172
IDynamicReactionModel* _dynReactionBulk; //!< Dynamic reactions in the bulk volume
173173

174+
// active* _temp;
175+
Eigen::MatrixXd _MconvMoityBulk; //!< Matrix with conservation of moieties in the bulk volume
176+
std::vector<int> _qsReacBulk; //!< Indices of components that are not conserved in the bulk volume
177+
std::vector<int> _QsCompBulk;
178+
unsigned int _nQsReacBulk;
179+
unsigned int _nMoitiesBulk;
180+
174181
class Exporter : public ISolutionExporter
175182
{
176183
public:

src/libcadet/model/reaction/CrystallizationReaction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ namespace cadet
197197

198198
virtual bool requiresConfiguration() const CADET_NOEXCEPT { return true; }
199199
virtual bool usesParamProviderInDiscretizationConfig() const CADET_NOEXCEPT { return false; }
200-
200+
void fillConservedMoietiesBulk(Eigen::MatrixXd& M, std::vector<int>& QSReaction, std::vector<int>& QSComponent) { }
201+
bool hasQuasiStationaryReactionsLiquid() { return false; }
202+
virtual int quasiStationaryFlux(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y,
203+
active* fluxes, std::vector<int>& mapQSReac, LinearBufferAllocator workSpace) const { return 0; }
201204
virtual bool configure(IParameterProvider& paramProvider, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx)
202205
{
203206
readScalarParameterOrArray(_bins, paramProvider, "CRY_BINS", 1);

src/libcadet/model/reaction/DummyReaction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class DummyDynamicReaction : public IDynamicReactionModel
9999

100100
virtual unsigned int numReactionsLiquid() const CADET_NOEXCEPT { return 0; }
101101
virtual unsigned int numReactionsCombined() const CADET_NOEXCEPT { return 0; }
102+
103+
void fillConservedMoietiesBulk(Eigen::MatrixXd& M, std::vector<int>& QSReaction, std::vector<int>& QSComponent) {}
104+
bool hasQuasiStationaryReactionsLiquid() { return false;}
105+
106+
virtual int quasiStationaryFlux(double t, unsigned int secIdx, const ColumnPosition& colPos, double const* y,
107+
active* fluxes, std::vector<int>& mapQSReac, LinearBufferAllocator workSpace) const {return 0;}
102108

103109
protected:
104110
};

0 commit comments

Comments
 (0)