Skip to content

Commit ffbfc5a

Browse files
authored
Heat Equation restart tutorial (#146)
1 parent 65cdc73 commit ffbfc5a

File tree

8 files changed

+442
-1
lines changed

8 files changed

+442
-1
lines changed

Docs/source/Basic_Tutorial.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ that see the Building_libamrex chapter of the amrex docs here. `here <https://am
7272

7373
The HeatEquation examples solve a 2D or 3D (determined by how you set DIM in the GNUmakefile)
7474
heat equation explicitly on a domain-decomposed mesh. This example is described in detail in
75-
the Basics_ chapter of the amrex Documentation
75+
the Basics_ chapter of the amrex Documentation.
76+
There is also an example code, HeadEquation_Restart, that shows how to add restart capability to a time-advancement code.
7677

7778
.. _Basics: https://amrex-codes.github.io/amrex/docs_html/Basics.html
7879

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
if (AMReX_SPACEDIM EQUAL 1)
2+
return()
3+
endif ()
4+
5+
# List of source files
6+
set(_sources main.cpp checkpoint.cpp myfunc.H)
7+
list(TRANSFORM _sources PREPEND "Source/")
8+
9+
# List of input files
10+
file( GLOB_RECURSE _input_files LIST_DIRECTORIES false Exec/input* )
11+
12+
setup_tutorial(_sources _input_files)
13+
14+
unset( _sources )
15+
unset( _input_files )
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# AMREX_HOME defines the directory in which we will find all the AMReX code.
2+
AMREX_HOME ?= ../../../../../amrex
3+
4+
DEBUG = FALSE
5+
USE_MPI = FALSE
6+
USE_OMP = FALSE
7+
COMP = gnu
8+
DIM = 3
9+
10+
include $(AMREX_HOME)/Tools/GNUMake/Make.defs
11+
12+
include ../Source/Make.package
13+
VPATH_LOCATIONS += ../Source
14+
INCLUDE_LOCATIONS += ./Source
15+
16+
include $(AMREX_HOME)/Src/Base/Make.package
17+
18+
include $(AMREX_HOME)/Tools/GNUMake/Make.rules
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
n_cell = 32
2+
max_grid_size = 16
3+
4+
nsteps = 1000
5+
plot_int = 100
6+
chk_int = 100
7+
8+
dt = 1.e-5
9+
10+
restart = -1
11+
#restart = 200
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CEXE_sources += main.cpp
2+
CEXE_sources += checkpoint.cpp
3+
CEXE_headers += myfunc.H
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include "myfunc.H"
2+
3+
namespace {
4+
void GotoNextLine (std::istream& is)
5+
{
6+
constexpr std::streamsize bl_ignore_max { 100000 };
7+
is.ignore(bl_ignore_max, '\n');
8+
}
9+
}
10+
11+
// create a checkpoint directory
12+
// write out time and BoxArray to a Header file
13+
// write out multifab data
14+
void WriteCheckpoint(const int& step,
15+
const Real& time,
16+
const MultiFab& phi)
17+
{
18+
19+
20+
// checkpoint file name, e.g., chk00010
21+
const std::string& checkpointname = Concatenate("chk",step,5);
22+
23+
BoxArray ba = phi.boxArray();
24+
25+
// single level problem
26+
int nlevels = 1;
27+
28+
// ---- prebuild a hierarchy of directories
29+
// ---- dirName is built first. if dirName exists, it is renamed. then build
30+
// ---- dirName/subDirPrefix_0 .. dirName/subDirPrefix_nlevels-1
31+
// ---- if callBarrier is true, call ParallelDescriptor::Barrier()
32+
// ---- after all directories are built
33+
// ---- ParallelDescriptor::IOProcessor() creates the directories
34+
PreBuildDirectorHierarchy(checkpointname, "Level_", nlevels, true);
35+
36+
VisMF::IO_Buffer io_buffer(VisMF::IO_Buffer_Size);
37+
38+
// write Header file to store time and BoxArray
39+
if (ParallelDescriptor::IOProcessor()) {
40+
41+
std::ofstream HeaderFile;
42+
HeaderFile.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size());
43+
std::string HeaderFileName(checkpointname + "/Header");
44+
HeaderFile.open(HeaderFileName.c_str(), std::ofstream::out |
45+
std::ofstream::trunc |
46+
std::ofstream::binary);
47+
48+
if( !HeaderFile.good()) {
49+
FileOpenFailed(HeaderFileName);
50+
}
51+
52+
HeaderFile.precision(17);
53+
54+
// write out title line
55+
HeaderFile << "Checkpoint file for MagneX\n";
56+
57+
// write out time
58+
HeaderFile << time << "\n";
59+
60+
// write the BoxArray
61+
ba.writeOn(HeaderFile);
62+
HeaderFile << '\n';
63+
}
64+
65+
// write the MultiFab data to, e.g., chk00010/Level_0/
66+
VisMF::Write(phi, MultiFabFileFullPrefix(0, checkpointname, "Level_", "phi"));
67+
}
68+
69+
// read in the time and BoxArray, then create a DistributionMapping
70+
// Define phi and fill it with data from the checkpoint file
71+
void ReadCheckpoint(const int& restart,
72+
Real& time,
73+
MultiFab& phi,
74+
BoxArray& ba,
75+
DistributionMapping& dm)
76+
{
77+
78+
// checkpoint file name, e.g., chk00010
79+
const std::string& checkpointname = Concatenate("chk",restart,5);
80+
81+
Print() << "Restart from checkpoint " << checkpointname << "\n";
82+
83+
VisMF::IO_Buffer io_buffer(VisMF::GetIOBufferSize());
84+
85+
std::string line, word;
86+
87+
// Header
88+
{
89+
std::string File(checkpointname + "/Header");
90+
Vector<char> fileCharPtr;
91+
ParallelDescriptor::ReadAndBcastFile(File, fileCharPtr);
92+
std::string fileCharPtrString(fileCharPtr.dataPtr());
93+
std::istringstream is(fileCharPtrString, std::istringstream::in);
94+
95+
// read in title line
96+
std::getline(is, line);
97+
98+
// read in time
99+
is >> time;
100+
GotoNextLine(is);
101+
102+
// read in BoxArray from Header
103+
ba.readFrom(is);
104+
GotoNextLine(is);
105+
106+
// create a distribution mapping
107+
dm.define(ba, ParallelDescriptor::NProcs());
108+
109+
// Nghost = number of ghost cells for each array
110+
int Nghost = 1;
111+
112+
// Ncomp = number of components for each array
113+
int Ncomp = 1;
114+
115+
phi.define(ba, dm, Ncomp, Nghost);
116+
}
117+
118+
// read in the MultiFab data
119+
VisMF::Read(phi, MultiFabFileFullPrefix(0, checkpointname, "Level_", "phi"));
120+
121+
}

0 commit comments

Comments
 (0)