Skip to content

Commit

Permalink
Create new exchange Modul
Browse files Browse the repository at this point in the history
Resolves #256
  • Loading branch information
AntoniaBerger authored and jbreue16 committed Nov 20, 2024
1 parent 5c77e7b commit 720a8c1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 68 deletions.
5 changes: 5 additions & 0 deletions src/libcadet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ set(LIBCADET_BINDINGMODEL_SOURCES
${CMAKE_SOURCE_DIR}/src/libcadet/model/binding/HICWaterOnHydrophobicSurfacesBinding.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/model/binding/HICConstantWaterActivityBinding.cpp
)
set(LIBCADET_PHASETRANSITIONMODELINTERFACE_HPP_
${CMAKE_SOURCE_DIR}/src/libcadet/model/exchange/LinearExchange.cpp
)



# LIBCADET_REACTIONMODEL_SOURCES holds all source files of reaction models
set(LIBCADET_REACTIONMODEL_SOURCES
Expand Down
28 changes: 12 additions & 16 deletions src/libcadet/model/MultiChannelTransportModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,28 +449,24 @@ bool MultiChannelTransportModel::configure(IParameterProvider& paramProvider)
dynReactionConfSuccess = _dynReactionBulk->configure(paramProvider, _unitOpIdx, ParTypeIndep);
paramProvider.popScope();
}
/*
if (!_exchange.empty()){
const unsigned int maxChannelStates = *std::max_element(_disc.strideBound, _disc.strideBound + _disc.nParType);
std::vector<ParameterId> initParams(maxBoundStates); //Q was macht initParams ?


_exchange->fillChannelInitialParameters(initParams.data(), _unitOpIdx, type);
if (_exchange[0]){

active* const iq = _initQ.data() + _disc.nBoundBeforeType[type];
for (unsigned int i = 0; i < _disc.strideBound[type]; ++i){
_parameters[initParams[i]] = iq + i;
}
std::vector<ParameterId> initParams(_disc.nChannel); //Q was macht initParams ? brauche ich maxBoundStates ?
_exchange[0]->fillChannelInitialParameters(initParams.data(), _unitOpIdx);
}
*/


// Reconfigure exchange model
bool exchangeConfSuccess = true;
/* if (!_exchange.empty()){
exchangeConfSuccess = _exchange-> configure(paramProvider, _unitOpIdx, type) && bindingConfSuccess;
if (_exchange[0] && paramProvider.exists("exchange") && _exchange[0]->requiresConfiguration()){

paramProvider.pushScope("exchange");
exchangeConfSuccess = _exchange[0]->configure(paramProvider, _unitOpIdx, cadet::ParTypeIndep); // Brauche ich PartypeIndep ?
paramProvider.popScope();
}
*/

return transportSuccess && dynReactionConfSuccess && exchangeConfSuccess;
}

Expand Down
4 changes: 3 additions & 1 deletion src/libcadet/model/PhaseTransitionModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class IPhaseTransitionModel
*/
virtual bool configure(IParameterProvider& paramProvider, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx) = 0;

virtual bool configure(IParameterProvider& paramProvider, UnitOpIdx unitOpIdx) = 0;

/**
* @brief Returns the ParameterId of all bound phase initial conditions (equations)
* @details The array has to be filled in the order of the equations.
Expand All @@ -130,7 +132,7 @@ class IPhaseTransitionModel
* @param [in] unitOpIdx Index of the unit operation this binding model belongs to
* @param [in] parTypeIdx Index of the particle type this binding model belongs to
*/
virtual void fillChannelInitialParameters(ParameterId* params, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx) const CADET_NOEXCEPT = 0;
virtual void fillChannelInitialParameters(ParameterId* params, UnitOpIdx unitOpIdx) const CADET_NOEXCEPT = 0;

