Skip to content

Commit f0ef933

Browse files
committed
Modularize particle DG discretization (#406)
1 parent 50b853f commit f0ef933

8 files changed

+2853
-2469
lines changed

src/libcadet/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ if (ENABLE_DG)
214214
list (APPEND LIBCADET_SOURCES
215215
${CMAKE_SOURCE_DIR}/src/libcadet/model/parts/DGToolbox.cpp
216216
${CMAKE_SOURCE_DIR}/src/libcadet/model/parts/ConvectionDispersionOperatorDG.cpp
217+
${CMAKE_SOURCE_DIR}/src/libcadet/model/parts/ParticleDiffusionOperatorDG.cpp
217218
${CMAKE_SOURCE_DIR}/src/libcadet/model/LumpedRateModelWithPoresDG.cpp
218219
${CMAKE_SOURCE_DIR}/src/libcadet/model/LumpedRateModelWithoutPoresDG.cpp
219220
${CMAKE_SOURCE_DIR}/src/libcadet/model/GeneralRateModelDG.cpp

src/libcadet/model/GeneralRateModelDG-InitialConditions.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ void GeneralRateModelDG::consistentInitialState(const SimulationTime& simTime, d
386386
active* const localAdY = adJac.adY ? adJac.adY + localOffsetToParticle + localOffsetInParticle : nullptr;
387387

388388
// r (particle) coordinate of current node
389-
const double r = static_cast<double>(_disc.deltaR[type]) * std::floor(node / _disc.nParNode[type])
390-
+ 0.5 * static_cast<double>(_disc.deltaR[type]) * (1 + _disc.parNodes[type][node % _disc.nParNode[type]]);
389+
const double r = _parDiffOp.relativeCoordinate(type, node);
391390
const ColumnPosition colPos{ z, 0.0, r };
392391

393392
// Determine whether nonlinear solver is required
@@ -398,8 +397,8 @@ void GeneralRateModelDG::consistentInitialState(const SimulationTime& simTime, d
398397
linalg::selectVectorSubset(qShell - _disc.nComp, mask, solution);
399398

400399
// Save values of conserved moieties
401-
const double epsQ = 1.0 - static_cast<double>(_parPorosity[type]);
402-
linalg::conservedMoietiesFromPartitionedMask(mask, _disc.nBound + type * _disc.nComp, _disc.nComp, qShell - _disc.nComp, conservedQuants, static_cast<double>(_parPorosity[type]), epsQ);
400+
const double epsQ = 1.0 - static_cast<double>(_parDiffOp._parPorosity[type]);
401+
linalg::conservedMoietiesFromPartitionedMask(mask, _disc.nBound + type * _disc.nComp, _disc.nComp, qShell - _disc.nComp, conservedQuants, static_cast<double>(_parDiffOp._parPorosity[type]), epsQ);
403402

404403
std::function<bool(double const* const, linalg::detail::DenseMatrixBase&)> jacFunc;
405404

@@ -478,7 +477,7 @@ void GeneralRateModelDG::consistentInitialState(const SimulationTime& simTime, d
478477
continue;
479478
}
480479

481-
mat.native(rIdx, rIdx) = static_cast<double>(_parPorosity[type]);
480+
mat.native(rIdx, rIdx) = static_cast<double>(_parDiffOp._parPorosity[type]);
482481

483482
for (unsigned int bnd = 0; bnd < _disc.nBound[_disc.nComp * type + comp]; ++bnd, ++bndIdx)
484483
{
@@ -526,7 +525,7 @@ void GeneralRateModelDG::consistentInitialState(const SimulationTime& simTime, d
526525
continue;
527526
}
528527

529-
mat.native(rIdx, rIdx) = static_cast<double>(_parPorosity[type]);
528+
mat.native(rIdx, rIdx) = static_cast<double>(_parDiffOp._parPorosity[type]);
530529

531530
for (unsigned int bnd = 0; bnd < _disc.nBound[_disc.nComp * type + comp]; ++bnd, ++bndIdx)
532531
{
@@ -573,7 +572,7 @@ void GeneralRateModelDG::consistentInitialState(const SimulationTime& simTime, d
573572
continue;
574573
}
575574

576-
r[rIdx] = static_cast<double>(_parPorosity[type]) * x[rIdx] - conservedQuants[rIdx];
575+
r[rIdx] = static_cast<double>(_parDiffOp._parPorosity[type]) * x[rIdx] - conservedQuants[rIdx];
577576

578577
for (unsigned int bnd = 0; bnd < _disc.nBound[_disc.nComp * type + comp]; ++bnd, ++bndIdx)
579578
{
@@ -714,9 +713,8 @@ void GeneralRateModelDG::consistentInitialTimeDerivative(const SimulationTime& s
714713
if (_binding[type]->dependsOnTime())
715714
{
716715
// r (particle) coordinate of current node (particle radius normed to 1) - needed in externally dependent adsorption kinetic
717-
const double r = (static_cast<double>(_disc.deltaR[type]) * std::floor(j / _disc.nParNode[type])
718-
+ 0.5 * static_cast<double>(_disc.deltaR[type]) * (1 + _disc.parNodes[type][j % _disc.nParNode[type]]))
719-
/ (static_cast<double>(_parRadius[type]) - static_cast<double>(_parCoreRadius[type]));
716+
const double r = _parDiffOp.relativeCoordinate(type, j);
717+
720718
_binding[type]->timeDerivativeQuasiStationaryFluxes(simTime.t, simTime.secIdx,
721719
ColumnPosition{ z, 0.0, r },
722720
qShellDot - _disc.nComp, qShellDot, dFluxDt, tlmAlloc);
@@ -799,7 +797,7 @@ void GeneralRateModelDG::consistentInitialTimeDerivative(const SimulationTime& s
799797
*/
800798
void GeneralRateModelDG::leanConsistentInitialState(const SimulationTime& simTime, double* const vecStateY, const AdJacobianParams& adJac, double errorTol, util::ThreadLocalStorage& threadLocalMem)
801799
{
802-
if (isSectionDependent(_parDiffusionMode) || isSectionDependent(_parSurfDiffusionMode))
800+
if (isSectionDependent(_parDiffOp._parDiffusionMode) || isSectionDependent(_parDiffOp._parSurfDiffusionMode))
803801
LOG(Warning) << "Lean consistent initialization is not appropriate for section-dependent pore and surface diffusion";
804802

805803
BENCH_SCOPE(_timerConsistentInit);
@@ -832,8 +830,7 @@ void GeneralRateModelDG::leanConsistentInitialState(const SimulationTime& simTim
832830
const int localOffsetInParticle = static_cast<int>(shell) * idxr.strideParNode(type) + idxr.strideParLiquid();
833831
double* const qShell = vecStateY + localOffsetToParticle + localOffsetInParticle;
834832
// r (particle) coordinate of current node
835-
const double r = static_cast<double>(_disc.deltaR[type]) * std::floor(shell / _disc.nParNode[type])
836-
+ 0.5 * static_cast<double>(_disc.deltaR[type]) * (1 + _disc.parNodes[type][shell % _disc.nParNode[type]]);
833+
const double r = _parDiffOp.relativeCoordinate(type, shell);
837834
const ColumnPosition colPos{ z, 0.0, r};
838835

839836
// Perform consistent initialization that does not require a full fledged nonlinear solver (that may fail or damage the current state vector)
@@ -843,7 +840,6 @@ void GeneralRateModelDG::leanConsistentInitialState(const SimulationTime& simTim
843840
} CADET_PARFOR_END;
844841
}
845842
}
846-
847843
}
848844

849845
/**
@@ -889,7 +885,7 @@ void GeneralRateModelDG::leanConsistentInitialState(const SimulationTime& simTim
889885
*/
890886
void GeneralRateModelDG::leanConsistentInitialTimeDerivative(double t, double const* const vecStateY, double* const vecStateYdot, double* const res, util::ThreadLocalStorage& threadLocalMem)
891887
{
892-
if (isSectionDependent(_parDiffusionMode) || isSectionDependent(_parSurfDiffusionMode))
888+
if (isSectionDependent(_parDiffOp._parDiffusionMode) || isSectionDependent(_parDiffOp._parSurfDiffusionMode))
893889
LOG(Warning) << "Lean consistent initialization is not appropriate for section-dependent pore and surface diffusion";
894890

895891
BENCH_SCOPE(_timerConsistentInit);
@@ -1272,7 +1268,7 @@ void GeneralRateModelDG::solveBulkTimeDerivativeSystem(const SimulationTime& sim
12721268
void GeneralRateModelDG::leanConsistentInitialSensitivity(const SimulationTime& simTime, const ConstSimulationState& simState,
12731269
std::vector<double*>& vecSensY, std::vector<double*>& vecSensYdot, active const* const adRes, util::ThreadLocalStorage& threadLocalMem)
12741270
{
1275-
if (isSectionDependent(_parDiffusionMode) || isSectionDependent(_parSurfDiffusionMode))
1271+
if (isSectionDependent(_parDiffOp._parDiffusionMode) || isSectionDependent(_parDiffOp._parSurfDiffusionMode))
12761272
LOG(Warning) << "Lean consistent initialization is not appropriate for section-dependent pore and surface diffusion";
12771273

12781274
BENCH_SCOPE(_timerConsistentInit);

src/libcadet/model/GeneralRateModelDG-LinearSolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ void GeneralRateModelDG::assembleDiscretizedGlobalJacobian(double alpha, Indexer
220220
*/
221221
void GeneralRateModelDG::addTimeDerivativeToJacobianParticleShell(linalg::BandedEigenSparseRowIterator& jac, const Indexer& idxr, double alpha, unsigned int parType)
222222
{
223-
parts::cell::addTimeDerivativeToJacobianParticleShell<linalg::BandedEigenSparseRowIterator, true>(jac, alpha, static_cast<double>(_parPorosity[parType]), _disc.nComp, _disc.nBound + _disc.nComp * parType,
223+
parts::cell::addTimeDerivativeToJacobianParticleShell<linalg::BandedEigenSparseRowIterator, true>(jac, alpha, static_cast<double>(_parDiffOp._parPorosity[parType]), _disc.nComp, _disc.nBound + _disc.nComp * parType,
224224
_poreAccessFactor.data() + _disc.nComp * parType, _disc.strideBound[parType], _disc.boundOffset + _disc.nComp * parType, _binding[parType]->reactionQuasiStationarity());
225225
}
226226

0 commit comments

Comments
 (0)