Skip to content

Commit

Permalink
cleanup to both SUNDIALS tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnonaka committed Sep 5, 2024
1 parent c5a3a7e commit 1215a42
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use_MRI = true
fast_dt_ratio = 0.02


# Use adaptive time stepping and set integrator relative and absolute tolerances
# Use adaptive time stepping (multi-stage integrators only!) and set integrator relative and absolute tolerances
# adapt_dt = true
# reltol = 1.0e-4
# abstol = 1.0e-9
Expand Down
35 changes: 13 additions & 22 deletions ExampleCodes/SUNDIALS/Reaction-Diffusion/Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ void main_main ()
Real reltol = 1.0e-4;
Real abstol = 1.0e-9;

// Reaction and Diffusion Coefficients
Real reaction_coef = 0.0;
Real diffusion_coef = 1.0;

// inputs parameters
{
// ParmParse is way of reading inputs from the inputs file
Expand Down Expand Up @@ -85,6 +89,8 @@ void main_main ()
pp.query("reltol",reltol);
pp.query("abstol",abstol);

pp.query("reaction_coef",reaction_coef);
pp.query("diffusion_coef",diffusion_coef);
}

// **********************************
Expand Down Expand Up @@ -132,7 +138,7 @@ void main_main ()
// How Boxes are distrubuted among MPI processes
DistributionMapping dm(ba);

// we allocate two phi multifabs; one will store the old state, the other the new.
// allocate phi MultiFab
MultiFab phi(ba, dm, Ncomp, Nghost);

// time = starting time in the simulation
Expand Down Expand Up @@ -171,8 +177,7 @@ void main_main ()
WriteSingleLevelPlotfile(pltfile, phi, {"phi"}, geom, time, 0);
}

auto rhs_fast_function = [&](MultiFab& S_rhs, MultiFab& S_data,
const Real /* time */) {
auto rhs_fast_function = [&](MultiFab& S_rhs, MultiFab& S_data, const Real /* time */) {

// fill periodic ghost cells
S_data.FillBoundary(geom.periodicity());
Expand All @@ -181,12 +186,6 @@ void main_main ()
auto& phi_data = S_data;
auto& phi_rhs = S_rhs;

//Reaction and Diffusion Coefficients
Real reaction_coef = 0.0;
Real diffusion_coef = 1.0;
ParmParse pp;
pp.query("diffusion_coef",diffusion_coef);

for ( MFIter mfi(phi_data); mfi.isValid(); ++mfi )
{
const Box& bx = mfi.validbox();
Expand All @@ -200,14 +199,14 @@ void main_main ()
phi_rhs_array(i,j,k) = diffusion_coef*( (phi_array(i+1,j,k) - 2.*phi_array(i,j,k) + phi_array(i-1,j,k)) / (dx[0]*dx[0])
+(phi_array(i,j+1,k) - 2.*phi_array(i,j,k) + phi_array(i,j-1,k)) / (dx[1]*dx[1])
#if (AMREX_SPACEDIM == 3)
+(phi_array(i,j,k+1) - 2.*phi_array(i,j,k) + phi_array(i,j,k-1)) / (dx[2]*dx[2]) );
+(phi_array(i,j,k+1) - 2.*phi_array(i,j,k) + phi_array(i,j,k-1)) / (dx[2]*dx[2])
#endif
);
});
}
};

