Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "../../common.h"
4#include <cmath>
5#include <fsgrid.hpp>
6#include <iostream>
7
8using namespace std;
9using namespace fsgrids;
10
12
13// Very simplified version of CalculateDerivatives from fieldsolver/derivatives.cpp
14void calculateDerivatives(const fsgrid::FsStencil& stencil, fsgrids::perbspan perb,
17 std::array<Real, fsgrids::dperb::N_DPERB>& dPerB = dperb[stencil.ooo()];
18 std::array<Real, fsgrids::bfield::N_BFIELD>& centPerB = perb[stencil.ooo()];
19
20 // Calculate x-derivatives (is not TVD for AMR mesh):
21
22 {
23 const auto& leftPerB = perb[stencil.moo()];
24 const auto& rghtPerB = perb[stencil.poo()];
25
28
30 dPerB[fsgrids::dperb::dPERBydxx] = 0.0;
31 dPerB[fsgrids::dperb::dPERBzdxx] = 0.0;
32 } else {
35 }
36 }
37
38 // Calculate y-derivatives (is not TVD for AMR mesh):
39
40 {
41 const auto& leftPerB = perb[stencil.omo()];
42 const auto& rghtPerB = perb[stencil.opo()];
43
46
48 dPerB[fsgrids::dperb::dPERBxdyy] = 0.0;
49 dPerB[fsgrids::dperb::dPERBzdyy] = 0.0;
50 } else {
53 }
54 }
55
56 // Calculate z-derivatives (is not TVD for AMR mesh):
57 {
58 const auto& leftPerB = perb[stencil.oom()];
59 const auto& rghtPerB = perb[stencil.oop()];
60
63
65 dPerB[fsgrids::dperb::dPERBxdzz] = 0.0;
66 dPerB[fsgrids::dperb::dPERBydzz] = 0.0;
67 } else {
70 }
71 }
72
74 dPerB[fsgrids::dperb::dPERBxdyz] = 0.0;
75 dPerB[fsgrids::dperb::dPERBydxz] = 0.0;
76 dPerB[fsgrids::dperb::dPERBzdxy] = 0.0;
77 } else {
78 // Calculate xy mixed derivatives:
79 {
80 const auto& botLeft = perb[stencil.mmo()];
81 const auto& botRght = perb[stencil.pmo()];
82 const auto& topLeft = perb[stencil.mpo()];
83 const auto& topRght = perb[stencil.ppo()];
84
86 }
87
88 // Calculate xz mixed derivatives:
89 {
90 const auto& botLeft = perb[stencil.mom()];
91 const auto& botRght = perb[stencil.pom()];
92 const auto& topLeft = perb[stencil.mop()];
93 const auto& topRght = perb[stencil.pop()];
94
96 }
97
98 // Calculate yz mixed derivatives:
99 {
100 const auto& botLeft = perb[stencil.omm()];
101 const auto& botRght = perb[stencil.opm()];
102 const auto& topLeft = perb[stencil.omp()];
103 const auto& topRght = perb[stencil.opp()];
104
106 }
107 }
108}
109
110int main(int argc, char** argv) {
111
112 // Init MPI
113 int required = MPI_THREAD_FUNNELED;
114 int provided;
115 int myRank;
116 MPI_Init_thread(&argc, &argv, required, &provided);
117 if (required > provided) {
118 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
119 if (myRank == MASTER_RANK)
120 cerr << "(MAIN): MPI_Init_thread failed! Got " << provided << ", need " << required << endl;
121 exit(1);
122 }
123 const int masterProcessID = 0;
124
125 // Parse parameters
126 if (argc == 1) {
127 cerr << "Running with default options. Run main --help to see available settings." << endl;
128 }
129 for (int i = 1; i < argc; i++) {
130 cerr << "Unknown command line option \"" << argv[i] << "\"" << endl;
131 cerr << endl;
132 cerr << "main" << endl;
133 cerr << "Paramters:" << endl;
134 cerr << " none! :D" << endl;
135
136 return 1;
137 }
138
139 phiprof::initialize();
140
141 // Set up fsgrids
142 const std::array<int, 3> fsGridDimensions = {5, 5, 5};
143 const std::array<bool, 3> periodicity{true, true, true};
144
145 const std::array gridSpacing{P::dx_ini / pow(2, P::amrMaxSpatialRefLevel),
148 const std::array physicalGlobalStart{P::xmin, P::ymin, P::zmin};
149 const auto decomposition = P::manualFsGridDecomposition;
150
151 MPI_Comm parentComm = MPI_COMM_WORLD;
152 const auto numFsProcs = [&]() {
153 auto parentCommSize = 0;
154 MPI_Comm_size(parentComm, &parentCommSize);
155 const auto envVar = getenv("FSGRID_PROCS");
156 const auto fsgridProcs = envVar != NULL ? atoi(envVar) : 0;
157 return parentCommSize > fsgridProcs && fsgridProcs > 0 ? fsgridProcs : parentCommSize;
158 }();
159
160 FieldSolverGrid fsgrid(fsGridDimensions, parentComm, numFsProcs, periodicity, gridSpacing, physicalGlobalStart,
161 decomposition);
162 fsgrid::FsData<fsgrids::technical> technical(fsgrid.getNumStorageCells());
163 fsgrid::FsData<std::array<Real, fsgrids::bfield::N_BFIELD>> perb(fsgrid.getNumStorageCells());
164 fsgrid::FsData<std::array<Real, fsgrids::dperb::N_DPERB>> dperb(fsgrid.getNumStorageCells());
165
166 // Fill in values
167 for (int i = 0; i < 5; i++) {
168 for (int j = 0; j < 5; j++) {
169 for (int k = 0; k < 5; k++) {
170 const auto stencil = fsgrid.makeStencil(i, j, k);
171 perb[stencil.ooo()][PERBX] = sin(j / 5. * 2. * M_PI) * sin(k / 5. * 2. * M_PI);
172 perb[stencil.ooo()][PERBY] = sin(i / 5. * 2. * M_PI) * sin(k / 5. * 2. * M_PI);
173 perb[stencil.ooo()][PERBZ] = sin(i / 5. * 2. * M_PI) * sin(j / 5. * 2. * M_PI);
175 }
176 }
177 }
178
179 // Output raw fsgrid to gnuplottable matrix file
180 ofstream fsGridFile("PERBX_fsgrid.dat");
181 for (int j = 0; j < 5; j++) {
182 for (int k = 0; k < 5; k++) {
183 const auto stencil = fsgrid.makeStencil(2, j, k);
184 fsGridFile << perb[stencil.ooo()][PERBX] << " ";
185 }
186 fsGridFile << endl;
187 }
188 fsGridFile.close();
189 cout << "--- Wrote fsgrid to PERBX_fsgrid.dat. ---" << endl;
190
191 // Calculate derivatives
192 for (int i = 0; i < 5; i++) {
193 for (int j = 0; j < 5; j++) {
194 for (int k = 0; k < 5; k++) {
195 const auto stencil = fsgrid.makeStencil(i, j, k);
196 calculateDerivatives(stencil, perb, dperb, technical, fsgrid);
197 }
198 }
199 }
200
201 // Sample at random points.
202 std::map<std::array<int, 3>, std::array<Real, Rec::N_REC_COEFFICIENTS>> cache;
203 ofstream sampleFile("samples.dat");
204 sampleFile << "# x y z Bx By Bz" << endl;
205 for (int i = 0; i < 1000; i++) {
206 // std::array<Real, 3> randPos{5.*rand()/RAND_MAX, 5.*rand()/RAND_MAX, 5.*rand()/RAND_MAX};
207 std::array<Real, 3> randPos{2.5, 5. * rand() / RAND_MAX, 5. * rand() / RAND_MAX};
208 std::array<int, 3> fsgridCell;
209 for (int c = 0; c < 3; c++) {
210 fsgridCell[c] = floor(randPos[c]); // Round-to-int, as DX = 1.
211 }
212 std::array<Real, 3> B = interpolatePerturbedB(perb.view(), dperb.view(), technical.view(), fsgrid, cache,
213 fsgridCell[0], fsgridCell[1], fsgridCell[2], randPos);
214 sampleFile << randPos[0] << " " << randPos[1] << " " << randPos[2] << " " << B[0] << " " << B[1] << " " << B[2] << endl;
215 }
216
217 cout << "--- DONE. ---" << endl;
218 return 0;
219}
for i
Definition Dispersion.m:24
Constants c
Definition Dispersion.m:45
void calculateDerivatives(const fsgrid::FsStencil &stencil, fsgrids::perbspan perb, fsgrids::dperbspan dperb, fsgrids::technicalspan technical, FieldSolverGrid &fsgrid)
Definition main.cpp:14
#define MASTER_RANK
Definition common.h:67
fsgrid::FsGrid< FS_STENCIL_WIDTH > FieldSolverGrid
Definition definitions.h:78
std::array< Real, 3 > interpolatePerturbedB(fsgrids::perbspan perb, fsgrids::constdperbspan dperb, fsgrids::technicalspan technical, FieldSolverGrid &fsgrid, std::map< std::array< int, 3 >, std::array< Real, Rec::N_REC_COEFFICIENTS > > &reconstructionCoefficientsCache, cint i, cint j, cint k, const std::array< Real, 3 > x)
const Real FOURTH
Definition fs_common.h:53
Definitions of the limiter functions used in the field solver.
T limiter(const T &left, const T &cent, const T &rght)
Definition fs_limiters.h:85
int myRank
Definition gpu_base.cpp:48
const int j
const int k
uint32_t uint
std::span< std::array< Real, fsgrids::bfield::N_BFIELD > > perbspan
Definition common.h:434
std::span< technical > technicalspan
Definition common.h:452
std::span< std::array< Real, fsgrids::dperb::N_DPERB > > dperbspan
Definition common.h:442
@ PERBY
Definition common.h:276
@ PERBZ
Definition common.h:277
@ PERBX
Definition common.h:275
@ dPERBzdxy
Definition common.h:338
@ dPERBzdy
Definition common.h:329
@ dPERBzdyy
Definition common.h:337
@ dPERBzdxx
Definition common.h:336
@ dPERBydxx
Definition common.h:333
@ dPERBydx
Definition common.h:326
@ dPERBydxz
Definition common.h:335
@ dPERBzdx
Definition common.h:328
@ dPERBxdzz
Definition common.h:331
@ dPERBxdyz
Definition common.h:332
@ dPERBydzz
Definition common.h:334
@ dPERBxdy
Definition common.h:324
@ dPERBxdyy
Definition common.h:330
@ dPERBxdz
Definition common.h:325
@ dPERBydz
Definition common.h:327
static Real dz_ini
Definition parameters.h:46
static uint ohmHallTerm
Definition parameters.h:142
static Real dx_ini
Definition parameters.h:44
static int amrMaxSpatialRefLevel
Definition parameters.h:190
static Real ymin
Definition parameters.h:40
static Real xmin
Definition parameters.h:38
static Real zmin
Definition parameters.h:42
static Real dy_ini
Definition parameters.h:45
static std::array< fsgrid::Task_t, 3 > manualFsGridDecomposition
Definition parameters.h:245
int main()
static ARCH_HOSTDEV VecSimple< T > floor(VecSimple< T > const &a)