Skip to content

Commit 6c89cc5

Browse files
committed
add initial constitutive relations api
1 parent 2b2aeaf commit 6c89cc5

File tree

8 files changed

+300
-1
lines changed

8 files changed

+300
-1
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set( hpcReact_headers
33
common/macros.hpp
44
common/CArrayWrapper.hpp
5+
constitutive/activity/Bdot.hpp
56
reactions/exampleSystems/BulkGeneric.hpp
67
reactions/geochemistry/Carbonate.hpp
78
reactions/geochemistry/Forge.hpp
@@ -67,10 +68,11 @@ message(STATUS "HPCReact/src CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DI
6768
# hpcReact_add_code_checks( PREFIX hpcReact
6869
# EXCLUDES "blt/*" )
6970

71+
add_subdirectory( common/unitTests )
72+
add_subdirectory( constitutive/unitTests)
7073
add_subdirectory( reactions/exampleSystems/unitTests )
7174
add_subdirectory( reactions/geochemistry/unitTests )
7275
add_subdirectory( reactions/massActions/unitTests )
73-
add_subdirectory( common/unitTests )
7476
add_subdirectory( docs )
7577

7678
hpcReact_add_code_checks( PREFIX hpcReact

src/constitutive/activity/Bdot.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* ------------------------------------------------------------------------------------------------------------
3+
* SPDX-License-Identifier: (BSD-3-Clause)
4+
*
5+
* Copyright (c) 2025- Lawrence Livermore National Security LLC
6+
* All rights reserved
7+
*
8+
* See top level LICENSE files for details.
9+
* ------------------------------------------------------------------------------------------------------------
10+
*/
11+
#pragma once
12+
13+
#include "common/macros.hpp"
14+
15+
namespace hpcReact
16+
{
17+
18+
template< typename REAL_TYPE,
19+
typename INDEX_TYPE,
20+
typename IONIC_STRENGTH_TYPE >
21+
class Bdot
22+
{
23+
public:
24+
using RealType = REAL_TYPE;
25+
using IndexType = INDEX_TYPE;
26+
27+
struct Params : public IONIC_STRENGTH_TYPE::PARAMS
28+
{
29+
30+
};
31+
32+
33+
template< typename ARRAY_1D_TO_CONST,
34+
typename ARRAY_1D >
35+
static inline HPCREACT_HOST_DEVICE
36+
void
37+
calculateActivities( IONIC_STRENGTH_TYPE::PARAMS const & params,
38+
ARRAY_1D_TO_CONST const & speciesConcentrations,
39+
ARRAY_1D & activities )
40+
{
41+
42+
RealType const ionicStrength = IONIC_STRENGTH_TYPE::calculate( params, speciesConcentrations );
43+
RealType const sqrtI = sqrt(ionicStrength);
44+
RealType const A_gamma = 2;
45+
RealType const B_gamma = 1.6;
46+
auto const & speciesCharge = params.m_speciesCharge;
47+
auto const & a = params.m_ionSizeParameter;
48+
auto const & b = params.m_bdotParameter;
49+
50+
51+
52+
constexpr IndexType numSpecies = params.numSpecies();
53+
for( IndexType i=0; i<numSpecies; ++i )
54+
{
55+
RealType const gamma_coeff = -A_gamma * sqrtI / ( 1 + a[i] * B_gamma * sqrtI );
56+
activities[i] = gamma_coeff * speciesCharge[i] * speciesCharge[i] + b[i] * ionicStrength;
57+
}
58+
}
59+
60+
};
61+
62+
63+
} // namespace hpcReact
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* ------------------------------------------------------------------------------------------------------------
3+
* SPDX-License-Identifier: (BSD-3-Clause)
4+
*
5+
* Copyright (c) 2025- Lawrence Livermore National Security LLC
6+
* All rights reserved
7+
*
8+
* See top level LICENSE files for details.
9+
* ------------------------------------------------------------------------------------------------------------
10+
*/
11+
#pragma once
12+
13+
#include "common/CArrayWrapper.hpp"
14+
#include "common/macros.hpp"
15+
16+
namespace hpcReact
17+
{
18+
19+
template< typename REAL_TYPE,
20+
typename INDEX_TYPE,
21+
int NUM_SPECIES >
22+
class SpeciatedIonicStrength
23+
{
24+
public:
25+
using RealType = REAL_TYPE;
26+
using IndexType = INDEX_TYPE;
27+
28+
struct Params
29+
{
30+
31+
HPCREACT_HOST_DEVICE static constexpr IndexType numSpecies() { return NUM_SPECIES; }
32+
HPCREACT_HOST_DEVICE constexpr CArrayWrapper< RealType, NUM_SPECIES > & speciesCharge() { return m_speciesCharge; }
33+
34+
CArrayWrapper< RealType, NUM_SPECIES > m_speciesCharge;
35+
};
36+
37+
38+
template< typename ARRAY_1D_TO_CONST >
39+
static inline HPCREACT_HOST_DEVICE
40+
REAL_TYPE
41+
calculate( Params const & params,
42+
ARRAY_1D_TO_CONST const & speciesConcentration )
43+
{
44+
REAL_TYPE I = 0.0;
45+
auto const & numSpecies = params.numSpecies();
46+
auto const & speciesCharge = params.m_speciesCharge;
47+
for( int i=0; i<numSpecies; ++i )
48+
{
49+
I += speciesConcentration[i] * speciesCharge[i] * speciesCharge[i];
50+
}
51+
I *= 0.5;
52+
return I;
53+
}
54+
55+
56+
};
57+
58+
59+
} // namespace hpcReact
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* ------------------------------------------------------------------------------------------------------------
3+
* SPDX-License-Identifier: (BSD-3-Clause)
4+
*
5+
* Copyright (c) 2025- Lawrence Livermore National Security LLC
6+
* All rights reserved
7+
*
8+
* See top level LICENSE files for details.
9+
* ------------------------------------------------------------------------------------------------------------
10+
*/
11+
#pragma once
12+
13+
#include "common/macros.hpp"
14+
15+
namespace hpcReact
16+
{
17+
18+
template< typename REAL_TYPE >
19+
class SpeciatedIonicStrength
20+
{
21+
public:
22+
23+
template< typename ARRAY_1D_TO_CONST >
24+
static inline HPCREACT_HOST_DEVICE
25+
REAL_TYPE
26+
calculate( ARRAY_1D_TO_CONST const & speciesConcentration,
27+
ARRAY_1D_TO_CONST const & speciesCharge,
28+
int const numSpecies )
29+
{
30+
REAL_TYPE I = 0.0;
31+
for( int i=0; i<numSpecies; ++i )
32+
{
33+
I += speciesConcentration[i] * speciesCharge[i] * speciesCharge[i];
34+
}
35+
I *= 0.5;
36+
return I;
37+
}
38+
39+
};
40+
41+
42+
} // namespace hpcReact
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Specify list of tests
2+
set( testSourceFiles
3+
testBdot.cpp
4+
testIonicStrength.cpp )
5+
6+
7+
set( dependencyList hpcReact gtest )
8+
9+
if( ENABLE_CUDA )
10+
list( APPEND dependencyList cuda )
11+
endif()
12+
13+
# Add gtest C++ based tests
14+
foreach(test ${testSourceFiles})
15+
get_filename_component( test_name ${test} NAME_WE )
16+
blt_add_executable( NAME ${test_name}
17+
SOURCES ${test}
18+
OUTPUT_DIR ${TEST_OUTPUT_DIRECTORY}
19+
DEPENDS_ON ${dependencyList} )
20+
blt_add_test( NAME ${test_name}
21+
COMMAND ${test_name} )
22+
endforeach()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* ------------------------------------------------------------------------------------------------------------
3+
* SPDX-License-Identifier: (BSD-3-Clause)
4+
*
5+
* Copyright (c) 2025- Lawrence Livermore National Security LLC
6+
* All rights reserved
7+
*
8+
* See top level LICENSE files for details.
9+
* ------------------------------------------------------------------------------------------------------------
10+
*/
11+
12+
13+
#include "constitutive/activity/Bdot.hpp"
14+
#include "reactions/reactionsSystems/Parameters.hpp"
15+
#include "constitutive/ionicStrength/SpeciatedIonicStrength.hpp"
16+
#include "common/pmpl.hpp"
17+
18+
#include <gtest/gtest.h>
19+
20+
using namespace hpcReact;
21+
22+
23+
24+
25+
constexpr SpeciatedIonicStrength<double, int>::Params<3> testParams
26+
{
27+
// Species charge
28+
{ 1.0, -1.0, 2.0 }
29+
};
30+
31+
TEST( testBdot, testIonicStrength )
32+
{
33+
double speciesConcentration[ testParams.numSpecies() ] = { 0.1, 0.2, 0.3 };
34+
35+
double I = SpeciatedIonicStrength< double, int >::calculate( testParams,
36+
speciesConcentration );
37+
38+
double expectedI = 0.5 * ( 0.1 * 1.0 * 1.0 + 0.2 * (-1.0) * (-1.0) + 0.3 * 2.0 * 2.0 );
39+
EXPECT_DOUBLE_EQ( I, expectedI );
40+
41+
}
42+
43+
44+
int main( int argc, char * * argv )
45+
{
46+
::testing::InitGoogleTest( &argc, argv );
47+
int const result = RUN_ALL_TESTS();
48+
return result;
49+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* ------------------------------------------------------------------------------------------------------------
3+
* SPDX-License-Identifier: (BSD-3-Clause)
4+
*
5+
* Copyright (c) 2025- Lawrence Livermore National Security LLC
6+
* All rights reserved
7+
*
8+
* See top level LICENSE files for details.
9+
* ------------------------------------------------------------------------------------------------------------
10+
*/
11+
12+
13+
#include "constitutive/ionicStrength/SpeciatedIonicStrength.hpp"
14+
#include "common/CArrayWrapper.hpp"
15+
#include "common/pmpl.hpp"
16+
17+
#include <gtest/gtest.h>
18+
19+
using namespace hpcReact;
20+
21+
22+
using IonicStrength = SpeciatedIonicStrength<double, int, 3>;
23+
24+
constexpr IonicStrength::Params testParams
25+
{
26+
// Species charge
27+
{ 1.0, -1.0, 2.0 }
28+
};
29+
30+
TEST( testBdot, testIonicStrength )
31+
{
32+
double speciesConcentration[ testParams.numSpecies() ] = { 0.1, 0.2, 0.3 };
33+
34+
double I = IonicStrength::calculate( testParams,
35+
speciesConcentration );
36+
37+
double expectedI = 0.5 * ( 0.1 * 1.0 * 1.0 + 0.2 * (-1.0) * (-1.0) + 0.3 * 2.0 * 2.0 );
38+
EXPECT_DOUBLE_EQ( I, expectedI );
39+
40+
}
41+
42+
43+
int main( int argc, char * * argv )
44+
{
45+
::testing::InitGoogleTest( &argc, argv );
46+
int const result = RUN_ALL_TESTS();
47+
return result;
48+
}

src/reactions/reactionsSystems/Parameters.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ struct KineticReactionsParameters
116116
};
117117

118118

119+
120+
121+
122+
119123
template< typename REAL_TYPE,
120124
typename INT_TYPE,
121125
typename INDEX_TYPE,
@@ -136,12 +140,18 @@ struct MixedReactionsParameters
136140
CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantForward,
137141
CArrayWrapper< RealType, NUM_REACTIONS > const & rateConstantReverse,
138142
CArrayWrapper< IntType, NUM_REACTIONS > mobileSecondarySpeciesFlag,
143+
CArrayWrapper< RealType, NUM_SPECIES > const & speciesCharge,
144+
CArrayWrapper< RealType, NUM_SPECIES > const & ionSizeParameter,
145+
CArrayWrapper< RealType, NUM_SPECIES > const & bdotParameter,
139146
IntType const reactionRatesUpdateOption = 1 ):
140147
m_stoichiometricMatrix( stoichiometricMatrix ),
141148
m_equilibriumConstant( equilibriumConstant ),
142149
m_rateConstantForward( rateConstantForward ),
143150
m_rateConstantReverse( rateConstantReverse ),
144151
m_mobileSecondarySpeciesFlag( mobileSecondarySpeciesFlag ),
152+
m_speciesCharge( speciesCharge ),
153+
m_ionSizeParameter( ionSizeParameter ),
154+
m_bdotParameter( bdotParameter ),
145155
m_reactionRatesUpdateOption( reactionRatesUpdateOption )
146156
{}
147157

@@ -250,6 +260,10 @@ struct MixedReactionsParameters
250260
CArrayWrapper< RealType, NUM_REACTIONS > m_rateConstantReverse;
251261
CArrayWrapper< IntType, NUM_REACTIONS > m_mobileSecondarySpeciesFlag;
252262

263+
CArrayWrapper< RealType, NUM_SPECIES > m_speciesCharge;
264+
CArrayWrapper< RealType, NUM_SPECIES > m_ionSizeParameter;
265+
CArrayWrapper< RealType, NUM_SPECIES > m_bdotParameter;
266+
253267
IntType m_reactionRatesUpdateOption; // 0: forward and reverse rate. 1: quotient form.
254268
};
255269

0 commit comments

Comments
 (0)