-
Notifications
You must be signed in to change notification settings - Fork 18
/
hermes-3.cxx
375 lines (324 loc) · 13.2 KB
/
hermes-3.cxx
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright B.Dudson, J.Leddy, University of York, 2016-2020
email: [email protected]
This file is part of Hermes-3 (Hot ion, multifluid)
Hermes is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hermes is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hermes. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hermes-3.hxx"
#include "revision.hxx"
#include "include/adas_carbon.hxx"
#include "include/adas_neon.hxx"
#include "include/adas_lithium.hxx"
#include "include/amjuel_helium.hxx"
#include "include/amjuel_hyd_ionisation.hxx"
#include "include/amjuel_hyd_recombination.hxx"
#include "include/anomalous_diffusion.hxx"
#include "include/classical_diffusion.hxx"
#include "include/binormal_stpm.hxx"
#include "include/collisions.hxx"
#include "include/diamagnetic_drift.hxx"
#include "include/electromagnetic.hxx"
#include "include/electron_force_balance.hxx"
#include "include/electron_viscosity.hxx"
#include "include/evolve_density.hxx"
#include "include/evolve_energy.hxx"
#include "include/evolve_momentum.hxx"
#include "include/evolve_pressure.hxx"
#include "include/fixed_density.hxx"
#include "include/fixed_fraction_ions.hxx"
#include "include/fixed_fraction_radiation.hxx"
#include "include/fixed_temperature.hxx"
#include "include/fixed_velocity.hxx"
#include "include/hydrogen_charge_exchange.hxx"
#include "include/ion_viscosity.hxx"
#include "include/ionisation.hxx"
#include "include/isothermal.hxx"
#include "include/neutral_boundary.hxx"
#include "include/neutral_mixed.hxx"
#include "include/neutral_parallel_diffusion.hxx"
#include "include/noflow_boundary.hxx"
#include "include/polarisation_drift.hxx"
#include "include/quasineutral.hxx"
#include "include/recycling.hxx"
#include "include/relax_potential.hxx"
#include "include/scale_timederivs.hxx"
#include "include/set_temperature.hxx"
#include "include/sheath_boundary.hxx"
#include "include/sheath_boundary_insulating.hxx"
#include "include/sheath_boundary_simple.hxx"
#include "include/sheath_closure.hxx"
#include "include/simple_conduction.hxx"
#include "include/snb_conduction.hxx"
#include "include/solkit_hydrogen_charge_exchange.hxx"
#include "include/solkit_neutral_parallel_diffusion.hxx"
#include "include/sound_speed.hxx"
#include "include/thermal_force.hxx"
#include "include/transform.hxx"
#include "include/upstream_density_feedback.hxx"
#include "include/temperature_feedback.hxx"
#include "include/detachment_controller.hxx"
#include "include/vorticity.hxx"
#include "include/zero_current.hxx"
#include "include/simple_pump.hxx"
#include <bout/constants.hxx>
#include <bout/boundary_factory.hxx>
#include <bout/boundary_op.hxx>
#include <bout/field_factory.hxx>
#include "include/loadmetric.hxx"
class DecayLengthBoundary : public BoundaryOp {
public:
DecayLengthBoundary() : gen(nullptr) {}
DecayLengthBoundary(BoundaryRegion* region, std::shared_ptr<FieldGenerator> g)
: BoundaryOp(region), gen(std::move(g)) {}
using BoundaryOp::clone;
/// Create a copy of this boundary condition
/// This is called by the Boundary Factory
BoundaryOp* clone(BoundaryRegion* region, const std::list<std::string>& args) override {
std::shared_ptr<FieldGenerator> newgen;
if (!args.empty()) {
// First argument should be an expression
newgen = FieldFactory::get()->parse(args.front());
}
return new DecayLengthBoundary(region, newgen);
}
// Only implementing for Field3D, no time dependence
void apply(Field3D& f) override {
// Ensure that field and boundary are on the same mesh
Mesh* mesh = bndry->localmesh;
ASSERT1(mesh == f.getMesh());
// Get cell radial length
Coordinates *coord = mesh->getCoordinates();
Field2D dx = coord->dx;
Field2D g11 = coord->g11;
Field2D dr = dx / sqrt(g11); // cell radial length. dr = dx/(Bpol * R) and g11 = (Bpol*R)**2
// Only implemented for cell centre quantities
ASSERT1(f.getLocation() == CELL_CENTRE);
// This loop goes over the first row of boundary cells (in X and Y)
for (bndry->first(); !bndry->isDone(); bndry->next1d()) {
for (int zk = 0; zk < mesh->LocalNz; zk++) { // Loop over Z points
BoutReal decay_length = 3; // Default decay length is 3 normalised units (usually ~3mm)
if (gen) {
// Pick up the boundary condition setting from the input file
// Must be specified in normalised units like the other BCs inputs
decay_length = gen->generate(bout::generator::Context(bndry, zk, CELL_CENTRE, 0.0, mesh));
}
// Set value in inner guard cell f(bndry->x, bndry->y, zk)
// using the final domain cell value f(bndry->x - bndry->bx, bndry->y - bndry->by, zk)
// Note: (bx, by) is the direction into the boundary, so
// (1, 0) X outer boundary (SOL)
// (-1, 0) X inner boundary (Core or PF)
// (0, 1) Y upper boundary (outer lower target)
// (0, -1) Y lower boundary (inner lower target)
// Distance between final cell centre and inner guard cell centre in normalised units
BoutReal distance = 0.5 * (dr(bndry->x, bndry->y) +
dr(bndry->x - bndry->bx, bndry->y - bndry->by));
// Exponential decay
f(bndry->x, bndry->y, zk) =
f(bndry->x - bndry->bx, bndry->y - bndry->by, zk) * exp(-1 * distance / decay_length);
// Set any remaining guard cells (i.e. the outer guards) to the same value
// Should the outer guards have the decay continue, or just copy what the inners have?
for (int i = 1; i < bndry->width; i++) {
f(bndry->x + i * bndry->bx, bndry->y + i * bndry->by, zk) = f(bndry->x, bndry->y, zk);
}
}
}
}
void apply(Field2D& f) override {
throw BoutException("DecayLengthBoundary not implemented for Field2D");
}
private:
std::shared_ptr<FieldGenerator> gen; // Generator
};
int Hermes::init(bool restarting) {
auto &options = Options::root()["hermes"];
output.write("\nGit Version of Hermes: {:s}\n", hermes::version::revision);
options["revision"] = hermes::version::revision;
options["revision"].setConditionallyUsed();
output.write("Slope limiter: {}\n", hermes::limiter_typename);
options["slope_limiter"] = hermes::limiter_typename;
options["slope_limiter"].setConditionallyUsed();
// Choose normalisations
Tnorm = options["Tnorm"].doc("Reference temperature [eV]").withDefault(100.);
Nnorm = options["Nnorm"].doc("Reference density [m^-3]").withDefault(1e19);
Bnorm = options["Bnorm"].doc("Reference magnetic field [T]").withDefault(1.0);
Cs0 = sqrt(SI::qe * Tnorm / SI::Mp); // Reference sound speed [m/s]
Omega_ci = SI::qe * Bnorm / SI::Mp; // Ion cyclotron frequency [1/s]
rho_s0 = Cs0 / Omega_ci; // Length scale [m]
// Put normalisation quantities into an Options to use later
units["inv_meters_cubed"] = Nnorm;
units["eV"] = Tnorm;
units["Tesla"] = Bnorm;
units["seconds"] = 1./Omega_ci;
units["meters"] = rho_s0;
// Put into the options tree, so quantities can be normalised
// when creating components
Options::root()["units"] = units.copy();
Options::root()["units"].setConditionallyUsed();
// Add the decay length boundary condition to the boundary factory
// This will make it available as an input option
// e.g. bndry_sol = decaylength(0.003 / rho_s0) sets up a decay length of 3mm
BoundaryFactory::getInstance()->add(new DecayLengthBoundary(), "decaylength");
/////////////////////////////////////////////////////////
// Load metric tensor from the mesh, passing length and B
// field normalisations
TRACE("Loading metric tensor");
if (options["loadmetric"]
.doc("Load Rxy, Bpxy etc. to create orthogonal metric?")
.withDefault(true)) {
LoadMetric(rho_s0, Bnorm);
} else if (options["normalise_metric"]
.doc("Normalise input metric tensor? (assumes input is in SI units)")
.withDefault<bool>(true)) {
Coordinates *coord = mesh->getCoordinates();
// To use non-orthogonal metric
// Normalise
coord->dx /= rho_s0 * rho_s0 * Bnorm;
coord->Bxy /= Bnorm;
// Metric is in grid file - just need to normalise
coord->g11 /= SQ(Bnorm * rho_s0);
coord->g22 *= SQ(rho_s0);
coord->g33 *= SQ(rho_s0);
coord->g12 /= Bnorm;
coord->g13 /= Bnorm;
coord->g23 *= SQ(rho_s0);
coord->J *= Bnorm / rho_s0;
coord->g_11 *= SQ(Bnorm * rho_s0);
coord->g_22 /= SQ(rho_s0);
coord->g_33 /= SQ(rho_s0);
coord->g_12 *= Bnorm;
coord->g_13 *= Bnorm;
coord->g_23 /= SQ(rho_s0);
coord->geometry(); // Calculate other metrics
}
// Tell the components if they are restarting
options["restarting"] = restarting;
options["restarting"].setConditionallyUsed();
TRACE("Creating components");
// Create the components
// Here options is passed as the scheduler configuration, so that
// settings in [hermes] are used.
// Options::root() is passed as the root of the component options, so that
// individual components use their own sections, rather than subsections of [hermes].
scheduler = ComponentScheduler::create(options, Options::root(), solver);
// Preconditioner
setPrecon((preconfunc)&Hermes::precon);
return 0;
}
int Hermes::rhs(BoutReal time) {
// Need to reset the state, since fields may be modified in transform steps
state = Options();
set(state["time"], time);
state["units"] = units.copy();
// Call all the components
scheduler->transform(state);
return 0;
}
/*!
* Preconditioner. Solves the heat conduction
*
* @param[in] t The simulation time
* @param[in] gamma Factor in front of the Jacobian in (I - gamma*J). Related
* to timestep
* @param[in] delta Not used here
*/
int Hermes::precon(BoutReal t, BoutReal gamma, BoutReal UNUSED(delta)) {
state["time"] = t;
scheduler->precon(state, gamma);
return 0;
}
void Hermes::outputVars(Options& options) {
AUTO_TRACE();
// Save the Hermes version in the output dump files
options["HERMES_REVISION"].force(hermes::version::revision);
options["HERMES_SLOPE_LIMITER"].force(hermes::limiter_typename);
// Save normalisation quantities. These may be used by components
// to calculate conversion factors to SI units
set_with_attrs(options["Tnorm"], Tnorm, {
{"units", "eV"},
{"conversion", 1}, // Already in SI units
{"standard_name", "temperature normalisation"},
{"long_name", "temperature normalisation"}
});
set_with_attrs(options["Nnorm"], Nnorm, {
{"units", "m^-3"},
{"conversion", 1},
{"standard_name", "density normalisation"},
{"long_name", "Number density normalisation"}
});
set_with_attrs(options["Bnorm"], Bnorm, {
{"units", "T"},
{"conversion", 1},
{"standard_name", "magnetic field normalisation"},
{"long_name", "Magnetic field normalisation"}
});
set_with_attrs(options["Cs0"], Cs0, {
{"units", "m/s"},
{"conversion", 1},
{"standard_name", "velocity normalisation"},
{"long_name", "Sound speed normalisation"}
});
set_with_attrs(options["Omega_ci"], Omega_ci, {
{"units", "s^-1"},
{"conversion", 1},
{"standard_name", "frequency normalisation"},
{"long_name", "Cyclotron frequency normalisation"}
});
set_with_attrs(options["rho_s0"], rho_s0, {
{"units", "m"},
{"conversion", 1},
{"standard_name", "length normalisation"},
{"long_name", "Gyro-radius length normalisation"}
});
scheduler->outputVars(options);
}
void Hermes::restartVars(Options& options) {
AUTO_TRACE();
set_with_attrs(options["Tnorm"], Tnorm, {
{"units", "eV"},
{"conversion", 1}, // Already in SI units
{"standard_name", "temperature normalisation"},
{"long_name", "temperature normalisation"}
});
set_with_attrs(options["Nnorm"], Nnorm, {
{"units", "m^-3"},
{"conversion", 1},
{"standard_name", "density normalisation"},
{"long_name", "Number density normalisation"}
});
set_with_attrs(options["Bnorm"], Bnorm, {
{"units", "T"},
{"conversion", 1},
{"standard_name", "magnetic field normalisation"},
{"long_name", "Magnetic field normalisation"}
});
set_with_attrs(options["Cs0"], Cs0, {
{"units", "m/s"},
{"conversion", 1},
{"standard_name", "velocity normalisation"},
{"long_name", "Sound speed normalisation"}
});
set_with_attrs(options["Omega_ci"], Omega_ci, {
{"units", "s^-1"},
{"conversion", 1},
{"standard_name", "frequency normalisation"},
{"long_name", "Cyclotron frequency normalisation"}
});
set_with_attrs(options["rho_s0"], rho_s0, {
{"units", "m"},
{"conversion", 1},
{"standard_name", "length normalisation"},
{"long_name", "Gyro-radius length normalisation"}
});
scheduler->restartVars(options);
}
// Standard main() function
BOUTMAIN(Hermes);