Skip to content

Commit bd455b5

Browse files
authored
Merge pull request #455 from dangglxxx/upstream
ODE GPU solver is accessed by DeepFlame!
2 parents 6fc4848 + 299f256 commit bd455b5

File tree

6 files changed

+153
-5
lines changed

6 files changed

+153
-5
lines changed

applications/solvers/dfLowMachFoam/Make/options

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ EXE_INC = -std=c++14 \
2828
$(PYTHON_INC_DIR) \
2929
$(if $(AMGX_DIR), -I$(DF_ROOT)/src_gpu,) \
3030
$(if $(AMGX_DIR), -I/usr/local/cuda/include,) \
31-
$(if $(AMGX_DIR), -I$(AMGX_DIR)/include,)
31+
$(if $(AMGX_DIR), -I$(AMGX_DIR)/include,) \
32+
$(if $(ODE_GPU_SOLVER), -I$(OPENCC_PATH)/include,) \
33+
$(if $(ODE_GPU_SOLVER), -DODE_GPU_SOLVER,)
3234

3335
EXE_LIBS = \
3436
-lcompressibleTransportModels \
@@ -52,4 +54,5 @@ EXE_LIBS = \
5254
$(if $(AMGX_DIR), /usr/local/cuda/lib64/libcudart.so,) \
5355
$(if $(AMGX_DIR), /usr/local/cuda/lib64/libnccl.so,) \
5456
$(if $(AMGX_DIR), $(DF_ROOT)/src_gpu/build/libdfMatrix.so,) \
55-
$(if $(AMGX_DIR), $(AMGX_DIR)/build/libamgxsh.so,)
57+
$(if $(AMGX_DIR), $(AMGX_DIR)/build/libamgxsh.so,) \
58+
$(if $(ODE_GPU_SOLVER), $(ODE_GPU_SOLVER)/lib/libopencc.so,)

applications/solvers/dfLowMachFoam/YEqn.H

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM);
8585
start1 = std::clock();
8686
tmp<volScalarField> DEff = chemistry->rhoD(i) + turbulence->mut()/Sct;
8787

88+
#ifdef ODE_GPU_SOLVER
89+
fvScalarMatrix YiEqn
90+
(
91+
fvm::ddt(rho, Yi)
92+
+
93+
(
94+
turbName == "laminar"
95+
? (mvConvection->fvmDiv(phi, Yi) + mvConvection->fvmDiv(phiUc, Yi))
96+
: mvConvection->fvmDiv(phi, Yi)
97+
)
98+
==
99+
(
100+
splitting
101+
? fvm::laplacian(DEff(), Yi)
102+
: (fvm::laplacian(DEff(), Yi) + RR_GPU[i])
103+
)
104+
);
105+
#else
88106
fvScalarMatrix YiEqn
89107
(
90108
fvm::ddt(rho, Yi)
@@ -101,6 +119,7 @@ if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM);
101119
: (fvm::laplacian(DEff(), Yi) + combustion->R(Yi))
102120
)
103121
);
122+
#endif
104123