/**
* @brief Sets external functions for this binding model
Expand Down
2 changes: 2 additions & 0 deletions src/libcadet/model/UnitOperationBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ namespace model
{

class IBindingModel;
class IPhaseTransitionModel;
class IDynamicReactionModel;

/**
Expand Down Expand Up @@ -78,6 +79,7 @@ class UnitOperationBase : public IUnitOperation

UnitOpIdx _unitOpIdx; //!< Unit operation index
std::vector<IBindingModel*> _binding; //!< Binding model
std::vector<IPhaseTransitionModel*> _exchange; //!< Exchange model
bool _singleBinding; //!< Determines whether only a single binding model is present
std::vector<IDynamicReactionModel*> _dynReaction; //!< Dynamic reaction model
bool _singleDynReaction; //!< Determines whether only a single dynamic reaction model is present
Expand Down
102 changes: 51 additions & 51 deletions src/libcadet/model/exchange/LinearExchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,40 @@ class LinearParamHandler : public ConstParamHandlerBase
* @details See IexchangeModel::configure() for details.
* @param [in] paramProvider IParameterProvider used for reading parameters
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @return @c true if the parameters were read and validated successfully, otherwise @c false
*/
inline bool configure(IParameterProvider& paramProvider, unsigned int nComp, unsigned int const* nChannelStates)
inline bool configure(IParameterProvider& paramProvider, unsigned int nComp, unsigned int const* nChannel)
{
_kA.configure("LIN_KA", paramProvider, nComp, nChannelStates);
_kD.configure("LIN_KD", paramProvider, nComp, nChannelStates);
return validateConfig(nComp, nChannelStates);
_kA.configure("LIN_KA", paramProvider, nComp, nChannel);
_kD.configure("LIN_KD", paramProvider, nComp, nChannel);
return validateConfig(nComp, nChannel);
}

/**
* @brief Registers all local parameters in a map for further use
* @param [in,out] parameters Map in which the parameters are stored
* @param [in] unitOpIdx Index of the unit operation used for registering the parameters
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
*/
inline void registerParameters(std::unordered_map<ParameterId, active*>& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nChannelStates)
inline void registerParameters(std::unordered_map<ParameterId, active*>& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nChannel)
{
_kA.registerParam("LIN_KA", parameters, unitOpIdx, parTypeIdx, nComp, nChannelStates);
_kD.registerParam("LIN_KD", parameters, unitOpIdx, parTypeIdx, nComp, nChannelStates);
_kA.registerParam("LIN_KA", parameters, unitOpIdx, parTypeIdx, nComp, nChannel);
_kD.registerParam("LIN_KD", parameters, unitOpIdx, parTypeIdx, nComp, nChannel);
}

/**
* @brief Reserves space in the storage of the parameters
* @param [in] numElem Number of elements (total)
* @param [in] numSlices Number of slices / exchange site types
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
*/
inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, unsigned int const* nChannelStates) \
inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, unsigned int const* nChannel) \
{
_kA.reserve(numElem, numSlices, nComp, nChannelStates);
_kD.reserve(numElem, numSlices, nComp, nChannelStates);
_kA.reserve(numElem, numSlices, nComp, nChannel);
_kD.reserve(numElem, numSlices, nComp, nChannel);
}

