From 33390bc439bb0c9d837abdf157df37162451274b Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Wed, 2 Apr 2025 14:45:08 -0700 Subject: [PATCH 01/28] feat: add as a geos submodule. --- hostconfigs/apple/macOS_base.cmake | 2 +- src/CMakeLists.txt | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/hostconfigs/apple/macOS_base.cmake b/hostconfigs/apple/macOS_base.cmake index 9a056a4..358672c 100644 --- a/hostconfigs/apple/macOS_base.cmake +++ b/hostconfigs/apple/macOS_base.cmake @@ -1,5 +1,5 @@ message( "this hostconfig assumes you are using homebrew") -message( "brew install bison cmake gcc git-lfs open-mpi openblas python3") +message( "brew install bison cmake gcc git-lfs open-mpi openblas python3 llvm cppcheck lcov") message( "CMAKE_SYSTEM_PROCESSOR = ${CMAKE_SYSTEM_PROCESSOR}" ) message("CONFIG_NAME = ${CONFIG_NAME}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 69f694e..4dffe17 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,8 +12,7 @@ set( hpcReact_headers reactions/bulkGeneric/SpeciesUtilities.hpp ) -set( hpcReact_sources - ) +set( hpcReact_sources) find_package(LAPACK REQUIRED) find_package(BLAS REQUIRED) @@ -35,11 +34,13 @@ blt_add_library( NAME hpcReact target_include_directories( hpcReact INTERFACE - $ - $ + $ + $ $ ) blt_print_target_properties( TARGET hpcReact ) +message(STATUS "HPCReact/src CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") + # install( FILES ${hpcReact_headers} # DESTINATION include ) From 47aadc32fea4bde62b3fdfd52d1859e9f54b7371 Mon Sep 17 00:00:00 2001 From: frankfeifan Date: Thu, 24 Apr 2025 16:00:23 -0700 Subject: [PATCH 02/28] make a typa alias so they can be directly used in ReactiveSinglePhaseFluid --- src/reactions/bulkGeneric/ParametersPredefined.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index be1b0bc..a141e69 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -34,8 +34,10 @@ namespace bulkGeneric // um1Constants }; +using simpleTestType = MixedReactionsParameters< double, int, int, 5, 2 >; + constexpr -MixedReactionsParameters< double, int, int, 5, 2 > +simpleTestType simpleTestRateParams = { // stoichiometric matrix @@ -51,6 +53,7 @@ simpleTestRateParams = { 1.0, 0.5 } }; +using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 11 > ; constexpr MixedReactionsParameters< double, int, int, 18, 11 > From 85fecabd829a0158cd85970552d63d355d8201ed Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Tue, 6 May 2025 08:18:26 -0700 Subject: [PATCH 03/28] feat: add mixed system. --- .../MixedEquilibriumKineticReactions.hpp | 91 +++++++++++++++++++ .../bulkGeneric/SpeciesUtilities.hpp | 45 +++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp new file mode 100644 index 0000000..a6be530 --- /dev/null +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -0,0 +1,91 @@ +#pragma once + +#include "common/macros.hpp" + +/** @file MixedEquilibriumKineticReactions.hpp + * @brief Header file for the MixedEquilibriumKineticReactions class. + * @author HPC-REACT Team + * @date 2025 + */ + +namespace hpcReact +{ +namespace bulkGeneric +{ + +/** + * @brief Class for computing reaction rates and species rates for a given set of reactions. + * @tparam REAL_TYPE The type of the real numbers used in the class. + * @tparam INT_TYPE The type of the integer numbers used in the class. + * @tparam INDEX_TYPE The type of the index used in the class. + * @tparam LOGE_CONCENTRATION Whether to use logarithm of concentration for the calculations. + * @details + * This class provides the ablity to compute kinetic reactions. + */ +template< typename REAL_TYPE, + typename INT_TYPE, + typename INDEX_TYPE, + bool LOGE_CONCENTRATION > +class MixedEquilibriumKineticReactions +{ +public: + + /// Type alias for the real type used in the class. + using RealType = REAL_TYPE; + + /// Type alias for the integer type used in the class. + using IntType = INT_TYPE; + + /// Type alias for the index type used in the class. + using IndexType = INDEX_TYPE; + + using kineticReactions = KineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, LOGE_CONCENTRATION >; + + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > + static HPCREACT_HOST_DEVICE inline void + updateMixedSystem( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D & logSecondarySpeciesConcentrations, + ARRAY_1D & aggregatePrimarySpeciesConcentrations, + ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & reactionRatesDerivatives ) + { + constexpr int numSpecies = PARAMS_DATA::numSpecies; + constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; + constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + + // 1. Compute new aggregate species from primary species + calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, + INT_TYPE, + INDEX_TYPE >( params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + aggregatePrimarySpeciesConcentrations, + dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) + + // 2. Compute the reaction rates for all kinetic reactions + kineticReactions::computeReactionRates( temperature, + params, + speciesConcentration, + reactionRates, + reactionRatesDerivatives ); + + // 3. Compute aggregate species rates + + + } + +private: + +}; + +} // namespace bulkGeneric +} // namespace hpcReact + +#include "MixedEquilibriumKineticReactions_impl.hpp" +#include "common/macrosCleanup.hpp" diff --git a/src/reactions/bulkGeneric/SpeciesUtilities.hpp b/src/reactions/bulkGeneric/SpeciesUtilities.hpp index 0f99ebd..c7987ce 100644 --- a/src/reactions/bulkGeneric/SpeciesUtilities.hpp +++ b/src/reactions/bulkGeneric/SpeciesUtilities.hpp @@ -89,6 +89,51 @@ void calculateLogSecondarySpeciesConcentrationWrtLogC( PARAMS_DATA const & param } ); } +template< typename REAL_TYPE, + typename INT_TYPE, + typename INDEX_TYPE, + typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > +HPCREACT_HOST_DEVICE +inline +void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D & logSecondarySpeciesConcentrations, + ARRAY_1D & aggregatePrimarySpeciesConcentrations, + ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) +{ + constexpr int numSpecies = PARAMS_DATA::numSpecies; + constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; + constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + + calculateLogSecondarySpeciesConcentration< REAL_TYPE, + INT_TYPE, + INDEX_TYPE >( params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations ); + + for( int i = 0; i < numPrimarySpecies; ++i ) + { + REAL_TYPE const speciesConcentration_i = exp( logPrimarySpeciesConcentrations[i] ); + aggregatePrimarySpeciesConcentrations[i] = speciesConcentration_i; + dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations( i, i ) = speciesConcentration_i; + for( int j = 0; j < numSecondarySpecies; ++j ) + { + REAL_TYPE const secondarySpeciesConcentrations_j = exp( logSecondarySpeciesConcentrations[j] ); + aggregatePrimarySpeciesConcentrations[i] += params.stoichiometricMatrix( j, i+numSecondarySpecies ) * secondarySpeciesConcentrations_j; + for( int k=0; k Date: Wed, 14 May 2025 15:12:41 -0700 Subject: [PATCH 04/28] mixed system functions completed. --- .../MixedEquilibriumKineticReactions.hpp | 127 ++++++++++++++++-- 1 file changed, 117 insertions(+), 10 deletions(-) diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp index a6be530..21a5958 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -25,7 +25,7 @@ namespace bulkGeneric template< typename REAL_TYPE, typename INT_TYPE, typename INDEX_TYPE, - bool LOGE_CONCENTRATION > + bool LOGE_CONCENTRATION = true > class MixedEquilibriumKineticReactions { public: @@ -51,13 +51,15 @@ class MixedEquilibriumKineticReactions ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, ARRAY_1D & logSecondarySpeciesConcentrations, ARRAY_1D & aggregatePrimarySpeciesConcentrations, - ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations, + ARRAY_2D & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, ARRAY_1D & reactionRates, - ARRAY_2D & reactionRatesDerivatives ) + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D & aggregateSpeciesRates, + ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) { - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + constexpr IntType numSpecies = PARAMS_DATA::numSpecies; + constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions; + constexpr IntType numPrimarySpecies = numSpecies - numSecondarySpecies; // 1. Compute new aggregate species from primary species calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, @@ -69,15 +71,120 @@ class MixedEquilibriumKineticReactions dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) // 2. Compute the reaction rates for all kinetic reactions + computeReactionRates( temperature, + params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + aggregatePrimarySpeciesConcentrations, + dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + reactionRates, + dReactionRates_dLogPrimarySpeciesConcentrations ); + + + // 3. Compute aggregate species rates + computeAggregateSpeciesRates( params, + logPrimarySpeciesConcentrations + reactionRates, + dReactionRates_dLogPrimarySpeciesConcentrations, + aggregateSpeciesRates, + dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); + + + } + + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > + static HPCREACT_HOST_DEVICE inline void + computeReactionRates( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, + ARRAY_2D_TO_CONST const & dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ) + + { + constexpr IntType numSpecies = PARAMS_DATA::numSpecies; + constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions; + constexpr IntType numPrimarySpecies = numSpecies - numSecondarySpecies; + + RealType logSpeciesConcentration[numSpecies] {}; + for ( IntType i = 0; i < numPrimarySpecies; ++i ) + { + speciesConcentration[i] = logPrimarySpeciesConcentrations[i]; + } + for ( IntType i = 0; i < numSecondarySpecies; ++i ) + { + logSpeciesConcentration[i+numPrimarySpecies] = logSecondarySpeciesConcentrations[i]; + } + + CArrayWrapper< RealType, PARAMS_DATA::numReactions, numSpecies > reactionRatesDerivatives; kineticReactions::computeReactionRates( temperature, params, - speciesConcentration, + logSpeciesConcentration, reactionRates, reactionRatesDerivatives ); - // 3. Compute aggregate species rates - - + // Compute the reaction rates derivatives w.r.t. log primary species concentrations + for( IntType i = 0; i < numKineticReactions; ++i ) + { + for( IntType j = 0; j < numPrimarySpecies; ++j ) + { + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j ); + for( IntType k = 0; k < numSecondarySpecies; ++k ) + { + RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentration = params.stoichiometricMatrix( k, j+numSecondarySpecies ); + + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) += + reactionRatesDerivatives( i, numPrimarySpecies + k ) * dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations( k, j ); + } + } + } + } + + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_2D_TO_CONST + typename ARRAY_1D, + typename ARRAY_2D > + static HPCREACT_HOST_DEVICE inline void + computeAggregateSpeciesRates( PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & speciesConcentration, + ARRAY_1D_TO_CONST const & reactionRates, + ARRAY_2D_TO_CONST const & reactionRatesDerivatives + ARRAY_1D & aggregatesRates, + ARRAY_2D & aggregatesRatesDerivatives ) + { + constexpr IntType numSpecies = PARAMS_DATA::numSpecies; + constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions; + constexpr IntType numPrimarySpecies = numSpecies - numSecondarySpecies; + + for( IntType i = 0; i < numPrimarySpecies; ++i ) + { + speciesRates[i] = 0.0; + if constexpr( CALCULATE_DERIVATIVES ) + { + for( IntType j = 0; j < numPrimarySpecies; ++j ) + { + speciesRatesDerivatives( i, j ) = 0.0; + } + } + for( IntType r=0; r Date: Fri, 16 May 2025 17:58:16 -0700 Subject: [PATCH 05/28] working on testing mixed system. --- src/CMakeLists.txt | 2 + src/common/CArrayWrapper.hpp | 130 +++++++++++-- ...ionsAggregatePrimaryConcentration_impl.hpp | 8 +- ...uilibriumReactionsReactionExtents_impl.hpp | 8 +- .../bulkGeneric/KineticReactions.hpp | 2 +- .../bulkGeneric/KineticReactions_impl.hpp | 37 ++-- .../MixedEquilibriumKineticReactions.hpp | 173 +++++++----------- src/reactions/bulkGeneric/Parameters.hpp | 142 +++++++------- .../bulkGeneric/ParametersPredefined.hpp | 78 +++++--- .../bulkGeneric/SpeciesUtilities.hpp | 27 +-- .../unitTests/testEquilibriumReactions.cpp | 21 +-- .../unitTests/testKineticReactions.cpp | 42 ++--- .../unitTests/testSpeciesUtilities.cpp | 13 +- 13 files changed, 389 insertions(+), 294 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4dffe17..01c7ed5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,8 @@ set( hpcReact_headers reactions/bulkGeneric/EquilibriumReactionsReactionExtents_impl.hpp reactions/bulkGeneric/KineticReactions.hpp reactions/bulkGeneric/KineticReactions_impl.hpp + reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp + reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp reactions/bulkGeneric/Parameters.hpp reactions/bulkGeneric/ParametersPredefined.hpp reactions/bulkGeneric/SpeciesUtilities.hpp diff --git a/src/common/CArrayWrapper.hpp b/src/common/CArrayWrapper.hpp index 397c58b..962900c 100644 --- a/src/common/CArrayWrapper.hpp +++ b/src/common/CArrayWrapper.hpp @@ -1,5 +1,6 @@ #pragma once #include "macros.hpp" +#include /** @@ -26,34 +27,53 @@ struct CArrayWrapper; template< typename T, int DIM0 > struct CArrayWrapper< T, DIM0 > { + + constexpr CArrayWrapper() = default; + + constexpr CArrayWrapper( std::initializer_list< T > init ) + { + int i = 0; + for( auto const & val : init ) + { + data[i++] = val; + } + } + + constexpr CArrayWrapper( CArrayWrapper const & src ) + { + for( size_t i = 0; i < DIM0; i++ ) + { + data[i] = src.data[i]; + } + } /** * @brief Read/write access to an element by index. * @param dim The index (must be in range [0, DIM0)). * @return Reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE inline T & operator()( int const dim ) { return data[dim]; } + HPCREACT_HOST_DEVICE constexpr inline T & operator()( int const dim ) { return data[dim]; } /** * @brief Read-only access to an element by index (const overload). * @param dim The index (must be in range [0, DIM0)). * @return Const reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE inline T const & operator()( int const dim ) const { return data[dim]; } + HPCREACT_HOST_DEVICE constexpr inline T const & operator()( int const dim ) const { return data[dim]; } /** * @brief Subscript operator for read/write access. * @param dim The index (must be in range [0, DIM0)). * @return Reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE inline T & operator[]( int const dim ) { return data[dim]; } + HPCREACT_HOST_DEVICE constexpr inline T & operator[]( int const dim ) { return data[dim]; } /** * @brief Subscript operator for read-only access (const overload). * @param dim The index (must be in range [0, DIM0)). * @return Const reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE inline T const & operator[]( int const dim ) const { return data[dim]; } + HPCREACT_HOST_DEVICE constexpr inline T const & operator[]( int const dim ) const { return data[dim]; } /// The underlying 1D C-style array. T data[DIM0]; @@ -72,13 +92,54 @@ struct CArrayWrapper< T, DIM0 > template< typename T, int DIM0, int DIM1 > struct CArrayWrapper< T, DIM0, DIM1 > { + + constexpr CArrayWrapper() = default; + + constexpr CArrayWrapper( CArrayWrapper const & src ) + { + for( size_t i = 0; i < DIM0; i++ ) + { + for ( size_t j = 0; j < DIM1; j++) + data[i][j] = src.data[i][j]; + } + } + + /** + * @brief Construct a 2D CArrayWrapper from nested initializer lists. + * + * Allows brace-initialization with a matrix-like structure: + * @code + * CArrayWrapper mat = { + * {1.0, 2.0, 3.0}, + * {4.0, 5.0, 6.0} + * }; + * @endcode + * + * @param init A nested initializer list with exactly D0 rows and D1 elements per row. + * + * @note No runtime bounds checking is performed on the initializer dimensions. + */ + constexpr CArrayWrapper( std::initializer_list< std::initializer_list< T > > init ) + { + int i = 0; + for( auto const & row : init ) + { + int j = 0; + for( auto const & val : row ) + { + data[i][j++] = val; + } + ++i; + } + } + /** * @brief Read/write access to an element by 2D indices. * @param dim0 Index in the first dimension (range [0, DIM0)). * @param dim1 Index in the second dimension (range [0, DIM1)). * @return Reference to the element at the specified 2D location. */ - HPCREACT_HOST_DEVICE inline T & operator()( int const dim0, int const dim1 ) + HPCREACT_HOST_DEVICE constexpr inline T & operator()( int const dim0, int const dim1 ) { return data[dim0][dim1]; } @@ -89,7 +150,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * @param dim1 Index in the second dimension (range [0, DIM1)). * @return Const reference to the element at the specified 2D location. */ - HPCREACT_HOST_DEVICE inline T const & operator()( int const dim0, int const dim1 ) const + HPCREACT_HOST_DEVICE constexpr inline T const & operator()( int const dim0, int const dim1 ) const { return data[dim0][dim1]; } @@ -101,7 +162,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * * This allows usage like `obj[dim0][dim1]`. */ - HPCREACT_HOST_DEVICE inline T ( & operator[]( int const dim0 ))[DIM1] + HPCREACT_HOST_DEVICE constexpr inline T ( & operator[]( int const dim0 ))[DIM1] { return data[dim0]; } @@ -111,7 +172,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * @param dim0 The row index (range [0, DIM0)). * @return Const reference to an array of type T[DIM1]. */ - HPCREACT_HOST_DEVICE inline T const (&operator[]( int const dim0 ) const)[DIM1] + HPCREACT_HOST_DEVICE constexpr inline T const (&operator[]( int const dim0 ) const)[DIM1] { return data[dim0]; } @@ -134,6 +195,51 @@ struct CArrayWrapper< T, DIM0, DIM1 > template< typename T, int DIM0, int DIM1, int DIM2 > struct CArrayWrapper< T, DIM0, DIM1, DIM2 > { + + constexpr CArrayWrapper() = default; + + /** + * @brief Construct a 3D CArrayWrapper from nested initializer lists. + * + * Enables tensor-like initialization using triple-nested braces: + * @code + * CArrayWrapper cube = { + * { + * {1.0, 2.0}, + * {3.0, 4.0} + * }, + * { + * {5.0, 6.0}, + * {7.0, 8.0} + * } + * }; + * @endcode + * + * @param init A three-level nested initializer list with D0 planes, D1 rows per plane, + * and D2 elements per row. + * + * @note This constructor does not perform size validation. Incorrect initializer sizes + * may lead to undefined behavior. + */ + constexpr CArrayWrapper( std::initializer_list< std::initializer_list< std::initializer_list< T > > > init ) + { + int i = 0; + for( auto const & plane : init ) + { + int j = 0; + for( auto const & row : plane ) + { + int k = 0; + for( auto const & val : row ) + { + data[i][j][k++] = val; + } + ++j; + } + ++i; + } + } + /** * @brief Read/write access to an element by 3D indices. * @param dim0 Index in the first dimension (range [0, DIM0)). @@ -144,7 +250,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * @note Currently, this function incorrectly indexes data[dim0][dim1], missing dim2. * It should be `data[dim0][dim1][dim2]`. Please correct if intended. */ - HPCREACT_HOST_DEVICE inline T & operator()( int const dim0, int const dim1, int const dim2 ) + HPCREACT_HOST_DEVICE constexpr inline T & operator()( int const dim0, int const dim1, int const dim2 ) { // NOTE: This looks like a bug in your original code. Should be data[dim0][dim1][dim2]. return data[dim0][dim1][dim2]; @@ -157,7 +263,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * @param dim2 Index in the third dimension (range [0, DIM2)). * @return Const reference to the element at the specified 3D location. */ - HPCREACT_HOST_DEVICE inline T const & operator()( int const dim0, int const dim1, int const dim2 ) const + HPCREACT_HOST_DEVICE constexpr inline T const & operator()( int const dim0, int const dim1, int const dim2 ) const { // NOTE: Same potential bug as above. Should be data[dim0][dim1][dim2]. return data[dim0][dim1][dim2]; @@ -170,7 +276,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * * This allows usage like `obj[dim0][dim1][dim2]`. */ - HPCREACT_HOST_DEVICE inline T ( & operator[]( int const dim0 ))[DIM1][DIM2] + HPCREACT_HOST_DEVICE constexpr inline T ( & operator[]( int const dim0 ))[DIM1][DIM2] { return data[dim0]; } @@ -180,7 +286,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * @param dim0 The slice index (range [0, DIM0)). * @return Const reference to an array of type T[DIM1][DIM2]. */ - HPCREACT_HOST_DEVICE inline T const (&operator[]( int const dim0 ) const)[DIM1][DIM2] + HPCREACT_HOST_DEVICE constexpr inline T const (&operator[]( int const dim0 ) const)[DIM1][DIM2] { return data[dim0]; } diff --git a/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp b/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp index 3454a12..ee4e62e 100644 --- a/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp +++ b/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp @@ -30,9 +30,7 @@ EquilibriumReactions< REAL_TYPE, ARRAY_2D & jacobian ) { HPCREACT_UNUSED_VAR( temperature ); - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); RealType aggregatePrimaryConcentrations[numPrimarySpecies] = {0.0}; ARRAY_2D dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations = {{{0.0}}}; @@ -68,9 +66,7 @@ EquilibriumReactions< REAL_TYPE, ARRAY_1D & logPrimarySpeciesConcentration ) { HPCREACT_UNUSED_VAR( temperature ); - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numReactions = PARAMS_DATA::numReactions; - constexpr int numPrimarySpecies = numSpecies - numReactions; + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); double residual[numPrimarySpecies] = { 0.0 }; // double aggregatePrimarySpeciesConcentration[numPrimarySpecies] = { 0.0 }; diff --git a/src/reactions/bulkGeneric/EquilibriumReactionsReactionExtents_impl.hpp b/src/reactions/bulkGeneric/EquilibriumReactionsReactionExtents_impl.hpp index 923b0c2..1078138 100644 --- a/src/reactions/bulkGeneric/EquilibriumReactionsReactionExtents_impl.hpp +++ b/src/reactions/bulkGeneric/EquilibriumReactionsReactionExtents_impl.hpp @@ -32,8 +32,8 @@ EquilibriumReactions< REAL_TYPE, { HPCREACT_UNUSED_VAR( temperature ); - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numReactions = PARAMS_DATA::numReactions; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); + constexpr int numReactions = PARAMS_DATA::numReactions(); // initialize the species concentration RealType speciesConcentration[numSpecies]; @@ -115,8 +115,8 @@ EquilibriumReactions< REAL_TYPE, ARRAY_1D & speciesConcentration ) { HPCREACT_UNUSED_VAR( temperature ); - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numReactions = PARAMS_DATA::numReactions; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); + constexpr int numReactions = PARAMS_DATA::numReactions(); double residual[numReactions] = { 0.0 }; double xi[numReactions] = { 0.0 }; double dxi[numReactions] = { 0.0 }; diff --git a/src/reactions/bulkGeneric/KineticReactions.hpp b/src/reactions/bulkGeneric/KineticReactions.hpp index 48b79f2..a094608 100644 --- a/src/reactions/bulkGeneric/KineticReactions.hpp +++ b/src/reactions/bulkGeneric/KineticReactions.hpp @@ -79,7 +79,7 @@ class KineticReactions ARRAY_1D_TO_CONST const & speciesConcentration, ARRAY_1D & reactionRates ) { - REAL_TYPE reactionRatesDerivatives[PARAMS_DATA::numReactions][PARAMS_DATA::numSpecies] = { {0.0} }; + REAL_TYPE reactionRatesDerivatives[PARAMS_DATA::numReactions()][PARAMS_DATA::numSpecies()] = { {0.0} }; computeReactionRates_impl< PARAMS_DATA, false >( temperature, params, speciesConcentration, diff --git a/src/reactions/bulkGeneric/KineticReactions_impl.hpp b/src/reactions/bulkGeneric/KineticReactions_impl.hpp index 225f239..322261e 100644 --- a/src/reactions/bulkGeneric/KineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/KineticReactions_impl.hpp @@ -47,7 +47,7 @@ KineticReactions< REAL_TYPE, } // loop over each reaction - for( IntType r=0; r 0.0 ) { - for( IntType j = 0; j < PARAMS_DATA::numSpecies; ++j ) + for( IntType j = 0; j < PARAMS_DATA::numSpecies(); ++j ) { if( i==j ) { @@ -173,7 +174,7 @@ KineticReactions< REAL_TYPE, if constexpr( CALCULATE_DERIVATIVES ) { - for( IntType i = 0; i < PARAMS_DATA::numSpecies; ++i ) + for( IntType i = 0; i < PARAMS_DATA::numSpecies(); ++i ) { reactionRatesDerivatives( r, i ) = forwardRateConstant * dProductConcForward_dC[i] - reverseRateConstant * dProductConcReverse_dC[i]; } @@ -205,8 +206,8 @@ KineticReactions< REAL_TYPE, ARRAY_1D & speciesRates, ARRAY_2D & speciesRatesDerivatives ) { - RealType reactionRates[PARAMS_DATA::numReactions] = { 0.0 }; - CArrayWrapper< double, PARAMS_DATA::numReactions, PARAMS_DATA::numSpecies > reactionRatesDerivatives; + RealType reactionRates[PARAMS_DATA::numReactions()] = { 0.0 }; + CArrayWrapper< double, PARAMS_DATA::numReactions(), PARAMS_DATA::numSpecies() > reactionRatesDerivatives; if constexpr( !CALCULATE_DERIVATIVES ) { @@ -215,23 +216,23 @@ KineticReactions< REAL_TYPE, computeReactionRates< PARAMS_DATA >( temperature, params, speciesConcentration, reactionRates, reactionRatesDerivatives ); - for( IntType i = 0; i < PARAMS_DATA::numSpecies; ++i ) + for( IntType i = 0; i < PARAMS_DATA::numSpecies(); ++i ) { speciesRates[i] = 0.0; if constexpr( CALCULATE_DERIVATIVES ) { - for( IntType j = 0; j < PARAMS_DATA::numSpecies; ++j ) + for( IntType j = 0; j < PARAMS_DATA::numSpecies(); ++j ) { speciesRatesDerivatives( i, j ) = 0.0; } } - for( IntType r=0; r + bool LOGE_CONCENTRATION > class MixedEquilibriumKineticReactions { public: @@ -57,39 +57,16 @@ class MixedEquilibriumKineticReactions ARRAY_1D & aggregateSpeciesRates, ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) { - constexpr IntType numSpecies = PARAMS_DATA::numSpecies; - constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr IntType numPrimarySpecies = numSpecies - numSecondarySpecies; - - // 1. Compute new aggregate species from primary species - calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, - INT_TYPE, - INDEX_TYPE >( params, - logPrimarySpeciesConcentrations, - logSecondarySpeciesConcentrations, - aggregatePrimarySpeciesConcentrations, - dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) - - // 2. Compute the reaction rates for all kinetic reactions - computeReactionRates( temperature, - params, - logPrimarySpeciesConcentrations, - logSecondarySpeciesConcentrations, - aggregatePrimarySpeciesConcentrations, - dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, - reactionRates, - dReactionRates_dLogPrimarySpeciesConcentrations ); - - - // 3. Compute aggregate species rates - computeAggregateSpeciesRates( params, - logPrimarySpeciesConcentrations - reactionRates, - dReactionRates_dLogPrimarySpeciesConcentrations, - aggregateSpeciesRates, - dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); - - + updateMixedSystem_impl( temperature, + params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + aggregatePrimarySpeciesConcentrations, + dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + reactionRates, + dReactionRates_dLogPrimarySpeciesConcentrations, + aggregateSpeciesRates, + dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); } template< typename PARAMS_DATA, @@ -101,93 +78,85 @@ class MixedEquilibriumKineticReactions PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, - ARRAY_2D_TO_CONST const & dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, ARRAY_1D & reactionRates, ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ) { - constexpr IntType numSpecies = PARAMS_DATA::numSpecies; - constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr IntType numPrimarySpecies = numSpecies - numSecondarySpecies; - - RealType logSpeciesConcentration[numSpecies] {}; - for ( IntType i = 0; i < numPrimarySpecies; ++i ) - { - speciesConcentration[i] = logPrimarySpeciesConcentrations[i]; - } - for ( IntType i = 0; i < numSecondarySpecies; ++i ) - { - logSpeciesConcentration[i+numPrimarySpecies] = logSecondarySpeciesConcentrations[i]; - } - - CArrayWrapper< RealType, PARAMS_DATA::numReactions, numSpecies > reactionRatesDerivatives; - kineticReactions::computeReactionRates( temperature, - params, - logSpeciesConcentration, - reactionRates, - reactionRatesDerivatives ); - - // Compute the reaction rates derivatives w.r.t. log primary species concentrations - for( IntType i = 0; i < numKineticReactions; ++i ) - { - for( IntType j = 0; j < numPrimarySpecies; ++j ) - { - dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j ); - for( IntType k = 0; k < numSecondarySpecies; ++k ) - { - RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentration = params.stoichiometricMatrix( k, j+numSecondarySpecies ); - - dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) += - reactionRatesDerivatives( i, numPrimarySpecies + k ) * dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations( k, j ); - } - } - } + computeReactionRates_impl( temperature, + params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + reactionRates, + dReactionRates_dPrimarySpeciesConcentrations ); } template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, - typename ARRAY_2D_TO_CONST + typename ARRAY_2D_TO_CONST, typename ARRAY_1D, typename ARRAY_2D > static HPCREACT_HOST_DEVICE inline void computeAggregateSpeciesRates( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & speciesConcentration, ARRAY_1D_TO_CONST const & reactionRates, - ARRAY_2D_TO_CONST const & reactionRatesDerivatives + ARRAY_2D_TO_CONST const & reactionRatesDerivatives, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ) { - constexpr IntType numSpecies = PARAMS_DATA::numSpecies; - constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr IntType numPrimarySpecies = numSpecies - numSecondarySpecies; - - for( IntType i = 0; i < numPrimarySpecies; ++i ) - { - speciesRates[i] = 0.0; - if constexpr( CALCULATE_DERIVATIVES ) - { - for( IntType j = 0; j < numPrimarySpecies; ++j ) - { - speciesRatesDerivatives( i, j ) = 0.0; - } - } - for( IntType r=0; r( params, + speciesConcentration, + reactionRates, + reactionRatesDerivatives, + aggregatesRates, + aggregatesRatesDerivatives ); } -private: + private: + + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > + static HPCREACT_HOST_DEVICE void + updateMixedSystem_impl( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D & logSecondarySpeciesConcentrations, + ARRAY_1D & aggregatePrimarySpeciesConcentrations, + ARRAY_2D & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D & aggregateSpeciesRates, + ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); + + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > + static HPCREACT_HOST_DEVICE void + computeReactionRates_impl( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ); + + + + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_2D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D, + bool CALCULATE_DERIVATIVES > + static HPCREACT_HOST_DEVICE void + computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & speciesConcentration, + ARRAY_1D_TO_CONST const & reactionRates, + ARRAY_2D_TO_CONST const & reactionRatesDerivatives, + ARRAY_1D & aggregatesRates, + ARRAY_2D & aggregatesRatesDerivatives ); + }; diff --git a/src/reactions/bulkGeneric/Parameters.hpp b/src/reactions/bulkGeneric/Parameters.hpp index c52e2b5..184023d 100644 --- a/src/reactions/bulkGeneric/Parameters.hpp +++ b/src/reactions/bulkGeneric/Parameters.hpp @@ -26,38 +26,28 @@ struct EquilibriumReactionsParameters using RealType = REAL_TYPE; using IntType = INT_TYPE; using IndexType = INDEX_TYPE; + + static constexpr IndexType numSpecies() { return NUM_SPECIES; } - static constexpr IndexType numSpecies = NUM_SPECIES; - static constexpr IndexType numReactions = NUM_REACTIONS; + static constexpr IndexType numReactions() { return NUM_REACTIONS; } + + static constexpr IndexType numPrimarySpecies() { return numSpecies() - numReactions(); } + + static constexpr IndexType numSecondarySpecies() { return numSpecies() - numPrimarySpecies(); } constexpr - EquilibriumReactionsParameters( RealType const (&stoichiometricMatrix)[numReactions][numSpecies], - RealType const (&equilibriumConstant)[numReactions] ): - EquilibriumReactionsParameters( stoichiometricMatrix, - equilibriumConstant, - std::make_index_sequence< NUM_REACTIONS >(), - std::make_index_sequence< NUM_REACTIONS *NUM_SPECIES >() ) + EquilibriumReactionsParameters( CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > const & stoichiometricMatrix, + CArrayWrapper< RealType, NUM_REACTIONS > equilibriumConstant ): + m_stoichiometricMatrix( stoichiometricMatrix ), + m_equilibriumConstant( equilibriumConstant ) {} RealType stoichiometricMatrix( IndexType const r, int const i ) const { return m_stoichiometricMatrix[r][i]; } RealType equilibriumConstant( IndexType const r ) const { return m_equilibriumConstant[r]; } - RealType m_stoichiometricMatrix[numReactions][numSpecies]; - RealType m_equilibriumConstant[numReactions]; - -private: - HPCREACT_NO_MISSING_BRACES_OPEN - template< std::size_t ... R, std::size_t ... RxS > - constexpr - EquilibriumReactionsParameters( RealType const (&stoichiometricMatrix)[numReactions][numSpecies], - RealType const (&equilibriumConstant)[numReactions], - std::index_sequence< R... >, - std::index_sequence< RxS... > ): - m_stoichiometricMatrix{ stoichiometricMatrix[RxS/numSpecies][RxS%numSpecies] ... }, - m_equilibriumConstant{ equilibriumConstant[R] ... } - {} - HPCREACT_NO_MISSING_BRACES_CLOSE + CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > m_stoichiometricMatrix; + CArrayWrapper< RealType, NUM_REACTIONS > m_equilibriumConstant; }; @@ -72,17 +62,16 @@ struct KineticReactionsParameters using IntType = INT_TYPE; using IndexType = INDEX_TYPE; - static constexpr IndexType numSpecies = NUM_SPECIES; - static constexpr IndexType numReactions = NUM_REACTIONS; - - KineticReactionsParameters( RealType const (&stoichiometricMatrix)[numReactions][numSpecies], - RealType const (&rateConstantForward)[numReactions], - RealType const (&rateConstantReverse)[numReactions] ): - KineticReactionsParameters( stoichiometricMatrix, - rateConstantForward, - rateConstantReverse, - std::make_index_sequence< NUM_REACTIONS >(), - std::make_index_sequence< NUM_REACTIONS *NUM_SPECIES >() ) + static constexpr IndexType numSpecies() { return NUM_SPECIES; } + + static constexpr IndexType numReactions() { return NUM_REACTIONS; } + + constexpr KineticReactionsParameters( CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > const & stoichiometricMatrix, + CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantForward, + CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantReverse ): + m_stoichiometricMatrix( stoichiometricMatrix ), + m_rateConstantForward( rateConstantForward ), + m_rateConstantReverse( rateConstantReverse ) {} @@ -91,24 +80,9 @@ struct KineticReactionsParameters RealType rateConstantReverse( IndexType const r ) const { return m_rateConstantReverse[r]; } - RealType m_stoichiometricMatrix[numReactions][numSpecies]; - RealType m_rateConstantForward[numReactions]; - RealType m_rateConstantReverse[numReactions]; - -private: - HPCREACT_NO_MISSING_BRACES( - template< std::size_t ... R, std::size_t ... RxS > - KineticReactionsParameters( RealType const (&stoichiometricMatrix)[numReactions][numSpecies], - RealType const (&rateConstantForward)[numReactions], - RealType const (&rateConstantReverse)[numReactions], - std::index_sequence< R... >, - std::index_sequence< RxS... > ) : - m_stoichiometricMatrix{ stoichiometricMatrix[RxS/numSpecies][RxS%numSpecies] ... }, - m_rateConstantForward{ rateConstantForward[R] ... }, - m_rateConstantReverse{ rateConstantReverse[R] ... } - {} - ) - + CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > m_stoichiometricMatrix; + CArrayWrapper< RealType, NUM_REACTIONS > m_rateConstantForward; + CArrayWrapper< RealType, NUM_REACTIONS > m_rateConstantReverse; }; @@ -116,33 +90,73 @@ template< typename REAL_TYPE, typename INT_TYPE, typename INDEX_TYPE, int NUM_SPECIES, - int NUM_REACTIONS > + int NUM_REACTIONS, + int NUM_EQ_REACTIONS > struct MixedReactionsParameters { + using RealType = REAL_TYPE; using IntType = INT_TYPE; using IndexType = INDEX_TYPE; - static constexpr IndexType numSpecies = NUM_SPECIES; - static constexpr IndexType numReactions = NUM_REACTIONS; + + constexpr MixedReactionsParameters() = default; + + static constexpr IndexType numReactions() { return NUM_REACTIONS; } + + static constexpr IndexType numKineticReactions() { return NUM_REACTIONS - NUM_EQ_REACTIONS; } + + static constexpr IndexType numEquilibriumReactions() { return NUM_EQ_REACTIONS; } + + static constexpr IndexType numSpecies() { return NUM_SPECIES; } + + static constexpr IndexType numPrimarySpecies() { return NUM_SPECIES - NUM_EQ_REACTIONS; } + + static constexpr IndexType numSecondarySpecies() { return NUM_EQ_REACTIONS; } constexpr - EquilibriumReactionsParameters< RealType, IntType, IndexType, numSpecies, numReactions > + EquilibriumReactionsParameters< RealType, IntType, IndexType, numSpecies(), numEquilibriumReactions() > equilibriumReactionsParameters() const { - return {m_stoichiometricMatrix, m_equilibriumConstant}; + CArrayWrapper< RealType, numEquilibriumReactions(), numSpecies() > eqMatrix{}; + CArrayWrapper< RealType, numEquilibriumReactions() > eqConstants{}; + + for (IntType i = 0; i < numEquilibriumReactions(); ++i) + { + for (IntType j = 0; j < numSpecies(); ++j) + { + eqMatrix(i, j) = m_stoichiometricMatrix(i, j); + } + eqConstants(i) = m_equilibriumConstant(i); + } + + return { eqMatrix, eqConstants }; } constexpr - KineticReactionsParameters< RealType, IntType, IndexType, numSpecies, numReactions > + KineticReactionsParameters< RealType, IntType, IndexType, numSpecies(), numKineticReactions() > kineticReactionsParameters() const { - return {m_stoichiometricMatrix, m_rateConstantForward, m_rateConstantReverse}; + CArrayWrapper< RealType, numKineticReactions(), numSpecies() > kineticMatrix{}; + CArrayWrapper< RealType, numKineticReactions() > rateConstantForward{}; + CArrayWrapper< RealType, numKineticReactions() > rateConstantReverse{}; + + for ( IndexType i = 0; i < numKineticReactions(); ++i ) + { + for ( IndexType j = 0; j < numSpecies(); ++j ) + { + kineticMatrix(i, j) = m_stoichiometricMatrix( numEquilibriumReactions() + i, j ); + } + rateConstantForward( i ) = m_rateConstantForward( numEquilibriumReactions() + i ); + rateConstantReverse( i ) = m_rateConstantReverse( numEquilibriumReactions() + i ); + } + + return { kineticMatrix, rateConstantForward, rateConstantReverse }; } void verifyParameterConsistency() { static constexpr int num_digits = 12; - for( int i = 0; i < numReactions; ++i ) + for( int i = 0; i < numReactions(); ++i ) { RealType & K = m_equilibriumConstant[i]; RealType & kf = m_rateConstantForward[i]; @@ -179,10 +193,10 @@ struct MixedReactionsParameters RealType rateConstantForward( IndexType const r ) const { return m_rateConstantForward[r]; } RealType rateConstantReverse( IndexType const r ) const { return m_rateConstantReverse[r]; } - RealType m_stoichiometricMatrix[numReactions][numSpecies]; - RealType m_equilibriumConstant[numReactions]; - RealType m_rateConstantForward[numReactions]; - RealType m_rateConstantReverse[numReactions]; + CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > m_stoichiometricMatrix; + CArrayWrapper< RealType, NUM_REACTIONS > m_equilibriumConstant; + CArrayWrapper< RealType, NUM_REACTIONS > m_rateConstantForward; + CArrayWrapper< RealType, NUM_REACTIONS > m_rateConstantReverse; }; diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index a141e69..b5e7d20 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -34,7 +34,26 @@ namespace bulkGeneric // um1Constants }; -using simpleTestType = MixedReactionsParameters< double, int, int, 5, 2 >; +using simpleKineticTestType = MixedReactionsParameters< double, int, int, 5, 2, 0 >; + +constexpr +simpleKineticTestType +simpleKineticTestRateParams = +{ + // stoichiometric matrix + { + { -2, 1, 1, 0, 0 }, + { 0, 0, -1, -1, 2 } + }, + // equilibrium constants + { 1.0, 1.0 }, + // forward rate constants + { 1.0, 0.5 }, + // reverse rate constants + { 1.0, 0.5 } +}; + +using simpleTestType = MixedReactionsParameters< double, int, int, 5, 2, 2 >; constexpr simpleTestType @@ -43,7 +62,7 @@ simpleTestRateParams = // stoichiometric matrix { { -2, 1, 1, 0, 0 }, - { 0, 0, -1, -1, 2 } + { 0, 0, -1, -1, 2 } }, // equilibrium constants { 1.0, 1.0 }, @@ -53,25 +72,26 @@ simpleTestRateParams = { 1.0, 0.5 } }; -using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 11 > ; +using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 12, 10 > ; constexpr -MixedReactionsParameters< double, int, int, 18, 11 > +MixedReactionsParameters< double, int, int, 18, 12, 10 > carbonateSystem = { // stoichiometric matrix - {// OH- CO2 CO3-2 H2CO3 CaHCO3+ CaCO3 CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ - { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0 }, // OH- + H+ = H2O + {// OH- CO2 CO3-2 H2CO3 CaHCO3+ CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- CaCO3 H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ + { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 }, // OH- + H+ = H2O { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, // CO2 + H2O = H+ + HCO3- { 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0 }, // CO3-2 + H+ = HCO3- { 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, // H2CO3 = H+ + HCO3- { 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 }, // CaHCO3+ = Ca+2 + HCO3- - { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 1, 1, 0, 0, 0, 0 }, // CaCO3 + H+ = Ca+2 + HCO3- - { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 }, // CaSO4 = Ca+2 + SO4-2 - { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 }, // CaCl+ = Ca+2 + Cl- - { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 2, 0, 0 }, // CaCl2 = Ca+2 + 2Cl- - { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 1, 0 }, // MgSO4 = Mg+2 + SO4-2 - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1 } // NaSO4- = Na+ + SO4-2 + { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 }, // CaSO4 = Ca+2 + SO4-2 + { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0 }, // CaCl+ = Ca+2 + Cl- + { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0 }, // CaCl2 = Ca+2 + 2Cl- + { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, // MgSO4 = Mg+2 + SO4-2 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1 }, // NaSO4- = Na+ + SO4-2 + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 1, 1, 0, 0, 0, 0 }, // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 0, 0, 0, 0 } // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) }, // equilibrium constants { 9.77E+13, // OH- + H+ = H2O @@ -79,37 +99,41 @@ carbonateSystem = 2.14E+10, // CO3-2 + H+ = HCO3- 1.70E-04, // H2CO3 = H+ + HCO3- 8.13E-02, // CaHCO3+ = Ca+2 + HCO3- - 1.17E+07, // CaCO3 + H+ = Ca+2 + HCO3- 6.92E-03, // CaSO4 = Ca+2 + SO4-2 4.68E+00, // CaCl+ = Ca+2 + Cl- 3.98E+00, // CaCl2 = Ca+2 + 2Cl- 3.72E-03, // MgSO4 = Mg+2 + SO4-2 - 1.51E-01 }, // NaSO4- = Na+ + SO4-2 + 1.51E-01, // NaSO4- = Na+ + SO4-2 + 1.17E+07, // CaCO3 + H+ = Ca+2 + HCO3- + 1 }, // forward rate constants { 1.4e11, // OH- + H+ = H2O 0.039, // CO2 + H2O = H+ + HCO3- 1.0e10, // CO3-2 + H+ = HCO3- 0.57, // H2CO3 = H+ + HCO3- 1.5e6, // CaHCO3+ = Ca+2 + HCO3- - 1.0e5, // CaCO3 + H+ = Ca+2 + HCO3- 1.0e5, // CaSO4 = Ca+2 + SO4-2 1.0e8, // CaCl+ = Ca+2 + Cl- 1.0e7, // CaCl2 = Ca+2 + 2Cl- 1.0e5, // MgSO4 = Mg+2 + SO4-2 - 1.0e7 // NaSO4- = Na+ + SO4-2 + 1.0e7, // NaSO4- = Na+ + SO4-2 + 1.0e5, // CaCO3 + H+ = Ca+2 + HCO3- + 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) }, // reverse rate constants - { 1.43E-03, - 8.92E+04, - 4.67E-01, - 3.35E+03, - 1.85E+07, - 8.55E-03, - 1.45E+07, - 2.14E+07, - 2.51E+06, - 2.69E+07, - 6.62E+07 } + { 1.43E-03, // OH- + H+ = H2O + 8.92E+04, // CO2 + H2O = H+ + HCO3- + 4.67E-01, // CO3-2 + H+ = HCO3- + 3.35E+03, // H2CO3 = H+ + HCO3- + 8.55E-03, // CaHCO3+ = Ca+2 + HCO3- + 1.45E+07, // CaSO4 = Ca+2 + SO4-2 + 2.14E+07, // CaCl+ = Ca+2 + Cl- + 2.51E+06, // CaCl2 = Ca+2 + 2Cl- + 2.69E+07, // MgSO4 = Mg+2 + SO4-2 + 6.62E+07, // NaSO4- = Na+ + SO4-2 + 1.85E+07, // CaCO3 + H+ = Ca+2 + HCO3- + 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) + } }; diff --git a/src/reactions/bulkGeneric/SpeciesUtilities.hpp b/src/reactions/bulkGeneric/SpeciesUtilities.hpp index c7987ce..1909916 100644 --- a/src/reactions/bulkGeneric/SpeciesUtilities.hpp +++ b/src/reactions/bulkGeneric/SpeciesUtilities.hpp @@ -26,9 +26,8 @@ void calculateLogSecondarySpeciesConcentration( PARAMS_DATA const & params, ARRAY_1D & logSecondarySpeciesConcentrations, FUNC && derivativeFunc ) { - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); for( int j=0; j HPCREACT_HOST_DEVICE inline -void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, +void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, ARRAY_1D & logSecondarySpeciesConcentrations, ARRAY_1D & aggregatePrimarySpeciesConcentrations, ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) { - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + calculateLogSecondarySpeciesConcentration< REAL_TYPE, INT_TYPE, @@ -148,9 +147,8 @@ void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, ARRAY_1D & aggregatePrimarySpeciesConcentrations, ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) { - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numSecondarySpecies = PARAMS_DATA::numReactions; - constexpr int numPrimarySpecies = numSpecies - numSecondarySpecies; + constexpr int numSecondarySpecies = PARAMS_DATA::numReactions(); + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); REAL_TYPE logSecondarySpeciesConcentrations[numSecondarySpecies] = {0}; @@ -180,14 +178,5 @@ void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, } } -// template< typename REAL_TYPE, -// typename INT_TYPE, -// typename INDEX_TYPE, -// typename PARAMS_DATA, -// typename ARRAY_1D_TO_CONST, -// typename ARRAY_1D, -// typename ARRAY_2D > -// void calculateAggregateBasedResidualAndJacobianWrtLogC( ) - } // namespace bulkGeneric } // namespace hpcReact diff --git a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp index 3cadb3b..d03696c 100644 --- a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp @@ -21,16 +21,16 @@ template< typename REAL_TYPE, int RESIDUAL_FORM, typename PARAMS_DATA > void computeResidualAndJacobianTest( PARAMS_DATA const & params, - REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies], - REAL_TYPE const (&expectedResidual)[PARAMS_DATA::numReactions], - REAL_TYPE const (&expectedJacobian)[PARAMS_DATA::numReactions][PARAMS_DATA::numReactions] ) + REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies()], + REAL_TYPE const (&expectedResidual)[PARAMS_DATA::numReactions()], + REAL_TYPE const (&expectedJacobian)[PARAMS_DATA::numReactions()][PARAMS_DATA::numReactions()] ) { using EquilibriumReactionsType = EquilibriumReactions< REAL_TYPE, int, int >; - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numReactions = PARAMS_DATA::numReactions; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); + constexpr int numReactions = PARAMS_DATA::numReactions(); double const temperature = 298.15; double speciesConcentration[numSpecies]; @@ -99,14 +99,14 @@ template< typename REAL_TYPE, int RESIDUAL_FORM, typename PARAMS_DATA > void testEnforceEquilibrium( PARAMS_DATA const & params, - REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies], - REAL_TYPE const (&expectedSpeciesConcentrations)[PARAMS_DATA::numSpecies] ) + REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies()], + REAL_TYPE const (&expectedSpeciesConcentrations)[PARAMS_DATA::numSpecies()] ) { using EquilibriumReactionsType = EquilibriumReactions< REAL_TYPE, int, int >; - constexpr int numSpecies = PARAMS_DATA::numSpecies; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); double const temperature = 298.15; double speciesConcentration0[numSpecies]; @@ -217,10 +217,7 @@ TEST( testEquilibriumReactions, testCarbonateSystem2 ) int, int >; - constexpr int numSpecies = carbonateSystem.numSpecies; - constexpr int numReactions = carbonateSystem.numReactions; - constexpr int numPrimarySpecies = numSpecies - numReactions; -// constexpr int numSecondarySpecies = numReactions; + constexpr int numPrimarySpecies = carbonateSystem.numPrimarySpecies(); double const initialPrimarySpeciesConcentration[numPrimarySpecies] = { diff --git a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp index 3945511..790f862 100644 --- a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp @@ -22,17 +22,17 @@ template< typename REAL_TYPE, bool LOGE_CONCENTRATION, typename PARAMS_DATA > void computeReactionRatesTest( PARAMS_DATA const & params, - REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies], - REAL_TYPE const (&expectedReactionRates)[PARAMS_DATA::numReactions], - REAL_TYPE const (&expectedReactionRatesDerivatives)[PARAMS_DATA::numReactions][PARAMS_DATA::numSpecies] ) + REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies()], + REAL_TYPE const (&expectedReactionRates)[PARAMS_DATA::numReactions()], + REAL_TYPE const (&expectedReactionRatesDerivatives)[PARAMS_DATA::numReactions()][PARAMS_DATA::numSpecies()] ) { using KineticReactionsType = KineticReactions< REAL_TYPE, int, int, LOGE_CONCENTRATION >; - constexpr int numSpecies = PARAMS_DATA::numSpecies; - constexpr int numReactions = PARAMS_DATA::numReactions; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); + constexpr int numReactions = PARAMS_DATA::numReactions(); double const temperature = 298.15; double speciesConcentration[numSpecies]; @@ -93,18 +93,18 @@ void computeReactionRatesTest( PARAMS_DATA const & params, //****************************************************************************** -TEST( testKineticReactions, computeReactionRatesTest_simpleTestRateParams ) +TEST( testKineticReactions, computeReactionRatesTest_simpleKineticTestRateParams ) { double const initialSpeciesConcentration[] = { 1.0, 1.0e-16, 0.5, 1.0, 1.0e-16 }; double const expectedReactionRates[] = { 1.0, 0.25 }; double const expectedReactionRatesDerivatives[][5] = { { 2.0, -0.5, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.5, 0.25, 0.0 } }; - computeReactionRatesTest< double, false >( simpleTestRateParams.kineticReactionsParameters(), + computeReactionRatesTest< double, false >( simpleKineticTestRateParams.kineticReactionsParameters(), initialSpeciesConcentration, expectedReactionRates, expectedReactionRatesDerivatives ); - computeReactionRatesTest< double, true >( simpleTestRateParams, + computeReactionRatesTest< double, true >( simpleKineticTestRateParams.kineticReactionsParameters(), initialSpeciesConcentration, expectedReactionRates, expectedReactionRatesDerivatives ); @@ -168,9 +168,9 @@ template< typename REAL_TYPE, bool LOGE_CONCENTRATION, typename PARAMS_DATA > void computeSpeciesRatesTest( PARAMS_DATA const & params, - REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies], - REAL_TYPE const (&expectedSpeciesRates)[PARAMS_DATA::numSpecies], - REAL_TYPE const (&expectedSpeciesRatesDerivatives)[PARAMS_DATA::numSpecies][PARAMS_DATA::numSpecies] ) + REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies()], + REAL_TYPE const (&expectedSpeciesRates)[PARAMS_DATA::numSpecies()], + REAL_TYPE const (&expectedSpeciesRatesDerivatives)[PARAMS_DATA::numSpecies()][PARAMS_DATA::numSpecies()] ) { using KineticReactionsType = KineticReactions< REAL_TYPE, @@ -178,7 +178,7 @@ void computeSpeciesRatesTest( PARAMS_DATA const & params, int, LOGE_CONCENTRATION >; - constexpr int numSpecies = PARAMS_DATA::numSpecies; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); double const temperature = 298.15; double speciesConcentration[numSpecies]; @@ -225,7 +225,7 @@ void computeSpeciesRatesTest( PARAMS_DATA const & params, } } -TEST( testKineticReactions, computeSpeciesRatesTest_simpleTestRateParams ) +TEST( testKineticReactions, computeSpeciesRatesTest_simpleKineticTestRateParams ) { double const initialSpeciesConcentration[5] = { 1.0, 1.0e-16, 0.5, 1.0, 1.0e-16 }; double const expectedSpeciesRates[5] = { -2.0, 1.0, 0.75, -0.25, 0.5 }; @@ -235,12 +235,12 @@ TEST( testKineticReactions, computeSpeciesRatesTest_simpleTestRateParams ) { 0.0, 0.0, -0.5, -0.25, 0.0 }, { 0.0, 0.0, 1.0, 0.5, 0.0 } }; - computeSpeciesRatesTest< double, false >( simpleTestRateParams, + computeSpeciesRatesTest< double, false >( simpleKineticTestRateParams, initialSpeciesConcentration, expectedSpeciesRates, expectedSpeciesRatesDerivatives ); - computeSpeciesRatesTest< double, true >( simpleTestRateParams, + computeSpeciesRatesTest< double, true >( simpleKineticTestRateParams, initialSpeciesConcentration, expectedSpeciesRates, expectedSpeciesRatesDerivatives ); @@ -289,15 +289,15 @@ template< typename REAL_TYPE, void timeStepTest( PARAMS_DATA const & params, REAL_TYPE const dt, int const numSteps, - REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies], - REAL_TYPE const (&expectedSpeciesConcentrations)[PARAMS_DATA::numSpecies] ) + REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numSpecies()], + REAL_TYPE const (&expectedSpeciesConcentrations)[PARAMS_DATA::numSpecies()] ) { using KineticReactionsType = KineticReactions< double, int, int, LOGE_CONCENTRATION >; - constexpr int numSpecies = PARAMS_DATA::numSpecies; + constexpr int numSpecies = PARAMS_DATA::numSpecies(); double const temperature = 298.15; double speciesConcentration[numSpecies]; @@ -357,14 +357,14 @@ TEST( testKineticReactions, testTimeStep ) double const initialSpeciesConcentration[5] = { 1.0, 1.0e-16, 0.5, 1.0, 1.0e-16 }; double const expectedSpeciesConcentrations[5] = { 3.92138293924124e-01, 3.03930853037938e-01, 5.05945480771998e-01, 7.02014627734060e-01, 5.95970744531880e-01 }; - timeStepTest< double, false >( simpleTestRateParams, + timeStepTest< double, false >( simpleKineticTestRateParams, 2.0, 10, initialSpeciesConcentration, expectedSpeciesConcentrations ); // ln(c) as the primary variable results in a singular system. - // timeStepTest< double, true >( simpleTestRateParams, + // timeStepTest< double, true >( simpleKineticTestRateParams, // 2.0, // 10, // initialSpeciesConcentration, @@ -424,7 +424,7 @@ TEST( testKineticReactions, testTimeStep_carbonateSystem ) expectedSpeciesConcentrations ); // ln(c) as the primary variable results in a singular system. - // timeStepTest< double, true >( simpleTestRateParams, + // timeStepTest< double, true >( simpleKineticTestRateParams, // 2.0, // 10, // initialSpeciesConcentration, diff --git a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp index 889888c..829ed78 100644 --- a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp +++ b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp @@ -11,10 +11,10 @@ using namespace hpcReact::bulkGeneric; TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) { - constexpr int numReactions = carbonateSystem.numReactions; - constexpr int numSpecies = carbonateSystem.numSpecies; - constexpr int numPrimarySpecies = numSpecies - numReactions; - constexpr int numSecondarySpecies = numReactions; + constexpr int numReactions = carbonateSystem.numReactions(); + constexpr int numSpecies = carbonateSystem.numSpecies(); + constexpr int numPrimarySpecies = carbonateSystem.numPrimarySpecies(); + constexpr int numSecondarySpecies = carbonateSystem.numSecondarySpecies(); double const logPrimarySpeciesSolution[numPrimarySpecies] = { @@ -98,10 +98,7 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) TEST( testUtilities, testcalculateAggregatePrimaryConcentrationsWrtLogC ) { - constexpr int numReactions = carbonateSystem.numReactions; - constexpr int numSpecies = carbonateSystem.numSpecies; - constexpr int numPrimarySpecies = numSpecies - numReactions; -// constexpr int numSecondarySpecies = numReactions; + constexpr int numPrimarySpecies = carbonateSystem.numPrimarySpecies(); double primarySpeciesSolution[numPrimarySpecies] = { From c32023269316205f82a95a083ed2673b5bc90263 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Fri, 16 May 2025 17:58:56 -0700 Subject: [PATCH 06/28] add MixedEquilibriumKineticReactions_impl.hpp --- .../MixedEquilibriumKineticReactions_impl.hpp | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp new file mode 100644 index 0000000..b5490e4 --- /dev/null +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -0,0 +1,195 @@ +#pragma once + +#include "common/constants.hpp" +#include "common/CArrayWrapper.hpp" +#include "SpeciesUtilities.hpp" + + +/** @file MixedEquilibriumKineticReactions_impl.hpp + * @brief Header file for the MixedEquilibriumKineticReactions implementation. + * @author HPC-REACT Team + * @date 2025 + */ + +namespace hpcReact +{ +namespace bulkGeneric +{ + +template< typename REAL_TYPE, + typename INT_TYPE, + typename INDEX_TYPE, + bool LOGE_CONCENTRATION > +template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > +HPCREACT_HOST_DEVICE inline void +MixedEquilibriumKineticReactions< REAL_TYPE, + INT_TYPE, + INDEX_TYPE, + LOGE_CONCENTRATION + >::updateMixedSystem_impl( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D & logSecondarySpeciesConcentrations, + ARRAY_1D & aggregatePrimarySpeciesConcentrations, + ARRAY_2D & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D & aggregateSpeciesRates, + ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) + { + constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions(); + constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + + // 1. Compute new aggregate species from primary species + calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, + INT_TYPE, + INDEX_TYPE >( params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + aggregatePrimarySpeciesConcentrations, + dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ); + + // 2. Compute the reaction rates for all kinetic reactions + computeReactionRates( temperature, + params, + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + aggregatePrimarySpeciesConcentrations, + dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + reactionRates, + dReactionRates_dLogPrimarySpeciesConcentrations ); + + + // 3. Compute aggregate species rates + computeAggregateSpeciesRates( params, + logPrimarySpeciesConcentrations, + reactionRates, + dReactionRates_dLogPrimarySpeciesConcentrations, + aggregateSpeciesRates, + dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); + + + } + + template< typename REAL_TYPE, + typename INT_TYPE, + typename INDEX_TYPE, + bool LOGE_CONCENTRATION > + template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D > + HPCREACT_HOST_DEVICE inline void + MixedEquilibriumKineticReactions< REAL_TYPE, + INT_TYPE, + INDEX_TYPE, + LOGE_CONCENTRATION + >::computeReactionRates_impl( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ) + + { + constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); + + RealType logSpeciesConcentration[numSpecies] {}; + for ( IntType i = 0; i < numPrimarySpecies; ++i ) + { + logSpeciesConcentration[i] = logPrimarySpeciesConcentrations[i]; + } + for ( IntType i = 0; i < numSecondarySpecies; ++i ) + { + logSpeciesConcentration[i+numPrimarySpecies] = logSecondarySpeciesConcentrations[i]; + } + + CArrayWrapper< RealType, numKineticReactions, numSpecies > reactionRatesDerivatives; + kineticReactions::computeReactionRates( temperature, + params, + logSpeciesConcentration, + reactionRates, + reactionRatesDerivatives ); + + // Compute the reaction rates derivatives w.r.t. log primary species concentrations + for( IntType i = 0; i < numKineticReactions; ++i ) + { + for( IntType j = 0; j < numPrimarySpecies; ++j ) + { + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j ); + for( IntType k = 0; k < numSecondarySpecies; ++k ) + { + RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentration = params.stoichiometricMatrix( k, j+numSecondarySpecies ); + + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) += + reactionRatesDerivatives( i, numPrimarySpecies + k ) * dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations( k, j ); + } + } + } + } + +template< typename REAL_TYPE, + typename INT_TYPE, + typename INDEX_TYPE, + bool LOGE_CONCENTRATION > +template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_2D_TO_CONST, + typename ARRAY_1D, + typename ARRAY_2D, + bool CALCULATE_DERIVATIVES > +HPCREACT_HOST_DEVICE inline void +MixedEquilibriumKineticReactions< REAL_TYPE, + INT_TYPE, + INDEX_TYPE, + LOGE_CONCENTRATION + >::computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & speciesConcentration, + ARRAY_1D_TO_CONST const & reactionRates, + ARRAY_2D_TO_CONST const & reactionRatesDerivatives, + ARRAY_1D & aggregatesRates, + ARRAY_2D & aggregatesRatesDerivatives ) + { + constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); + constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + + for( IntType i = 0; i < numPrimarySpecies; ++i ) + { + speciesRates[i] = 0.0; + if constexpr( CALCULATE_DERIVATIVES ) + { + for( IntType j = 0; j < numPrimarySpecies; ++j ) + { + speciesRatesDerivatives( i, j ) = 0.0; + } + } + for( IntType r=0; r Date: Mon, 19 May 2025 09:44:37 -0700 Subject: [PATCH 07/28] Everything compiles again. --- src/common/CArrayWrapper.hpp | 2 +- src/reactions/bulkGeneric/Parameters.hpp | 10 +++ .../bulkGeneric/ParametersPredefined.hpp | 72 ++++++++++--------- .../unitTests/testEquilibriumReactions.cpp | 18 ++--- .../unitTests/testKineticReactions.cpp | 26 +++---- .../unitTests/testSpeciesUtilities.cpp | 16 ++--- 6 files changed, 79 insertions(+), 65 deletions(-) diff --git a/src/common/CArrayWrapper.hpp b/src/common/CArrayWrapper.hpp index 962900c..376b669 100644 --- a/src/common/CArrayWrapper.hpp +++ b/src/common/CArrayWrapper.hpp @@ -276,7 +276,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * * This allows usage like `obj[dim0][dim1][dim2]`. */ - HPCREACT_HOST_DEVICE constexpr inline T ( & operator[]( int const dim0 ))[DIM1][DIM2] + HPCREACT_HOST_DEVICE constexpr inline T ( & operator[]( int const dim0 ))[DIM1][DIM2] { return data[dim0]; } diff --git a/src/reactions/bulkGeneric/Parameters.hpp b/src/reactions/bulkGeneric/Parameters.hpp index 184023d..9e168a4 100644 --- a/src/reactions/bulkGeneric/Parameters.hpp +++ b/src/reactions/bulkGeneric/Parameters.hpp @@ -101,6 +101,16 @@ struct MixedReactionsParameters constexpr MixedReactionsParameters() = default; + constexpr MixedReactionsParameters( CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > const & stoichiometricMatrix, + CArrayWrapper< RealType, NUM_REACTIONS > const & equilibriumConstant, + CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantForward, + CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantReverse ): + m_stoichiometricMatrix( stoichiometricMatrix ), + m_equilibriumConstant( equilibriumConstant ), + m_rateConstantForward( rateConstantForward ), + m_rateConstantReverse( rateConstantReverse ) + {} + static constexpr IndexType numReactions() { return NUM_REACTIONS; } static constexpr IndexType numKineticReactions() { return NUM_REACTIONS - NUM_EQ_REACTIONS; } diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index b5e7d20..e6eb242 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -72,14 +72,12 @@ simpleTestRateParams = { 1.0, 0.5 } }; -using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 12, 10 > ; +using carbonateSystemAllKineticType = MixedReactionsParameters< double, int, int, 18, 11, 0 >; +using carbonateSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 18, 11, 11 >; +using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 11, 10 >; -constexpr -MixedReactionsParameters< double, int, int, 18, 12, 10 > -carbonateSystem = -{ - // stoichiometric matrix - {// OH- CO2 CO3-2 H2CO3 CaHCO3+ CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- CaCO3 H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ +constexpr CArrayWrapper stoichMatrix = + { // OH- CO2 CO3-2 H2CO3 CaHCO3+ CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- CaCO3 H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 }, // OH- + H+ = H2O { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, // CO2 + H2O = H+ + HCO3- { 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0 }, // CO3-2 + H+ = HCO3- @@ -90,11 +88,13 @@ carbonateSystem = { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0 }, // CaCl2 = Ca+2 + 2Cl- { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, // MgSO4 = Mg+2 + SO4-2 { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1 }, // NaSO4- = Na+ + SO4-2 - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 1, 1, 0, 0, 0, 0 }, // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 0, 0, 0, 0 } // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) - }, - // equilibrium constants - { 9.77E+13, // OH- + H+ = H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 1, 1, 0, 0, 0, 0 } // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + // { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 0, 0, 0, 0 } // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) + }; + +constexpr CArrayWrapper equilibriumConstants = + { + 9.77E+13, // OH- + H+ = H2O 4.37E-07, // CO2 + H2O = H+ + HCO3- 2.14E+10, // CO3-2 + H+ = HCO3- 1.70E-04, // H2CO3 = H+ + HCO3- @@ -104,23 +104,27 @@ carbonateSystem = 3.98E+00, // CaCl2 = Ca+2 + 2Cl- 3.72E-03, // MgSO4 = Mg+2 + SO4-2 1.51E-01, // NaSO4- = Na+ + SO4-2 - 1.17E+07, // CaCO3 + H+ = Ca+2 + HCO3- - 1 }, - // forward rate constants - { 1.4e11, // OH- + H+ = H2O - 0.039, // CO2 + H2O = H+ + HCO3- - 1.0e10, // CO3-2 + H+ = HCO3- - 0.57, // H2CO3 = H+ + HCO3- - 1.5e6, // CaHCO3+ = Ca+2 + HCO3- - 1.0e5, // CaSO4 = Ca+2 + SO4-2 - 1.0e8, // CaCl+ = Ca+2 + Cl- - 1.0e7, // CaCl2 = Ca+2 + 2Cl- - 1.0e5, // MgSO4 = Mg+2 + SO4-2 - 1.0e7, // NaSO4- = Na+ + SO4-2 - 1.0e5, // CaCO3 + H+ = Ca+2 + HCO3- - 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) - }, - // reverse rate constants + 1.17E+07 // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + // 1 + }; // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) + +constexpr CArrayWrapper forwardRates = + { + 9.77E+13, // OH- + H+ = H2O + 4.37E-07, // CO2 + H2O = H+ + HCO3- + 2.14E+10, // CO3-2 + H+ = HCO3- + 1.70E-04, // H2CO3 = H+ + HCO3- + 8.13E-02, // CaHCO3+ = Ca+2 + HCO3- + 6.92E-03, // CaSO4 = Ca+2 + SO4-2 + 4.68E+00, // CaCl+ = Ca+2 + Cl- + 3.98E+00, // CaCl2 = Ca+2 + 2Cl- + 3.72E-03, // MgSO4 = Mg+2 + SO4-2 + 1.51E-01, // NaSO4- = Na+ + SO4-2 + 1.17E+07 // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + // 1 + }; // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) + +constexpr CArrayWrapper reverseRates = { 1.43E-03, // OH- + H+ = H2O 8.92E+04, // CO2 + H2O = H+ + HCO3- 4.67E-01, // CO3-2 + H+ = HCO3- @@ -131,11 +135,13 @@ carbonateSystem = 2.51E+06, // CaCl2 = Ca+2 + 2Cl- 2.69E+07, // MgSO4 = Mg+2 + SO4-2 6.62E+07, // NaSO4- = Na+ + SO4-2 - 1.85E+07, // CaCO3 + H+ = Ca+2 + HCO3- - 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) - } -}; + 1.85E+07 // CaCO3 + H+ = Ca+2 + HCO3- + // 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) + }; + carbonateSystemAllKineticType carbonateSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + carbonateSystemAllEquilibriumType carbonateSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + carbonateSystemType carbonateSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); // *****UNCRUSTIFY-ON****** } // namespace bulkGeneric diff --git a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp index d03696c..e83ecf9 100644 --- a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp @@ -148,7 +148,7 @@ TEST( testEquilibriumReactions, testEnforceEquilibrium ) //****************************************************************************** -TEST( testEquilibriumReactions, testCarbonateSystem ) +TEST( testEquilibriumReactions, testcarbonateSystemAllEquilibrium ) { double const initialSpeciesConcentration[18] = { @@ -157,12 +157,12 @@ TEST( testEquilibriumReactions, testCarbonateSystem ) 1.0e-16, // CO3-2 1.0e-16, // H2CO3 1.0e-16, // CaHCO3+ - 1.0e-16, // CaCO3 1.0e-16, // CaSO4 1.0e-16, // CaCl+ 1.0e-16, // CaCl2 1.0e-16, // MgSO4 1.0e-16, // NaSO4- + 1.0e-16, // CaCO3 3.76e-1, // H+ 3.76e-1, // HCO3- 3.87e-2, // Ca+2 @@ -178,12 +178,12 @@ TEST( testEquilibriumReactions, testCarbonateSystem ) 3.956656978189456e-11, // CO3-2 9.629355924567627e-04, // H2CO3 6.739226982791492e-05, // CaHCO3+ - 1.065032288527957e-09, // CaCO3 5.298329882666738e-03, // CaSO4 5.844517547638333e-03, // CaCl+ 1.277319392670652e-02, // CaCl2 6.618125707964991e-03, // MgSO4 1.769217213462983e-02, // NaSO4- + 1.065032288527957e-09, // CaCO3 4.396954721488358e-04, // H+ 3.723009698453808e-04, // HCO3- 1.471656530812871e-02, // Ca+2 @@ -194,30 +194,30 @@ TEST( testEquilibriumReactions, testCarbonateSystem ) }; std::cout<<" RESIDUAL_FORM 0:"<( carbonateSystem.equilibriumReactionsParameters(), + testEnforceEquilibrium< double, 0 >( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), initialSpeciesConcentration, expectedSpeciesConcentrations ); // std::cout<<" RESIDUAL_FORM 1:"<( carbonateSystem.equilibriumReactionsParameters(), + // testEnforceEquilibrium< double, 1 >( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), // initialSpeciesConcentration, // expectedSpeciesConcentrations ); std::cout<<" RESIDUAL_FORM 2:"<( carbonateSystem.equilibriumReactionsParameters(), + testEnforceEquilibrium< double, 2 >( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), initialSpeciesConcentration, expectedSpeciesConcentrations ); } -TEST( testEquilibriumReactions, testCarbonateSystem2 ) +TEST( testEquilibriumReactions, testcarbonateSystemAllEquilibrium2 ) { using EquilibriumReactionsType = EquilibriumReactions< double, int, int >; - constexpr int numPrimarySpecies = carbonateSystem.numPrimarySpecies(); + constexpr int numPrimarySpecies = carbonateSystemAllEquilibrium.numPrimarySpecies(); double const initialPrimarySpeciesConcentration[numPrimarySpecies] = { @@ -245,7 +245,7 @@ TEST( testEquilibriumReactions, testCarbonateSystem2 ) double logPrimarySpeciesConcentration[numPrimarySpecies]; EquilibriumReactionsType::enforceEquilibrium_Aggregate( 0, - carbonateSystem, + carbonateSystemAllEquilibrium, logInitialPrimarySpeciesConcentration, logPrimarySpeciesConcentration ); diff --git a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp index 790f862..5a3386d 100644 --- a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp @@ -110,7 +110,7 @@ TEST( testKineticReactions, computeReactionRatesTest_simpleKineticTestRateParams expectedReactionRatesDerivatives ); } -TEST( testKineticReactions, computeReactionRatesTest_carbonateSystem ) +TEST( testKineticReactions, computeReactionRatesTest_carbonateSystemAllKinetic ) { double const initialSpeciesConcentration[18] = { @@ -119,12 +119,12 @@ TEST( testKineticReactions, computeReactionRatesTest_carbonateSystem ) 1.0e-16, // CO3-2 1.0e-16, // H2CO3 1.0e-16, // CaHCO3+ - 1.0e-16, // CaCO3 1.0e-16, // CaSO4 1.0e-16, // CaCl+ 1.0e-16, // CaCl2 1.0e-16, // MgSO4 1.0e-16, // NaSO4- + 1.0e-16, // CaCO3 3.76e-1, // H+ 3.76e-1, // HCO3- 3.87e-2, // Ca+2 @@ -151,11 +151,11 @@ TEST( testKineticReactions, computeReactionRatesTest_carbonateSystem ) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.e7, 0, 0, 0, -7.2158e7, 0, 0, -2.12502e6 } }; - computeReactionRatesTest< double, false >( carbonateSystem, + computeReactionRatesTest< double, false >( carbonateSystemAllKinetic.kineticReactionsParameters(), initialSpeciesConcentration, expectedReactionRates, expectedReactionRatesDerivatives ); - computeReactionRatesTest< double, true >( carbonateSystem, + computeReactionRatesTest< double, true >( carbonateSystemAllKinetic.kineticReactionsParameters(), initialSpeciesConcentration, expectedReactionRates, expectedReactionRatesDerivatives ); @@ -235,19 +235,19 @@ TEST( testKineticReactions, computeSpeciesRatesTest_simpleKineticTestRateParams { 0.0, 0.0, -0.5, -0.25, 0.0 }, { 0.0, 0.0, 1.0, 0.5, 0.0 } }; - computeSpeciesRatesTest< double, false >( simpleKineticTestRateParams, + computeSpeciesRatesTest< double, false >( simpleKineticTestRateParams.kineticReactionsParameters(), initialSpeciesConcentration, expectedSpeciesRates, expectedSpeciesRatesDerivatives ); - computeSpeciesRatesTest< double, true >( simpleKineticTestRateParams, + computeSpeciesRatesTest< double, true >( simpleKineticTestRateParams.kineticReactionsParameters(), initialSpeciesConcentration, expectedSpeciesRates, expectedSpeciesRatesDerivatives ); } -// TEST( testKineticReactions, computeSpeciesRatesTest_carbonateSystem ) +// TEST( testKineticReactions, computeSpeciesRatesTest_carbonateSystemAllKinetic ) // { // double const initialSpeciesConcentration[18] = // { @@ -274,7 +274,7 @@ TEST( testKineticReactions, computeSpeciesRatesTest_simpleKineticTestRateParams // double const expectedSpeciesRates[18] = { 0 }; // double const expectedSpeciesRatesDerivatives[18][18] = {{ 0}}; -// computeSpeciesRatesTest< double, false >( carbonateSystem, +// computeSpeciesRatesTest< double, false >( carbonateSystemAllKinetic, // initialSpeciesConcentration, // expectedSpeciesRates, // expectedSpeciesRatesDerivatives ); @@ -357,7 +357,7 @@ TEST( testKineticReactions, testTimeStep ) double const initialSpeciesConcentration[5] = { 1.0, 1.0e-16, 0.5, 1.0, 1.0e-16 }; double const expectedSpeciesConcentrations[5] = { 3.92138293924124e-01, 3.03930853037938e-01, 5.05945480771998e-01, 7.02014627734060e-01, 5.95970744531880e-01 }; - timeStepTest< double, false >( simpleKineticTestRateParams, + timeStepTest< double, false >( simpleKineticTestRateParams.kineticReactionsParameters(), 2.0, 10, initialSpeciesConcentration, @@ -372,7 +372,7 @@ TEST( testKineticReactions, testTimeStep ) } -TEST( testKineticReactions, testTimeStep_carbonateSystem ) +TEST( testKineticReactions, testTimeStep_carbonateSystemAllKinetic ) { double const initialSpeciesConcentration[18] = { @@ -381,12 +381,12 @@ TEST( testKineticReactions, testTimeStep_carbonateSystem ) 1.0e-16, // CO3-2 1.0e-16, // H2CO3 1.0e-16, // CaHCO3+ - 1.0e-16, // CaCO3 1.0e-16, // CaSO4 1.0e-16, // CaCl+ 1.0e-16, // CaCl2 1.0e-16, // MgSO4 1.0e-16, // NaSO4- + 1.0e-16, // CaCO3 3.76e-1, // H+ 3.76e-1, // HCO3- 3.87e-2, // Ca+2 @@ -402,12 +402,12 @@ TEST( testKineticReactions, testTimeStep_carbonateSystem ) 3.956656978189456e-11, // CO3-2 9.629355924567627e-04, // H2CO3 6.739226982791492e-05, // CaHCO3+ - 1.065032288527957e-09, // CaCO3 5.298329882666738e-03, // CaSO4 5.844517547638333e-03, // CaCl+ 1.277319392670652e-02, // CaCl2 6.618125707964991e-03, // MgSO4 1.769217213462983e-02, // NaSO4- + 1.065032288527957e-09, // CaCO3 4.396954721488358e-04, // H+ 3.723009698453808e-04, // HCO3- 1.471656530812871e-02, // Ca+2 @@ -417,7 +417,7 @@ TEST( testKineticReactions, testTimeStep_carbonateSystem ) 1.072307827865370e+00 // Na+1 }; - timeStepTest< double, false >( carbonateSystem, + timeStepTest< double, false >( carbonateSystemAllKinetic.kineticReactionsParameters(), 2.0, 100000, initialSpeciesConcentration, diff --git a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp index 829ed78..134d7e7 100644 --- a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp +++ b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp @@ -11,10 +11,8 @@ using namespace hpcReact::bulkGeneric; TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) { - constexpr int numReactions = carbonateSystem.numReactions(); - constexpr int numSpecies = carbonateSystem.numSpecies(); - constexpr int numPrimarySpecies = carbonateSystem.numPrimarySpecies(); - constexpr int numSecondarySpecies = carbonateSystem.numSecondarySpecies(); + constexpr int numPrimarySpecies = carbonateSystemAllEquilibrium.numPrimarySpecies(); + constexpr int numSecondarySpecies = carbonateSystemAllEquilibrium.numSecondarySpecies(); double const logPrimarySpeciesSolution[numPrimarySpecies] = { @@ -31,7 +29,7 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) calculateLogSecondarySpeciesConcentration< double, int, - int >( carbonateSystem, + int >( carbonateSystemAllEquilibrium, logPrimarySpeciesSolution, logSecondarySpeciesConcentrations ); @@ -62,7 +60,7 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) calculateLogSecondarySpeciesConcentrationWrtLogC< double, int, - int >( carbonateSystem, + int >( carbonateSystemAllEquilibrium, logPrimarySpeciesSolution, logSecondarySpeciesConcentrations, dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations ); @@ -98,7 +96,7 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) TEST( testUtilities, testcalculateAggregatePrimaryConcentrationsWrtLogC ) { - constexpr int numPrimarySpecies = carbonateSystem.numPrimarySpecies(); + constexpr int numPrimarySpecies = carbonateSystemAllEquilibrium.numPrimarySpecies(); double primarySpeciesSolution[numPrimarySpecies] = { @@ -120,9 +118,9 @@ TEST( testUtilities, testcalculateAggregatePrimaryConcentrationsWrtLogC ) double aggregatePrimarySpeciesConcentration[numPrimarySpecies] = {0}; - CArrayWrapper< double, numPrimarySpecies, numPrimarySpecies > dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations = {{{0.0}}}; + CArrayWrapper< double, numPrimarySpecies, numPrimarySpecies > dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations = {{0.0}}; - calculateAggregatePrimaryConcentrationsWrtLogC< double, int, int >( carbonateSystem, + calculateAggregatePrimaryConcentrationsWrtLogC< double, int, int >( carbonateSystemAllEquilibrium, primarySpeciesSolution, aggregatePrimarySpeciesConcentration, dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ); From e82f1fc043ca729164182f02f74499fc64845cc6 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Mon, 19 May 2025 15:39:16 -0700 Subject: [PATCH 08/28] unitTests are passing. --- src/common/CArrayWrapper.hpp | 43 +++++++++++++++---- .../bulkGeneric/ParametersPredefined.hpp | 33 +++++++------- .../bulkGeneric/SpeciesUtilities.hpp | 3 +- .../unitTests/testEquilibriumReactions.cpp | 4 +- .../unitTests/testKineticReactions.cpp | 27 ++++++++---- .../unitTests/testSpeciesUtilities.cpp | 24 +++++++---- 6 files changed, 90 insertions(+), 44 deletions(-) diff --git a/src/common/CArrayWrapper.hpp b/src/common/CArrayWrapper.hpp index 376b669..9236981 100644 --- a/src/common/CArrayWrapper.hpp +++ b/src/common/CArrayWrapper.hpp @@ -27,18 +27,34 @@ struct CArrayWrapper; template< typename T, int DIM0 > struct CArrayWrapper< T, DIM0 > { - + // default constructor constexpr CArrayWrapper() = default; - + + /** + * @brief Construct a CArrayWrapper from an initializer list. + * + * Allows brace-initialization with a list of values: + * @code + * CArrayWrapper< double, 3 > arr = {1.0, 2.0, 3.0}; + * @endcode + * + * @param init An initializer list with exactly DIM0 elements. + * + * @note No runtime bounds checking is performed on the initializer size. + */ constexpr CArrayWrapper( std::initializer_list< T > init ) { + // static_assert(init.size() == DIM0, "Size mismatch"); // needs c++20 int i = 0; for( auto const & val : init ) { data[i++] = val; } } - + /** + * @brief Copy constructor. + * @param src The source CArrayWrapper to copy from. + */ constexpr CArrayWrapper( CArrayWrapper const & src ) { for( size_t i = 0; i < DIM0; i++ ) @@ -76,7 +92,7 @@ struct CArrayWrapper< T, DIM0 > HPCREACT_HOST_DEVICE constexpr inline T const & operator[]( int const dim ) const { return data[dim]; } /// The underlying 1D C-style array. - T data[DIM0]; + T data[DIM0]{}; }; /** @@ -92,9 +108,13 @@ struct CArrayWrapper< T, DIM0 > template< typename T, int DIM0, int DIM1 > struct CArrayWrapper< T, DIM0, DIM1 > { - + // default constructor constexpr CArrayWrapper() = default; - + + /** + * @brief Copy constructor. + * @param src The source CArrayWrapper to copy from. + */ constexpr CArrayWrapper( CArrayWrapper const & src ) { for( size_t i = 0; i < DIM0; i++ ) @@ -121,9 +141,11 @@ struct CArrayWrapper< T, DIM0, DIM1 > */ constexpr CArrayWrapper( std::initializer_list< std::initializer_list< T > > init ) { + // static_assert(init.size() == DIM0, "Size mismatch"); // needs c++20 int i = 0; for( auto const & row : init ) { + // static_assert(row.size() == DIM1, "Size mismatch"); // needs c++20 int j = 0; for( auto const & val : row ) { @@ -178,7 +200,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > } /// The underlying 2D C-style array of size DIM0 x DIM1. - T data[DIM0][DIM1]; + T data[DIM0][DIM1]{}; }; /** @@ -195,7 +217,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > template< typename T, int DIM0, int DIM1, int DIM2 > struct CArrayWrapper< T, DIM0, DIM1, DIM2 > { - + // default constructor constexpr CArrayWrapper() = default; /** @@ -223,12 +245,15 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > */ constexpr CArrayWrapper( std::initializer_list< std::initializer_list< std::initializer_list< T > > > init ) { + // static_assert(init.size() == DIM0, "Size mismatch"); // needs c++20 int i = 0; for( auto const & plane : init ) { + // static_assert(plane.size() == DIM1, "Size mismatch"); // needs c++20 int j = 0; for( auto const & row : plane ) { + // static_assert(row.size() == DIM2, "Size mismatch"); // needs c++20 int k = 0; for( auto const & val : row ) { @@ -292,5 +317,5 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > } /// The underlying 3D C-style array of size DIM0 x DIM1 x DIM2. - T data[DIM0][DIM1][DIM2]; + T data[DIM0][DIM1][DIM2]{}; }; diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index e6eb242..469c92d 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -77,8 +77,8 @@ using carbonateSystemAllEquilibriumType = MixedReactionsParameters< double, int, using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 11, 10 >; constexpr CArrayWrapper stoichMatrix = - { // OH- CO2 CO3-2 H2CO3 CaHCO3+ CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- CaCO3 H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ - { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 }, // OH- + H+ = H2O + { // OH- CO2 CO3-2 H2CO3 CaHCO3+ CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- CaCO3 H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ + { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0 }, // OH- + H+ = H2O { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, // CO2 + H2O = H+ + HCO3- { 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0 }, // CO3-2 + H+ = HCO3- { 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, // H2CO3 = H+ + HCO3- @@ -89,7 +89,7 @@ constexpr CArrayWrapper stoichMatrix = { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0 }, // MgSO4 = Mg+2 + SO4-2 { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1 }, // NaSO4- = Na+ + SO4-2 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 1, 1, 0, 0, 0, 0 } // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) - // { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 0, 0, 0, 0 } // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) + // { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 0, 0, 0, 0 } // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) }; constexpr CArrayWrapper equilibriumConstants = @@ -110,17 +110,18 @@ constexpr CArrayWrapper equilibriumConstants = constexpr CArrayWrapper forwardRates = { - 9.77E+13, // OH- + H+ = H2O - 4.37E-07, // CO2 + H2O = H+ + HCO3- - 2.14E+10, // CO3-2 + H+ = HCO3- - 1.70E-04, // H2CO3 = H+ + HCO3- - 8.13E-02, // CaHCO3+ = Ca+2 + HCO3- - 6.92E-03, // CaSO4 = Ca+2 + SO4-2 - 4.68E+00, // CaCl+ = Ca+2 + Cl- - 3.98E+00, // CaCl2 = Ca+2 + 2Cl- - 3.72E-03, // MgSO4 = Mg+2 + SO4-2 - 1.51E-01, // NaSO4- = Na+ + SO4-2 - 1.17E+07 // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + 1.4e11, // OH- + H+ = H2O + 0.039, // CO2 + H2O = H+ + HCO3- + 1.0e10, // CO3-2 + H+ = HCO3- + 0.57, // H2CO3 = H+ + HCO3- + 1.5e6, // CaHCO3+ = Ca+2 + HCO3- + 1.0e5, // CaSO4 = Ca+2 + SO4-2 + 1.0e8, // CaCl+ = Ca+2 + Cl- + 1.0e7, // CaCl2 = Ca+2 + 2Cl- + 1.0e5, // MgSO4 = Mg+2 + SO4-2 + 1.0e7, // NaSO4- = Na+ + SO4-2 + 1.0e5 // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + // 1 }; // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) @@ -129,13 +130,13 @@ constexpr CArrayWrapper reverseRates = 8.92E+04, // CO2 + H2O = H+ + HCO3- 4.67E-01, // CO3-2 + H+ = HCO3- 3.35E+03, // H2CO3 = H+ + HCO3- - 8.55E-03, // CaHCO3+ = Ca+2 + HCO3- + 1.85E+07, // CaHCO3+ = Ca+2 + HCO3- 1.45E+07, // CaSO4 = Ca+2 + SO4-2 2.14E+07, // CaCl+ = Ca+2 + Cl- 2.51E+06, // CaCl2 = Ca+2 + 2Cl- 2.69E+07, // MgSO4 = Mg+2 + SO4-2 6.62E+07, // NaSO4- = Na+ + SO4-2 - 1.85E+07 // CaCO3 + H+ = Ca+2 + HCO3- + 8.55E-03 // CaCO3 + H+ = Ca+2 + HCO3- // 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) }; diff --git a/src/reactions/bulkGeneric/SpeciesUtilities.hpp b/src/reactions/bulkGeneric/SpeciesUtilities.hpp index 1909916..cd20f20 100644 --- a/src/reactions/bulkGeneric/SpeciesUtilities.hpp +++ b/src/reactions/bulkGeneric/SpeciesUtilities.hpp @@ -3,6 +3,7 @@ #include "common/macros.hpp" #include #include +#include namespace hpcReact { @@ -147,7 +148,7 @@ void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, ARRAY_1D & aggregatePrimarySpeciesConcentrations, ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) { - constexpr int numSecondarySpecies = PARAMS_DATA::numReactions(); + constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); REAL_TYPE logSecondarySpeciesConcentrations[numSecondarySpecies] = {0}; diff --git a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp index e83ecf9..7468540 100644 --- a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp @@ -225,9 +225,9 @@ TEST( testEquilibriumReactions, testcarbonateSystemAllEquilibrium2 ) 3.76e-1, // HCO3- 3.87e-2, // Ca+2 3.21e-2, // SO4-2 - 1.89, // Cl- + 1.89, // Cl- 1.65e-2, // Mg+2 - 1.09 // Na+1 + 1.09 // Na+1 }; diff --git a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp index 5a3386d..41e83af 100644 --- a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp @@ -134,8 +134,18 @@ TEST( testKineticReactions, computeReactionRatesTest_carbonateSystemAllKinetic ) 1.09 // Na+1 }; - double const expectedReactionRates[11] = { -0.001424736, -12610.7392, -0.175591624, -473.6096, -269197.19999999984, -0.00012441275624000003, -18012.914999999986, -1.56526019999999e6, - -346983.07769999903, -14247.58499999999, -2.316271799999999e6 }; + double const expectedReactionRates[11] = { -0.001424736, // OH- + H+ = H2O + -12610.7392, // CO2 + H2O = H+ + HCO3- + -0.175591624, // CO3-2 + H+ = HCO3- + -473.6096, // H2CO3 = H+ + HCO3- + -269197.19999999984, // CaHCO3+ = Ca+2 + HCO3- + -18012.914999999986, // CaSO4 = Ca+2 + SO4-2 + -1.56526019999999e6, // CaCl+ = Ca+2 + Cl- + -346983.07769999903, // CaCl2 = Ca+2 + 2Cl- + -14247.58499999999, // MgSO4 = Mg+2 + SO4-2 + -2.316271799999999e6, // NaSO4- = Na+ + SO4-2 + -0.00012441275624000003 // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) + }; double const expectedReactionRatesDerivatives[11][18] = { { 5.264e10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000014, 0, 0, 0, 0, 0, 0 }, @@ -143,12 +153,13 @@ TEST( testKineticReactions, computeReactionRatesTest_carbonateSystemAllKinetic ) { 0, 0, 3.76e9, 0, 0, 0, 0, 0, 0, 0, 0, 1.e-6, -0.467, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0.57, 0, 0, 0, 0, 0, 0, 0, -1259.6, -1259.6, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1.5e6, 0, 0, 0, 0, 0, 0, 0, -715950., -6.956e6, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 37600., 0, 0, 0, 0, 0, 1.e-11, -0.000330885, -0.0032148000000000003, 0, 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 100000., 0, 0, 0, 0, 0, 0, -465449.99999999994, -561150., 0, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 1.e8, 0, 0, 0, 0, 0, -4.0446e7, 0, -828180., 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 1.e7, 0, 0, 0, 0, -8.965971e6, 0, -367177.86, 0, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 100000., 0, 0, 0, 0, -443850., 0, -863489.9999999999, 0 }, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.e7, 0, 0, 0, -7.2158e7, 0, 0, -2.12502e6 } + { 0, 0, 0, 0, 0, 100000., 0, 0, 0, 0, 0, 0, 0, -465449.99999999994, -561150., 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 1.e8, 0, 0, 0, 0, 0, 0, -4.0446e7, 0, -828180., 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 1.e7, 0, 0, 0, 0, 0, -8.965971e6, 0, -367177.86, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 100000., 0, 0, 0, 0, 0, -443850., 0, -863489.9999999999, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.e7, 0, 0, 0, 0, -7.2158e7, 0, 0, -2.12502e6 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37600., 1.e-11, -0.000330885, -0.0032148000000000003, 0, 0, 0, 0 } + }; computeReactionRatesTest< double, false >( carbonateSystemAllKinetic.kineticReactionsParameters(), diff --git a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp index 134d7e7..aaf9594 100644 --- a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp +++ b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp @@ -29,7 +29,7 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) calculateLogSecondarySpeciesConcentration< double, int, - int >( carbonateSystemAllEquilibrium, + int >( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), logPrimarySpeciesSolution, logSecondarySpeciesConcentrations ); @@ -40,12 +40,12 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) 3.956656978189425e-11, 0.0009629355924566718, 0.00006739226982791149, - 1.065032288527949e-9, 0.005298329882666744, 0.005844517547638335, 0.012773193926706526, 0.006618125707964999, - 0.01769217213462983 + 0.01769217213462983, + 1.065032288527949e-9 }; for( int j=0; j( carbonateSystemAllEquilibrium, + int >( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), logPrimarySpeciesSolution, logSecondarySpeciesConcentrations, dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations ); @@ -74,12 +74,13 @@ TEST( testUtilities, test_calculateLogSecondarySpeciesConcentration ) { -1, 1, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 0, 0, 0 }, - { -1, 1, 1, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0 }, { 0, 0, 1, 0, 1, 0, 0 }, { 0, 0, 1, 0, 2, 0, 0 }, { 0, 0, 0, 1, 0, 1, 0 }, - { 0, 0, 0, 1, 0, 0, 1 } + { 0, 0, 0, 1, 0, 0, 1 }, + { -1, 1, 1, 0, 0, 0, 0 } + }; for( int i=0; i dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations = {{0.0}}; + CArrayWrapper< double, numPrimarySpecies, numPrimarySpecies > dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations; + for( int i = 0; i < numPrimarySpecies; ++i ) + { + for( int k=0; k( carbonateSystemAllEquilibrium, + calculateAggregatePrimaryConcentrationsWrtLogC< double, int, int >( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), primarySpeciesSolution, aggregatePrimarySpeciesConcentration, dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ); From cfe82872056a3db72e8e0eeb13a02e16b18ac382 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Mon, 19 May 2025 15:40:09 -0700 Subject: [PATCH 09/28] add constexpr. --- src/reactions/bulkGeneric/ParametersPredefined.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index 469c92d..6668d90 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -140,9 +140,9 @@ constexpr CArrayWrapper reverseRates = // 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) }; - carbonateSystemAllKineticType carbonateSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - carbonateSystemAllEquilibriumType carbonateSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - carbonateSystemType carbonateSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + constexpr carbonateSystemAllKineticType carbonateSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + constexpr carbonateSystemAllEquilibriumType carbonateSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + constexpr carbonateSystemType carbonateSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); // *****UNCRUSTIFY-ON****** } // namespace bulkGeneric From 696957a5bceb2555be76e1b0f4ab8ac2b29f79d2 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Mon, 19 May 2025 17:35:44 -0700 Subject: [PATCH 10/28] add std:: to size_t --- src/common/CArrayWrapper.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/CArrayWrapper.hpp b/src/common/CArrayWrapper.hpp index 9236981..c4dffec 100644 --- a/src/common/CArrayWrapper.hpp +++ b/src/common/CArrayWrapper.hpp @@ -57,7 +57,7 @@ struct CArrayWrapper< T, DIM0 > */ constexpr CArrayWrapper( CArrayWrapper const & src ) { - for( size_t i = 0; i < DIM0; i++ ) + for( std::size_t i = 0; i < DIM0; i++ ) { data[i] = src.data[i]; } @@ -117,9 +117,9 @@ struct CArrayWrapper< T, DIM0, DIM1 > */ constexpr CArrayWrapper( CArrayWrapper const & src ) { - for( size_t i = 0; i < DIM0; i++ ) + for( std::size_t i = 0; i < DIM0; i++ ) { - for ( size_t j = 0; j < DIM1; j++) + for ( std::size_t j = 0; j < DIM1; j++) data[i][j] = src.data[i][j]; } } From fbccdf29023f61464d5ac6ba75f310ee4ab9aa44 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Tue, 20 May 2025 22:28:41 -0700 Subject: [PATCH 11/28] fix some variables names and inputs. --- .../bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp index b5490e4..354d5b7 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -58,8 +58,6 @@ MixedEquilibriumKineticReactions< REAL_TYPE, params, logPrimarySpeciesConcentrations, logSecondarySpeciesConcentrations, - aggregatePrimarySpeciesConcentrations, - dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations ); @@ -164,7 +162,7 @@ MixedEquilibriumKineticReactions< REAL_TYPE, for( IntType i = 0; i < numPrimarySpecies; ++i ) { - speciesRates[i] = 0.0; + aggregatesRates[i] = 0.0; if constexpr( CALCULATE_DERIVATIVES ) { for( IntType j = 0; j < numPrimarySpecies; ++j ) @@ -175,12 +173,12 @@ MixedEquilibriumKineticReactions< REAL_TYPE, for( IntType r=0; r Date: Thu, 22 May 2025 15:08:31 -0700 Subject: [PATCH 12/28] Implement mixed ultramafic system with serpentinization rxn --- .../bulkGeneric/ParametersPredefined.hpp | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index 6668d90..4510cb3 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -144,6 +144,118 @@ constexpr CArrayWrapper reverseRates = constexpr carbonateSystemAllEquilibriumType carbonateSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); constexpr carbonateSystemType carbonateSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); +// ################################## Ultramafic rxn set ################################## + +//using ultramaficSystemAllKineticType = MixedReactionsParameters< double, int, int, 25, 21, 0 >; +//using ultramaficSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 25, 21, 21 >; +using ultramaficSystemType = MixedReactionsParameters< double, int, int, 25, 21, 16 >; + +constexpr CArrayWrapper stoichMatrix = + { // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) + { -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 }, // OH- + H+ = H2O + { 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 }, // CO2(aq) + H2O = HCO3- + H+ + { 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 }, // CO3-- + H+ = HCO3- + { 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 2 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O + { 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 4 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + { 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 }, // MgOH+ + H+ = Mg++ + H2O + { 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 2 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + { 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- + { 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 }, // MgHCO3+ = Mg++ + HCO3- + { 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + { 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + { 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 -1 0 1 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 -2 0 0 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 -4 0 0 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 -2 0 0 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 -4 0 2 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 1 }, // SiO2 = SiO2(aq) + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -6 0 3 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -2 0 1 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; + +// 2Mg2SiO4 + 3H2O → Mg3Si2O5(OH)4 + Mg(OH)2 Serpentinization reaction + +constexpr CArrayWrapper equilibriumConstants = + { + 9.89E+13, // OH- + H+ = H2O + 4.42E-07, // CO2(aq) + H2O = HCO3- + H+ + 2.23E+10, // CO3-- + H+ = HCO3- + 2.32E+13, // Mg2OH+++ + H+ = 2Mg++ + H2O + 4.47E+39, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + 6.18E+11, // MgOH+ + H+ = Mg++ + H2O + 7.66E+06, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + 2.67E+07, // MgCO3 + H+ = Mg++ + HCO3- + 9.77E-02, // MgHCO3+ = Mg++ + HCO3- + 3.45E+14, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + 9.49E+16, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + 1.96E+08, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + 8.08E+22, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + 6.44E+09, // H3SiO4- + H+ = SiO2(aq) + 2H2O + 5.39E+35, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + 2.72E+13, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + 1.40E+28, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + 2.73E+02, // MgCO3 + H+ = Mg++ + HCO3- + 1.93E-03, // SiO2 = SiO2(aq) + 3.54E+31, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + 2.75E+16 // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; + +constexpr CArrayWrapper forwardRates = + { + 1.00E+10, // OH- + H+ = H2O + 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ + 1.00E+10, // CO3-- + H+ = HCO3- + 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O + 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + 1.00E+10, // MgOH+ + H+ = Mg++ + H2O + 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- + 1.00E+10, // MgHCO3+ = Mg++ + HCO3- + 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O + 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + 2.29E-11, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + 4.57E-10, // MgCO3 + H+ = Mg++ + HCO3- + 1.70E-13, // SiO2 = SiO2(aq) + 1.00E-12, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + 5.75E-09 // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; + +constexpr CArrayWrapper reverseRates = + { + 1.00E+10, // OH- + H+ = H2O + 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ + 1.00E+10, // CO3-- + H+ = HCO3- + 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O + 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + 1.00E+10, // MgOH+ + H+ = Mg++ + H2O + 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- + 1.00E+10, // MgHCO3+ = Mg++ + HCO3- + 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O + 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + 1.65E-39, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + 1.67E-12, // MgCO3 + H+ = Mg++ + HCO3- + 8.78E-11, // SiO2 = SiO2(aq) + 2.83E-44, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + 2.10E-25 // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; + + //constexpr ultramaficSystemAllKineticType ultramaficSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + //constexpr ultramaficSystemAllEquilibriumType ultramaficSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + constexpr ultramaficSystemType ultramaficSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + // *****UNCRUSTIFY-ON****** } // namespace bulkGeneric } // namespace hpcReact From bd47423facd7d848fef9ab284ec406fba0339a0c Mon Sep 17 00:00:00 2001 From: Randolph Settgast Date: Fri, 23 May 2025 14:18:14 -0700 Subject: [PATCH 13/28] fix order of constexpr host device --- src/common/CArrayWrapper.hpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/common/CArrayWrapper.hpp b/src/common/CArrayWrapper.hpp index c4dffec..d35dcd2 100644 --- a/src/common/CArrayWrapper.hpp +++ b/src/common/CArrayWrapper.hpp @@ -68,28 +68,28 @@ struct CArrayWrapper< T, DIM0 > * @param dim The index (must be in range [0, DIM0)). * @return Reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE constexpr inline T & operator()( int const dim ) { return data[dim]; } + constexpr HPCREACT_HOST_DEVICE inline T & operator()( int const dim ) { return data[dim]; } /** * @brief Read-only access to an element by index (const overload). * @param dim The index (must be in range [0, DIM0)). * @return Const reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE constexpr inline T const & operator()( int const dim ) const { return data[dim]; } + constexpr HPCREACT_HOST_DEVICE inline T const & operator()( int const dim ) const { return data[dim]; } /** * @brief Subscript operator for read/write access. * @param dim The index (must be in range [0, DIM0)). * @return Reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE constexpr inline T & operator[]( int const dim ) { return data[dim]; } + constexpr HPCREACT_HOST_DEVICE inline T & operator[]( int const dim ) { return data[dim]; } /** * @brief Subscript operator for read-only access (const overload). * @param dim The index (must be in range [0, DIM0)). * @return Const reference to the element at the specified index. */ - HPCREACT_HOST_DEVICE constexpr inline T const & operator[]( int const dim ) const { return data[dim]; } + constexpr HPCREACT_HOST_DEVICE inline T const & operator[]( int const dim ) const { return data[dim]; } /// The underlying 1D C-style array. T data[DIM0]{}; @@ -161,7 +161,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * @param dim1 Index in the second dimension (range [0, DIM1)). * @return Reference to the element at the specified 2D location. */ - HPCREACT_HOST_DEVICE constexpr inline T & operator()( int const dim0, int const dim1 ) + constexpr HPCREACT_HOST_DEVICE inline T & operator()( int const dim0, int const dim1 ) { return data[dim0][dim1]; } @@ -172,7 +172,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * @param dim1 Index in the second dimension (range [0, DIM1)). * @return Const reference to the element at the specified 2D location. */ - HPCREACT_HOST_DEVICE constexpr inline T const & operator()( int const dim0, int const dim1 ) const + constexpr HPCREACT_HOST_DEVICE inline T const & operator()( int const dim0, int const dim1 ) const { return data[dim0][dim1]; } @@ -184,7 +184,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * * This allows usage like `obj[dim0][dim1]`. */ - HPCREACT_HOST_DEVICE constexpr inline T ( & operator[]( int const dim0 ))[DIM1] + constexpr HPCREACT_HOST_DEVICE inline T ( & operator[]( int const dim0 ))[DIM1] { return data[dim0]; } @@ -194,7 +194,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > * @param dim0 The row index (range [0, DIM0)). * @return Const reference to an array of type T[DIM1]. */ - HPCREACT_HOST_DEVICE constexpr inline T const (&operator[]( int const dim0 ) const)[DIM1] + constexpr HPCREACT_HOST_DEVICE inline T const (&operator[]( int const dim0 ) const)[DIM1] { return data[dim0]; } @@ -275,7 +275,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * @note Currently, this function incorrectly indexes data[dim0][dim1], missing dim2. * It should be `data[dim0][dim1][dim2]`. Please correct if intended. */ - HPCREACT_HOST_DEVICE constexpr inline T & operator()( int const dim0, int const dim1, int const dim2 ) + constexpr HPCREACT_HOST_DEVICE inline T & operator()( int const dim0, int const dim1, int const dim2 ) { // NOTE: This looks like a bug in your original code. Should be data[dim0][dim1][dim2]. return data[dim0][dim1][dim2]; @@ -288,7 +288,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * @param dim2 Index in the third dimension (range [0, DIM2)). * @return Const reference to the element at the specified 3D location. */ - HPCREACT_HOST_DEVICE constexpr inline T const & operator()( int const dim0, int const dim1, int const dim2 ) const + constexpr HPCREACT_HOST_DEVICE inline T const & operator()( int const dim0, int const dim1, int const dim2 ) const { // NOTE: Same potential bug as above. Should be data[dim0][dim1][dim2]. return data[dim0][dim1][dim2]; @@ -301,7 +301,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * * This allows usage like `obj[dim0][dim1][dim2]`. */ - HPCREACT_HOST_DEVICE constexpr inline T ( & operator[]( int const dim0 ))[DIM1][DIM2] + constexpr HPCREACT_HOST_DEVICE inline T ( & operator[]( int const dim0 ))[DIM1][DIM2] { return data[dim0]; } @@ -311,7 +311,7 @@ struct CArrayWrapper< T, DIM0, DIM1, DIM2 > * @param dim0 The slice index (range [0, DIM0)). * @return Const reference to an array of type T[DIM1][DIM2]. */ - HPCREACT_HOST_DEVICE constexpr inline T const (&operator[]( int const dim0 ) const)[DIM1][DIM2] + constexpr HPCREACT_HOST_DEVICE inline T const (&operator[]( int const dim0 ) const)[DIM1][DIM2] { return data[dim0]; } From f85dac10e6b6d48ad56871c66b0e78cb2c441f86 Mon Sep 17 00:00:00 2001 From: frankfeifan Date: Wed, 28 May 2025 11:43:47 -0700 Subject: [PATCH 14/28] temporarily change the typename and turned off the ultramafic to pass the compile --- .../bulkGeneric/EquilibriumReactions.hpp | 3 +- ...ionsAggregatePrimaryConcentration_impl.hpp | 3 +- .../MixedEquilibriumKineticReactions.hpp | 27 ++- .../bulkGeneric/ParametersPredefined.hpp | 195 +++++++++--------- 4 files changed, 120 insertions(+), 108 deletions(-) diff --git a/src/reactions/bulkGeneric/EquilibriumReactions.hpp b/src/reactions/bulkGeneric/EquilibriumReactions.hpp index f6568ec..fca5a2c 100644 --- a/src/reactions/bulkGeneric/EquilibriumReactions.hpp +++ b/src/reactions/bulkGeneric/EquilibriumReactions.hpp @@ -132,12 +132,13 @@ class EquilibriumReactions template< typename PARAMS_DATA, typename ARRAY_1D, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_2D > static HPCREACT_HOST_DEVICE void computeResidualAndJacobianAggregatePrimaryConcentrations( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & targetAggregatePrimaryConcentrations, - ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentration, + ARRAY_1D_TO_CONST2 const & logPrimarySpeciesConcentration, ARRAY_1D & residual, ARRAY_2D & jacobian ); }; diff --git a/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp b/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp index ee4e62e..1b8e534 100644 --- a/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp +++ b/src/reactions/bulkGeneric/EquilibriumReactionsAggregatePrimaryConcentration_impl.hpp @@ -16,6 +16,7 @@ template< typename REAL_TYPE, template< typename PARAMS_DATA, typename ARRAY_1D, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_2D > HPCREACT_HOST_DEVICE inline @@ -25,7 +26,7 @@ EquilibriumReactions< REAL_TYPE, INDEX_TYPE >::computeResidualAndJacobianAggregatePrimaryConcentrations( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & targetAggregatePrimaryConcentrations, - ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentration, + ARRAY_1D_TO_CONST2 const & logPrimarySpeciesConcentration, ARRAY_1D & residual, ARRAY_2D & jacobian ) { diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp index 8bc455f..ae1b77f 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -1,6 +1,7 @@ #pragma once #include "common/macros.hpp" +#include "KineticReactions.hpp" /** @file MixedEquilibriumKineticReactions.hpp * @brief Header file for the MixedEquilibriumKineticReactions class. @@ -71,15 +72,16 @@ class MixedEquilibriumKineticReactions template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_1D, typename ARRAY_2D > static HPCREACT_HOST_DEVICE inline void computeReactionRates( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, + ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ) + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ) { computeReactionRates_impl( temperature, @@ -87,23 +89,30 @@ class MixedEquilibriumKineticReactions logPrimarySpeciesConcentrations, logSecondarySpeciesConcentrations, reactionRates, - dReactionRates_dPrimarySpeciesConcentrations ); + dReactionRates_dLogPrimarySpeciesConcentrations ); } template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_2D_TO_CONST, typename ARRAY_1D, typename ARRAY_2D > static HPCREACT_HOST_DEVICE inline void computeAggregateSpeciesRates( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & speciesConcentration, - ARRAY_1D_TO_CONST const & reactionRates, + ARRAY_1D_TO_CONST2 const & reactionRates, ARRAY_2D_TO_CONST const & reactionRatesDerivatives, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ) { - computeAggregateSpeciesRates_impl< true >( params, + computeAggregateSpeciesRates_impl< PARAMS_DATA, + ARRAY_1D_TO_CONST, + ARRAY_1D_TO_CONST2, + ARRAY_2D_TO_CONST, + ARRAY_1D, + ARRAY_2D, + true >( params, speciesConcentration, reactionRates, reactionRatesDerivatives, @@ -131,20 +140,22 @@ class MixedEquilibriumKineticReactions template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_1D, typename ARRAY_2D > static HPCREACT_HOST_DEVICE void computeReactionRates_impl( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, + ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ); + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ); template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_2D_TO_CONST, typename ARRAY_1D, typename ARRAY_2D, @@ -152,7 +163,7 @@ class MixedEquilibriumKineticReactions static HPCREACT_HOST_DEVICE void computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & speciesConcentration, - ARRAY_1D_TO_CONST const & reactionRates, + ARRAY_1D_TO_CONST2 const & reactionRates, ARRAY_2D_TO_CONST const & reactionRatesDerivatives, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ); diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index 4510cb3..9c27d4f 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -150,112 +150,111 @@ constexpr CArrayWrapper reverseRates = //using ultramaficSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 25, 21, 21 >; using ultramaficSystemType = MixedReactionsParameters< double, int, int, 25, 21, 16 >; -constexpr CArrayWrapper stoichMatrix = - { // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) - { -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 }, // OH- + H+ = H2O - { 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 }, // CO2(aq) + H2O = HCO3- + H+ - { 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 }, // CO3-- + H+ = HCO3- - { 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 2 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O - { 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 4 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O - { 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 }, // MgOH+ + H+ = Mg++ + H2O - { 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 2 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- - { 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- - { 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 }, // MgHCO3+ = Mg++ + HCO3- - { 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O - { 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O - { 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 -1 0 1 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 -2 0 0 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 -4 0 0 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 -2 0 0 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 -4 0 2 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 1 }, // SiO2 = SiO2(aq) - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -6 0 3 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O - { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -2 0 1 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O - }; +// constexpr CArrayWrapper stoichMatrix = +// { // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) +// { -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 }, // OH- + H+ = H2O +// { 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 }, // CO2(aq) + H2O = HCO3- + H+ +// { 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 }, // CO3-- + H+ = HCO3- +// { 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 2 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O +// { 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 4 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O +// { 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 }, // MgOH+ + H+ = Mg++ + H2O +// { 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 2 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- +// { 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- +// { 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 }, // MgHCO3+ = Mg++ + HCO3- +// { 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O +// { 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O +// { 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 -1 0 1 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 -2 0 0 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 -4 0 0 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 -2 0 0 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 -4 0 2 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 1 }, // SiO2 = SiO2(aq) +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -6 0 3 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O +// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -2 0 1 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O +// }; // 2Mg2SiO4 + 3H2O → Mg3Si2O5(OH)4 + Mg(OH)2 Serpentinization reaction -constexpr CArrayWrapper equilibriumConstants = - { - 9.89E+13, // OH- + H+ = H2O - 4.42E-07, // CO2(aq) + H2O = HCO3- + H+ - 2.23E+10, // CO3-- + H+ = HCO3- - 2.32E+13, // Mg2OH+++ + H+ = 2Mg++ + H2O - 4.47E+39, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O - 6.18E+11, // MgOH+ + H+ = Mg++ + H2O - 7.66E+06, // Mg2CO3++ + H+ = 2Mg++ + HCO3- - 2.67E+07, // MgCO3 + H+ = Mg++ + HCO3- - 9.77E-02, // MgHCO3+ = Mg++ + HCO3- - 3.45E+14, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O - 9.49E+16, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O - 1.96E+08, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O - 8.08E+22, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O - 6.44E+09, // H3SiO4- + H+ = SiO2(aq) + 2H2O - 5.39E+35, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O - 2.72E+13, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O - 1.40E+28, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O - 2.73E+02, // MgCO3 + H+ = Mg++ + HCO3- - 1.93E-03, // SiO2 = SiO2(aq) - 3.54E+31, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O - 2.75E+16 // Mg(OH)2 + 2H+ = Mg++ + 2H2O - }; +// constexpr CArrayWrapper equilibriumConstants = +// { +// 9.89E+13, // OH- + H+ = H2O +// 4.42E-07, // CO2(aq) + H2O = HCO3- + H+ +// 2.23E+10, // CO3-- + H+ = HCO3- +// 2.32E+13, // Mg2OH+++ + H+ = 2Mg++ + H2O +// 4.47E+39, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O +// 6.18E+11, // MgOH+ + H+ = Mg++ + H2O +// 7.66E+06, // Mg2CO3++ + H+ = 2Mg++ + HCO3- +// 2.67E+07, // MgCO3 + H+ = Mg++ + HCO3- +// 9.77E-02, // MgHCO3+ = Mg++ + HCO3- +// 3.45E+14, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O +// 9.49E+16, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O +// 1.96E+08, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O +// 8.08E+22, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O +// 6.44E+09, // H3SiO4- + H+ = SiO2(aq) + 2H2O +// 5.39E+35, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O +// 2.72E+13, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O +// 1.40E+28, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O +// 2.73E+02, // MgCO3 + H+ = Mg++ + HCO3- +// 1.93E-03, // SiO2 = SiO2(aq) +// 3.54E+31, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O +// 2.75E+16 // Mg(OH)2 + 2H+ = Mg++ + 2H2O +// }; -constexpr CArrayWrapper forwardRates = - { - 1.00E+10, // OH- + H+ = H2O - 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ - 1.00E+10, // CO3-- + H+ = HCO3- - 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O - 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O - 1.00E+10, // MgOH+ + H+ = Mg++ + H2O - 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- - 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- - 1.00E+10, // MgHCO3+ = Mg++ + HCO3- - 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O - 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O - 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O - 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O - 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O - 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O - 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O - 2.29E-11, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O - 4.57E-10, // MgCO3 + H+ = Mg++ + HCO3- - 1.70E-13, // SiO2 = SiO2(aq) - 1.00E-12, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O - 5.75E-09 // Mg(OH)2 + 2H+ = Mg++ + 2H2O - }; +// constexpr CArrayWrapper forwardRates = +// { +// 1.00E+10, // OH- + H+ = H2O +// 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ +// 1.00E+10, // CO3-- + H+ = HCO3- +// 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O +// 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O +// 1.00E+10, // MgOH+ + H+ = Mg++ + H2O +// 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- +// 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- +// 1.00E+10, // MgHCO3+ = Mg++ + HCO3- +// 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O +// 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O +// 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O +// 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O +// 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O +// 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O +// 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O +// 2.29E-11, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O +// 4.57E-10, // MgCO3 + H+ = Mg++ + HCO3- +// 1.70E-13, // SiO2 = SiO2(aq) +// 1.00E-12, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O +// 5.75E-09 // Mg(OH)2 + 2H+ = Mg++ + 2H2O +// }; -constexpr CArrayWrapper reverseRates = - { - 1.00E+10, // OH- + H+ = H2O - 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ - 1.00E+10, // CO3-- + H+ = HCO3- - 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O - 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O - 1.00E+10, // MgOH+ + H+ = Mg++ + H2O - 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- - 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- - 1.00E+10, // MgHCO3+ = Mg++ + HCO3- - 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O - 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O - 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O - 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O - 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O - 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O - 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O - 1.65E-39, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O - 1.67E-12, // MgCO3 + H+ = Mg++ + HCO3- - 8.78E-11, // SiO2 = SiO2(aq) - 2.83E-44, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O - 2.10E-25 // Mg(OH)2 + 2H+ = Mg++ + 2H2O - }; +// constexpr CArrayWrapper reverseRates = +// { +// 1.00E+10, // OH- + H+ = H2O +// 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ +// 1.00E+10, // CO3-- + H+ = HCO3- +// 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O +// 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O +// 1.00E+10, // MgOH+ + H+ = Mg++ + H2O +// 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- +// 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- +// 1.00E+10, // MgHCO3+ = Mg++ + HCO3- +// 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O +// 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O +// 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O +// 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O +// 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O +// 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O +// 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O +// 1.65E-39, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O +// 1.67E-12, // MgCO3 + H+ = Mg++ + HCO3- +// 8.78E-11, // SiO2 = SiO2(aq) +// 2.83E-44, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O +// 2.10E-25 // Mg(OH)2 + 2H+ = Mg++ + 2H2O +// }; //constexpr ultramaficSystemAllKineticType ultramaficSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); //constexpr ultramaficSystemAllEquilibriumType ultramaficSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - constexpr ultramaficSystemType ultramaficSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + // constexpr ultramaficSystemType ultramaficSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); -// *****UNCRUSTIFY-ON****** } // namespace bulkGeneric } // namespace hpcReact From 71bde44f4b5c9ade70b8cd51088978780007ddca Mon Sep 17 00:00:00 2001 From: frankfeifan Date: Wed, 28 May 2025 11:45:39 -0700 Subject: [PATCH 15/28] corrected a few indices in the loop to match the order in ParametersPredefined --- .../MixedEquilibriumKineticReactions_impl.hpp | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp index 354d5b7..ca4eed2 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -40,10 +40,10 @@ MixedEquilibriumKineticReactions< REAL_TYPE, ARRAY_1D & aggregateSpeciesRates, ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) { - constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); - constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions(); - constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); - + // constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + // constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions(); + // constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + // 1. Compute new aggregate species from primary species calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, INT_TYPE, @@ -51,7 +51,7 @@ MixedEquilibriumKineticReactions< REAL_TYPE, logPrimarySpeciesConcentrations, logSecondarySpeciesConcentrations, aggregatePrimarySpeciesConcentrations, - dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ); + dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations ); // 2. Compute the reaction rates for all kinetic reactions computeReactionRates( temperature, @@ -79,6 +79,7 @@ MixedEquilibriumKineticReactions< REAL_TYPE, bool LOGE_CONCENTRATION > template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_1D, typename ARRAY_2D > HPCREACT_HOST_DEVICE inline void @@ -89,9 +90,9 @@ MixedEquilibriumKineticReactions< REAL_TYPE, >::computeReactionRates_impl( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D_TO_CONST const & logSecondarySpeciesConcentrations, + ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dPrimarySpeciesConcentrations ) + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ) { constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); @@ -100,18 +101,19 @@ MixedEquilibriumKineticReactions< REAL_TYPE, constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); RealType logSpeciesConcentration[numSpecies] {}; - for ( IntType i = 0; i < numPrimarySpecies; ++i ) + for ( IntType i = 0; i < numSecondarySpecies; ++i ) { - logSpeciesConcentration[i] = logPrimarySpeciesConcentrations[i]; + logSpeciesConcentration[i] = logSecondarySpeciesConcentrations[i]; } - for ( IntType i = 0; i < numSecondarySpecies; ++i ) + for ( IntType i = 0; i < numPrimarySpecies; ++i ) { - logSpeciesConcentration[i+numPrimarySpecies] = logSecondarySpeciesConcentrations[i]; + logSpeciesConcentration[i+numSecondarySpecies] = logPrimarySpeciesConcentrations[i]; } CArrayWrapper< RealType, numKineticReactions, numSpecies > reactionRatesDerivatives; + kineticReactions::computeReactionRates( temperature, - params, + params.kineticReactionsParameters(), logSpeciesConcentration, reactionRates, reactionRatesDerivatives ); @@ -120,15 +122,16 @@ MixedEquilibriumKineticReactions< REAL_TYPE, for( IntType i = 0; i < numKineticReactions; ++i ) { for( IntType j = 0; j < numPrimarySpecies; ++j ) - { - dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j ); + { + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j + numSecondarySpecies ); + for( IntType k = 0; k < numSecondarySpecies; ++k ) { - RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentration = params.stoichiometricMatrix( k, j+numSecondarySpecies ); + RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations = params.stoichiometricMatrix( k, j + numSecondarySpecies ); dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) += - reactionRatesDerivatives( i, numPrimarySpecies + k ) * dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations( k, j ); - } + reactionRatesDerivatives( i, k ) * dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations; + } } } } @@ -139,6 +142,7 @@ template< typename REAL_TYPE, bool LOGE_CONCENTRATION > template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, typename ARRAY_2D_TO_CONST, typename ARRAY_1D, typename ARRAY_2D, @@ -150,12 +154,13 @@ MixedEquilibriumKineticReactions< REAL_TYPE, LOGE_CONCENTRATION >::computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & speciesConcentration, - ARRAY_1D_TO_CONST const & reactionRates, + ARRAY_1D_TO_CONST2 const & reactionRates, ARRAY_2D_TO_CONST const & reactionRatesDerivatives, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ) { - constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + GEOS_UNUSED_VAR( speciesConcentration ); + // constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); @@ -167,12 +172,12 @@ MixedEquilibriumKineticReactions< REAL_TYPE, { for( IntType j = 0; j < numPrimarySpecies; ++j ) { - speciesRatesDerivatives( i, j ) = 0.0; + aggregatesRatesDerivatives( i, j ) = 0.0; } } for( IntType r=0; r Date: Thu, 29 May 2025 15:37:51 -0700 Subject: [PATCH 16/28] fix ultramafics --- .../bulkGeneric/ParametersPredefined.hpp | 224 +++++++++--------- 1 file changed, 115 insertions(+), 109 deletions(-) diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index 9c27d4f..64109a2 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -72,9 +72,8 @@ simpleTestRateParams = { 1.0, 0.5 } }; -using carbonateSystemAllKineticType = MixedReactionsParameters< double, int, int, 18, 11, 0 >; -using carbonateSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 18, 11, 11 >; -using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 11, 10 >; +namespace carbonate +{ constexpr CArrayWrapper stoichMatrix = { // OH- CO2 CO3-2 H2CO3 CaHCO3+ CaSO4 CaCl+ CaCl2 MgSO4 NaSO4- CaCO3 H+ HCO3- Ca+2 SO4-2 Cl- Mg+2 Na+ @@ -139,122 +138,129 @@ constexpr CArrayWrapper reverseRates = 8.55E-03 // CaCO3 + H+ = Ca+2 + HCO3- // 1 // Ca(OH)2​(s) + 2H+ = Ca2+ + 2H2​O (kinetic) }; + } + using carbonateSystemAllKineticType = MixedReactionsParameters< double, int, int, 18, 11, 0 >; + using carbonateSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 18, 11, 11 >; + using carbonateSystemType = MixedReactionsParameters< double, int, int, 18, 11, 10 >; - constexpr carbonateSystemAllKineticType carbonateSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - constexpr carbonateSystemAllEquilibriumType carbonateSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - constexpr carbonateSystemType carbonateSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + constexpr carbonateSystemAllKineticType carbonateSystemAllKinetic( carbonate::stoichMatrix, carbonate::equilibriumConstants, carbonate::forwardRates, carbonate::reverseRates ); + constexpr carbonateSystemAllEquilibriumType carbonateSystemAllEquilibrium( carbonate::stoichMatrix, carbonate::equilibriumConstants, carbonate::forwardRates, carbonate::reverseRates ); + constexpr carbonateSystemType carbonateSystem( carbonate::stoichMatrix, carbonate::equilibriumConstants, carbonate::forwardRates, carbonate::reverseRates ); // ################################## Ultramafic rxn set ################################## +namespace ultramafics +{ -//using ultramaficSystemAllKineticType = MixedReactionsParameters< double, int, int, 25, 21, 0 >; -//using ultramaficSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 25, 21, 21 >; -using ultramaficSystemType = MixedReactionsParameters< double, int, int, 25, 21, 16 >; - -// constexpr CArrayWrapper stoichMatrix = -// { // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) -// { -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 }, // OH- + H+ = H2O -// { 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 }, // CO2(aq) + H2O = HCO3- + H+ -// { 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 }, // CO3-- + H+ = HCO3- -// { 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 2 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O -// { 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -4 0 4 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O -// { 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 1 0 }, // MgOH+ + H+ = Mg++ + H2O -// { 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 2 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- -// { 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- -// { 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 }, // MgHCO3+ = Mg++ + HCO3- -// { 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O -// { 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -2 0 1 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O -// { 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 -1 0 1 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 -2 0 0 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 -4 0 0 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 -2 0 0 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 -4 0 2 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 -1 1 1 0 }, // MgCO3 + H+ = Mg++ + HCO3- -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 1 }, // SiO2 = SiO2(aq) -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 -6 0 3 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O -// { 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -2 0 1 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O -// }; +constexpr CArrayWrapper stoichMatrix = +{ // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) + { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, // OH- + H+ = H2O + { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, // CO2(aq) + H2O = HCO3- + H+ + { 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0 }, // CO3-- + H+ = HCO3- + { 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 2, 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O + { 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 4, 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, -1, 0, 1, 0 }, // MgOH+ + H+ = Mg++ + H2O + { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 2, 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, 0 }, // MgCO3 + H+ = Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, // MgHCO3+ = Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, -4, 0, 0, 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -2, 0, 0, 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -4, 0, 2, 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 1, 1, 0 }, // MgCO3 + H+ = Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1 }, // SiO2 = SiO2(aq) + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -6, 0, 3, 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, 0, 1, 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; // 2Mg2SiO4 + 3H2O → Mg3Si2O5(OH)4 + Mg(OH)2 Serpentinization reaction -// constexpr CArrayWrapper equilibriumConstants = -// { -// 9.89E+13, // OH- + H+ = H2O -// 4.42E-07, // CO2(aq) + H2O = HCO3- + H+ -// 2.23E+10, // CO3-- + H+ = HCO3- -// 2.32E+13, // Mg2OH+++ + H+ = 2Mg++ + H2O -// 4.47E+39, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O -// 6.18E+11, // MgOH+ + H+ = Mg++ + H2O -// 7.66E+06, // Mg2CO3++ + H+ = 2Mg++ + HCO3- -// 2.67E+07, // MgCO3 + H+ = Mg++ + HCO3- -// 9.77E-02, // MgHCO3+ = Mg++ + HCO3- -// 3.45E+14, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O -// 9.49E+16, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O -// 1.96E+08, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O -// 8.08E+22, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O -// 6.44E+09, // H3SiO4- + H+ = SiO2(aq) + 2H2O -// 5.39E+35, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O -// 2.72E+13, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O -// 1.40E+28, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O -// 2.73E+02, // MgCO3 + H+ = Mg++ + HCO3- -// 1.93E-03, // SiO2 = SiO2(aq) -// 3.54E+31, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O -// 2.75E+16 // Mg(OH)2 + 2H+ = Mg++ + 2H2O -// }; +constexpr CArrayWrapper equilibriumConstants = + { + 9.89E+13, // OH- + H+ = H2O + 4.42E-07, // CO2(aq) + H2O = HCO3- + H+ + 2.23E+10, // CO3-- + H+ = HCO3- + 2.32E+13, // Mg2OH+++ + H+ = 2Mg++ + H2O + 4.47E+39, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + 6.18E+11, // MgOH+ + H+ = Mg++ + H2O + 7.66E+06, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + 2.67E+07, // MgCO3 + H+ = Mg++ + HCO3- + 9.77E-02, // MgHCO3+ = Mg++ + HCO3- + 3.45E+14, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + 9.49E+16, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + 1.96E+08, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + 8.08E+22, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + 6.44E+09, // H3SiO4- + H+ = SiO2(aq) + 2H2O + 5.39E+35, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + 2.72E+13, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + 1.40E+28, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + 2.73E+02, // MgCO3 + H+ = Mg++ + HCO3- + 1.93E-03, // SiO2 = SiO2(aq) + 3.54E+31, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + 2.75E+16 // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; -// constexpr CArrayWrapper forwardRates = -// { -// 1.00E+10, // OH- + H+ = H2O -// 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ -// 1.00E+10, // CO3-- + H+ = HCO3- -// 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O -// 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O -// 1.00E+10, // MgOH+ + H+ = Mg++ + H2O -// 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- -// 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- -// 1.00E+10, // MgHCO3+ = Mg++ + HCO3- -// 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O -// 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O -// 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O -// 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O -// 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O -// 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O -// 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O -// 2.29E-11, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O -// 4.57E-10, // MgCO3 + H+ = Mg++ + HCO3- -// 1.70E-13, // SiO2 = SiO2(aq) -// 1.00E-12, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O -// 5.75E-09 // Mg(OH)2 + 2H+ = Mg++ + 2H2O -// }; +constexpr CArrayWrapper forwardRates = + { + 1.00E+10, // OH- + H+ = H2O + 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ + 1.00E+10, // CO3-- + H+ = HCO3- + 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O + 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + 1.00E+10, // MgOH+ + H+ = Mg++ + H2O + 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- + 1.00E+10, // MgHCO3+ = Mg++ + HCO3- + 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O + 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + 2.29E-11, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + 4.57E-10, // MgCO3 + H+ = Mg++ + HCO3- + 1.70E-13, // SiO2 = SiO2(aq) + 1.00E-12, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + 5.75E-09 // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; -// constexpr CArrayWrapper reverseRates = -// { -// 1.00E+10, // OH- + H+ = H2O -// 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ -// 1.00E+10, // CO3-- + H+ = HCO3- -// 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O -// 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O -// 1.00E+10, // MgOH+ + H+ = Mg++ + H2O -// 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- -// 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- -// 1.00E+10, // MgHCO3+ = Mg++ + HCO3- -// 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O -// 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O -// 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O -// 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O -// 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O -// 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O -// 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O -// 1.65E-39, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O -// 1.67E-12, // MgCO3 + H+ = Mg++ + HCO3- -// 8.78E-11, // SiO2 = SiO2(aq) -// 2.83E-44, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O -// 2.10E-25 // Mg(OH)2 + 2H+ = Mg++ + 2H2O -// }; +constexpr CArrayWrapper reverseRates = + { + 1.00E+10, // OH- + H+ = H2O + 1.00E+10, // CO2(aq) + H2O = HCO3- + H+ + 1.00E+10, // CO3-- + H+ = HCO3- + 1.00E+10, // Mg2OH+++ + H+ = 2Mg++ + H2O + 1.00E+10, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + 1.00E+10, // MgOH+ + H+ = Mg++ + H2O + 1.00E+10, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + 1.00E+10, // MgCO3 + H+ = Mg++ + HCO3- + 1.00E+10, // MgHCO3+ = Mg++ + HCO3- + 1.00E+10, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + 1.00E+10, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + 1.00E+10, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + 1.00E+10, // H3SiO4- + H+ = SiO2(aq) + 2H2O + 1.00E+10, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + 1.00E+10, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + 1.65E-39, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + 1.67E-12, // MgCO3 + H+ = Mg++ + HCO3- + 8.78E-11, // SiO2 = SiO2(aq) + 2.83E-44, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + 2.10E-25 // Mg(OH)2 + 2H+ = Mg++ + 2H2O + }; +}; + + using ultramaficSystemAllKineticType = MixedReactionsParameters< double, int, int, 25, 21, 0 >; + using ultramaficSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 25, 21, 21 >; + using ultramaficSystemType = MixedReactionsParameters< double, int, int, 25, 21, 16 >; - //constexpr ultramaficSystemAllKineticType ultramaficSystemAllKinetic( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - //constexpr ultramaficSystemAllEquilibriumType ultramaficSystemAllEquilibrium( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); - // constexpr ultramaficSystemType ultramaficSystem( stoichMatrix, equilibriumConstants, forwardRates, reverseRates ); + constexpr ultramaficSystemAllKineticType ultramaficSystemAllKinetic( ultramafics::stoichMatrix, ultramafics::equilibriumConstants, ultramafics::forwardRates, ultramafics::reverseRates ); + constexpr ultramaficSystemAllEquilibriumType ultramaficSystemAllEquilibrium( ultramafics::stoichMatrix, ultramafics::equilibriumConstants, ultramafics::forwardRates, ultramafics::reverseRates ); + constexpr ultramaficSystemType ultramaficSystem( ultramafics::stoichMatrix, ultramafics::equilibriumConstants, ultramafics::forwardRates, ultramafics::reverseRates ); } // namespace bulkGeneric } // namespace hpcReact From 462beae6e50569ed1097f662e31a7d3c1150d5ff Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Thu, 29 May 2025 15:43:25 -0700 Subject: [PATCH 17/28] forgotten comma --- .../bulkGeneric/ParametersPredefined.hpp | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index 64109a2..cf37159 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -152,28 +152,28 @@ namespace ultramafics { constexpr CArrayWrapper stoichMatrix = -{ // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) - { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, // OH- + H+ = H2O - { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, // CO2(aq) + H2O = HCO3- + H+ - { 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0 }, // CO3-- + H+ = HCO3- - { 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 2, 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O - { 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 4, 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O - { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0, 0, -1, 0, 1, 0 }, // MgOH+ + H+ = Mg++ + H2O - { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 2, 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- - { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, 0 }, // MgCO3 + H+ = Mg++ + HCO3- - { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, // MgHCO3+ = Mg++ + HCO3- - { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, -4, 0, 0, 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -2, 0, 0, 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -4, 0, 2, 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 1, 1, 0 }, // MgCO3 + H+ = Mg++ + HCO3- - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1 }, // SiO2 = SiO2(aq) - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -6, 0, 3, 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, 0, 1, 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O +{ // OH- CO2(aq) CO3-- Mg2OH+++ Mg4(OH)++++ MgOH+ Mg2CO3++ MgCO3(aq) MgHCO3+ Mg(H3SiO4)2 MgH2SiO4 MgH3SiO4+ H2SiO4-- H3SiO4- H4(H2SiO4)---- H6(H2SiO4)-- Mg2SiO4 MgCO3 SiO2 Mg3Si2O5(OH)4 Mg(OH)2 H+ HCO3- Mg++ SiO2(aq) + { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0 }, // OH- + H+ = H2O + { 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 }, // CO2(aq) + H2O = HCO3- + H+ + { 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0 }, // CO3-- + H+ = HCO3- + { 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 2, 0 }, // Mg2OH+++ + H+ = 2Mg++ + H2O + { 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -4, 0, 4, 0 }, // Mg4(OH)++++ + 4H+ = 4Mg++ + 4H2O + { 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0 }, // MgOH+ + H+ = Mg++ + H2O + { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 2, 0 }, // Mg2CO3++ + H+ = 2Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 1, 1, 0 }, // MgCO3 + H+ = Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, // MgHCO3+ = Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 1 }, // Mg(H3SiO4)2 + 2H+ = Mg++ + SiO2(aq) + 4H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 1, 1 }, // MgH2SiO4 + 2H+ = Mg++ + SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 1 }, // MgH3SiO4+ + H+ = Mg++ + SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, -2, 0, 0, 1 }, // H2SiO4-- + 2H+ = SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 1 }, // H3SiO4- + H+ = SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, -4, 0, 0, 4 }, // H4(H2SiO4)---- + 4H+ = 4SiO2(aq) + 8H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -2, 0, 0, 4 }, // H6(H2SiO4)-- + 2H+ = 4SiO2 + 8H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -4, 0, 2, 1 }, // Mg2SiO4 + 4H+ = 2Mg++ + SiO2(aq) + 2H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 1, 1, 0 }, // MgCO3 + H+ = Mg++ + HCO3- + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1 }, // SiO2 = SiO2(aq) + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -6, 0, 3, 2 }, // Mg3Si2O5(OH)4 + 6H+ = 3Mg++ + 2SiO2(aq) + 5H2O + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, 0, 1, 0 } // Mg(OH)2 + 2H+ = Mg++ + 2H2O }; // 2Mg2SiO4 + 3H2O → Mg3Si2O5(OH)4 + Mg(OH)2 Serpentinization reaction From 8dbbaac2f90318615de4005fbc628a6b7dbf6bce Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Mon, 2 Jun 2025 09:38:56 -0700 Subject: [PATCH 18/28] wip: add mixedReactions unit test. --- src/common/nonlinearSolvers.hpp | 107 +++++++++++ .../MixedEquilibriumKineticReactions.hpp | 42 ++-- .../MixedEquilibriumKineticReactions_impl.hpp | 40 ++-- .../bulkGeneric/SpeciesUtilities.hpp | 9 +- .../bulkGeneric/unitTests/CMakeLists.txt | 1 + .../unitTests/testMixedReactions.cpp | 179 ++++++++++++++++++ 6 files changed, 336 insertions(+), 42 deletions(-) create mode 100644 src/common/nonlinearSolvers.hpp create mode 100644 src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp diff --git a/src/common/nonlinearSolvers.hpp b/src/common/nonlinearSolvers.hpp new file mode 100644 index 0000000..cbec950 --- /dev/null +++ b/src/common/nonlinearSolvers.hpp @@ -0,0 +1,107 @@ +#pragma once + +#include "macros.hpp" +#include "DirectSystemSolve.hpp" +#include + +namespace hpcReact +{ + +namespace nonlinearSolvers +{ + +namespace internal +{ + +template< int N > +HPCREACT_HOST_DEVICE +double norm( double const (&r)[N]) +{ + double sum = 0.0; + for ( int i = 0; i < N; ++i ) sum += r[i] * r[i]; + return ::sqrt( sum ); +} + + +template< int N > +HPCREACT_HOST_DEVICE +void add(double (&x)[N], double const (&dx)[N]) +{ + for (int i = 0; i < N; ++i) x[i] += dx[i]; +} + +template< int N > +HPCREACT_HOST_DEVICE +void scale( double (&x)[N], double const value ) +{ + for (int i = 0; i < N; ++i) x[i] *= value; +} + +} + +template< int N, + typename REAL_TYPE, + typename ResidualFunc, + typename JacobianFunc > +HPCREACT_HOST_DEVICE +void newtonRaphson( REAL_TYPE (&x)[N], + ResidualFunc computeResidual, + JacobianFunc computeJacobian, + int maxIters = 25, + double tol = 1e-10 ) +{ + REAL_TYPE residual[N]; + REAL_TYPE dx[N]; + REAL_TYPE jacobian[N][N]; + + for ( int iter = 0; iter < maxIters; ++iter ) + { + computeResidual( x, residual ); + + if ( internal::norm< N >( residual ) < tol ) return; + + computeJacobian( x, jacobian ); + + solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); + + internal::add< N >( x, dx ); + } +} + +template< int N, + typename REAL_TYPE, + typename FUNCTION_TYPE > +HPCREACT_HOST_DEVICE +bool newtonRaphson( REAL_TYPE (&x)[N], + FUNCTION_TYPE computeResidualAndJacobian, + int maxIters = 25, + double tol = 1e-10 ) +{ + REAL_TYPE residual[N]; + REAL_TYPE dx[N]; + REAL_TYPE jacobian[N][N]; + + for ( int iter = 0; iter < maxIters; ++iter ) + { + computeResidualAndJacobian( x, residual, jacobian ); + + double const norm = internal::norm< N >( residual ); + + printf( "--Iter %d: Residual norm = %.12e\n", iter, norm ); + + if ( norm < tol ) { + printf( "--Converged.\n" ); + return true; + } + internal::scale( residual, -1.0); + solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); + internal::add< N >( x, dx ); + } + + printf( "--Newton solver error: Max iterations reached without convergence.\n" ); + + return false; +} + +} +} \ No newline at end of file diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp index ae1b77f..38e8041 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -44,19 +44,22 @@ class MixedEquilibriumKineticReactions template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, - typename ARRAY_1D, - typename ARRAY_2D > + typename ARRAY_1D_PRIMARY, + typename ARRAY_1D_SECONDARY, + typename ARRAY_1D_KINETIC, + typename ARRAY_2D_PRIMARY, + typename ARRAY_2D_KINETIC > static HPCREACT_HOST_DEVICE inline void updateMixedSystem( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D & logSecondarySpeciesConcentrations, - ARRAY_1D & aggregatePrimarySpeciesConcentrations, - ARRAY_2D & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, - ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations, - ARRAY_1D & aggregateSpeciesRates, - ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) + ARRAY_1D_SECONDARY & logSecondarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregatePrimarySpeciesConcentrations, + ARRAY_2D_PRIMARY & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D_KINETIC & reactionRates, + ARRAY_2D_KINETIC & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregateSpeciesRates, + ARRAY_2D_PRIMARY & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) { updateMixedSystem_impl( temperature, params, @@ -124,19 +127,22 @@ class MixedEquilibriumKineticReactions template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, - typename ARRAY_1D, - typename ARRAY_2D > + typename ARRAY_1D_PRIMARY, + typename ARRAY_1D_SECONDARY, + typename ARRAY_1D_KINETIC, + typename ARRAY_2D_PRIMARY, + typename ARRAY_2D_KINETIC > static HPCREACT_HOST_DEVICE void updateMixedSystem_impl( RealType const & temperature, PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D & logSecondarySpeciesConcentrations, - ARRAY_1D & aggregatePrimarySpeciesConcentrations, - ARRAY_2D & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, - ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations, - ARRAY_1D & aggregateSpeciesRates, - ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); + ARRAY_1D_SECONDARY & logSecondarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregatePrimarySpeciesConcentrations, + ARRAY_2D_PRIMARY & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D_KINETIC & reactionRates, + ARRAY_2D_KINETIC & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregateSpeciesRates, + ARRAY_2D_PRIMARY & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp index ca4eed2..52a31c5 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -21,33 +21,33 @@ template< typename REAL_TYPE, typename INDEX_TYPE, bool LOGE_CONCENTRATION > template< typename PARAMS_DATA, - typename ARRAY_1D_TO_CONST, - typename ARRAY_1D, - typename ARRAY_2D > + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_PRIMARY, + typename ARRAY_1D_SECONDARY, + typename ARRAY_1D_KINETIC, + typename ARRAY_2D_PRIMARY, + typename ARRAY_2D_KINETIC > HPCREACT_HOST_DEVICE inline void MixedEquilibriumKineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, LOGE_CONCENTRATION >::updateMixedSystem_impl( RealType const & temperature, - PARAMS_DATA const & params, - ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D & logSecondarySpeciesConcentrations, - ARRAY_1D & aggregatePrimarySpeciesConcentrations, - ARRAY_2D & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, - ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations, - ARRAY_1D & aggregateSpeciesRates, - ARRAY_2D & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_SECONDARY & logSecondarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregatePrimarySpeciesConcentrations, + ARRAY_2D_PRIMARY & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D_KINETIC & reactionRates, + ARRAY_2D_KINETIC & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregateSpeciesRates, + ARRAY_2D_PRIMARY & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) { - // constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); - // constexpr IntType numSecondarySpecies = PARAMS_DATA::numReactions(); - // constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); // 1. Compute new aggregate species from primary species calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, - INT_TYPE, - INDEX_TYPE >( params, + INT_TYPE, + INDEX_TYPE >( params.equilibriumReactionsParameters(), logPrimarySpeciesConcentrations, logSecondarySpeciesConcentrations, aggregatePrimarySpeciesConcentrations, @@ -105,9 +105,9 @@ MixedEquilibriumKineticReactions< REAL_TYPE, { logSpeciesConcentration[i] = logSecondarySpeciesConcentrations[i]; } - for ( IntType i = 0; i < numPrimarySpecies; ++i ) + for ( IntType i = numSecondarySpecies; i < numSpecies; ++i ) { - logSpeciesConcentration[i+numSecondarySpecies] = logPrimarySpeciesConcentrations[i]; + logSpeciesConcentration[i] = logPrimarySpeciesConcentrations[i]; } CArrayWrapper< RealType, numKineticReactions, numSpecies > reactionRatesDerivatives; @@ -159,7 +159,7 @@ MixedEquilibriumKineticReactions< REAL_TYPE, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ) { - GEOS_UNUSED_VAR( speciesConcentration ); + HPCREACT_UNUSED_VAR( speciesConcentration ); // constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); diff --git a/src/reactions/bulkGeneric/SpeciesUtilities.hpp b/src/reactions/bulkGeneric/SpeciesUtilities.hpp index cd20f20..1b36e6f 100644 --- a/src/reactions/bulkGeneric/SpeciesUtilities.hpp +++ b/src/reactions/bulkGeneric/SpeciesUtilities.hpp @@ -94,14 +94,15 @@ template< typename REAL_TYPE, typename INDEX_TYPE, typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, - typename ARRAY_1D, + typename ARRAY_1D_PRIMARY, + typename ARRAY_1D_SECONDARY, typename ARRAY_2D > HPCREACT_HOST_DEVICE inline -void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA & params, +void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D & logSecondarySpeciesConcentrations, - ARRAY_1D & aggregatePrimarySpeciesConcentrations, + ARRAY_1D_SECONDARY & logSecondarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregatePrimarySpeciesConcentrations, ARRAY_2D & dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations ) { constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); diff --git a/src/reactions/bulkGeneric/unitTests/CMakeLists.txt b/src/reactions/bulkGeneric/unitTests/CMakeLists.txt index 6a30651..eabb67e 100644 --- a/src/reactions/bulkGeneric/unitTests/CMakeLists.txt +++ b/src/reactions/bulkGeneric/unitTests/CMakeLists.txt @@ -3,6 +3,7 @@ set( testSourceFiles testEquilibriumReactions.cpp testKineticReactions.cpp testSpeciesUtilities.cpp + testMixedReactions.cpp ) set( dependencyList hpcReact gtest ) diff --git a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp new file mode 100644 index 0000000..97ed8d7 --- /dev/null +++ b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp @@ -0,0 +1,179 @@ + +#include "../MixedEquilibriumKineticReactions.hpp" +#include "../EquilibriumReactions.hpp" +#include "../ParametersPredefined.hpp" +#include "common/macros.hpp" +#include "common/printers.hpp" +#include "common/nonlinearSolvers.hpp" + +#include + + +using namespace hpcReact; +using namespace hpcReact::bulkGeneric; + +template< typename REAL_TYPE > +REAL_TYPE tolerance( REAL_TYPE const a, REAL_TYPE const b, REAL_TYPE const ndigits ) +{ + return std::numeric_limits< double >::epsilon() * std::max( fabs( a ), fabs( b ) ) * pow( 10, ndigits ); +} + +//****************************************************************************** +template< typename REAL_TYPE, + bool LOGE_CONCENTRATION, + typename PARAMS_DATA > +void timeStepTest( PARAMS_DATA const & params, + REAL_TYPE const dt, + int const numSteps, + REAL_TYPE const (&initialSpeciesConcentration)[PARAMS_DATA::numPrimarySpecies()], + REAL_TYPE const (&expectedSpeciesConcentrations)[PARAMS_DATA::numPrimarySpecies()] ) +{ + HPCREACT_UNUSED_VAR( expectedSpeciesConcentrations ); + + using MixedReactionsType = MixedEquilibriumKineticReactions< REAL_TYPE, + int, + int, + LOGE_CONCENTRATION >; + using EquilibriumReactionsType = EquilibriumReactions< REAL_TYPE, + int, + int >; + + + // constexpr int numSpecies = PARAMS_DATA::numSpecies(); + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr int numKineticReactions = PARAMS_DATA::numKineticReactions(); + + // define variables + double const temperature = 298.15; + REAL_TYPE logPrimarySpeciesConcentration[numPrimarySpecies]; + // must use CArrayWrapper to ensure correct capture in the lambda functions + CArrayWrapper< REAL_TYPE, numSecondarySpecies > logSecondarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregatePrimarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregatePrimarySpeciesConcentration_n; + CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numKineticReactions > reactionRates; + CArrayWrapper< REAL_TYPE, numKineticReactions, numPrimarySpecies > dReactionRates_dlogPrimarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregateSpeciesRates; + CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregateSpeciesRates_dlogPrimarySpeciesConcentration; + + + // Initialize species concentrations + for( int i = 0; i < numPrimarySpecies; ++i ) + { + logPrimarySpeciesConcentration[i] = ::log( initialSpeciesConcentration[i] ); + aggregatePrimarySpeciesConcentration[i] = initialSpeciesConcentration[i]; + } + + printf( "Initial equilibrium solve\n"); + EquilibriumReactionsType::enforceEquilibrium_Aggregate( temperature, + carbonateSystem.equilibriumReactionsParameters(), + logPrimarySpeciesConcentration, + logPrimarySpeciesConcentration ); + + for( int i = 0; i < numPrimarySpecies; ++i ) + { + std::cout << "aggregatePrimarySpeciesConcentration[" << i << "] = " + << aggregatePrimarySpeciesConcentration[i] << ", "; + + std::cout << "logPrimarySpeciesConcentration[" << i << "] = " + << logPrimarySpeciesConcentration[i] << std::endl; + } + + /// Time step loop + double time = 0.0; + for( int t = 0; t < numSteps; ++t ) + { + printf( "Timestep %d, Time = %.6e\n", t, time ); + + for( int i=0; i < numPrimarySpecies; ++i ) + { + aggregatePrimarySpeciesConcentration_n[i] = aggregatePrimarySpeciesConcentration[i]; + } + + auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&logPrimarySpeciesConcentration)[numPrimarySpecies], + REAL_TYPE (&r)[numPrimarySpecies], + REAL_TYPE (&J)[numPrimarySpecies][numPrimarySpecies] ) + { + MixedReactionsType::updateMixedSystem( temperature, + params, + logPrimarySpeciesConcentration, + logSecondarySpeciesConcentration, + aggregatePrimarySpeciesConcentration, + dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration, + reactionRates, + dReactionRates_dlogPrimarySpeciesConcentration, + aggregateSpeciesRates, + dAggregateSpeciesRates_dlogPrimarySpeciesConcentration ); + + for( int i = 0; i < numPrimarySpecies; ++i ) + { + std::cout << "aggregateSpeciesRates[" << i << "] = " + << aggregateSpeciesRates[i] << ", "; + std::cout << std::endl; + for ( int j = 0; j < numPrimarySpecies; ++j ) + { + std::cout << "dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[" << i << "][" << j << "] = " + << dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] << ", "; + } + std::cout << std::endl; + } + + for ( int i = 0; i < numPrimarySpecies; ++i ) + { + r[i] = ( aggregatePrimarySpeciesConcentration[i] - aggregatePrimarySpeciesConcentration_n[i] ) - aggregateSpeciesRates[i] * dt; + for ( int j = 0; j < numPrimarySpecies; ++j ) + { + J[i][j] = dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] - dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[i][j] * dt; + } + } + }; + + nonlinearSolvers::newtonRaphson< numPrimarySpecies >( logPrimarySpeciesConcentration, computeResidualAndJacobian ); + + time += dt; + } +} + +TEST( testMixedReactions, testTimeStep_carbonateSystem ) +{ + constexpr int numPrimarySpecies = carbonateSystemType::numPrimarySpecies(); + + double const initialSpeciesConcentration[numPrimarySpecies] = + { + 3.76e-3, // + 3.76e-1, // H+ + 3.76e-1, // HCO3- + 3.87e-2, // Ca+2 + 3.21e-2, // SO4-2 + 1.89, // Cl- + 1.65e-2, // Mg+2 + 1.09 // Na+1 + }; + + double const expectedSpeciesConcentrations[numPrimarySpecies] = + { + 1.0e-3, // + 3.76e-1, // H+ + 3.76e-1, // HCO3- + 3.87e-2, // Ca+2 + 3.21e-2, // SO4-2 + 1.89, // Cl- + 1.65e-2, // Mg+2 + 1.09 // Na+1 + }; + + timeStepTest< double, true >( carbonateSystem, + 0.2, + 1, + initialSpeciesConcentration, + expectedSpeciesConcentrations ); + +} + +int main( int argc, char * * argv ) +{ + ::testing::InitGoogleTest( &argc, argv ); + int const result = RUN_ALL_TESTS(); + return result; +} From 76fceb72cac12910f4652e88f374b6776c8ef6fb Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Tue, 3 Jun 2025 10:00:00 -0700 Subject: [PATCH 19/28] mixed reactions test working. --- src/common/nonlinearSolvers.hpp | 51 +++++++++++++++++-- .../MixedEquilibriumKineticReactions_impl.hpp | 18 +++++-- .../bulkGeneric/SpeciesUtilities.hpp | 20 ++++++++ .../unitTests/testMixedReactions.cpp | 35 +++---------- 4 files changed, 87 insertions(+), 37 deletions(-) diff --git a/src/common/nonlinearSolvers.hpp b/src/common/nonlinearSolvers.hpp index cbec950..5098b4b 100644 --- a/src/common/nonlinearSolvers.hpp +++ b/src/common/nonlinearSolvers.hpp @@ -39,6 +39,39 @@ void scale( double (&x)[N], double const value ) } +namespace utils +{ +template< int N > +HPCREACT_HOST_DEVICE +void print(double const (&J)[N][N], double const (&r)[N], double const (&dx)[N]) +{ + printf("=======================\nJacobian matrix:\n=======================\n"); + printf(" RowID ColID Value\n"); for ( int i = 0; i < N; ++i ) + { + for ( int j = 0; j < N; ++j ) + { + printf("%10d%16d%27.16e\n", i, j, J[i][j]); + } + } + + printf("\n=======================\nSystem right-hand side:\n=======================\n"); + printf(" RowID Value\n"); + + for ( int i = 0; i < N; ++i ) + { + printf("%10d%27.16e\n", i, r[i]); + } + + printf("\n=======================\nDelta update vector:\n=======================\n"); + printf(" RowID Value\n"); + + for ( int i = 0; i < N; ++i ) + { + printf("%10d%27.16e\n", i, dx[i]); + } +} +} + template< int N, typename REAL_TYPE, typename ResidualFunc, @@ -74,12 +107,13 @@ template< int N, HPCREACT_HOST_DEVICE bool newtonRaphson( REAL_TYPE (&x)[N], FUNCTION_TYPE computeResidualAndJacobian, - int maxIters = 25, - double tol = 1e-10 ) + int maxIters = 12, + double tol = 1e-10, + bool const do_print = false ) { - REAL_TYPE residual[N]; - REAL_TYPE dx[N]; - REAL_TYPE jacobian[N][N]; + REAL_TYPE residual[N]{}; + REAL_TYPE dx[N]{}; + REAL_TYPE jacobian[N][N]{}; for ( int iter = 0; iter < maxIters; ++iter ) { @@ -94,8 +128,15 @@ bool newtonRaphson( REAL_TYPE (&x)[N], return true; } internal::scale( residual, -1.0); + + if( do_print ) + { + utils::print( jacobian, residual, dx ); + } + solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); internal::add< N >( x, dx ); + } printf( "--Newton solver error: Max iterations reached without convergence.\n" ); diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp index 52a31c5..58f558a 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -101,13 +101,21 @@ MixedEquilibriumKineticReactions< REAL_TYPE, constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); RealType logSpeciesConcentration[numSpecies] {}; - for ( IntType i = 0; i < numSecondarySpecies; ++i ) + for ( INDEX_TYPE i = 0; i < numSecondarySpecies; ++i ) { logSpeciesConcentration[i] = logSecondarySpeciesConcentrations[i]; } - for ( IntType i = numSecondarySpecies; i < numSpecies; ++i ) + for ( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) { - logSpeciesConcentration[i] = logPrimarySpeciesConcentrations[i]; + logSpeciesConcentration[i+numSecondarySpecies] = logPrimarySpeciesConcentrations[i]; + } + + for( INDEX_TYPE i = 0; i < numKineticReactions; ++i ) + { + for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) + { + dReactionRates_dLogPrimarySpeciesConcentrations[i][j] = 0.0; + } } CArrayWrapper< RealType, numKineticReactions, numSpecies > reactionRatesDerivatives; @@ -165,12 +173,12 @@ MixedEquilibriumKineticReactions< REAL_TYPE, constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); - for( IntType i = 0; i < numPrimarySpecies; ++i ) + for( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) { aggregatesRates[i] = 0.0; if constexpr( CALCULATE_DERIVATIVES ) { - for( IntType j = 0; j < numPrimarySpecies; ++j ) + for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) { aggregatesRatesDerivatives( i, j ) = 0.0; } diff --git a/src/reactions/bulkGeneric/SpeciesUtilities.hpp b/src/reactions/bulkGeneric/SpeciesUtilities.hpp index 1b36e6f..86f406b 100644 --- a/src/reactions/bulkGeneric/SpeciesUtilities.hpp +++ b/src/reactions/bulkGeneric/SpeciesUtilities.hpp @@ -30,6 +30,11 @@ void calculateLogSecondarySpeciesConcentration( PARAMS_DATA const & params, constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + for (INDEX_TYPE i = 0; i < numSecondarySpecies; ++i) + { + logSecondarySpeciesConcentrations[i] = 0.0; + } + for( int j=0; j( params, logPrimarySpeciesConcentrations, logSecondarySpeciesConcentrations ); + for( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) + { + for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) + { + dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations[i][j] = 0.0; + } + } for( int i = 0; i < numPrimarySpecies; ++i ) { @@ -154,6 +166,14 @@ void calculateAggregatePrimaryConcentrationsWrtLogC( PARAMS_DATA const & params, REAL_TYPE logSecondarySpeciesConcentrations[numSecondarySpecies] = {0}; + for( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) + { + for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) + { + dAggregatePrimarySpeciesConcentrationsDerivatives_dLogPrimarySpeciesConcentrations[i][j] = 0.0; + } + } + calculateLogSecondarySpeciesConcentration< REAL_TYPE, INT_TYPE, INDEX_TYPE >( params, diff --git a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp index 97ed8d7..4068a97 100644 --- a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp @@ -65,20 +65,11 @@ void timeStepTest( PARAMS_DATA const & params, aggregatePrimarySpeciesConcentration[i] = initialSpeciesConcentration[i]; } - printf( "Initial equilibrium solve\n"); EquilibriumReactionsType::enforceEquilibrium_Aggregate( temperature, carbonateSystem.equilibriumReactionsParameters(), logPrimarySpeciesConcentration, logPrimarySpeciesConcentration ); - - for( int i = 0; i < numPrimarySpecies; ++i ) - { - std::cout << "aggregatePrimarySpeciesConcentration[" << i << "] = " - << aggregatePrimarySpeciesConcentration[i] << ", "; - - std::cout << "logPrimarySpeciesConcentration[" << i << "] = " - << logPrimarySpeciesConcentration[i] << std::endl; - } + /// Time step loop double time = 0.0; @@ -106,19 +97,7 @@ void timeStepTest( PARAMS_DATA const & params, aggregateSpeciesRates, dAggregateSpeciesRates_dlogPrimarySpeciesConcentration ); - for( int i = 0; i < numPrimarySpecies; ++i ) - { - std::cout << "aggregateSpeciesRates[" << i << "] = " - << aggregateSpeciesRates[i] << ", "; - std::cout << std::endl; - for ( int j = 0; j < numPrimarySpecies; ++j ) - { - std::cout << "dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[" << i << "][" << j << "] = " - << dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] << ", "; - } - std::cout << std::endl; - } - + for ( int i = 0; i < numPrimarySpecies; ++i ) { r[i] = ( aggregatePrimarySpeciesConcentration[i] - aggregatePrimarySpeciesConcentration_n[i] ) - aggregateSpeciesRates[i] * dt; @@ -126,13 +105,15 @@ void timeStepTest( PARAMS_DATA const & params, { J[i][j] = dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] - dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[i][j] * dt; } - } + } }; nonlinearSolvers::newtonRaphson< numPrimarySpecies >( logPrimarySpeciesConcentration, computeResidualAndJacobian ); time += dt; } + + EXPECT_NEAR( reactionRatesDerivatives( r, i ), expectedReactionRatesDerivatives[r][i], std::max( magScale, fabs( expectedReactionRatesDerivatives[r][i] ) ) * 1.0e-8 ); } TEST( testMixedReactions, testTimeStep_carbonateSystem ) @@ -141,7 +122,7 @@ TEST( testMixedReactions, testTimeStep_carbonateSystem ) double const initialSpeciesConcentration[numPrimarySpecies] = { - 3.76e-3, // + 3.76e-3, // CaCO3 3.76e-1, // H+ 3.76e-1, // HCO3- 3.87e-2, // Ca+2 @@ -153,7 +134,7 @@ TEST( testMixedReactions, testTimeStep_carbonateSystem ) double const expectedSpeciesConcentrations[numPrimarySpecies] = { - 1.0e-3, // + 1.0e-3, // CaCO3 3.76e-1, // H+ 3.76e-1, // HCO3- 3.87e-2, // Ca+2 @@ -165,7 +146,7 @@ TEST( testMixedReactions, testTimeStep_carbonateSystem ) timeStepTest< double, true >( carbonateSystem, 0.2, - 1, + 10, initialSpeciesConcentration, expectedSpeciesConcentrations ); From 56211c5f6f54c328fe09cf9b3476dbf3c13ab1c6 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Tue, 3 Jun 2025 11:11:19 -0700 Subject: [PATCH 20/28] added pmpl --- .../unitTests/testMixedReactions.cpp | 174 ++++++++++-------- 1 file changed, 96 insertions(+), 78 deletions(-) diff --git a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp index 4068a97..bef7efe 100644 --- a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp @@ -5,6 +5,7 @@ #include "common/macros.hpp" #include "common/printers.hpp" #include "common/nonlinearSolvers.hpp" +#include "common/pmpl.hpp" #include @@ -30,97 +31,114 @@ void timeStepTest( PARAMS_DATA const & params, { HPCREACT_UNUSED_VAR( expectedSpeciesConcentrations ); - using MixedReactionsType = MixedEquilibriumKineticReactions< REAL_TYPE, + CArrayWrapper< REAL_TYPE, PARAMS_DATA::numPrimarySpecies() > primarySpeciesConcentration; + + for( int i = 0; i < PARAMS_DATA::numPrimarySpecies(); ++i ) + { + primarySpeciesConcentration[i] = initialSpeciesConcentration[i]; + } + + pmpl::genericKernelWrapper( PARAMS_DATA::numPrimarySpecies(), + primarySpeciesConcentration.data, + [=] HPCREACT_HOST_DEVICE ( auto * speciesConcentration ) + { + using MixedReactionsType = MixedEquilibriumKineticReactions< REAL_TYPE, int, int, LOGE_CONCENTRATION >; - using EquilibriumReactionsType = EquilibriumReactions< REAL_TYPE, - int, - int >; - - - // constexpr int numSpecies = PARAMS_DATA::numSpecies(); - constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); - constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); - constexpr int numKineticReactions = PARAMS_DATA::numKineticReactions(); + using EquilibriumReactionsType = EquilibriumReactions< REAL_TYPE, + int, + int >; + + // constexpr int numSpecies = PARAMS_DATA::numSpecies(); + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr int numKineticReactions = PARAMS_DATA::numKineticReactions(); - // define variables - double const temperature = 298.15; - REAL_TYPE logPrimarySpeciesConcentration[numPrimarySpecies]; - // must use CArrayWrapper to ensure correct capture in the lambda functions - CArrayWrapper< REAL_TYPE, numSecondarySpecies > logSecondarySpeciesConcentration; - CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregatePrimarySpeciesConcentration; - CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregatePrimarySpeciesConcentration_n; - CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration; - CArrayWrapper< REAL_TYPE, numKineticReactions > reactionRates; - CArrayWrapper< REAL_TYPE, numKineticReactions, numPrimarySpecies > dReactionRates_dlogPrimarySpeciesConcentration; - CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregateSpeciesRates; - CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregateSpeciesRates_dlogPrimarySpeciesConcentration; - - - // Initialize species concentrations - for( int i = 0; i < numPrimarySpecies; ++i ) - { - logPrimarySpeciesConcentration[i] = ::log( initialSpeciesConcentration[i] ); - aggregatePrimarySpeciesConcentration[i] = initialSpeciesConcentration[i]; - } - - EquilibriumReactionsType::enforceEquilibrium_Aggregate( temperature, - carbonateSystem.equilibriumReactionsParameters(), - logPrimarySpeciesConcentration, - logPrimarySpeciesConcentration ); - + // define variables + double const temperature = 298.15; + REAL_TYPE logPrimarySpeciesConcentration[numPrimarySpecies]; + // must use CArrayWrapper to ensure correct capture in the lambda functions + CArrayWrapper< REAL_TYPE, numSecondarySpecies > logSecondarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregatePrimarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregatePrimarySpeciesConcentration_n; + CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numKineticReactions > reactionRates; + CArrayWrapper< REAL_TYPE, numKineticReactions, numPrimarySpecies > dReactionRates_dlogPrimarySpeciesConcentration; + CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregateSpeciesRates; + CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregateSpeciesRates_dlogPrimarySpeciesConcentration; - /// Time step loop - double time = 0.0; - for( int t = 0; t < numSteps; ++t ) - { - printf( "Timestep %d, Time = %.6e\n", t, time ); - - for( int i=0; i < numPrimarySpecies; ++i ) + // Initialize species concentrations + for( int i = 0; i < numPrimarySpecies; ++i ) { - aggregatePrimarySpeciesConcentration_n[i] = aggregatePrimarySpeciesConcentration[i]; + logPrimarySpeciesConcentration[i] = ::log( speciesConcentration[i] ); + aggregatePrimarySpeciesConcentration[i] = speciesConcentration[i]; } + + EquilibriumReactionsType::enforceEquilibrium_Aggregate( temperature, + carbonateSystem.equilibriumReactionsParameters(), + logPrimarySpeciesConcentration, + logPrimarySpeciesConcentration ); + + /// Time step loop + double time = 0.0; + for( int t = 0; t < numSteps; ++t ) + { + printf( "Timestep %d, Time = %.6e\n", t, time ); - auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&logPrimarySpeciesConcentration)[numPrimarySpecies], + for( int i=0; i < numPrimarySpecies; ++i ) + { + aggregatePrimarySpeciesConcentration_n[i] = aggregatePrimarySpeciesConcentration[i]; + } + + auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&logPrimarySpeciesConcentration)[numPrimarySpecies], REAL_TYPE (&r)[numPrimarySpecies], REAL_TYPE (&J)[numPrimarySpecies][numPrimarySpecies] ) - { - MixedReactionsType::updateMixedSystem( temperature, - params, - logPrimarySpeciesConcentration, - logSecondarySpeciesConcentration, - aggregatePrimarySpeciesConcentration, - dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration, - reactionRates, - dReactionRates_dlogPrimarySpeciesConcentration, - aggregateSpeciesRates, - dAggregateSpeciesRates_dlogPrimarySpeciesConcentration ); + { + MixedReactionsType::updateMixedSystem( temperature, + params, + logPrimarySpeciesConcentration, + logSecondarySpeciesConcentration, + aggregatePrimarySpeciesConcentration, + dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration, + reactionRates, + dReactionRates_dlogPrimarySpeciesConcentration, + aggregateSpeciesRates, + dAggregateSpeciesRates_dlogPrimarySpeciesConcentration ); - for ( int i = 0; i < numPrimarySpecies; ++i ) - { - r[i] = ( aggregatePrimarySpeciesConcentration[i] - aggregatePrimarySpeciesConcentration_n[i] ) - aggregateSpeciesRates[i] * dt; - for ( int j = 0; j < numPrimarySpecies; ++j ) + for ( int i = 0; i < numPrimarySpecies; ++i ) { - J[i][j] = dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] - dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[i][j] * dt; - } - } - }; + r[i] = ( aggregatePrimarySpeciesConcentration[i] - aggregatePrimarySpeciesConcentration_n[i] ) - aggregateSpeciesRates[i] * dt; + for ( int j = 0; j < numPrimarySpecies; ++j ) + { + J[i][j] = dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] - dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[i][j] * dt; + } + } + }; - nonlinearSolvers::newtonRaphson< numPrimarySpecies >( logPrimarySpeciesConcentration, computeResidualAndJacobian ); + nonlinearSolvers::newtonRaphson< numPrimarySpecies >( logPrimarySpeciesConcentration, computeResidualAndJacobian ); - time += dt; + time += dt; + } + for(int i = 0; i < numPrimarySpecies; ++i ) + { + speciesConcentration[i] = exp( logPrimarySpeciesConcentration[i] ); + } + } ); + + // Check results + for(int i = 0; i < PARAMS_DATA::numPrimarySpecies(); ++i ) + { + EXPECT_NEAR( primarySpeciesConcentration[ i ], expectedSpeciesConcentrations[ i ], 1.0e-8 * expectedSpeciesConcentrations[ i ]); } - - EXPECT_NEAR( reactionRatesDerivatives( r, i ), expectedReactionRatesDerivatives[r][i], std::max( magScale, fabs( expectedReactionRatesDerivatives[r][i] ) ) * 1.0e-8 ); } TEST( testMixedReactions, testTimeStep_carbonateSystem ) { constexpr int numPrimarySpecies = carbonateSystemType::numPrimarySpecies(); - double const initialSpeciesConcentration[numPrimarySpecies] = + double const initialAggregateSpeciesConcentration[numPrimarySpecies] = { 3.76e-3, // CaCO3 3.76e-1, // H+ @@ -134,20 +152,20 @@ TEST( testMixedReactions, testTimeStep_carbonateSystem ) double const expectedSpeciesConcentrations[numPrimarySpecies] = { - 1.0e-3, // CaCO3 - 3.76e-1, // H+ - 3.76e-1, // HCO3- - 3.87e-2, // Ca+2 - 3.21e-2, // SO4-2 - 1.89, // Cl- - 1.65e-2, // Mg+2 - 1.09 // Na+1 + 3.3318075516669661e-05, // CaCO3 + 2.5894448848121536e-05, // H+ + 0.0062660162912796741, // HCO3- + 0.015741214773567921, // Ca+2 + 0.0024602709470074127, // SO4-2 + 1.8564927944498291, // Cl- + 0.0099316034080546619, // Mg+2 + 1.0725251492409775 // Na+1 }; timeStepTest< double, true >( carbonateSystem, 0.2, 10, - initialSpeciesConcentration, + initialAggregateSpeciesConcentration, expectedSpeciesConcentrations ); } From 8f7e7ae4accd91427c909b7dacc855916e221ac0 Mon Sep 17 00:00:00 2001 From: Matteo Cusini Date: Tue, 3 Jun 2025 11:48:24 -0700 Subject: [PATCH 21/28] add if constexpr for cases with no kinetics. --- .../MixedEquilibriumKineticReactions_impl.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp index 58f558a..8ba87b5 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -52,7 +52,11 @@ MixedEquilibriumKineticReactions< REAL_TYPE, logSecondarySpeciesConcentrations, aggregatePrimarySpeciesConcentrations, dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations ); - + + if constexpr( PARAMS_DATA::numKineticReactions() > 0 ) + { + + // 2. Compute the reaction rates for all kinetic reactions computeReactionRates( temperature, params, @@ -69,7 +73,10 @@ MixedEquilibriumKineticReactions< REAL_TYPE, dReactionRates_dLogPrimarySpeciesConcentrations, aggregateSpeciesRates, dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); - + } else + { + GEOS_UNUSED_VAR( reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations, aggregateSpeciesRates, dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); + } } From 220b4f0bce98e693515916113bbfe0a91e42f2dc Mon Sep 17 00:00:00 2001 From: Matteo Cusini <49037133+CusiniM@users.noreply.github.com> Date: Wed, 18 Jun 2025 10:31:40 -0700 Subject: [PATCH 22/28] fix shadowing error. --- src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp index bef7efe..7e38027 100644 --- a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp @@ -91,13 +91,13 @@ void timeStepTest( PARAMS_DATA const & params, aggregatePrimarySpeciesConcentration_n[i] = aggregatePrimarySpeciesConcentration[i]; } - auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&logPrimarySpeciesConcentration)[numPrimarySpecies], + auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&primarySpeciesConcentration)[numPrimarySpecies], REAL_TYPE (&r)[numPrimarySpecies], REAL_TYPE (&J)[numPrimarySpecies][numPrimarySpecies] ) { MixedReactionsType::updateMixedSystem( temperature, params, - logPrimarySpeciesConcentration, + primarySpeciesConcentration, logSecondarySpeciesConcentration, aggregatePrimarySpeciesConcentration, dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration, From 01e18275d4bbd49461ea2e891b52f0f7cdddc7ce Mon Sep 17 00:00:00 2001 From: Matteo Cusini <49037133+CusiniM@users.noreply.github.com> Date: Wed, 18 Jun 2025 11:15:47 -0700 Subject: [PATCH 23/28] again, new name. --- src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp index 7e38027..d3f1a5f 100644 --- a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp @@ -91,13 +91,13 @@ void timeStepTest( PARAMS_DATA const & params, aggregatePrimarySpeciesConcentration_n[i] = aggregatePrimarySpeciesConcentration[i]; } - auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&primarySpeciesConcentration)[numPrimarySpecies], + auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&X)[numPrimarySpecies], REAL_TYPE (&r)[numPrimarySpecies], REAL_TYPE (&J)[numPrimarySpecies][numPrimarySpecies] ) { MixedReactionsType::updateMixedSystem( temperature, params, - primarySpeciesConcentration, + X, logSecondarySpeciesConcentration, aggregatePrimarySpeciesConcentration, dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration, From 6605ef624e78eb4d2721ef5f2f713c1e8c5053e9 Mon Sep 17 00:00:00 2001 From: Matteo Cusini <49037133+CusiniM@users.noreply.github.com> Date: Wed, 18 Jun 2025 11:51:59 -0700 Subject: [PATCH 24/28] Doxygen --- .../MixedEquilibriumKineticReactions.hpp | 115 +++++++++++++++++- 1 file changed, 110 insertions(+), 5 deletions(-) diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp index 38e8041..662617e 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -41,7 +41,30 @@ class MixedEquilibriumKineticReactions using IndexType = INDEX_TYPE; using kineticReactions = KineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, LOGE_CONCENTRATION >; - + + /** + * @brief Update a mixed chemical system by computing secondary species concentrations, + * aggregate primary species concentrations, and reaction rates. + * + * @tparam PARAMS_DATA Struct providing all parameter access (stoichiometry, rate constants, etc.) + * @tparam ARRAY_1D_TO_CONST Read-only 1D array type for primary log-concentrations + * @tparam ARRAY_1D_PRIMARY Mutable 1D array type for primary species outputs + * @tparam ARRAY_1D_SECONDARY Mutable 1D array type for secondary log-concentrations + * @tparam ARRAY_1D_KINETIC Mutable 1D array type for reaction rates + * @tparam ARRAY_2D_PRIMARY Mutable 2D array type for primary derivatives + * @tparam ARRAY_2D_KINETIC Mutable 2D array type for reaction rate derivatives + * + * @param temperature Temperature of the system (in Kelvin) + * @param params Parameter object for stoichiometry, rates, etc. + * @param logPrimarySpeciesConcentrations Log of primary species concentrations + * @param logSecondarySpeciesConcentrations Output log concentrations for secondary species + * @param aggregatePrimarySpeciesConcentrations Output aggregate concentrations (per primary) + * @param dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations Derivatives of aggregate concentrations w.r.t. log primary + * @param reactionRates Output vector of kinetic reaction rates + * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species + * @param aggregateSpeciesRates Output net source/sink for each primary species + * @param dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations Derivatives of aggregate source terms + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_PRIMARY, @@ -73,6 +96,22 @@ class MixedEquilibriumKineticReactions dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); } + /** + * @brief Compute reaction rates and their derivatives. + * + * @tparam PARAMS_DATA Struct providing reaction parameters + * @tparam ARRAY_1D_TO_CONST Read-only array of primary species (log-space) + * @tparam ARRAY_1D_TO_CONST2 Read-only array of secondary species (log-space) + * @tparam ARRAY_1D Output array type for reaction rates + * @tparam ARRAY_2D Output array type for reaction rate derivatives + * + * @param temperature Temperature in Kelvin + * @param params Parameter data for the reaction system + * @param logPrimarySpeciesConcentrations Log concentrations of primary species + * @param logSecondarySpeciesConcentrations Log concentrations of secondary species + * @param reactionRates Output reaction rates for each kinetic reaction + * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_TO_CONST2, @@ -94,7 +133,24 @@ class MixedEquilibriumKineticReactions reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations ); } - + + /** + * @brief Compute net reaction rate for each primary species by aggregating contributions from all reactions. + * + * @tparam PARAMS_DATA Struct with stoichiometry and mappings + * @tparam ARRAY_1D_TO_CONST Array type for primary species concentrations + * @tparam ARRAY_1D_TO_CONST2 Array type for reaction rates + * @tparam ARRAY_2D_TO_CONST Array type for reaction rate derivatives + * @tparam ARRAY_1D Output type for net species rates + * @tparam ARRAY_2D Output type for rate derivatives + * + * @param params Reaction parameters + * @param speciesConcentration Current concentrations of primary species + * @param reactionRates Computed reaction rates + * @param reactionRatesDerivatives Derivatives of reaction rates w.r.t. log concentrations + * @param aggregatesRates Output: net rate for each primary species + * @param aggregatesRatesDerivatives Output: derivative of net rates w.r.t. log concentrations + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_TO_CONST2, @@ -124,7 +180,30 @@ class MixedEquilibriumKineticReactions } private: - + /** + * @brief Internal implementation of updateMixedSystem with template-dispatched logic. + * + * @details Called by the public `updateMixedSystem` function. Handles the complete chain: + * secondary speciation, aggregation, reaction rate evaluation, and net source terms. + * @tparam PARAMS_DATA Struct providing all parameter access (stoichiometry, rate constants, etc.) + * @tparam ARRAY_1D_TO_CONST Read-only 1D array type for primary log-concentrations + * @tparam ARRAY_1D_PRIMARY Mutable 1D array type for primary species outputs + * @tparam ARRAY_1D_SECONDARY Mutable 1D array type for secondary log-concentrations + * @tparam ARRAY_1D_KINETIC Mutable 1D array type for reaction rates + * @tparam ARRAY_2D_PRIMARY Mutable 2D array type for primary derivatives + * @tparam ARRAY_2D_KINETIC Mutable 2D array type for reaction rate derivatives + * + * @param temperature Temperature of the system (in Kelvin) + * @param params Parameter object for stoichiometry, rates, etc. + * @param logPrimarySpeciesConcentrations Log of primary species concentrations + * @param logSecondarySpeciesConcentrations Output log concentrations for secondary species + * @param aggregatePrimarySpeciesConcentrations Output aggregate concentrations (per primary) + * @param dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations Derivatives of aggregate concentrations w.r.t. log primary + * @param reactionRates Output vector of kinetic reaction rates + * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species + * @param aggregateSpeciesRates Output net source/sink for each primary species + * @param dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations Derivatives of aggregate source terms + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_PRIMARY, @@ -143,7 +222,17 @@ class MixedEquilibriumKineticReactions ARRAY_2D_KINETIC & dReactionRates_dLogPrimarySpeciesConcentrations, ARRAY_1D_PRIMARY & aggregateSpeciesRates, ARRAY_2D_PRIMARY & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); - + /** + * @brief Internal implementation of computeReactionRates. + * + * @details Handles kinetic rate law evaluation for forward and reverse reactions. + * @param temperature Temperature in Kelvin + * @param params Parameter data for the reaction system + * @param logPrimarySpeciesConcentrations Log concentrations of primary species + * @param logSecondarySpeciesConcentrations Log concentrations of secondary species + * @param reactionRates Output reaction rates for each kinetic reaction + * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_TO_CONST2, @@ -158,7 +247,23 @@ class MixedEquilibriumKineticReactions ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ); - + /** + * @brief Internal implementation of computeAggregateSpeciesRates. + * + * @tparam CALCULATE_DERIVATIVES Whether to compute Jacobian derivatives + * @tparam ARRAY_1D_TO_CONST Array type for primary species concentrations + * @tparam ARRAY_1D_TO_CONST2 Array type for reaction rates + * @tparam ARRAY_2D_TO_CONST Array type for reaction rate derivatives + * @tparam ARRAY_1D Output type for net species rates + * @tparam ARRAY_2D Output type for rate derivatives + * + * @param params Reaction parameters + * @param speciesConcentration Current concentrations of primary species + * @param reactionRates Computed reaction rates + * @param reactionRatesDerivatives Derivatives of reaction rates w.r.t. log concentrations + * @param aggregatesRates Output: net rate for each primary species + * @param aggregatesRatesDerivatives Output: derivative of net rates w.r.t. log concentrations + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_TO_CONST2, From 162e22080a98dfcd8acf8993d029c5095f835e64 Mon Sep 17 00:00:00 2001 From: Matteo Cusini <49037133+CusiniM@users.noreply.github.com> Date: Wed, 18 Jun 2025 12:02:37 -0700 Subject: [PATCH 25/28] one more doxygen error. --- src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp index 662617e..8808892 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -39,7 +39,8 @@ class MixedEquilibriumKineticReactions /// Type alias for the index type used in the class. using IndexType = INDEX_TYPE; - + + /// Type alias for the Kinetic reactions type used in the class. using kineticReactions = KineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, LOGE_CONCENTRATION >; /** From 0aee07ff3570a1dfa793c5b993dc1e3325d7d375 Mon Sep 17 00:00:00 2001 From: Randolph Settgast Date: Wed, 18 Jun 2025 13:56:39 -0700 Subject: [PATCH 26/28] some codecov exlusions --- .gitignore | 4 ++- src/common/nonlinearSolvers.hpp | 50 +++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 7583b4b..980f50f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build* -cmake/blt* \ No newline at end of file +install* +cmake/blt* +.vscode* \ No newline at end of file diff --git a/src/common/nonlinearSolvers.hpp b/src/common/nonlinearSolvers.hpp index 5098b4b..2e567c1 100644 --- a/src/common/nonlinearSolvers.hpp +++ b/src/common/nonlinearSolvers.hpp @@ -13,6 +13,13 @@ namespace nonlinearSolvers namespace internal { +/** + * Computes the norm of a vector. + * This function calculates the Euclidean norm (L2 norm) of a vector. + * @tparam N The size of the vector. + * @param r The input vector. + * @return The Euclidean norm of the vector. + */ template< int N > HPCREACT_HOST_DEVICE double norm( double const (&r)[N]) @@ -22,7 +29,13 @@ double norm( double const (&r)[N]) return ::sqrt( sum ); } - +/** + * Adds two vectors element-wise. + * This function adds the elements of the second vector to the first vector. + * @tparam N The size of the vectors. + * @param x The first vector, which will be modified. + * @param dx The second vector, which will be added to the first vector. + */ template< int N > HPCREACT_HOST_DEVICE void add(double (&x)[N], double const (&dx)[N]) @@ -30,6 +43,13 @@ void add(double (&x)[N], double const (&dx)[N]) for (int i = 0; i < N; ++i) x[i] += dx[i]; } +/** + * Scales a vector by a constant value. + * This function multiplies each element of the vector by a given value. + * @tparam N The size of the vector. + * @param x The vector to be scaled. + * @param value The scaling factor. + */ template< int N > HPCREACT_HOST_DEVICE void scale( double (&x)[N], double const value ) @@ -41,6 +61,17 @@ void scale( double (&x)[N], double const value ) namespace utils { +// LCOV_EXCL_START + +/** + * Prints the Jacobian matrix, residual vector, and delta update vector. + * This function is useful for debugging and understanding the + * state of the Newton-Raphson method at each iteration. + * @tparam N The size of the vectors and matrix. + * @param J The Jacobian matrix. + * @param r The residual vector. + * @param dx The delta update vector. + */ template< int N > HPCREACT_HOST_DEVICE void print(double const (&J)[N][N], double const (&r)[N], double const (&dx)[N]) @@ -70,6 +101,8 @@ void print(double const (&J)[N][N], double const (&r)[N], double const (&dx)[N]) printf("%10d%27.16e\n", i, dx[i]); } } +// LCOV_EXCL_STOP + } template< int N, @@ -114,8 +147,9 @@ bool newtonRaphson( REAL_TYPE (&x)[N], REAL_TYPE residual[N]{}; REAL_TYPE dx[N]{}; REAL_TYPE jacobian[N][N]{}; + bool isConverged = false; - for ( int iter = 0; iter < maxIters; ++iter ) + for ( int iter = 0; iter < maxIters; ++iter ) { computeResidualAndJacobian( x, residual, jacobian ); @@ -125,13 +159,14 @@ bool newtonRaphson( REAL_TYPE (&x)[N], if ( norm < tol ) { printf( "--Converged.\n" ); - return true; + isConverged = true; + break; } internal::scale( residual, -1.0); if( do_print ) { - utils::print( jacobian, residual, dx ); + utils::print( jacobian, residual, dx ); // LCOV_EXCL_LINE } solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); @@ -139,9 +174,12 @@ bool newtonRaphson( REAL_TYPE (&x)[N], } - printf( "--Newton solver error: Max iterations reached without convergence.\n" ); + if( !isConverged ) + { + printf( "--Newton solver error: Max iterations reached without convergence.\n" ); + } - return false; + return isConverged; } } From f8ffaec84d6d4c9c2415c2da96fc4c04412858cf Mon Sep 17 00:00:00 2001 From: Randolph Settgast Date: Wed, 18 Jun 2025 13:59:58 -0700 Subject: [PATCH 27/28] uncrustify --- src/common/CArrayWrapper.hpp | 8 +- src/common/nonlinearSolvers.hpp | 173 +++++++------ .../MixedEquilibriumKineticReactions.hpp | 80 +++--- .../MixedEquilibriumKineticReactions_impl.hpp | 239 +++++++++--------- src/reactions/bulkGeneric/Parameters.hpp | 46 ++-- .../bulkGeneric/ParametersPredefined.hpp | 3 +- .../bulkGeneric/SpeciesUtilities.hpp | 4 +- .../unitTests/testEquilibriumReactions.cpp | 4 +- .../unitTests/testKineticReactions.cpp | 22 +- .../unitTests/testMixedReactions.cpp | 68 ++--- .../unitTests/testSpeciesUtilities.cpp | 5 +- 11 files changed, 331 insertions(+), 321 deletions(-) diff --git a/src/common/CArrayWrapper.hpp b/src/common/CArrayWrapper.hpp index d35dcd2..c5a5c7c 100644 --- a/src/common/CArrayWrapper.hpp +++ b/src/common/CArrayWrapper.hpp @@ -29,7 +29,7 @@ struct CArrayWrapper< T, DIM0 > { // default constructor constexpr CArrayWrapper() = default; - + /** * @brief Construct a CArrayWrapper from an initializer list. * @@ -54,7 +54,7 @@ struct CArrayWrapper< T, DIM0 > /** * @brief Copy constructor. * @param src The source CArrayWrapper to copy from. - */ + */ constexpr CArrayWrapper( CArrayWrapper const & src ) { for( std::size_t i = 0; i < DIM0; i++ ) @@ -110,7 +110,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > { // default constructor constexpr CArrayWrapper() = default; - + /** * @brief Copy constructor. * @param src The source CArrayWrapper to copy from. @@ -119,7 +119,7 @@ struct CArrayWrapper< T, DIM0, DIM1 > { for( std::size_t i = 0; i < DIM0; i++ ) { - for ( std::size_t j = 0; j < DIM1; j++) + for( std::size_t j = 0; j < DIM1; j++ ) data[i][j] = src.data[i][j]; } } diff --git a/src/common/nonlinearSolvers.hpp b/src/common/nonlinearSolvers.hpp index 2e567c1..c297078 100644 --- a/src/common/nonlinearSolvers.hpp +++ b/src/common/nonlinearSolvers.hpp @@ -4,7 +4,7 @@ #include "DirectSystemSolve.hpp" #include -namespace hpcReact +namespace hpcReact { namespace nonlinearSolvers @@ -21,12 +21,13 @@ namespace internal * @return The Euclidean norm of the vector. */ template< int N > -HPCREACT_HOST_DEVICE -double norm( double const (&r)[N]) +HPCREACT_HOST_DEVICE +double norm( double const (&r)[N] ) { - double sum = 0.0; - for ( int i = 0; i < N; ++i ) sum += r[i] * r[i]; - return ::sqrt( sum ); + double sum = 0.0; + for( int i = 0; i < N; ++i ) + sum += r[i] * r[i]; + return ::sqrt( sum ); } /** @@ -37,10 +38,11 @@ double norm( double const (&r)[N]) * @param dx The second vector, which will be added to the first vector. */ template< int N > -HPCREACT_HOST_DEVICE -void add(double (&x)[N], double const (&dx)[N]) +HPCREACT_HOST_DEVICE +void add( double (& x)[N], double const (&dx)[N] ) { - for (int i = 0; i < N; ++i) x[i] += dx[i]; + for( int i = 0; i < N; ++i ) + x[i] += dx[i]; } /** @@ -51,16 +53,17 @@ void add(double (&x)[N], double const (&dx)[N]) * @param value The scaling factor. */ template< int N > -HPCREACT_HOST_DEVICE -void scale( double (&x)[N], double const value ) +HPCREACT_HOST_DEVICE +void scale( double (& x)[N], double const value ) { - for (int i = 0; i < N; ++i) x[i] *= value; + for( int i = 0; i < N; ++i ) + x[i] *= value; } } namespace utils -{ +{ // LCOV_EXCL_START /** @@ -74,113 +77,115 @@ namespace utils */ template< int N > HPCREACT_HOST_DEVICE -void print(double const (&J)[N][N], double const (&r)[N], double const (&dx)[N]) +void print( double const (&J)[N][N], double const (&r)[N], double const (&dx)[N] ) { - printf("=======================\nJacobian matrix:\n=======================\n"); - printf(" RowID ColID Value\n"); for ( int i = 0; i < N; ++i ) + printf( "=======================\nJacobian matrix:\n=======================\n" ); + printf( " RowID ColID Value\n" ); for( int i = 0; i < N; ++i ) + { + for( int j = 0; j < N; ++j ) { - for ( int j = 0; j < N; ++j ) - { - printf("%10d%16d%27.16e\n", i, j, J[i][j]); - } + printf( "%10d%16d%27.16e\n", i, j, J[i][j] ); } + } - printf("\n=======================\nSystem right-hand side:\n=======================\n"); - printf(" RowID Value\n"); + printf( "\n=======================\nSystem right-hand side:\n=======================\n" ); + printf( " RowID Value\n" ); - for ( int i = 0; i < N; ++i ) - { - printf("%10d%27.16e\n", i, r[i]); - } - - printf("\n=======================\nDelta update vector:\n=======================\n"); - printf(" RowID Value\n"); - - for ( int i = 0; i < N; ++i ) - { - printf("%10d%27.16e\n", i, dx[i]); - } + for( int i = 0; i < N; ++i ) + { + printf( "%10d%27.16e\n", i, r[i] ); + } + + printf( "\n=======================\nDelta update vector:\n=======================\n" ); + printf( " RowID Value\n" ); + + for( int i = 0; i < N; ++i ) + { + printf( "%10d%27.16e\n", i, dx[i] ); + } } // LCOV_EXCL_STOP } template< int N, - typename REAL_TYPE, - typename ResidualFunc, + typename REAL_TYPE, + typename ResidualFunc, typename JacobianFunc > -HPCREACT_HOST_DEVICE -void newtonRaphson( REAL_TYPE (&x)[N], +HPCREACT_HOST_DEVICE +void newtonRaphson( REAL_TYPE (& x)[N], ResidualFunc computeResidual, JacobianFunc computeJacobian, int maxIters = 25, double tol = 1e-10 ) { - REAL_TYPE residual[N]; - REAL_TYPE dx[N]; - REAL_TYPE jacobian[N][N]; + REAL_TYPE residual[N]; + REAL_TYPE dx[N]; + REAL_TYPE jacobian[N][N]; - for ( int iter = 0; iter < maxIters; ++iter ) - { - computeResidual( x, residual ); + for( int iter = 0; iter < maxIters; ++iter ) + { + computeResidual( x, residual ); - if ( internal::norm< N >( residual ) < tol ) return; + if( internal::norm< N >( residual ) < tol ) + return; - computeJacobian( x, jacobian ); + computeJacobian( x, jacobian ); - solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); + solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); - internal::add< N >( x, dx ); - } + internal::add< N >( x, dx ); + } } template< int N, - typename REAL_TYPE, - typename FUNCTION_TYPE > -HPCREACT_HOST_DEVICE -bool newtonRaphson( REAL_TYPE (&x)[N], + typename REAL_TYPE, + typename FUNCTION_TYPE > +HPCREACT_HOST_DEVICE +bool newtonRaphson( REAL_TYPE (& x)[N], FUNCTION_TYPE computeResidualAndJacobian, int maxIters = 12, double tol = 1e-10, bool const do_print = false ) { - REAL_TYPE residual[N]{}; - REAL_TYPE dx[N]{}; - REAL_TYPE jacobian[N][N]{}; - bool isConverged = false; + REAL_TYPE residual[N]{}; + REAL_TYPE dx[N]{}; + REAL_TYPE jacobian[N][N]{}; + bool isConverged = false; + + for( int iter = 0; iter < maxIters; ++iter ) + { + computeResidualAndJacobian( x, residual, jacobian ); + + double const norm = internal::norm< N >( residual ); + + printf( "--Iter %d: Residual norm = %.12e\n", iter, norm ); - for ( int iter = 0; iter < maxIters; ++iter ) + if( norm < tol ) { - computeResidualAndJacobian( x, residual, jacobian ); - - double const norm = internal::norm< N >( residual ); - - printf( "--Iter %d: Residual norm = %.12e\n", iter, norm ); - - if ( norm < tol ) { - printf( "--Converged.\n" ); - isConverged = true; - break; - } - internal::scale( residual, -1.0); - - if( do_print ) - { - utils::print( jacobian, residual, dx ); // LCOV_EXCL_LINE - } - - solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); - internal::add< N >( x, dx ); - + printf( "--Converged.\n" ); + isConverged = true; + break; } - - if( !isConverged ) + internal::scale< N >( residual, -1.0 ); + + if( do_print ) { - printf( "--Newton solver error: Max iterations reached without convergence.\n" ); + utils::print( jacobian, residual, dx ); // LCOV_EXCL_LINE } - return isConverged; + solveNxN_pivoted< REAL_TYPE, N >( jacobian, residual, dx ); + internal::add< N >( x, dx ); + + } + + if( !isConverged ) + { + printf( "--Newton solver error: Max iterations reached without convergence.\n" ); + } + + return isConverged; } } -} \ No newline at end of file +} diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp index 8808892..fb2950a 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions.hpp @@ -39,10 +39,10 @@ class MixedEquilibriumKineticReactions /// Type alias for the index type used in the class. using IndexType = INDEX_TYPE; - + /// Type alias for the Kinetic reactions type used in the class. using kineticReactions = KineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, LOGE_CONCENTRATION >; - + /** * @brief Update a mixed chemical system by computing secondary species concentrations, * aggregate primary species concentrations, and reaction rates. @@ -60,7 +60,8 @@ class MixedEquilibriumKineticReactions * @param logPrimarySpeciesConcentrations Log of primary species concentrations * @param logSecondarySpeciesConcentrations Output log concentrations for secondary species * @param aggregatePrimarySpeciesConcentrations Output aggregate concentrations (per primary) - * @param dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations Derivatives of aggregate concentrations w.r.t. log primary + * @param dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations Derivatives of aggregate concentrations w.r.t. log + * primary * @param reactionRates Output vector of kinetic reaction rates * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species * @param aggregateSpeciesRates Output net source/sink for each primary species @@ -96,23 +97,23 @@ class MixedEquilibriumKineticReactions aggregateSpeciesRates, dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); } - - /** - * @brief Compute reaction rates and their derivatives. - * - * @tparam PARAMS_DATA Struct providing reaction parameters - * @tparam ARRAY_1D_TO_CONST Read-only array of primary species (log-space) - * @tparam ARRAY_1D_TO_CONST2 Read-only array of secondary species (log-space) - * @tparam ARRAY_1D Output array type for reaction rates - * @tparam ARRAY_2D Output array type for reaction rate derivatives - * - * @param temperature Temperature in Kelvin - * @param params Parameter data for the reaction system - * @param logPrimarySpeciesConcentrations Log concentrations of primary species - * @param logSecondarySpeciesConcentrations Log concentrations of secondary species - * @param reactionRates Output reaction rates for each kinetic reaction - * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species - */ + + /** + * @brief Compute reaction rates and their derivatives. + * + * @tparam PARAMS_DATA Struct providing reaction parameters + * @tparam ARRAY_1D_TO_CONST Read-only array of primary species (log-space) + * @tparam ARRAY_1D_TO_CONST2 Read-only array of secondary species (log-space) + * @tparam ARRAY_1D Output array type for reaction rates + * @tparam ARRAY_2D Output array type for reaction rate derivatives + * + * @param temperature Temperature in Kelvin + * @param params Parameter data for the reaction system + * @param logPrimarySpeciesConcentrations Log concentrations of primary species + * @param logSecondarySpeciesConcentrations Log concentrations of secondary species + * @param reactionRates Output reaction rates for each kinetic reaction + * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species + */ template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_TO_CONST2, @@ -133,8 +134,8 @@ class MixedEquilibriumKineticReactions logSecondarySpeciesConcentrations, reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations ); - } - + } + /** * @brief Compute net reaction rate for each primary species by aggregating contributions from all reactions. * @@ -162,17 +163,17 @@ class MixedEquilibriumKineticReactions computeAggregateSpeciesRates( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & speciesConcentration, ARRAY_1D_TO_CONST2 const & reactionRates, - ARRAY_2D_TO_CONST const & reactionRatesDerivatives, + ARRAY_2D_TO_CONST const & reactionRatesDerivatives, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ) { - computeAggregateSpeciesRates_impl< PARAMS_DATA, - ARRAY_1D_TO_CONST, - ARRAY_1D_TO_CONST2, - ARRAY_2D_TO_CONST, - ARRAY_1D, - ARRAY_2D, - true >( params, + computeAggregateSpeciesRates_impl< PARAMS_DATA, + ARRAY_1D_TO_CONST, + ARRAY_1D_TO_CONST2, + ARRAY_2D_TO_CONST, + ARRAY_1D, + ARRAY_2D, + true >( params, speciesConcentration, reactionRates, reactionRatesDerivatives, @@ -180,7 +181,7 @@ class MixedEquilibriumKineticReactions aggregatesRatesDerivatives ); } - private: +private: /** * @brief Internal implementation of updateMixedSystem with template-dispatched logic. * @@ -199,7 +200,8 @@ class MixedEquilibriumKineticReactions * @param logPrimarySpeciesConcentrations Log of primary species concentrations * @param logSecondarySpeciesConcentrations Output log concentrations for secondary species * @param aggregatePrimarySpeciesConcentrations Output aggregate concentrations (per primary) - * @param dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations Derivatives of aggregate concentrations w.r.t. log primary + * @param dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations Derivatives of aggregate concentrations w.r.t. log + * primary * @param reactionRates Output vector of kinetic reaction rates * @param dReactionRates_dLogPrimarySpeciesConcentrations Derivatives of reaction rates w.r.t. log primary species * @param aggregateSpeciesRates Output net source/sink for each primary species @@ -209,7 +211,7 @@ class MixedEquilibriumKineticReactions typename ARRAY_1D_TO_CONST, typename ARRAY_1D_PRIMARY, typename ARRAY_1D_SECONDARY, - typename ARRAY_1D_KINETIC, + typename ARRAY_1D_KINETIC, typename ARRAY_2D_PRIMARY, typename ARRAY_2D_KINETIC > static HPCREACT_HOST_DEVICE void @@ -241,11 +243,11 @@ class MixedEquilibriumKineticReactions typename ARRAY_2D > static HPCREACT_HOST_DEVICE void computeReactionRates_impl( RealType const & temperature, - PARAMS_DATA const & params, - ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, - ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ); + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ); /** @@ -276,7 +278,7 @@ class MixedEquilibriumKineticReactions computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, ARRAY_1D_TO_CONST const & speciesConcentration, ARRAY_1D_TO_CONST2 const & reactionRates, - ARRAY_2D_TO_CONST const & reactionRatesDerivatives, + ARRAY_2D_TO_CONST const & reactionRatesDerivatives, ARRAY_1D & aggregatesRates, ARRAY_2D & aggregatesRatesDerivatives ); diff --git a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp index 8ba87b5..627527e 100644 --- a/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp +++ b/src/reactions/bulkGeneric/MixedEquilibriumKineticReactions_impl.hpp @@ -19,7 +19,7 @@ namespace bulkGeneric template< typename REAL_TYPE, typename INT_TYPE, typename INDEX_TYPE, - bool LOGE_CONCENTRATION > + bool LOGE_CONCENTRATION > template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_PRIMARY, @@ -31,32 +31,32 @@ HPCREACT_HOST_DEVICE inline void MixedEquilibriumKineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, - LOGE_CONCENTRATION - >::updateMixedSystem_impl( RealType const & temperature, - PARAMS_DATA const & params, - ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D_SECONDARY & logSecondarySpeciesConcentrations, - ARRAY_1D_PRIMARY & aggregatePrimarySpeciesConcentrations, - ARRAY_2D_PRIMARY & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, - ARRAY_1D_KINETIC & reactionRates, - ARRAY_2D_KINETIC & dReactionRates_dLogPrimarySpeciesConcentrations, - ARRAY_1D_PRIMARY & aggregateSpeciesRates, - ARRAY_2D_PRIMARY & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) + LOGE_CONCENTRATION + >::updateMixedSystem_impl( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_SECONDARY & logSecondarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregatePrimarySpeciesConcentrations, + ARRAY_2D_PRIMARY & dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations, + ARRAY_1D_KINETIC & reactionRates, + ARRAY_2D_KINETIC & dReactionRates_dLogPrimarySpeciesConcentrations, + ARRAY_1D_PRIMARY & aggregateSpeciesRates, + ARRAY_2D_PRIMARY & dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ) +{ + + // 1. Compute new aggregate species from primary species + calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, + INT_TYPE, + INDEX_TYPE >( params.equilibriumReactionsParameters(), + logPrimarySpeciesConcentrations, + logSecondarySpeciesConcentrations, + aggregatePrimarySpeciesConcentrations, + dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations ); + + if constexpr( PARAMS_DATA::numKineticReactions() > 0 ) { - - // 1. Compute new aggregate species from primary species - calculateAggregatePrimaryConcentrationsWrtLogC< REAL_TYPE, - INT_TYPE, - INDEX_TYPE >( params.equilibriumReactionsParameters(), - logPrimarySpeciesConcentrations, - logSecondarySpeciesConcentrations, - aggregatePrimarySpeciesConcentrations, - dAggregatePrimarySpeciesConcentrations_dLogPrimarySpeciesConcentrations ); - - if constexpr( PARAMS_DATA::numKineticReactions() > 0 ) - { - - + + // 2. Compute the reaction rates for all kinetic reactions computeReactionRates( temperature, params, @@ -64,7 +64,7 @@ MixedEquilibriumKineticReactions< REAL_TYPE, logSecondarySpeciesConcentrations, reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations ); - + // 3. Compute aggregate species rates computeAggregateSpeciesRates( params, @@ -73,88 +73,89 @@ MixedEquilibriumKineticReactions< REAL_TYPE, dReactionRates_dLogPrimarySpeciesConcentrations, aggregateSpeciesRates, dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); - } else - { - GEOS_UNUSED_VAR( reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations, aggregateSpeciesRates, dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); - } - } - - template< typename REAL_TYPE, + else + { + GEOS_UNUSED_VAR( reactionRates, dReactionRates_dLogPrimarySpeciesConcentrations, aggregateSpeciesRates, dAggregateSpeciesRates_dLogPrimarySpeciesConcentrations ); + } + +} + +template< typename REAL_TYPE, typename INT_TYPE, typename INDEX_TYPE, - bool LOGE_CONCENTRATION > - template< typename PARAMS_DATA, - typename ARRAY_1D_TO_CONST, - typename ARRAY_1D_TO_CONST2, - typename ARRAY_1D, - typename ARRAY_2D > - HPCREACT_HOST_DEVICE inline void - MixedEquilibriumKineticReactions< REAL_TYPE, - INT_TYPE, - INDEX_TYPE, - LOGE_CONCENTRATION - >::computeReactionRates_impl( RealType const & temperature, - PARAMS_DATA const & params, - ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, - ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, - ARRAY_1D & reactionRates, - ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ) + bool LOGE_CONCENTRATION > +template< typename PARAMS_DATA, + typename ARRAY_1D_TO_CONST, + typename ARRAY_1D_TO_CONST2, + typename ARRAY_1D, + typename ARRAY_2D > +HPCREACT_HOST_DEVICE inline void +MixedEquilibriumKineticReactions< REAL_TYPE, + INT_TYPE, + INDEX_TYPE, + LOGE_CONCENTRATION + >::computeReactionRates_impl( RealType const & temperature, + PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & logPrimarySpeciesConcentrations, + ARRAY_1D_TO_CONST2 const & logSecondarySpeciesConcentrations, + ARRAY_1D & reactionRates, + ARRAY_2D & dReactionRates_dLogPrimarySpeciesConcentrations ) + +{ + constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); + RealType logSpeciesConcentration[numSpecies] {}; + for( INDEX_TYPE i = 0; i < numSecondarySpecies; ++i ) { - constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); - constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); - constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); - constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); + logSpeciesConcentration[i] = logSecondarySpeciesConcentrations[i]; + } + for( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) + { + logSpeciesConcentration[i+numSecondarySpecies] = logPrimarySpeciesConcentrations[i]; + } - RealType logSpeciesConcentration[numSpecies] {}; - for ( INDEX_TYPE i = 0; i < numSecondarySpecies; ++i ) - { - logSpeciesConcentration[i] = logSecondarySpeciesConcentrations[i]; - } - for ( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) + for( INDEX_TYPE i = 0; i < numKineticReactions; ++i ) + { + for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) { - logSpeciesConcentration[i+numSecondarySpecies] = logPrimarySpeciesConcentrations[i]; + dReactionRates_dLogPrimarySpeciesConcentrations[i][j] = 0.0; } + } - for( INDEX_TYPE i = 0; i < numKineticReactions; ++i ) - { - for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) - { - dReactionRates_dLogPrimarySpeciesConcentrations[i][j] = 0.0; - } - } - - CArrayWrapper< RealType, numKineticReactions, numSpecies > reactionRatesDerivatives; + CArrayWrapper< RealType, numKineticReactions, numSpecies > reactionRatesDerivatives; - kineticReactions::computeReactionRates( temperature, - params.kineticReactionsParameters(), - logSpeciesConcentration, - reactionRates, - reactionRatesDerivatives ); + kineticReactions::computeReactionRates( temperature, + params.kineticReactionsParameters(), + logSpeciesConcentration, + reactionRates, + reactionRatesDerivatives ); - // Compute the reaction rates derivatives w.r.t. log primary species concentrations - for( IntType i = 0; i < numKineticReactions; ++i ) + // Compute the reaction rates derivatives w.r.t. log primary species concentrations + for( IntType i = 0; i < numKineticReactions; ++i ) + { + for( IntType j = 0; j < numPrimarySpecies; ++j ) { - for( IntType j = 0; j < numPrimarySpecies; ++j ) - { - dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j + numSecondarySpecies ); + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) = reactionRatesDerivatives( i, j + numSecondarySpecies ); - for( IntType k = 0; k < numSecondarySpecies; ++k ) - { - RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations = params.stoichiometricMatrix( k, j + numSecondarySpecies ); - - dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) += + for( IntType k = 0; k < numSecondarySpecies; ++k ) + { + RealType const dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations = params.stoichiometricMatrix( k, j + numSecondarySpecies ); + + dReactionRates_dLogPrimarySpeciesConcentrations( i, j ) += reactionRatesDerivatives( i, k ) * dLogSecondarySpeciesConcentrations_dLogPrimarySpeciesConcentrations; - } } } - } + } +} template< typename REAL_TYPE, typename INT_TYPE, typename INDEX_TYPE, - bool LOGE_CONCENTRATION > + bool LOGE_CONCENTRATION > template< typename PARAMS_DATA, typename ARRAY_1D_TO_CONST, typename ARRAY_1D_TO_CONST2, @@ -166,46 +167,46 @@ HPCREACT_HOST_DEVICE inline void MixedEquilibriumKineticReactions< REAL_TYPE, INT_TYPE, INDEX_TYPE, - LOGE_CONCENTRATION - >::computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, - ARRAY_1D_TO_CONST const & speciesConcentration, - ARRAY_1D_TO_CONST2 const & reactionRates, - ARRAY_2D_TO_CONST const & reactionRatesDerivatives, - ARRAY_1D & aggregatesRates, - ARRAY_2D & aggregatesRatesDerivatives ) - { - HPCREACT_UNUSED_VAR( speciesConcentration ); - // constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); - constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); - constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); - constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + LOGE_CONCENTRATION + >::computeAggregateSpeciesRates_impl( PARAMS_DATA const & params, + ARRAY_1D_TO_CONST const & speciesConcentration, + ARRAY_1D_TO_CONST2 const & reactionRates, + ARRAY_2D_TO_CONST const & reactionRatesDerivatives, + ARRAY_1D & aggregatesRates, + ARRAY_2D & aggregatesRatesDerivatives ) +{ + HPCREACT_UNUSED_VAR( speciesConcentration ); + // constexpr IntType numSpecies = PARAMS_DATA::numSpecies(); + constexpr IntType numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); + constexpr IntType numKineticReactions = PARAMS_DATA::numKineticReactions(); + constexpr IntType numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); - for( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) + for( INDEX_TYPE i = 0; i < numPrimarySpecies; ++i ) + { + aggregatesRates[i] = 0.0; + if constexpr( CALCULATE_DERIVATIVES ) { - aggregatesRates[i] = 0.0; - if constexpr( CALCULATE_DERIVATIVES ) + for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) { - for( INDEX_TYPE j = 0; j < numPrimarySpecies; ++j ) - { - aggregatesRatesDerivatives( i, j ) = 0.0; - } + aggregatesRatesDerivatives( i, j ) = 0.0; } - for( IntType r=0; r const & stoichiometricMatrix, CArrayWrapper< RealType, NUM_REACTIONS > equilibriumConstant ): - m_stoichiometricMatrix( stoichiometricMatrix ), - m_equilibriumConstant( equilibriumConstant ) + m_stoichiometricMatrix( stoichiometricMatrix ), + m_equilibriumConstant( equilibriumConstant ) {} RealType stoichiometricMatrix( IndexType const r, int const i ) const { return m_stoichiometricMatrix[r][i]; } RealType equilibriumConstant( IndexType const r ) const { return m_equilibriumConstant[r]; } - CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > m_stoichiometricMatrix; - CArrayWrapper< RealType, NUM_REACTIONS > m_equilibriumConstant; + CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > m_stoichiometricMatrix; + CArrayWrapper< RealType, NUM_REACTIONS > m_equilibriumConstant; }; @@ -69,9 +69,9 @@ struct KineticReactionsParameters constexpr KineticReactionsParameters( CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > const & stoichiometricMatrix, CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantForward, CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantReverse ): - m_stoichiometricMatrix( stoichiometricMatrix ), - m_rateConstantForward( rateConstantForward ), - m_rateConstantReverse( rateConstantReverse ) + m_stoichiometricMatrix( stoichiometricMatrix ), + m_rateConstantForward( rateConstantForward ), + m_rateConstantReverse( rateConstantReverse ) {} @@ -102,13 +102,13 @@ struct MixedReactionsParameters constexpr MixedReactionsParameters() = default; constexpr MixedReactionsParameters( CArrayWrapper< RealType, NUM_REACTIONS, NUM_SPECIES > const & stoichiometricMatrix, - CArrayWrapper< RealType, NUM_REACTIONS > const & equilibriumConstant, + CArrayWrapper< RealType, NUM_REACTIONS > const & equilibriumConstant, CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantForward, CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantReverse ): - m_stoichiometricMatrix( stoichiometricMatrix ), - m_equilibriumConstant( equilibriumConstant ), - m_rateConstantForward( rateConstantForward ), - m_rateConstantReverse( rateConstantReverse ) + m_stoichiometricMatrix( stoichiometricMatrix ), + m_equilibriumConstant( equilibriumConstant ), + m_rateConstantForward( rateConstantForward ), + m_rateConstantReverse( rateConstantReverse ) {} static constexpr IndexType numReactions() { return NUM_REACTIONS; } @@ -118,7 +118,7 @@ struct MixedReactionsParameters static constexpr IndexType numEquilibriumReactions() { return NUM_EQ_REACTIONS; } static constexpr IndexType numSpecies() { return NUM_SPECIES; } - + static constexpr IndexType numPrimarySpecies() { return NUM_SPECIES - NUM_EQ_REACTIONS; } static constexpr IndexType numSecondarySpecies() { return NUM_EQ_REACTIONS; } @@ -129,14 +129,14 @@ struct MixedReactionsParameters { CArrayWrapper< RealType, numEquilibriumReactions(), numSpecies() > eqMatrix{}; CArrayWrapper< RealType, numEquilibriumReactions() > eqConstants{}; - - for (IntType i = 0; i < numEquilibriumReactions(); ++i) + + for( IntType i = 0; i < numEquilibriumReactions(); ++i ) { - for (IntType j = 0; j < numSpecies(); ++j) + for( IntType j = 0; j < numSpecies(); ++j ) { - eqMatrix(i, j) = m_stoichiometricMatrix(i, j); + eqMatrix( i, j ) = m_stoichiometricMatrix( i, j ); } - eqConstants(i) = m_equilibriumConstant(i); + eqConstants( i ) = m_equilibriumConstant( i ); } return { eqMatrix, eqConstants }; @@ -149,12 +149,12 @@ struct MixedReactionsParameters CArrayWrapper< RealType, numKineticReactions(), numSpecies() > kineticMatrix{}; CArrayWrapper< RealType, numKineticReactions() > rateConstantForward{}; CArrayWrapper< RealType, numKineticReactions() > rateConstantReverse{}; - - for ( IndexType i = 0; i < numKineticReactions(); ++i ) + + for( IndexType i = 0; i < numKineticReactions(); ++i ) { - for ( IndexType j = 0; j < numSpecies(); ++j ) + for( IndexType j = 0; j < numSpecies(); ++j ) { - kineticMatrix(i, j) = m_stoichiometricMatrix( numEquilibriumReactions() + i, j ); + kineticMatrix( i, j ) = m_stoichiometricMatrix( numEquilibriumReactions() + i, j ); } rateConstantForward( i ) = m_rateConstantForward( numEquilibriumReactions() + i ); rateConstantReverse( i ) = m_rateConstantReverse( numEquilibriumReactions() + i ); diff --git a/src/reactions/bulkGeneric/ParametersPredefined.hpp b/src/reactions/bulkGeneric/ParametersPredefined.hpp index cf37159..2f6edc3 100644 --- a/src/reactions/bulkGeneric/ParametersPredefined.hpp +++ b/src/reactions/bulkGeneric/ParametersPredefined.hpp @@ -253,7 +253,7 @@ constexpr CArrayWrapper reverseRates = 2.10E-25 // Mg(OH)2 + 2H+ = Mg++ + 2H2O }; }; - + using ultramaficSystemAllKineticType = MixedReactionsParameters< double, int, int, 25, 21, 0 >; using ultramaficSystemAllEquilibriumType = MixedReactionsParameters< double, int, int, 25, 21, 21 >; using ultramaficSystemType = MixedReactionsParameters< double, int, int, 25, 21, 16 >; @@ -262,5 +262,6 @@ constexpr CArrayWrapper reverseRates = constexpr ultramaficSystemAllEquilibriumType ultramaficSystemAllEquilibrium( ultramafics::stoichMatrix, ultramafics::equilibriumConstants, ultramafics::forwardRates, ultramafics::reverseRates ); constexpr ultramaficSystemType ultramaficSystem( ultramafics::stoichMatrix, ultramafics::equilibriumConstants, ultramafics::forwardRates, ultramafics::reverseRates ); +// UNCRUSTIFY-ON } // namespace bulkGeneric } // namespace hpcReact diff --git a/src/reactions/bulkGeneric/SpeciesUtilities.hpp b/src/reactions/bulkGeneric/SpeciesUtilities.hpp index 86f406b..be205c4 100644 --- a/src/reactions/bulkGeneric/SpeciesUtilities.hpp +++ b/src/reactions/bulkGeneric/SpeciesUtilities.hpp @@ -28,9 +28,9 @@ void calculateLogSecondarySpeciesConcentration( PARAMS_DATA const & params, FUNC && derivativeFunc ) { constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); - constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); + constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); - for (INDEX_TYPE i = 0; i < numSecondarySpecies; ++i) + for( INDEX_TYPE i = 0; i < numSecondarySpecies; ++i ) { logSecondarySpeciesConcentrations[i] = 0.0; } diff --git a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp index 7468540..e83ecf9 100644 --- a/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testEquilibriumReactions.cpp @@ -225,9 +225,9 @@ TEST( testEquilibriumReactions, testcarbonateSystemAllEquilibrium2 ) 3.76e-1, // HCO3- 3.87e-2, // Ca+2 3.21e-2, // SO4-2 - 1.89, // Cl- + 1.89, // Cl- 1.65e-2, // Mg+2 - 1.09 // Na+1 + 1.09 // Na+1 }; diff --git a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp index 41e83af..ae089a4 100644 --- a/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testKineticReactions.cpp @@ -134,18 +134,18 @@ TEST( testKineticReactions, computeReactionRatesTest_carbonateSystemAllKinetic ) 1.09 // Na+1 }; - double const expectedReactionRates[11] = { -0.001424736, // OH- + H+ = H2O - -12610.7392, // CO2 + H2O = H+ + HCO3- - -0.175591624, // CO3-2 + H+ = HCO3- - -473.6096, // H2CO3 = H+ + HCO3- - -269197.19999999984, // CaHCO3+ = Ca+2 + HCO3- - -18012.914999999986, // CaSO4 = Ca+2 + SO4-2 - -1.56526019999999e6, // CaCl+ = Ca+2 + Cl- - -346983.07769999903, // CaCl2 = Ca+2 + 2Cl- - -14247.58499999999, // MgSO4 = Mg+2 + SO4-2 - -2.316271799999999e6, // NaSO4- = Na+ + SO4-2 + double const expectedReactionRates[11] = { -0.001424736, // OH- + H+ = H2O + -12610.7392, // CO2 + H2O = H+ + HCO3- + -0.175591624, // CO3-2 + H+ = HCO3- + -473.6096, // H2CO3 = H+ + HCO3- + -269197.19999999984, // CaHCO3+ = Ca+2 + HCO3- + -18012.914999999986, // CaSO4 = Ca+2 + SO4-2 + -1.56526019999999e6, // CaCl+ = Ca+2 + Cl- + -346983.07769999903, // CaCl2 = Ca+2 + 2Cl- + -14247.58499999999, // MgSO4 = Mg+2 + SO4-2 + -2.316271799999999e6, // NaSO4- = Na+ + SO4-2 -0.00012441275624000003 // CaCO3 + H+ = Ca+2 + HCO3- (kinetic) - }; + }; double const expectedReactionRatesDerivatives[11][18] = { { 5.264e10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.000014, 0, 0, 0, 0, 0, 0 }, diff --git a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp index d3f1a5f..6ae37d3 100644 --- a/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp +++ b/src/reactions/bulkGeneric/unitTests/testMixedReactions.cpp @@ -32,29 +32,29 @@ void timeStepTest( PARAMS_DATA const & params, HPCREACT_UNUSED_VAR( expectedSpeciesConcentrations ); CArrayWrapper< REAL_TYPE, PARAMS_DATA::numPrimarySpecies() > primarySpeciesConcentration; - + for( int i = 0; i < PARAMS_DATA::numPrimarySpecies(); ++i ) { primarySpeciesConcentration[i] = initialSpeciesConcentration[i]; } - pmpl::genericKernelWrapper( PARAMS_DATA::numPrimarySpecies(), - primarySpeciesConcentration.data, - [=] HPCREACT_HOST_DEVICE ( auto * speciesConcentration ) - { + pmpl::genericKernelWrapper( PARAMS_DATA::numPrimarySpecies(), + primarySpeciesConcentration.data, + [=] HPCREACT_HOST_DEVICE ( auto * speciesConcentration ) + { using MixedReactionsType = MixedEquilibriumKineticReactions< REAL_TYPE, - int, - int, - LOGE_CONCENTRATION >; + int, + int, + LOGE_CONCENTRATION >; using EquilibriumReactionsType = EquilibriumReactions< REAL_TYPE, int, int >; - + // constexpr int numSpecies = PARAMS_DATA::numSpecies(); constexpr int numPrimarySpecies = PARAMS_DATA::numPrimarySpecies(); constexpr int numSecondarySpecies = PARAMS_DATA::numSecondarySpecies(); constexpr int numKineticReactions = PARAMS_DATA::numKineticReactions(); - + // define variables double const temperature = 298.15; REAL_TYPE logPrimarySpeciesConcentration[numPrimarySpecies]; @@ -67,34 +67,34 @@ void timeStepTest( PARAMS_DATA const & params, CArrayWrapper< REAL_TYPE, numKineticReactions, numPrimarySpecies > dReactionRates_dlogPrimarySpeciesConcentration; CArrayWrapper< REAL_TYPE, numPrimarySpecies > aggregateSpeciesRates; CArrayWrapper< REAL_TYPE, numPrimarySpecies, numPrimarySpecies > dAggregateSpeciesRates_dlogPrimarySpeciesConcentration; - + // Initialize species concentrations for( int i = 0; i < numPrimarySpecies; ++i ) { logPrimarySpeciesConcentration[i] = ::log( speciesConcentration[i] ); aggregatePrimarySpeciesConcentration[i] = speciesConcentration[i]; } - + EquilibriumReactionsType::enforceEquilibrium_Aggregate( temperature, carbonateSystem.equilibriumReactionsParameters(), logPrimarySpeciesConcentration, logPrimarySpeciesConcentration ); - - /// Time step loop + + /// Time step loop double time = 0.0; for( int t = 0; t < numSteps; ++t ) - { + { printf( "Timestep %d, Time = %.6e\n", t, time ); for( int i=0; i < numPrimarySpecies; ++i ) { aggregatePrimarySpeciesConcentration_n[i] = aggregatePrimarySpeciesConcentration[i]; } - - auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&X)[numPrimarySpecies], - REAL_TYPE (&r)[numPrimarySpecies], - REAL_TYPE (&J)[numPrimarySpecies][numPrimarySpecies] ) - { + + auto computeResidualAndJacobian = [&] HPCREACT_HOST_DEVICE ( REAL_TYPE const (&X)[numPrimarySpecies], + REAL_TYPE ( &r )[numPrimarySpecies], + REAL_TYPE ( &J )[numPrimarySpecies][numPrimarySpecies] ) + { MixedReactionsType::updateMixedSystem( temperature, params, X, @@ -106,31 +106,31 @@ void timeStepTest( PARAMS_DATA const & params, aggregateSpeciesRates, dAggregateSpeciesRates_dlogPrimarySpeciesConcentration ); - - for ( int i = 0; i < numPrimarySpecies; ++i ) + + for( int i = 0; i < numPrimarySpecies; ++i ) { r[i] = ( aggregatePrimarySpeciesConcentration[i] - aggregatePrimarySpeciesConcentration_n[i] ) - aggregateSpeciesRates[i] * dt; - for ( int j = 0; j < numPrimarySpecies; ++j ) + for( int j = 0; j < numPrimarySpecies; ++j ) { J[i][j] = dAggregatePrimarySpeciesConcentrations_dlogPrimarySpeciesConcentration[i][j] - dAggregateSpeciesRates_dlogPrimarySpeciesConcentration[i][j] * dt; } - } + } }; - + nonlinearSolvers::newtonRaphson< numPrimarySpecies >( logPrimarySpeciesConcentration, computeResidualAndJacobian ); time += dt; } - for(int i = 0; i < numPrimarySpecies; ++i ) + for( int i = 0; i < numPrimarySpecies; ++i ) { speciesConcentration[i] = exp( logPrimarySpeciesConcentration[i] ); } } ); // Check results - for(int i = 0; i < PARAMS_DATA::numPrimarySpecies(); ++i ) + for( int i = 0; i < PARAMS_DATA::numPrimarySpecies(); ++i ) { - EXPECT_NEAR( primarySpeciesConcentration[ i ], expectedSpeciesConcentrations[ i ], 1.0e-8 * expectedSpeciesConcentrations[ i ]); + EXPECT_NEAR( primarySpeciesConcentration[ i ], expectedSpeciesConcentrations[ i ], 1.0e-8 * expectedSpeciesConcentrations[ i ] ); } } @@ -140,26 +140,26 @@ TEST( testMixedReactions, testTimeStep_carbonateSystem ) double const initialAggregateSpeciesConcentration[numPrimarySpecies] = { - 3.76e-3, // CaCO3 + 3.76e-3, // CaCO3 3.76e-1, // H+ 3.76e-1, // HCO3- 3.87e-2, // Ca+2 3.21e-2, // SO4-2 - 1.89, // Cl- + 1.89, // Cl- 1.65e-2, // Mg+2 - 1.09 // Na+1 + 1.09 // Na+1 }; double const expectedSpeciesConcentrations[numPrimarySpecies] = { - 3.3318075516669661e-05, // CaCO3 + 3.3318075516669661e-05, // CaCO3 2.5894448848121536e-05, // H+ 0.0062660162912796741, // HCO3- 0.015741214773567921, // Ca+2 0.0024602709470074127, // SO4-2 - 1.8564927944498291, // Cl- + 1.8564927944498291, // Cl- 0.0099316034080546619, // Mg+2 - 1.0725251492409775 // Na+1 + 1.0725251492409775 // Na+1 }; timeStepTest< double, true >( carbonateSystem, diff --git a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp index aaf9594..1378453 100644 --- a/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp +++ b/src/reactions/bulkGeneric/unitTests/testSpeciesUtilities.cpp @@ -124,8 +124,9 @@ TEST( testUtilities, testcalculateAggregatePrimaryConcentrationsWrtLogC ) { for( int k=0; k( carbonateSystemAllEquilibrium.equilibriumReactionsParameters(), From 9b05d1cb1080c1d1561b9192a53363562ff16aeb Mon Sep 17 00:00:00 2001 From: Randolph Settgast Date: Wed, 18 Jun 2025 14:10:01 -0700 Subject: [PATCH 28/28] more lcov exclusions --- src/common/nonlinearSolvers.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/nonlinearSolvers.hpp b/src/common/nonlinearSolvers.hpp index c297078..dd2dd19 100644 --- a/src/common/nonlinearSolvers.hpp +++ b/src/common/nonlinearSolvers.hpp @@ -181,7 +181,7 @@ bool newtonRaphson( REAL_TYPE (& x)[N], if( !isConverged ) { - printf( "--Newton solver error: Max iterations reached without convergence.\n" ); + printf( "--Newton solver error: Max iterations reached without convergence.\n" ); // LCOV_EXCL_LINE } return isConverged;