105124
end1 = std::clock();
106125
time_monitor_YEqn_mtxAssembly += double(end1 - start1) / double(CLOCKS_PER_SEC);
@@ -264,7 +283,43 @@ if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM);
264283
if (!splitting)
265284
{
266285
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
286+
287+
#ifdef ODE_GPU_SOLVER
288+
scalar dt = runTime.deltaTValue();
289+
290+
memcpy(h_T, &T[0], num_cells * sizeof(double));
291+
memcpy(h_p, &p[0], num_cells * sizeof(double));
292+
293+
forAll(Y, speciesI) {
294+
volScalarField& Yi = Y[speciesI];
295+
memcpy(h_y + speciesI * num_cells, &Yi[0], num_cells * sizeof(double));
296+
}
297+
298+
for (int i = 0; i < num_cells; i++) {
299+
for (int j = 0; j < sp_num; j++) {
300+
h_y_t[j + i*sp_num] = h_y[i + j*num_cells];
301+
}
302+
}
303+
304+
opencc_ode_all(h_T, h_p, h_y_t, 1e-10, dt, CPU);
305+
306+
for (int i = 0; i < num_cells; i++) {
307+
for (int j = 0; j < sp_num; j++) {
308+
Ynew[i + j*num_cells] = h_y_t[j + i*sp_num];
309+
}
310+
}
311+
312+
QdotGPU = Zero;
313+
forAll(QdotGPU,celli)
314+
{
315+
for (int sp = 0; sp < sp_num; sp++)
316+
{
317+
RRGPU[sp][celli] = (Ynew[sp*num_cells+celli]-Y[sp][celli])*rho[celli]/dt;
318+
}
319+
}
320+
#else
267321
combustion->correct();
322+
#endif
268323
//label flag_mpi_init;
269324
//MPI_Initialized(&flag_mpi_init);
270325
if(flag_mpi_init) MPI_Barrier(PstreamGlobals::MPI_COMM_FOAM);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************************
2+
* This file is used to add OpenCC calls to OpenFoam,
3+
* initialize OpenCC scopes and request GPU space.
4+
*
5+
* @author Lynn Dang
6+
******************************************************************************************/
7+
8+
volScalarField QdotGPU
9+
(
10+
IOobject
11+
(
12+
"QdotGPU",
13+
mesh.time().timeName(),
14+
mesh,
15+
IOobject::READ_IF_PRESENT,
16+
IOobject::AUTO_WRITE
17+
),
18+
mesh,
19+
dimensionedScalar(dimEnergy/dimVolume/dimTime, 0)
20+
);
21+
22+
PtrList<volScalarField::Internal> RRGPU(Y.size());
23+
forAll(RRGPU, fieldi)
24+
{
25+
RRGPU.set
26+
(
27+
fieldi,
28+
new volScalarField::Internal
29+
(
30+
IOobject
31+
(
32+
"RRGPU." + Y[fieldi].name(),
33+
mesh.time().timeName(),
34+
mesh,
35+
IOobject::NO_READ,
36+
IOobject::NO_WRITE
37+
),
38+
mesh,
39+
dimensionedScalar(dimMass/dimVolume/dimTime, 0)
40+
)
41+
);
42+
}
43+
44+
int num_cells = T.size();
45+
int num_species = Y.size();
46+
47+
double* h_T = new double[num_cells];
48+
double* h_p = new double[num_cells];
49+
double* h_y = new double[num_cells * num_species];
50+
double* h_y_t = new double[num_cells * num_species];
51+
int* h_size = new int[1];
52+
53+
memcpy(h_T, &T[0], num_cells * sizeof(double));
54+
memcpy(h_p, &p[0], num_cells * sizeof(double));
55+
56+
forAll(Y, speciesI) {
57+
volScalarField& Yi = Y[speciesI];
58+
memcpy(h_y + speciesI * num_cells, &Yi[0], num_cells * sizeof(double));
59+
}
60+
61+
h_size[0] = num_cells;
62+
63+
int sp_num = num_species;
64+
65+
string mechanismFile = CanteraTorchProperties.lookupOrDefault("CanteraMechanismFile", string(""));
66+
char target_mechanismFile[100];
67+
std::strcpy(target_mechanismFile, mechanismFile.c_str());
68+
69+
opencc_ode_init(target_mechanismFile, num_cells, h_T, h_p, h_y);
70+
71+
double* Ynew = new double[num_cells * num_species];

applications/solvers/dfLowMachFoam/dfLowMachFoam.C

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ Description
4949
#include "DNNInferencer.H"
5050
#endif
5151

52+
#ifdef ODE_GPU_SOLVER
53+
#include "opencc.h"
54+
#endif
55+
5256
#include "fvCFD.H"
5357
#include "fluidThermo.H"
5458
#include "turbulentFluidThermoModel.H"
@@ -137,6 +141,10 @@ int main(int argc, char *argv[])
137141
#include "createFields.H"
138142
#include "createRhoUfIfPresent.H"
139143

144+
#ifdef ODE_GPU_SOLVER
145+
#include "createFields_GPU.H"
146+
#endif
147+
140148
double time_monitor_init = 0;
141149

142150
double time_monitor_other = 0;

bashrc.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ export CANTERA_DATA=$CANTERA_ROOT/share/cantera/data
99
export LD_LIBRARY_PATH=$LIBTORCH_ROOT/lib:$LD_LIBRARY_PATH
1010
export LD_LIBRARY_PATH=$CANTERA_ROOT/lib:$LD_LIBRARY_PATH
1111
export AMGX_DIR=@AMGX_DIR@
12+
export ODE_GPU_SOLVER=@ODE_GPU_SOLVER@
1213

1314
export DF_APPBIN=pwd/platforms/$WM_OPTIONS/bin
1415
export DF_LIBBIN=pwd/platforms/$WM_OPTIONS/lib
1516
export PATH=$DF_APPBIN:$PATH
1617
export LD_LIBRARY_PATH=$DF_LIBBIN:$LD_LIBRARY_PATH
1718
export LD_LIBRARY_PATH=$DF_ROOT/src_gpu/build:$LD_LIBRARY_PATH
1819
export LD_LIBRARY_PATH=$AMGX_DIR/build:$LD_LIBRARY_PATH
19-
export LD_LIBRARY_PATH=$DF_ROOT/src/dfChemistryModel/DNNInferencer/build:$LD_LIBRARY_PATH
20+
export LD_LIBRARY_PATH=$DF_ROOT/src/dfChemistryModel/DNNInferencer/build:$LD_LIBRARY_PATH

configure.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ unset PYTORCH_INC
99
unset PYTORCH_LIB
1010
unset USE_GPUSOLVER
1111
unset AMGX_DIR
12+
unset ODE_GPU_SOLVER
1213

1314
print_usage() {
1415
echo "Usage: . install.sh --libtorch_no (default) | --libtorch_dir _path_to_libtorch | --libtorch_autodownload | --use_pytorch | --libcantera_dir _path_to_libcantera
15-
| --amgx_dir _path_to_amgx"
16+
| --amgx_dir _path_to_amgx | --use_ode_gpu_solver"
1617
}
1718

1819
# default
1920
LIBTORCH_AUTO=false
2021
USE_LIBTORCH=false
2122
USE_PYTORCH=false
2223
USE_GPUSOLVER=false
24+
USE_ODE_GPU_SOLVER=false
2325

2426
while test $# -gt 0; do
2527
case "$1" in
@@ -75,6 +77,10 @@ while test $# -gt 0; do
7577
fi
7678
shift
7779
;;
80+
--use_ode_gpu_solver)
81+
shift
82+
USE_ODE_GPU_SOLVER=true
83+
;;
7884
-h|--help)
7985
shift
8086
print_usage
@@ -163,6 +169,9 @@ fi
163169
if [ $USE_GPUSOLVER = true ]; then
164170
echo AMGX_DIR=$AMGX_DIR
165171
fi
172+
if [ $USE_ODE_GPU_SOLVER = true ]; then
173+
echo ODE_GPU_SOLVER=$OPENCC_PATH
174+
fi
166175

167176
cp bashrc.in bashrc
168177
sed -i "s#pwd#$PWD#g" ./bashrc
@@ -171,6 +180,7 @@ sed -i "s#PYTORCH_INC#$PYTORCH_INC#g" ./bashrc
171180
sed -i "s#PYTORCH_LIB#$PYTORCH_LIB#g" ./bashrc
172181
sed -i "s#LIBCANTERA_DIR#$LIBCANTERA_DIR#g" ./bashrc
173182
sed -i "s#@AMGX_DIR@#$AMGX_DIR#g" ./bashrc
183+
sed -i "s#@ODE_GPU_SOLVER@#$OPENCC_PATH#g" ./bashrc
174184

175185

176186

@@ -190,4 +200,4 @@ else
190200
cp -r $FOAM_SRC/lagrangian/turbulence src_orig/lagrangian
191201
cp -r $FOAM_SRC/regionModels/surfaceFilmModels src_orig/regionModels
192202
cp -r $FOAM_SRC/functionObjects/field src_orig/functionObjects
193-
fi
203+
fi

0 commit comments

Comments
 (0)