/**
Expand All @@ -110,11 +110,11 @@ class LinearParamHandler : public ConstParamHandlerBase
* @param [in] r Radial coordinate in the bead
* @param [in] secIdx Index of the current section
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @param [in,out] workSpace Memory buffer for updated data
* @return Externally dependent parameter values
*/
inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannelStates, LinearBufferAllocator& workSpace) const
inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannel, LinearBufferAllocator& workSpace) const
{
return &_localParams;
}
Expand All @@ -126,11 +126,11 @@ class LinearParamHandler : public ConstParamHandlerBase
* @param [in] r Radial coordinate in the bead
* @param [in] secIdx Index of the current section
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @param [in,out] workSpace Memory buffer for updated data
* @return Time derivatives of externally dependent parameters
*/
inline std::tuple<ParamsHandle, ParamsHandle> updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannelStates, LinearBufferAllocator& workSpace) const
inline std::tuple<ParamsHandle, ParamsHandle> updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannel, LinearBufferAllocator& workSpace) const
{
return std::make_tuple<ParamsHandle, ParamsHandle>(&_localParams, nullptr);
}
Expand All @@ -140,10 +140,10 @@ class LinearParamHandler : public ConstParamHandlerBase
/**
* @brief Validates recently read parameters
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @return @c true if the parameters were validated successfully, otherwise @c false
*/
inline bool validateConfig(unsigned int nComp, unsigned int const* nChannelStates)
inline bool validateConfig(unsigned int nComp, unsigned int const* nChannel)
{
if ((_kA.size() != _kD.size()) || (_kA.size() < nComp))
throw InvalidParameterException("LIN_KA and LIN_KD have to have the same size");
Expand Down Expand Up @@ -196,43 +196,43 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase
* @details See IBindingModel::configure() for details.
* @param [in] paramProvider IParameterProvider used for reading parameters
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @return @c true if the parameters were read and validated successfully, otherwise @c false
*/
inline bool configure(IParameterProvider& paramProvider, unsigned int nComp, unsigned int const* nChannelStates)
inline bool configure(IParameterProvider& paramProvider, unsigned int nComp, unsigned int const* nChannel)
{
_kA.configure("LIN_KA", paramProvider, nComp, nChannelStates);
_kD.configure("LIN_KD", paramProvider, nComp, nChannelStates);
_kA.configure("LIN_KA", paramProvider, nComp, nChannel);
_kD.configure("LIN_KD", paramProvider, nComp, nChannel);

// Number of externally dependent parameters (2) needs to be given to ExternalParamHandlerBase::configure()
ExternalParamHandlerBase::configure(paramProvider, 2);
return validateConfig(nComp, nChannelStates);
return validateConfig(nComp, nChannel);
}

/**
* @brief Registers all local parameters in a map for further use
* @param [in,out] parameters Map in which the parameters are stored
* @param [in] unitOpIdx Index of the unit operation used for registering the parameters
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
*/
inline void registerParameters(std::unordered_map<ParameterId, active*>& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nChannelStates)
inline void registerParameters(std::unordered_map<ParameterId, active*>& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nChannel)
{
_kA.registerParam("LIN_KA", parameters, unitOpIdx, parTypeIdx, nComp, nChannelStates);
_kD.registerParam("LIN_KD", parameters, unitOpIdx, parTypeIdx, nComp, nChannelStates);
_kA.registerParam("LIN_KA", parameters, unitOpIdx, parTypeIdx, nComp, nChannel);
_kD.registerParam("LIN_KD", parameters, unitOpIdx, parTypeIdx, nComp, nChannel);
}

/**
* @brief Reserves space in the storage of the parameters
* @param [in] numElem Number of elements (total)
* @param [in] numSlices Number of slices / exchange site types
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
*/
inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, unsigned int const* nChannelStates) \
inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, unsigned int const* nChannel) \
{
_kA.reserve(numElem, numSlices, nComp, nChannelStates);
_kD.reserve(numElem, numSlices, nComp, nChannelStates);
_kA.reserve(numElem, numSlices, nComp, nChannel);
_kD.reserve(numElem, numSlices, nComp, nChannel);
}

/**
Expand All @@ -243,11 +243,11 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase
* @param [in] r Radial coordinate in the bead
* @param [in] secIdx Index of the current section
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @param [in,out] workSpace Memory buffer for updated data
* @return Externally dependent parameter values
*/
inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannelStates, LinearBufferAllocator& workSpace) const
inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannel, LinearBufferAllocator& workSpace) const
{
// Allocate params_t and buffer for function evaluation
BufferedScalar<params_t> localParams = workSpace.scalar<params_t>();
Expand All @@ -258,10 +258,10 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase

// Prepare the buffer for the data and update the data
_kA.prepareCache(localParams->kA, workSpace);
_kA.update(cadet::util::dataOfLocalVersion(localParams->kA), extFunBuffer[0], nComp, nChannelStates);
_kA.update(cadet::util::dataOfLocalVersion(localParams->kA), extFunBuffer[0], nComp, nChannel);

_kD.prepareCache(localParams->kD, workSpace);
_kD.update(cadet::util::dataOfLocalVersion(localParams->kD), extFunBuffer[1], nComp, nChannelStates);
_kD.update(cadet::util::dataOfLocalVersion(localParams->kD), extFunBuffer[1], nComp, nChannel);

return localParams;
}
Expand All @@ -274,11 +274,11 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase
* @param [in] r Radial coordinate in the bead
* @param [in] secIdx Index of the current section
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @param [in,out] workSpace Memory buffer for updated data
* @return Tuple with externally dependent parameter values and their time derivatives
*/
inline std::tuple<ParamsHandle, ParamsHandle> updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannelStates, LinearBufferAllocator& workSpace) const
inline std::tuple<ParamsHandle, ParamsHandle> updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nChannel, LinearBufferAllocator& workSpace) const
{
// Allocate params_t for parameters and their time derivatives
BufferedScalar<params_t> localParams = workSpace.scalar<params_t>();
Expand All @@ -294,16 +294,16 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase

// Prepare the buffer for the data and update the data
_kA.prepareCache(localParams->kA, workSpace);
_kA.update(cadet::util::dataOfLocalVersion(localParams->kA), extFunBuffer[0], nComp, nChannelStates);
_kA.update(cadet::util::dataOfLocalVersion(localParams->kA), extFunBuffer[0], nComp, nChannel);

_kA.prepareCache(p->kA, workSpace);
_kA.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->kA), extFunBuffer[0], extDerivBuffer[0], nComp, nChannelStates);
_kA.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->kA), extFunBuffer[0], extDerivBuffer[0], nComp, nChannel);