auto rhs_function = [&](MultiFab& S_rhs, MultiFab& S_data,
const Real /* time */) {
auto rhs_function = [&](MultiFab& S_rhs, MultiFab& S_data, const Real /* time */) {

// fill periodic ghost cells
S_data.FillBoundary(geom.periodicity());
Expand All @@ -216,13 +215,6 @@ void main_main ()
auto& phi_data = S_data;
auto& phi_rhs = S_rhs;

//Reaction and Diffusion Coefficients
Real reaction_coef = 0.0;
Real diffusion_coef = 1.0;
ParmParse pp;
pp.query("reaction_coef",reaction_coef);
pp.query("diffusion_coef",diffusion_coef);

for ( MFIter mfi(phi_data); mfi.isValid(); ++mfi )
{
const Box& bx = mfi.validbox();
Expand All @@ -239,15 +231,14 @@ void main_main ()
phi_rhs_array(i,j,k) = diffusion_coef*( (phi_array(i+1,j,k) - 2.*phi_array(i,j,k) + phi_array(i-1,j,k)) / (dx[0]*dx[0])
+(phi_array(i,j+1,k) - 2.*phi_array(i,j,k) + phi_array(i,j-1,k)) / (dx[1]*dx[1])
#if (AMREX_SPACEDIM == 3)
+(phi_array(i,j,k+1) - 2.*phi_array(i,j,k) + phi_array(i,j,k-1)) / (dx[2]*dx[2]) )
+(phi_array(i,j,k+1) - 2.*phi_array(i,j,k) + phi_array(i,j,k-1)) / (dx[2]*dx[2])
#endif
-1.*reaction_coef*phi_array(i,j,k);
) - 1.*reaction_coef*phi_array(i,j,k);
}
});
}
};


TimeIntegrator<MultiFab> integrator(phi, time);
integrator.set_rhs(rhs_function);
if (use_MRI) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# AMREX_HOME defines the directory in which we will find all the AMReX code.
AMREX_HOME ?= ../../../../amrex
AMREX_HOME ?= ../../../../../amrex

DEBUG = FALSE
USE_MPI = TRUE
Expand All @@ -17,9 +17,9 @@ INCLUDE_LOCATIONS += ../Source

ifeq ($(USE_SUNDIALS),TRUE)
ifeq ($(USE_CUDA),TRUE)
SUNDIALS_ROOT ?= $(TOP)../../../../sundials/instdir_cuda
SUNDIALS_ROOT ?= $(TOP)../../../../../sundials/instdir_cuda
else
SUNDIALS_ROOT ?= $(TOP)../../../../sundials/instdir
SUNDIALS_ROOT ?= $(TOP)../../../../../sundials/instdir
endif
ifeq ($(NERSC_HOST),perlmutter)
SUNDIALS_LIB_DIR ?= $(SUNDIALS_ROOT)/lib64
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ dt = 1.e-4
#plot_int = 20
#dt = 2.5e-5

# Use adaptive time stepping (multi-stage integrators only!) and set integrator relative and absolute tolerances
# adapt_dt = true
# reltol = 1.0e-4
# abstol = 1.0e-9

# INTEGRATION
## integration.type can take on the following values:
## 0 or "ForwardEuler" => Native AMReX Forward Euler integrator
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main_main ()
Real dt;

// use adaptive time step (dt used to set output times)
int adapt_dt = 0;
bool adapt_dt = false;

// adaptive time step relative and absolute tolerances
Real reltol = 1.0e-4;
Expand Down Expand Up @@ -129,9 +129,6 @@ void main_main ()
// time = starting time in the simulation
Real time = 0.0;

Print() << "dt = " << dt << std::endl;
Print() << "Diffusive CFL time step = " << dx[0]*dx[0]/(2.*AMREX_SPACEDIM) << std::endl;

// **********************************
// INITIALIZE DATA

Expand Down Expand Up @@ -165,15 +162,15 @@ void main_main ()
WriteSingleLevelPlotfile(pltfile, phi, {"phi"}, geom, time, 0);
}

auto rhs_function = [&](MultiFab& S_rhs, MultiFab& S_data,
const Real /* time */) {
auto rhs_function = [&](MultiFab& S_rhs, MultiFab& S_data, const Real /* time */) {

// fill periodic ghost cells
S_data.FillBoundary(geom.periodicity());

// loop over boxes
auto& phi_data = S_data;
auto& phi_rhs = S_rhs;

for ( MFIter mfi(phi_data); mfi.isValid(); ++mfi )
{
const Box& bx = mfi.validbox();
Expand Down
File renamed without changes.

0 comments on commit 1215a42

Please sign in to comment.