Skip to content

Commit

Permalink
Add an exchange module for the MCT (#257)
Browse files Browse the repository at this point in the history
Key changes :

* Add PhaseTransitionModel.hpp and first changes for Linear Exchange

* Add comments for possible changes

* Add comments where to add exchange modul

* Add linear flux residual to MCT

* Add jacobian to MCT modul

---------

Co-authored-by: Jan Breuer <[email protected]>
  • Loading branch information
AntoniaBerger and jbreue16 authored Nov 20, 2024
1 parent 020834d commit f9e963a
Show file tree
Hide file tree
Showing 21 changed files with 593 additions and 70 deletions.
2 changes: 1 addition & 1 deletion include/cadet/cadet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @authors Please refer to CONTRIBUTING.md
* @version 5.0.1
* @date 2008-2024
* @date 2008-present
* @copyright GNU General Public License v3.0 (or, at your option, any later version).
*/

Expand Down
6 changes: 6 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_EXCHANGEMODEL_SOURCES
${CMAKE_SOURCE_DIR}/src/libcadet/model/exchange/LinearExchange.cpp
)



# LIBCADET_REACTIONMODEL_SOURCES holds all source files of reaction models
set(LIBCADET_REACTIONMODEL_SOURCES
Expand All @@ -123,6 +128,7 @@ set(LIBCADET_SOURCES
${CMAKE_SOURCE_DIR}/src/libcadet/AdUtils.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/Weno.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/BindingModelFactory.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/ExchangeModelFactory.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/ReactionModelFactory.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/ParameterDependenceFactory.cpp
${CMAKE_SOURCE_DIR}/src/libcadet/graph/GraphAlgos.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/libcadet/ConfigurationHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class IExternalFunction;
namespace model
{
class IBindingModel;
class IExchangeModel;
class IDynamicReactionModel;
class IParameterStateDependence;
class IParameterParameterDependence;
Expand Down Expand Up @@ -58,6 +59,9 @@ class IConfigHelper
*/
virtual model::IBindingModel* createBindingModel(const std::string& name) const = 0;

virtual model::IExchangeModel* createExchangeModel(const std::string& name) const = 0;


/**
* @brief Checks if there is an IBindingModel of the given @p name
* @param [in] name Name of the IBindingModel object
Expand Down
74 changes: 74 additions & 0 deletions src/libcadet/ExchangeModelFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// =============================================================================
// CADET
//
// Copyright © 2008-present: The CADET-Core Authors
// Please see the AUTHORS.md file.
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the GNU Public License v3.0 (or, at
// your option, any later version) which accompanies this distribution, and
// is available at http://www.gnu.org/licenses/gpl.html
// =============================================================================

#include "ExchangeModelFactory.hpp"
#include "cadet/Exceptions.hpp"


namespace cadet
{
namespace model
{
namespace exchange
{
void registerLinearExModel(std::unordered_map<std::string, std::function<model::IExchangeModel* ()>>& exchange);
}
}

ExchangeModelFactory::ExchangeModelFactory()
{
// Register all ExchangeModels here
model::exchange::registerLinearExModel(_exchangeModels);

}

ExchangeModelFactory::~ExchangeModelFactory() { }

template <class ExchangeModel_t>
void ExchangeModelFactory::registerModel(const std::string& name)
{
_exchangeModels[name] = []() { return new ExchangeModel_t(); };
}

template <class ExchangeModel_t>
void ExchangeModelFactory::registerModel()
{
registerModel<ExchangeModel_t>(ExchangeModel_t::identifier());
}

model::IExchangeModel* ExchangeModelFactory::create(const std::string& name) const
{
const auto it = _exchangeModels.find(name);
if (it == _exchangeModels.end())
{
// ExchangeModel was not found
return nullptr;
}

// Call factory function (thanks to type erasure of std::function we can store
// all factory functions in one container)
return it->second();
}

void ExchangeModelFactory::registerModel(const std::string& name, std::function<model::IExchangeModel*()> factory)
{
if (_exchangeModels.find(name) == _exchangeModels.end())
_exchangeModels[name] = factory;
else
throw InvalidParameterException("IExchange implementation with the name " + name + " is already registered and cannot be overwritten");
}

bool ExchangeModelFactory::exists(const std::string& name) const
{
return _exchangeModels.find(name) != _exchangeModels.end();
}
} // namespace cadet
90 changes: 90 additions & 0 deletions src/libcadet/ExchangeModelFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// =============================================================================
// CADET
//
// Copyright © 2008-present: The CADET-Core Authors
// Please see the AUTHORS.md file.
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the GNU Public License v3.0 (or, at
// your option, any later version) which accompanies this distribution, and
// is available at http://www.gnu.org/licenses/gpl.html
// =============================================================================

/**
* @file
* Defines the ExchangeModelFactory
*/

#ifndef LIBCADET_EXCHANGEMODELFACTORY_HPP_
#define LIBCADET_EXCHANGEMODELFACTORY_HPP_

#include <string>
#include <unordered_map>
#include <functional>

namespace cadet
{

namespace model
{
class IExchangeModel;
}

/**
* @brief Creates Exchange models
*/
class ExchangeModelFactory
{
public:
/**
* @brief Construct the ExchangeModelFactory
* @details All internal exchange models are registered here.
*/
ExchangeModelFactory();

~ExchangeModelFactory();

/**
* @brief Creates exchange models with the given @p name
* @param [in] name Name of the exchange model
* @return The exchange model or @c NULL if a exchange model with this name does not exist
*/
model::IExchangeModel* create(const std::string& name) const;

/**
* @brief Registers the given exchange model implementation
* @param [in] name Name of the IExchangeModel implementation
* @param [in] factory Function that creates an object of the IExchangeModel class
*/
void registerModel(const std::string& name, std::function<model::IExchangeModel*()> factory);

/**
* @brief Returns whether a exchange model of the given name @p name exists
* @param [in] name Name of the exchange model
* @return @c true if a exchange model of this name exists, otherwise @c false
*/
bool exists(const std::string& name) const;
protected:

/**
* @brief Registers an IExchangeModel
* @param [in] name Name of the exchange model
* @tparam ExchangeModel_t Type of the exchange model
*/
template <class ExchangeModel_t>
void registerModel(const std::string& name);

/**
* @brief Registers an IExchangeModel
* @details The name of the exchange model is inferred from the static function IExchangeModel::identifier().
* @tparam ExchangeModel_t Type of the exchange model
*/
template <class ExchangeModel_t>
void registerModel();

std::unordered_map<std::string, std::function<model::IExchangeModel* ()>> _exchangeModels; //!< Map with factory functions
};

} // namespace cadet

#endif // LIBCADET_EXCHANGEMODELFACTORY_HPP_
4 changes: 2 additions & 2 deletions src/libcadet/HighResKoren.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// =============================================================================
// CADET
//
// Copyright © 2008-2022: 2008-present: The CADET-Core Authors
// Please see the AUTHORS and CONTRIBUTORS file.
// Copyright © 2008-present: 2008-present: The CADET-Core Authors
// Please see the AUTHORS.md file.
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the GNU Public License v3.0 (or, at
Expand Down
5 changes: 5 additions & 0 deletions src/libcadet/ModelBuilderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ namespace cadet
return _bindingModels.create(name);
}

model::IExchangeModel* ModelBuilder::createExchangeModel(const std::string& name) const
{
return _exchangeModels.create(name);
}

bool ModelBuilder::isValidBindingModel(const std::string& name) const
{
return _bindingModels.exists(name);
Expand Down
3 changes: 3 additions & 0 deletions src/libcadet/ModelBuilderImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "cadet/ModelBuilder.hpp"
#include "BindingModelFactory.hpp"
#include "ExchangeModelFactory.hpp"
#include "ReactionModelFactory.hpp"
#include "ParameterDependenceFactory.hpp"
#include "ConfigurationHelper.hpp"
Expand Down Expand Up @@ -60,6 +61,7 @@ class ModelBuilder : public IModelBuilder, public IConfigHelper

virtual IInletProfile* createInletProfile(const std::string& type) const;
virtual model::IBindingModel* createBindingModel(const std::string& name) const;
virtual model::IExchangeModel* createExchangeModel(const std::string& name) const;
virtual bool isValidBindingModel(const std::string& name) const;
virtual model::IDynamicReactionModel* createDynamicReactionModel(const std::string& name) const;
virtual bool isValidDynamicReactionModel(const std::string& name) const;
Expand Down Expand Up @@ -88,6 +90,7 @@ class ModelBuilder : public IModelBuilder, public IConfigHelper
void registerModel();

BindingModelFactory _bindingModels; //!< Factory for IBindingModel implementations
ExchangeModelFactory _exchangeModels; //!< Factory for IExchangeModel implementations
ReactionModelFactory _reactionModels; //!< Factory for IDynamicReactionModel implementations
ParameterDependenceFactory _paramDeps; //!< Factory for IParameterStateDependence implementations

Expand Down
73 changes: 73 additions & 0 deletions src/libcadet/model/ExchangeModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// =============================================================================
// CADET
//
// Copyright © 2008-present: The CADET-Core Authors
// Please see the AUTHORS.md file.
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the GNU Public License v3.0 (or, at
// your option, any later version) which accompanies this distribution, and
// is available at http://www.gnu.org/licenses/gpl.html
// =============================================================================

/**
* @file
* Defines the ExchangeModel interface.
*/

#ifndef LIBCADET_EXCHANGEMODELINTERFACE_HPP_
#define LIBCADET_EXCHANGEMODELINTERFACE_HPP_

#include <unordered_map>

#include "CompileTimeConfig.hpp"
#include "cadet/ParameterProvider.hpp"
#include "cadet/ParameterId.hpp"
#include "linalg/DenseMatrix.hpp"
#include "linalg/BandMatrix.hpp"

#ifdef ENABLE_DG
#include "linalg/BandedEigenSparseRowIterator.hpp"
#endif

#include "AutoDiff.hpp"
#include "SimulationTypes.hpp"
#include "Memory.hpp"

namespace cadet
{

class IParameterProvider;
class IExternalFunction;

struct ColumnPosition;

namespace model
{

class IExchangeModel
{
public:

virtual ~IExchangeModel() CADET_NOEXCEPT { }

virtual const char* name() const CADET_NOEXCEPT = 0;

//virtual bool requiresConfiguration() const CADET_NOEXCEPT = 0;

virtual bool configureModelDiscretization(IParameterProvider& paramProvider, unsigned int nComp, unsigned int nChannel, unsigned int nCol) = 0;

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

virtual int residual(active const* y, active* res, WithParamSensitivity, bool wantJac, linalg::BandedSparseRowIterator jacBegin) const = 0;
virtual int residual(active const* y, active* res, WithoutParamSensitivity, bool wantJac, linalg::BandedSparseRowIterator jacBegin) const = 0;
virtual int residual(double const* y, active* res, WithParamSensitivity, bool wantJac, linalg::BandedSparseRowIterator jacBegin) const = 0;
virtual int residual(double const* y, double* res, WithoutParamSensitivity, bool wantJac, linalg::BandedSparseRowIterator jacBegin) const = 0;

protected:
};

} // namespace model
} // namespace cadet

#endif // LIBCADET_EXCHANGEMODELINTERFACE_HPP_
11 changes: 11 additions & 0 deletions src/libcadet/model/ModelUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ inline unsigned int numBoundStates(unsigned int const* const nBound, unsigned in
return totalBound;
}

/**
* @brief Returns the number of channel states for each component
* @param [in] nChannel Number of channels
* @param [in] nComp Number of components
* @return Number of channel states of each component
*/
inline unsigned int numExchangeStates(unsigned int nChannel, unsigned int nComp)
{
return nChannel * nComp;
}

/**
* @brief Returns the number of binding components (i.e., components that have at least @c 1 bound state)
* @param [in] nBound Array with number of bound states for each component
Expand Down
Loading

0 comments on commit f9e963a

Please sign in to comment.