_kD.prepareCache(localParams->kD, workSpace);
_kD.update(cadet::util::dataOfLocalVersion(localParams->kD), extFunBuffer[1], nComp, nChannelStates);
_kD.update(cadet::util::dataOfLocalVersion(localParams->kD), extFunBuffer[1], nComp, nChannel);

_kD.prepareCache(p->kD, workSpace);
_kD.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->kD), extFunBuffer[1], extDerivBuffer[1], nComp, nChannelStates);
_kD.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->kD), extFunBuffer[1], extDerivBuffer[1], nComp, nChannel);

return std::make_tuple<ParamsHandle, ParamsHandle>(std::move(localParams), std::move(p));;
}
Expand All @@ -313,10 +313,10 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase
* @details Memory size in bytes.
* @param [in] nComp Number of components
* @param [in] totalNumBoundStates Total number of bound states
* @param [in] nChannelStates Array with bound states for each component
* @param [in] nChannel Array with bound states for each component
* @return Memory size in bytes
*/
inline std::size_t cacheSize(unsigned int nComp, unsigned int totalNumBoundStates, unsigned int const* nChannelStates) const CADET_NOEXCEPT
inline std::size_t cacheSize(unsigned int nComp, unsigned int totalNumBoundStates, unsigned int const* nChannel) const CADET_NOEXCEPT
{
// Required buffer memory:
// + params_t object
Expand All @@ -326,18 +326,18 @@ class ExtLinearParamHandler : public ExternalParamHandlerBase
// + buffer for parameter time derivatives (memory for _kA data + memory for _kD data)
return 2 * sizeof(params_t) + alignof(params_t)
+ 2 * 2 * sizeof(double) + alignof(double)
+ 2 * (_kA.additionalDynamicMemory(nComp, totalNumBoundStates, nChannelStates) + _kD.additionalDynamicMemory(nComp, totalNumBoundStates, nChannelStates));
+ 2 * (_kA.additionalDynamicMemory(nComp, totalNumBoundStates, nChannel) + _kD.additionalDynamicMemory(nComp, totalNumBoundStates, nChannel));
}

protected:

/**
* @brief Validates recently read parameters
* @param [in] nComp Number of components
* @param [in] nChannelStates Array with number of bound states for each component
* @param [in] nChannel Array with number of bound states for each component
* @return @c true if the parameters were validated successfully, otherwise @c false
*/
inline bool validateConfig(unsigned int nComp, unsigned int const* nChannelStates)
inline bool validateConfig(unsigned int nComp, unsigned int const* nChannel)
{
if ((_kA.size() != _kD.size()) || (_kA.size() < nComp))
throw InvalidParameterException("LIN_KA and LIN_KD have to have the same size");
Expand Down Expand Up @@ -384,8 +384,8 @@ class LinearExchangeBase : public IPhaseTransitionModel
{
_parameters.clear();

// Read parameters (k_a and k_d,corss section area)
_paramHandler.configure(paramProvider, _nComp, _nChannel); // add cross section area in parameter
// Read parameters (k_a and k_d)
_paramHandler.configure(paramProvider, _nComp, _nChannel);

// Register parameters
_paramHandler.registerParameters(_parameters, unitOpIdx, parTypeIdx, _nComp, _nChannel);
Expand Down Expand Up @@ -449,9 +449,9 @@ class LinearExchangeBase : public IPhaseTransitionModel
return nullptr;
}

virtual unsigned int workspaceSize(unsigned int nComp, unsigned int totalNumBoundStates, unsigned int const* nChannelStates) const CADET_NOEXCEPT
virtual unsigned int workspaceSize(unsigned int nComp, unsigned int totalNumBoundStates, unsigned int const* nChannel) const CADET_NOEXCEPT
{
return _paramHandler.cacheSize(nComp, totalNumBoundStates, nChannelStates);
return _paramHandler.cacheSize(nComp, totalNumBoundStates, nChannel);
}

virtual void setExternalFunctions(IExternalFunction** extFuns, unsigned int size) { _paramHandler.setExternalFunctions(extFuns, size); }
Expand Down

0 comments on commit 720a8c1

Please sign in to comment.