From ad8f5c6c569c6b734666821f71f18c340a331de0 Mon Sep 17 00:00:00 2001 From: Samuel Leweke Date: Mon, 11 Sep 2023 23:43:22 +0200 Subject: [PATCH] Revert external function templates --- src/libcadet/CMakeLists.txt | 8 +- .../binding/ExternalFunctionTemplate.cpp | 682 ------------------ .../binding/ExternalFunctionTemplate.cpp.in | 433 +++++++++++ .../reaction/ExternalFunctionTemplate.cpp | 682 ------------------ .../reaction/ExternalFunctionTemplate.cpp.in | 433 +++++++++++ 5 files changed, 870 insertions(+), 1368 deletions(-) delete mode 100644 src/libcadet/model/binding/ExternalFunctionTemplate.cpp create mode 100644 src/libcadet/model/binding/ExternalFunctionTemplate.cpp.in delete mode 100644 src/libcadet/model/reaction/ExternalFunctionTemplate.cpp create mode 100644 src/libcadet/model/reaction/ExternalFunctionTemplate.cpp.in diff --git a/src/libcadet/CMakeLists.txt b/src/libcadet/CMakeLists.txt index dd483342f..94f06b341 100644 --- a/src/libcadet/CMakeLists.txt +++ b/src/libcadet/CMakeLists.txt @@ -193,9 +193,9 @@ foreach(BM IN LISTS LIBCADET_BINDINGMODEL_SOURCES) get_filename_component(BMFILEWE ${BM} NAME_WE) get_filename_component(BMFILE ${BM} NAME) add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${BMFILE} - COMMAND templateCodeGen ${CMAKE_SOURCE_DIR}/src/libcadet/model/binding/ExternalFunctionTemplate.cpp ${BM} ${CMAKE_CURRENT_BINARY_DIR}/${BMFILE} + COMMAND templateCodeGen ${CMAKE_SOURCE_DIR}/src/libcadet/model/binding/ExternalFunctionTemplate.cpp.in ${BM} ${CMAKE_CURRENT_BINARY_DIR}/${BMFILE} MAIN_DEPENDENCY ${BM} - DEPENDS ${CMAKE_SOURCE_DIR}/src/libcadet/model/binding/ExternalFunctionTemplate.cpp + DEPENDS ${CMAKE_SOURCE_DIR}/src/libcadet/model/binding/ExternalFunctionTemplate.cpp.in COMMENT "Generating code for ${BMFILEWE}" ) list (APPEND LIBCADET_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/${BMFILE}") @@ -205,9 +205,9 @@ foreach(RM IN LISTS LIBCADET_REACTIONMODEL_SOURCES) get_filename_component(RMFILEWE ${RM} NAME_WE) get_filename_component(RMFILE ${RM} NAME) add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${RMFILE} - COMMAND templateCodeGen ${CMAKE_SOURCE_DIR}/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp ${RM} ${CMAKE_CURRENT_BINARY_DIR}/${RMFILE} + COMMAND templateCodeGen ${CMAKE_SOURCE_DIR}/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp.in ${RM} ${CMAKE_CURRENT_BINARY_DIR}/${RMFILE} MAIN_DEPENDENCY ${RM} - DEPENDS ${CMAKE_SOURCE_DIR}/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp + DEPENDS ${CMAKE_SOURCE_DIR}/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp.in COMMENT "Generating code for ${RMFILEWE}" ) list (APPEND LIBCADET_SOURCES "${CMAKE_CURRENT_BINARY_DIR}/${RMFILE}") diff --git a/src/libcadet/model/binding/ExternalFunctionTemplate.cpp b/src/libcadet/model/binding/ExternalFunctionTemplate.cpp deleted file mode 100644 index d34a386c8..000000000 --- a/src/libcadet/model/binding/ExternalFunctionTemplate.cpp +++ /dev/null @@ -1,682 +0,0 @@ -// ============================================================================= -// CADET -// -// Copyright © 2008-2022: The CADET Authors -// Please see the AUTHORS and CONTRIBUTORS 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 - * Template for external function support in binding models. - * This file serves as a template for generating parameter handler classes that - * hold, configure, and update parameters, which may also depend on external - * functions. - */ - -/* */ -#include "Memory.hpp" - -#include - -namespace cadet -{ - -namespace model -{ - -class -{ - { - name - } -} : public cadet::model::ConstParamHandlerBase -{ -public: - struct ConstParams - { - {% for p in parameters % - } - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - - { - % if length (p / varName) > 1 % - } - - {% for v in p/varName % - } - - typename - { - { - p / type - } - } - ::storage_t{{v}}; - - { % endfor % } - - { - % else % - } - - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - - { % endif % } - - { % endfor % } { - % endif % - } - }; - - typedef ConstParams params_t; - typedef ConstParams const* ParamsHandle; - - static inline const char* identifier() CADET_NOEXCEPT; - - {{name}}() CADET_NOEXCEPT: - {% for p in parameters % - } - _{{p / varName}}(&_localParams.{{p / varName}}) - { - % if not is_last % - } - , { % endif % } { % endfor % } - { - % if exists ("constantParameters") % - } - , - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}({ % for v in p / varName % } & _localParams.{{v}} { % if not is_last % }, - { % endif % } { % endfor % }){ % else % } _{{p / varName}}(&_localParams.{{p / varName}}){ - % endif % } - { - % if not is_last % - } - , { % endif % } { % endfor % } { % endif % } - { - } - - inline bool configure(IParameterProvider & paramProvider, unsigned int nComp, unsigned int const* nBoundStates) - { - {% for p in parameters % - } - { - % if not existsIn(p, "skipConfig") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if not existsIn(p, "skipConfig") % - } - { - % if length (p / varName) > 1 % - } - { - % if existsIn (p, "default") % - } - _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); - { % endif % } { % else % } { - % if existsIn (p, "default") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endif % } { % endif % } { % endfor % } { - % endif % - } - return validateConfig(nComp, nBoundStates); - } - - inline void registerParameters(std::unordered_map & parameters, UnitOpIdx unitOpIdx, - ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); - { % else % } _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, - nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, - unsigned int const* nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.reserve(numElem, numSlices, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.reserve(numElem, numSlices, nComp, nBoundStates); - { % else % } _{{p / varName}}.reserve(numElem, numSlices, nComp, nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - return &_localParams; - } - - inline std::tuple updateTimeDerivative( - double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - return std::make_tuple(&_localParams, nullptr); - } - - {% for p in parameters % - } - inline const {{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline {{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) == 1 % - } - inline const {{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline {{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { - % else % - } - inline const {{p / type}} & {{p / objName}}() const CADET_NOEXCEPT - { - return _{{p / objName}}; - } - inline {{p / type}} & {{p / objName}}() CADET_NOEXCEPT - { - return _{{p / objName}}; - } - { % endif % } { % endfor % } { - % endif % - } - - inline char const* prefixInConfiguration() const CADET_NOEXCEPT - { - return ""; - } - -protected: - inline bool validateConfig(unsigned int nComp, unsigned int const* nBoundStates); - - ConstParams _localParams; - - {% for p in parameters % - } - { - { - p / type - } - } - _{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - { - { - p / type - } - } - _{{p / objName}}; - { % else % } { - { - p / type - } - } - _{{p / varName}}; - { % endif % } { % endfor % } { - % endif % - } -}; - -class -{ - { - externalName - } -} : public cadet::model::ExternalParamHandlerBase -{ -public: - struct ConstParams - { - { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - - { - % if length (p / varName) > 1 % - } - - {% for v in p/varName % - } - - typename - { - { - p / type - } - } - ::storage_t{{v}}; - - { % endfor % } - - { - % else % - } - - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - - { % endif % } - - { % endfor % } { - % endif % - } - }; - - struct VariableParams - { - {% for p in parameters % - } - typename util::localVersionOf < - { - { - p / type - } - } - ::storage_t > ::type{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - - { - % if length (p / varName) > 1 % - } - - {% for v in p/varName % - } - - typename - { - { - p / type - } - } - ::storage_t{{v}}; - - { % endfor % } - - { - % else % - } - - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - - { % endif % } - - { % endfor % } { - % endif % - } - }; - - typedef VariableParams params_t; - typedef ConstBufferedScalar ParamsHandle; - - static inline const char* identifier() CADET_NOEXCEPT; - - {{externalName}}() CADET_NOEXCEPT - { - % if exists ("constantParameters") % - } - : - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}({ % for v in p / varName % } & _constParams.{{v}} { % if not is_last % }, - { % endif % } { % endfor % }){ % else % } _{{p / varName}}(&_constParams.{{p / varName}}){ - % endif % } - { - % if not is_last % - } - , { % endif % } { % endfor % } { % endif % } - { - } - - inline bool configure(IParameterProvider & paramProvider, unsigned int nComp, unsigned int const* nBoundStates) - { - {% for p in parameters % - } - { - % if not existsIn(p, "skipConfig") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if not existsIn(p, "skipConfig") % - } - { - % if length (p / varName) > 1 % - } - { - % if existsIn (p, "default") % - } - _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); - { % endif % } { % else % } { - % if existsIn (p, "default") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endif % } { % endif % } { % endfor % } { % endif % } ExternalParamHandlerBase::configure( - paramProvider, {{length(parameters)}}); - return validateConfig(nComp, nBoundStates); - } - - inline void registerParameters(std::unordered_map & parameters, UnitOpIdx unitOpIdx, - ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, - nBoundStates); - { % else % } _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, - nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, - unsigned int const* nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.reserve(numElem, numSlices, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.reserve(numElem, numSlices, nComp, nBoundStates); - { % else % } _{{p / varName}}.reserve(numElem, numSlices, nComp, nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - BufferedScalar localParams = workSpace.scalar(); - BufferedArray extFunBuffer = workSpace.array({{length(parameters)}}); - evaluateExternalFunctions(t, secIdx, colPos, {{length(parameters)}}, static_cast(extFunBuffer)); - { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - {% for v in p/varName % - } - localParams->{{v}} = _constParams.{{v}}; - { % endfor % } { % else % } localParams->{{p / varName}} = _constParams.{{p / varName}}; - { % endif % } { % endfor % } { % endif % } - - {% for p in parameters % - } - _{{p / varName}}.prepareCache(localParams->{{p / varName}}, workSpace); - _{{p / varName}}.update(cadet::util::dataOfLocalVersion(localParams->{{p / varName}}), - extFunBuffer[{{index}}], nComp, nBoundStates); - { - % endfor % - } - - return localParams; - } - - inline std::tuple updateTimeDerivative( - double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - BufferedScalar localParams = workSpace.scalar(); - BufferedScalar p = workSpace.scalar(); - BufferedArray extFunBuffer = workSpace.array({{length(parameters)}}); - BufferedArray extDerivBuffer = workSpace.array({{length(parameters)}}); - evaluateExternalFunctions(t, secIdx, colPos, {{length(parameters)}}, static_cast(extFunBuffer)); - evaluateTimeDerivativeExternalFunctions(t, secIdx, colPos, {{length(parameters)}}, - static_cast(extDerivBuffer)); - - { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - {% for v in p/varName % - } - localParams->{{v}} = _constParams.{{v}}; - p->{{v}} = _constParams.{{v}}; - { % endfor % } { % else % } localParams->{{p / varName}} = _constParams.{{p / varName}}; - p->{{p / varName}} = _constParams.{{p / varName}}; - { % endif % } { % endfor % } { % endif % } - - {% for p in parameters % - } - _{{p / varName}}.prepareCache(localParams->{{p / varName}}, workSpace); - _{{p / varName}}.update(cadet::util::dataOfLocalVersion(localParams->{{p / varName}}), - extFunBuffer[{{index}}], nComp, nBoundStates); - - _{{p / varName}}.prepareCache(p->{{p / varName}}, workSpace); - _{{p / varName}}.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->{{p / varName}}), - extFunBuffer[{{index}}], extDerivBuffer[{{index}}], nComp, - nBoundStates); - { - % endfor % - } - - return std::make_tuple(std::move(localParams), std::move(p)); - } - - inline std::size_t cacheSize(unsigned int nComp, unsigned int totalNumBoundStates, - unsigned int const* nBoundStates) const CADET_NOEXCEPT - { - return 2 * sizeof(params_t) + alignof(params_t) + 2 * {{length(parameters)}} * sizeof(double) + - alignof(double) + - 2 * ({ % for p in parameters % } _{{p / varName}}.additionalDynamicMemory(nComp, totalNumBoundStates, - nBoundStates) { - % if not is_last % - } + { % endif % } { % endfor % }); - } - - {% for p in parameters % - } - inline const External{{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline External{{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) == 1 % - } - inline const {{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline {{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { - % else % - } - inline const {{p / type}} & {{p / objName}}() const CADET_NOEXCEPT - { - return _{{p / objName}}; - } - inline {{p / type}} & {{p / objName}}() CADET_NOEXCEPT - { - return _{{p / objName}}; - } - { % endif % } { % endfor % } { - % endif % - } - - inline char const* prefixInConfiguration() const CADET_NOEXCEPT - { - return "EXT_"; - } - - protected: - inline bool validateConfig(unsigned int nComp, unsigned int const* nBoundStates); - - ConstParams _constParams; - - {% for p in parameters % - } - External - { - { - p / type - } - } - _{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - { - { - p / type - } - } - _{{p / objName}}; - { % else % } { - { - p / type - } - } - _{{p / varName}}; - { % endif % } { % endfor % } { - % endif % - } -}; - -} // namespace model -} // namespace cadet diff --git a/src/libcadet/model/binding/ExternalFunctionTemplate.cpp.in b/src/libcadet/model/binding/ExternalFunctionTemplate.cpp.in new file mode 100644 index 000000000..7fc91b04f --- /dev/null +++ b/src/libcadet/model/binding/ExternalFunctionTemplate.cpp.in @@ -0,0 +1,433 @@ +// ============================================================================= +// CADET +// +// Copyright © 2008-2022: The CADET Authors +// Please see the AUTHORS and CONTRIBUTORS 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 + * Template for external function support in binding models. + * This file serves as a template for generating parameter handler classes that + * hold, configure, and update parameters, which may also depend on external + * functions. + */ + +/* */ +#include "Memory.hpp" + +#include + +namespace cadet +{ + +namespace model +{ + +class {{ name }} : public cadet::model::ConstParamHandlerBase +{ +public: + struct ConstParams + { +{% for p in parameters %} + typename {{ p/type }}::storage_t {{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + + {% if length(p/varName) > 1 %} + + {% for v in p/varName %} + + typename {{ p/type }}::storage_t {{ v }}; + + {% endfor %} + + {% else %} + + typename {{ p/type }}::storage_t {{ p/varName }}; + + {% endif %} + + {% endfor %} +{% endif %} + }; + + typedef ConstParams params_t; + typedef ConstParams const* ParamsHandle; + + static inline const char* identifier() CADET_NOEXCEPT; + + {{ name }}() CADET_NOEXCEPT : +{% for p in parameters %} + _{{ p/varName }}(&_localParams.{{ p/varName }}) + {% if not is_last %} + , + {% endif %} +{% endfor %} +{% if exists("constantParameters") %} + , + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}({% for v in p/varName %} &_localParams.{{ v }} {% if not is_last %},{% endif %} {% endfor %}) + {% else %} + _{{ p/varName }}(&_localParams.{{ p/varName }}) + {% endif %} + {% if not is_last %},{% endif %} + {% endfor %} +{% endif %} + { } + + inline bool configure(IParameterProvider& paramProvider, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + {% if not existsIn(p, "skipConfig") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if not existsIn(p, "skipConfig") %} + {% if length(p/varName) > 1 %} + {% if existsIn(p, "default") %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% else %} + {% if existsIn(p, "default") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% endif %} + {% endif %} + {% endfor %} +{% endif %} + return validateConfig(nComp, nBoundStates); + } + + inline void registerParameters(std::unordered_map& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.reserve(numElem, numSlices, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.reserve(numElem, numSlices, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.reserve(numElem, numSlices, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + return &_localParams; + } + + inline std::tuple updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + return std::make_tuple(&_localParams, nullptr); + } + +{% for p in parameters %} + inline const {{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline {{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) == 1 %} + inline const {{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline {{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } + {% else %} + inline const {{ p/type }}& {{ p/objName }}() const CADET_NOEXCEPT { return _{{ p/objName }}; } + inline {{ p/type }}& {{ p/objName }}() CADET_NOEXCEPT { return _{{ p/objName }}; } + {% endif %} + {% endfor %} +{% endif %} + + inline char const* prefixInConfiguration() const CADET_NOEXCEPT { return ""; } + +protected: + inline bool validateConfig(unsigned int nComp, unsigned int const* nBoundStates); + + ConstParams _localParams; + +{% for p in parameters %} + {{ p/type }} _{{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {{ p/type }} _{{ p/objName }}; + {% else %} + {{ p/type }} _{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} +}; + +class {{ externalName }} : public cadet::model::ExternalParamHandlerBase +{ +public: + struct ConstParams + { +{% if exists("constantParameters") %} + {% for p in constantParameters %} + + {% if length(p/varName) > 1 %} + + {% for v in p/varName %} + + typename {{ p/type }}::storage_t {{ v }}; + + {% endfor %} + + {% else %} + + typename {{ p/type }}::storage_t {{ p/varName }}; + + {% endif %} + + {% endfor %} +{% endif %} + }; + + struct VariableParams + { +{% for p in parameters %} + typename util::localVersionOf<{{ p/type }}::storage_t>::type {{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + + {% if length(p/varName) > 1 %} + + {% for v in p/varName %} + + typename {{ p/type }}::storage_t {{ v }}; + + {% endfor %} + + {% else %} + + typename {{ p/type }}::storage_t {{ p/varName }}; + + {% endif %} + + {% endfor %} +{% endif %} + }; + + typedef VariableParams params_t; + typedef ConstBufferedScalar ParamsHandle; + + static inline const char* identifier() CADET_NOEXCEPT; + + {{ externalName }}() CADET_NOEXCEPT +{% if exists("constantParameters") %} + : + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}({% for v in p/varName %} &_constParams.{{ v }} {% if not is_last %},{% endif %} {% endfor %}) + {% else %} + _{{ p/varName }}(&_constParams.{{ p/varName }}) + {% endif %} + {% if not is_last %},{% endif %} + {% endfor %} +{% endif %} + { } + + inline bool configure(IParameterProvider& paramProvider, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + {% if not existsIn(p, "skipConfig") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if not existsIn(p, "skipConfig") %} + {% if length(p/varName) > 1 %} + {% if existsIn(p, "default") %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% else %} + {% if existsIn(p, "default") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% endif %} + {% endif %} + {% endfor %} +{% endif %} + ExternalParamHandlerBase::configure(paramProvider, {{ length(parameters) }}); + return validateConfig(nComp, nBoundStates); + } + + inline void registerParameters(std::unordered_map& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline void reserve(unsigned int numElem, unsigned int numSlices, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.reserve(numElem, numSlices, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.reserve(numElem, numSlices, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.reserve(numElem, numSlices, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + BufferedScalar localParams = workSpace.scalar(); + BufferedArray extFunBuffer = workSpace.array({{ length(parameters) }}); + evaluateExternalFunctions(t, secIdx, colPos, {{ length(parameters) }}, static_cast(extFunBuffer)); +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {% for v in p/varName %} + localParams->{{ v }} = _constParams.{{ v }}; + {% endfor %} + {% else %} + localParams->{{ p/varName }} = _constParams.{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} + +{% for p in parameters %} + _{{ p/varName }}.prepareCache(localParams->{{ p/varName }}, workSpace); + _{{ p/varName }}.update(cadet::util::dataOfLocalVersion(localParams->{{ p/varName }}), extFunBuffer[{{ index }}], nComp, nBoundStates); +{% endfor %} + + return localParams; + } + + inline std::tuple updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + BufferedScalar localParams = workSpace.scalar(); + BufferedScalar p = workSpace.scalar(); + BufferedArray extFunBuffer = workSpace.array({{ length(parameters) }}); + BufferedArray extDerivBuffer = workSpace.array({{ length(parameters) }}); + evaluateExternalFunctions(t, secIdx, colPos, {{ length(parameters) }}, static_cast(extFunBuffer)); + evaluateTimeDerivativeExternalFunctions(t, secIdx, colPos, {{ length(parameters) }}, static_cast(extDerivBuffer)); + +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {% for v in p/varName %} + localParams->{{ v }} = _constParams.{{ v }}; + p->{{ v }} = _constParams.{{ v }}; + {% endfor %} + {% else %} + localParams->{{ p/varName }} = _constParams.{{ p/varName }}; + p->{{ p/varName }} = _constParams.{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} + +{% for p in parameters %} + _{{ p/varName }}.prepareCache(localParams->{{ p/varName }}, workSpace); + _{{ p/varName }}.update(cadet::util::dataOfLocalVersion(localParams->{{ p/varName }}), extFunBuffer[{{ index }}], nComp, nBoundStates); + + _{{ p/varName }}.prepareCache(p->{{ p/varName }}, workSpace); + _{{ p/varName }}.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->{{ p/varName }}), extFunBuffer[{{ index }}], extDerivBuffer[{{ index }}], nComp, nBoundStates); +{% endfor %} + + return std::make_tuple(std::move(localParams), std::move(p)); + } + + inline std::size_t cacheSize(unsigned int nComp, unsigned int totalNumBoundStates, unsigned int const* nBoundStates) const CADET_NOEXCEPT + { + return 2 * sizeof(params_t) + alignof(params_t) + 2 * {{ length(parameters) }} * sizeof(double) + alignof(double) + 2 * ( +{% for p in parameters %} + _{{ p/varName }}.additionalDynamicMemory(nComp, totalNumBoundStates, nBoundStates) {% if not is_last %} + {% endif %} +{% endfor %} + ); + } + +{% for p in parameters %} + inline const External{{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline External{{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) == 1 %} + inline const {{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline {{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } + {% else %} + inline const {{ p/type }}& {{ p/objName }}() const CADET_NOEXCEPT { return _{{ p/objName }}; } + inline {{ p/type }}& {{ p/objName }}() CADET_NOEXCEPT { return _{{ p/objName }}; } + {% endif %} + {% endfor %} +{% endif %} + + inline char const* prefixInConfiguration() const CADET_NOEXCEPT { return "EXT_"; } + +protected: + inline bool validateConfig(unsigned int nComp, unsigned int const* nBoundStates); + + ConstParams _constParams; + +{% for p in parameters %} + External{{ p/type }} _{{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {{ p/type }} _{{ p/objName }}; + {% else %} + {{ p/type }} _{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} +}; + +} // namespace model +} // namespace cadet diff --git a/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp b/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp deleted file mode 100644 index 8b30dccde..000000000 --- a/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp +++ /dev/null @@ -1,682 +0,0 @@ -// ============================================================================= -// CADET -// -// Copyright © 2008-2022: The CADET Authors -// Please see the AUTHORS and CONTRIBUTORS 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 - * Template for external function support in reaction models. - * This file serves as a template for generating parameter handler classes that - * hold, configure, and update parameters, which may also depend on external - * functions. - */ - -/* */ -#include "Memory.hpp" - -#include - -namespace cadet -{ - -namespace model -{ - -class -{ - { - name - } -} : public cadet::model::ConstParamHandlerBase -{ -public: - struct ConstParams - { - {% for p in parameters % - } - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - - { - % if length (p / varName) > 1 % - } - - {% for v in p/varName % - } - - typename - { - { - p / type - } - } - ::storage_t{{v}}; - - { % endfor % } - - { - % else % - } - - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - - { % endif % } - - { % endfor % } { - % endif % - } - }; - - typedef ConstParams params_t; - typedef ConstParams const* ParamsHandle; - - static inline const char* identifier() CADET_NOEXCEPT; - - {{name}}() CADET_NOEXCEPT: - {% for p in parameters % - } - _{{p / varName}}(&_localParams.{{p / varName}}) - { - % if not is_last % - } - , { % endif % } { % endfor % } - { - % if exists ("constantParameters") % - } - , - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}({ % for v in p / varName % } & _localParams.{{v}} { % if not is_last % }, - { % endif % } { % endfor % }){ % else % } _{{p / varName}}(&_localParams.{{p / varName}}){ - % endif % } - { - % if not is_last % - } - , { % endif % } { % endfor % } { % endif % } - { - } - - inline bool configure(IParameterProvider & paramProvider, unsigned int nReactions, unsigned int nComp, - unsigned int const* nBoundStates) - { - {% for p in parameters % - } - { - % if not existsIn(p, "skipConfig") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if not existsIn(p, "skipConfig") % - } - { - % if length (p / varName) > 1 % - } - { - % if existsIn (p, "default") % - } - _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); - { % endif % } { % else % } { - % if existsIn (p, "default") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endif % } { % endif % } { % endfor % } { - % endif % - } - return validateConfig(nReactions, nComp, nBoundStates); - } - - inline void registerParameters(std::unordered_map & parameters, UnitOpIdx unitOpIdx, - ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); - { % else % } _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, - nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline void reserve(unsigned int nReactions, unsigned int nComp, unsigned int nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.reserve(nReactions, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.reserve(nReactions, nComp, nBoundStates); - { % else % } _{{p / varName}}.reserve(nReactions, nComp, nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - return &_localParams; - } - - inline std::tuple updateTimeDerivative( - double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - return std::make_tuple(&_localParams, nullptr); - } - - {% for p in parameters % - } - inline const {{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline {{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) == 1 % - } - inline const {{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline {{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { - % else % - } - inline const {{p / type}} & {{p / objName}}() const CADET_NOEXCEPT - { - return _{{p / objName}}; - } - inline {{p / type}} & {{p / objName}}() CADET_NOEXCEPT - { - return _{{p / objName}}; - } - { % endif % } { % endfor % } { - % endif % - } - - inline char const* prefixInConfiguration() const CADET_NOEXCEPT - { - return ""; - } - -protected: - inline bool validateConfig(unsigned int nReactions, unsigned int nComp, unsigned int const* nBoundStates); - - ConstParams _localParams; - - {% for p in parameters % - } - { - { - p / type - } - } - _{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - { - { - p / type - } - } - _{{p / objName}}; - { % else % } { - { - p / type - } - } - _{{p / varName}}; - { % endif % } { % endfor % } { - % endif % - } -}; - -class -{ - { - externalName - } -} : public cadet::model::ExternalParamHandlerBase -{ -public: - struct ConstParams - { - { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - - { - % if length (p / varName) > 1 % - } - - {% for v in p/varName % - } - - typename - { - { - p / type - } - } - ::storage_t{{v}}; - - { % endfor % } - - { - % else % - } - - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - - { % endif % } - - { % endfor % } { - % endif % - } - }; - - struct VariableParams - { - {% for p in parameters % - } - typename util::localVersionOf < - { - { - p / type - } - } - ::storage_t > ::type{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - - { - % if length (p / varName) > 1 % - } - - {% for v in p/varName % - } - - typename - { - { - p / type - } - } - ::storage_t{{v}}; - - { % endfor % } - - { - % else % - } - - typename - { - { - p / type - } - } - ::storage_t{{p / varName}}; - - { % endif % } - - { % endfor % } { - % endif % - } - }; - - typedef VariableParams params_t; - typedef ConstBufferedScalar ParamsHandle; - - static inline const char* identifier() CADET_NOEXCEPT; - - {{externalName}}() CADET_NOEXCEPT - { - % if exists ("constantParameters") % - } - : - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}({ % for v in p / varName % } & _constParams.{{v}} { % if not is_last % }, - { % endif % } { % endfor % }){ % else % } _{{p / varName}}(&_constParams.{{p / varName}}){ - % endif % } - { - % if not is_last % - } - , { % endif % } { % endfor % } { % endif % } - { - } - - inline bool configure(IParameterProvider & paramProvider, unsigned int nReactions, unsigned int nComp, - unsigned int const* nBoundStates) - { - {% for p in parameters % - } - { - % if not existsIn(p, "skipConfig") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if not existsIn(p, "skipConfig") % - } - { - % if length (p / varName) > 1 % - } - { - % if existsIn (p, "default") % - } - _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / objName}}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); - { % endif % } { % else % } { - % if existsIn (p, "default") % - } - _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{p / default}}); - { % else % } _{{p / varName}}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); - { % endif % } { % endif % } { % endif % } { % endfor % } { % endif % } ExternalParamHandlerBase::configure( - paramProvider, {{length(parameters)}}); - return validateConfig(nReactions, nComp, nBoundStates); - } - - inline void registerParameters(std::unordered_map & parameters, UnitOpIdx unitOpIdx, - ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, - nBoundStates); - { % else % } _{{p / varName}}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, - nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline void reserve(unsigned int nReactions, unsigned int nComp, unsigned int nBoundStates) - { - {% for p in parameters % - } - _{{p / varName}}.reserve(nReactions, nComp, nBoundStates); - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - _{{p / objName}}.reserve(nReactions, nComp, nBoundStates); - { % else % } _{{p / varName}}.reserve(nReactions, nComp, nBoundStates); - { % endif % } { % endfor % } { - % endif % - } - } - - inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - BufferedScalar localParams = workSpace.scalar(); - BufferedArray extFunBuffer = workSpace.array({{length(parameters)}}); - evaluateExternalFunctions(t, secIdx, colPos, {{length(parameters)}}, static_cast(extFunBuffer)); - { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - {% for v in p/varName % - } - localParams->{{v}} = _constParams.{{v}}; - { % endfor % } { % else % } localParams->{{p / varName}} = _constParams.{{p / varName}}; - { % endif % } { % endfor % } { % endif % } - - {% for p in parameters % - } - _{{p / varName}}.prepareCache(localParams->{{p / varName}}, workSpace); - _{{p / varName}}.update(cadet::util::dataOfLocalVersion(localParams->{{p / varName}}), - extFunBuffer[{{index}}], nComp, nBoundStates); - { - % endfor % - } - - return localParams; - } - - inline std::tuple updateTimeDerivative( - double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, - unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const - { - BufferedScalar localParams = workSpace.scalar(); - BufferedScalar p = workSpace.scalar(); - BufferedArray extFunBuffer = workSpace.array({{length(parameters)}}); - BufferedArray extDerivBuffer = workSpace.array({{length(parameters)}}); - evaluateExternalFunctions(t, secIdx, colPos, {{length(parameters)}}, static_cast(extFunBuffer)); - evaluateTimeDerivativeExternalFunctions(t, secIdx, colPos, {{length(parameters)}}, - static_cast(extDerivBuffer)); - - { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - {% for v in p/varName % - } - localParams->{{v}} = _constParams.{{v}}; - p->{{v}} = _constParams.{{v}}; - { % endfor % } { % else % } localParams->{{p / varName}} = _constParams.{{p / varName}}; - p->{{p / varName}} = _constParams.{{p / varName}}; - { % endif % } { % endfor % } { % endif % } - - {% for p in parameters % - } - _{{p / varName}}.prepareCache(localParams->{{p / varName}}, workSpace); - _{{p / varName}}.update(cadet::util::dataOfLocalVersion(localParams->{{p / varName}}), - extFunBuffer[{{index}}], nComp, nBoundStates); - - _{{p / varName}}.prepareCache(p->{{p / varName}}, workSpace); - _{{p / varName}}.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->{{p / varName}}), - extFunBuffer[{{index}}], extDerivBuffer[{{index}}], nComp, - nBoundStates); - { - % endfor % - } - - return std::make_tuple(std::move(localParams), std::move(p)); - } - - inline std::size_t cacheSize(unsigned int nReactions, unsigned int nComp, unsigned int totalNumBoundStates) - const CADET_NOEXCEPT - { - return 2 * sizeof(params_t) + alignof(params_t) + 2 * {{length(parameters)}} * sizeof(double) + - alignof(double) + - 2 * ({ % for p in parameters % } _{{p / varName}}.additionalDynamicMemory(nReactions, nComp, - totalNumBoundStates) { - % if not is_last % - } + { % endif % } { % endfor % }); - } - - {% for p in parameters % - } - inline const External{{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline External{{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) == 1 % - } - inline const {{p / type}} & {{p / varName}}() const CADET_NOEXCEPT - { - return _{{p / varName}}; - } - inline {{p / type}} & {{p / varName}}() CADET_NOEXCEPT - { - return _{{p / varName}}; - } - { - % else % - } - inline const {{p / type}} & {{p / objName}}() const CADET_NOEXCEPT - { - return _{{p / objName}}; - } - inline {{p / type}} & {{p / objName}}() CADET_NOEXCEPT - { - return _{{p / objName}}; - } - { % endif % } { % endfor % } { - % endif % - } - - inline char const* prefixInConfiguration() const CADET_NOEXCEPT - { - return "EXT_"; - } - - protected: - inline bool validateConfig(unsigned int nReactions, unsigned int nComp, unsigned int const* nBoundStates); - - ConstParams _constParams; - - {% for p in parameters % - } - External - { - { - p / type - } - } - _{{p / varName}}; - { % endfor % } { - % if exists ("constantParameters") % - } - {% for p in constantParameters % - } - { - % if length (p / varName) > 1 % - } - { - { - p / type - } - } - _{{p / objName}}; - { % else % } { - { - p / type - } - } - _{{p / varName}}; - { % endif % } { % endfor % } { - % endif % - } -}; - -} // namespace model -} // namespace cadet diff --git a/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp.in b/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp.in new file mode 100644 index 000000000..d0ac4e16b --- /dev/null +++ b/src/libcadet/model/reaction/ExternalFunctionTemplate.cpp.in @@ -0,0 +1,433 @@ +// ============================================================================= +// CADET +// +// Copyright © 2008-2022: The CADET Authors +// Please see the AUTHORS and CONTRIBUTORS 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 + * Template for external function support in reaction models. + * This file serves as a template for generating parameter handler classes that + * hold, configure, and update parameters, which may also depend on external + * functions. + */ + +/* */ +#include "Memory.hpp" + +#include + +namespace cadet +{ + +namespace model +{ + +class {{ name }} : public cadet::model::ConstParamHandlerBase +{ +public: + struct ConstParams + { +{% for p in parameters %} + typename {{ p/type }}::storage_t {{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + + {% if length(p/varName) > 1 %} + + {% for v in p/varName %} + + typename {{ p/type }}::storage_t {{ v }}; + + {% endfor %} + + {% else %} + + typename {{ p/type }}::storage_t {{ p/varName }}; + + {% endif %} + + {% endfor %} +{% endif %} + }; + + typedef ConstParams params_t; + typedef ConstParams const* ParamsHandle; + + static inline const char* identifier() CADET_NOEXCEPT; + + {{ name }}() CADET_NOEXCEPT : +{% for p in parameters %} + _{{ p/varName }}(&_localParams.{{ p/varName }}) + {% if not is_last %} + , + {% endif %} +{% endfor %} +{% if exists("constantParameters") %} + , + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}({% for v in p/varName %} &_localParams.{{ v }} {% if not is_last %},{% endif %} {% endfor %}) + {% else %} + _{{ p/varName }}(&_localParams.{{ p/varName }}) + {% endif %} + {% if not is_last %},{% endif %} + {% endfor %} +{% endif %} + { } + + inline bool configure(IParameterProvider& paramProvider, unsigned int nReactions, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + {% if not existsIn(p, "skipConfig") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if not existsIn(p, "skipConfig") %} + {% if length(p/varName) > 1 %} + {% if existsIn(p, "default") %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% else %} + {% if existsIn(p, "default") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% endif %} + {% endif %} + {% endfor %} +{% endif %} + return validateConfig(nReactions, nComp, nBoundStates); + } + + inline void registerParameters(std::unordered_map& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline void reserve(unsigned int nReactions, unsigned int nComp, unsigned int nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.reserve(nReactions, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.reserve(nReactions, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.reserve(nReactions, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + return &_localParams; + } + + inline std::tuple updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + return std::make_tuple(&_localParams, nullptr); + } + +{% for p in parameters %} + inline const {{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline {{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) == 1 %} + inline const {{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline {{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } + {% else %} + inline const {{ p/type }}& {{ p/objName }}() const CADET_NOEXCEPT { return _{{ p/objName }}; } + inline {{ p/type }}& {{ p/objName }}() CADET_NOEXCEPT { return _{{ p/objName }}; } + {% endif %} + {% endfor %} +{% endif %} + + inline char const* prefixInConfiguration() const CADET_NOEXCEPT { return ""; } + +protected: + inline bool validateConfig(unsigned int nReactions, unsigned int nComp, unsigned int const* nBoundStates); + + ConstParams _localParams; + +{% for p in parameters %} + {{ p/type }} _{{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {{ p/type }} _{{ p/objName }}; + {% else %} + {{ p/type }} _{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} +}; + +class {{ externalName }} : public cadet::model::ExternalParamHandlerBase +{ +public: + struct ConstParams + { +{% if exists("constantParameters") %} + {% for p in constantParameters %} + + {% if length(p/varName) > 1 %} + + {% for v in p/varName %} + + typename {{ p/type }}::storage_t {{ v }}; + + {% endfor %} + + {% else %} + + typename {{ p/type }}::storage_t {{ p/varName }}; + + {% endif %} + + {% endfor %} +{% endif %} + }; + + struct VariableParams + { +{% for p in parameters %} + typename util::localVersionOf<{{ p/type }}::storage_t>::type {{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + + {% if length(p/varName) > 1 %} + + {% for v in p/varName %} + + typename {{ p/type }}::storage_t {{ v }}; + + {% endfor %} + + {% else %} + + typename {{ p/type }}::storage_t {{ p/varName }}; + + {% endif %} + + {% endfor %} +{% endif %} + }; + + typedef VariableParams params_t; + typedef ConstBufferedScalar ParamsHandle; + + static inline const char* identifier() CADET_NOEXCEPT; + + {{ externalName }}() CADET_NOEXCEPT +{% if exists("constantParameters") %} + : + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}({% for v in p/varName %} &_constParams.{{ v }} {% if not is_last %},{% endif %} {% endfor %}) + {% else %} + _{{ p/varName }}(&_constParams.{{ p/varName }}) + {% endif %} + {% if not is_last %},{% endif %} + {% endfor %} +{% endif %} + { } + + inline bool configure(IParameterProvider& paramProvider, unsigned int nReactions, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + {% if not existsIn(p, "skipConfig") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if not existsIn(p, "skipConfig") %} + {% if length(p/varName) > 1 %} + {% if existsIn(p, "default") %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/objName }}.configure("{{ p/confPrefix }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% else %} + {% if existsIn(p, "default") %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates, {{ p/default }}); + {% else %} + _{{ p/varName }}.configure("{{ p/confName }}", paramProvider, nComp, nBoundStates); + {% endif %} + {% endif %} + {% endif %} + {% endfor %} +{% endif %} + ExternalParamHandlerBase::configure(paramProvider, {{ length(parameters) }}); + return validateConfig(nReactions, nComp, nBoundStates); + } + + inline void registerParameters(std::unordered_map& parameters, UnitOpIdx unitOpIdx, ParticleTypeIdx parTypeIdx, unsigned int nComp, unsigned int const* nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.registerParam("{{ p/confPrefix }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.registerParam("{{ p/confName }}", parameters, unitOpIdx, parTypeIdx, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline void reserve(unsigned int nReactions, unsigned int nComp, unsigned int nBoundStates) + { +{% for p in parameters %} + _{{ p/varName }}.reserve(nReactions, nComp, nBoundStates); +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + _{{ p/objName }}.reserve(nReactions, nComp, nBoundStates); + {% else %} + _{{ p/varName }}.reserve(nReactions, nComp, nBoundStates); + {% endif %} + {% endfor %} +{% endif %} + } + + inline ParamsHandle update(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + BufferedScalar localParams = workSpace.scalar(); + BufferedArray extFunBuffer = workSpace.array({{ length(parameters) }}); + evaluateExternalFunctions(t, secIdx, colPos, {{ length(parameters) }}, static_cast(extFunBuffer)); +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {% for v in p/varName %} + localParams->{{ v }} = _constParams.{{ v }}; + {% endfor %} + {% else %} + localParams->{{ p/varName }} = _constParams.{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} + +{% for p in parameters %} + _{{ p/varName }}.prepareCache(localParams->{{ p/varName }}, workSpace); + _{{ p/varName }}.update(cadet::util::dataOfLocalVersion(localParams->{{ p/varName }}), extFunBuffer[{{ index }}], nComp, nBoundStates); +{% endfor %} + + return localParams; + } + + inline std::tuple updateTimeDerivative(double t, unsigned int secIdx, const ColumnPosition& colPos, unsigned int nComp, unsigned int const* nBoundStates, LinearBufferAllocator& workSpace) const + { + BufferedScalar localParams = workSpace.scalar(); + BufferedScalar p = workSpace.scalar(); + BufferedArray extFunBuffer = workSpace.array({{ length(parameters) }}); + BufferedArray extDerivBuffer = workSpace.array({{ length(parameters) }}); + evaluateExternalFunctions(t, secIdx, colPos, {{ length(parameters) }}, static_cast(extFunBuffer)); + evaluateTimeDerivativeExternalFunctions(t, secIdx, colPos, {{ length(parameters) }}, static_cast(extDerivBuffer)); + +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {% for v in p/varName %} + localParams->{{ v }} = _constParams.{{ v }}; + p->{{ v }} = _constParams.{{ v }}; + {% endfor %} + {% else %} + localParams->{{ p/varName }} = _constParams.{{ p/varName }}; + p->{{ p/varName }} = _constParams.{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} + +{% for p in parameters %} + _{{ p/varName }}.prepareCache(localParams->{{ p/varName }}, workSpace); + _{{ p/varName }}.update(cadet::util::dataOfLocalVersion(localParams->{{ p/varName }}), extFunBuffer[{{ index }}], nComp, nBoundStates); + + _{{ p/varName }}.prepareCache(p->{{ p/varName }}, workSpace); + _{{ p/varName }}.updateTimeDerivative(cadet::util::dataOfLocalVersion(p->{{ p/varName }}), extFunBuffer[{{ index }}], extDerivBuffer[{{ index }}], nComp, nBoundStates); +{% endfor %} + + return std::make_tuple(std::move(localParams), std::move(p)); + } + + inline std::size_t cacheSize(unsigned int nReactions, unsigned int nComp, unsigned int totalNumBoundStates) const CADET_NOEXCEPT + { + return 2 * sizeof(params_t) + alignof(params_t) + 2 * {{ length(parameters) }} * sizeof(double) + alignof(double) + 2 * ( +{% for p in parameters %} + _{{ p/varName }}.additionalDynamicMemory(nReactions, nComp, totalNumBoundStates) {% if not is_last %} + {% endif %} +{% endfor %} + ); + } + +{% for p in parameters %} + inline const External{{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline External{{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) == 1 %} + inline const {{ p/type }}& {{ p/varName }}() const CADET_NOEXCEPT { return _{{ p/varName }}; } + inline {{ p/type }}& {{ p/varName }}() CADET_NOEXCEPT { return _{{ p/varName }}; } + {% else %} + inline const {{ p/type }}& {{ p/objName }}() const CADET_NOEXCEPT { return _{{ p/objName }}; } + inline {{ p/type }}& {{ p/objName }}() CADET_NOEXCEPT { return _{{ p/objName }}; } + {% endif %} + {% endfor %} +{% endif %} + + inline char const* prefixInConfiguration() const CADET_NOEXCEPT { return "EXT_"; } + +protected: + inline bool validateConfig(unsigned int nReactions, unsigned int nComp, unsigned int const* nBoundStates); + + ConstParams _constParams; + +{% for p in parameters %} + External{{ p/type }} _{{ p/varName }}; +{% endfor %} +{% if exists("constantParameters") %} + {% for p in constantParameters %} + {% if length(p/varName) > 1 %} + {{ p/type }} _{{ p/objName }}; + {% else %} + {{ p/type }} _{{ p/varName }}; + {% endif %} + {% endfor %} +{% endif %} +}; + +} // namespace model +} // namespace cadet