forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
173 lines (140 loc) · 5.41 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <AMReX.H>
#include <AMReX_ParmParse.H>
#include <AMReX_PlotFileUtil.H>
#include <AMReX_Particles.H>
using namespace amrex;
int main(int argc, char* argv[])
{
amrex::Initialize(argc,argv);
{
const int nghost = 0;
int ncells, max_grid_size, ncomp, nlevs, nppc;
int restart_check = 0;
ParmParse pp;
pp.get("ncells", ncells);
pp.get("max_grid_size", max_grid_size);
pp.get("ncomp", ncomp);
pp.get("nlevs", nlevs);
pp.get("nppc", nppc);
pp.query("restart_check", restart_check);
AMREX_ALWAYS_ASSERT(nlevs < 2); // relax this later
IntVect domain_lo(AMREX_D_DECL(0, 0, 0));
IntVect domain_hi(AMREX_D_DECL(ncells-1, ncells-1, ncells-1));
const Box domain(domain_lo, domain_hi);
RealBox real_box;
for (int n = 0; n < AMREX_SPACEDIM; n++) {
real_box.setLo(n, 0.0);
real_box.setHi(n, 1.0);
}
// Define the refinement ratio
Vector<IntVect> ref_ratio(nlevs-1);
for (int lev = 1; lev < nlevs; lev++)
ref_ratio[lev-1] = IntVect(AMREX_D_DECL(2, 2, 2));
// This sets the boundary conditions to be doubly or triply periodic
int is_per[AMREX_SPACEDIM];
for (int i = 0; i < AMREX_SPACEDIM; i++)
is_per[i] = 1;
// This defines a Geometry object for each level
Vector<Geometry> geom(nlevs);
geom[0].define(domain, &real_box, CoordSys::cartesian, is_per);
for (int lev = 1; lev < nlevs; lev++) {
geom[lev].define(amrex::refine(geom[lev-1].Domain(), ref_ratio[lev-1]),
&real_box, CoordSys::cartesian, is_per);
}
Vector<BoxArray> ba(nlevs);
ba[0].define(domain);
// Now we make the refined level be the center eighth of the domain
if (nlevs > 1) {
int n_fine = ncells*ref_ratio[0][0];
IntVect refined_lo(D_DECL(n_fine/4,n_fine/4,n_fine/4));
IntVect refined_hi(D_DECL(3*n_fine/4-1,3*n_fine/4-1,3*n_fine/4-1));
// Build a box for the level 1 domain
Box refined_patch(refined_lo, refined_hi);
ba[1].define(refined_patch);
}
// break the BoxArrays at both levels into max_grid_size^3 boxes
for (int lev = 0; lev < nlevs; lev++) {
ba[lev].maxSize(max_grid_size);
}
Vector<DistributionMapping> dmap(nlevs);
Vector<std::unique_ptr<MultiFab> > mf(nlevs);
for (int lev = 0; lev < nlevs; lev++) {
dmap[lev] = DistributionMapping{ba[lev]};
mf[lev].reset(new MultiFab(ba[lev], dmap[lev], ncomp, nghost));
mf[lev]->setVal(lev);
}
// Add some particles
constexpr int NStructReal = 4;
constexpr int NStructInt = 1;
constexpr int NArrayReal = 8;
constexpr int NArrayInt = 3;
typedef ParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt> MyPC;
MyPC myPC(geom, dmap, ba, ref_ratio);
myPC.SetVerbose(false);
int num_particles = nppc * AMREX_D_TERM(ncells, * ncells, * ncells);
bool serialize = false;
int iseed = 451;
MyPC::ParticleInitData pdata = {1.0, 2.0, 3.0, 4.0, 5, 6.0,
7.0, 8.0, 9.0, 10.0, 11.0,
12.0, 13.0, 14, 15, 16};
myPC.InitRandom(num_particles, iseed, pdata, serialize);
// these don't really matter, make something up
const Real time = 0.0;
const Real dt = 0.0;
Vector<std::string> varnames;
for (int i = 0; i < ncomp; ++i)
{
varnames.push_back("component_" + std::to_string(i));
}
Vector<int> level_steps(nlevs, 0);
WriteMultiLevelPlotfile("plt00000", nlevs, amrex::GetVecOfConstPtrs(mf),
varnames, geom, time, level_steps, ref_ratio);
#ifdef AMREX_USE_HDF5
WriteMultiLevelPlotfileHDF5("plt00000", nlevs, amrex::GetVecOfConstPtrs(mf),
varnames, geom, time, level_steps, ref_ratio);
#endif
Vector<std::string> particle_realnames;
for (int i = 0; i < NStructReal + NArrayReal; ++i)
{
particle_realnames.push_back("particle_real_component_" + std::to_string(i));
}
Vector<std::string> particle_intnames;
for (int i = 0; i < NStructInt + NArrayInt; ++i)
{
particle_intnames.push_back("particle_int_component_" + std::to_string(i));
}
#ifdef AMREX_USE_HDF5
myPC.CheckpointHDF5("plt00000", "particle0", false, particle_realnames, particle_intnames);
#else
myPC.Checkpoint("plt00000", "particle0", false, particle_realnames, particle_intnames);
/* myPC.WriteAsciiFile("particle0_ascii"); */
#endif
if (restart_check)
{
MyPC newPC(geom, dmap, ba, ref_ratio);
#ifdef AMREX_USE_HDF5
newPC.RestartHDF5("plt00000", "particle0");
#else
newPC.Restart("plt00000", "particle0");
#endif
using PType = typename MyPC::SuperParticleType;
for (int icomp=0; icomp<NStructReal+NArrayReal+NStructInt+NArrayInt; ++icomp)
{
auto sm_new = amrex::ReduceSum(newPC,
[=] AMREX_GPU_HOST_DEVICE (const PType& p) -> Real
{
return p.rdata(1);
});
auto sm_old = amrex::ReduceSum(myPC,
[=] AMREX_GPU_HOST_DEVICE (const PType& p) -> Real
{
return p.rdata(1);
});
ParallelDescriptor::ReduceRealSum(sm_new);
ParallelDescriptor::ReduceRealSum(sm_old);
AMREX_ALWAYS_ASSERT(sm_old = sm_new);
}
}
}
amrex::Finalize();
}