Skip to content

Commit ca9c9cd

Browse files
committed
Set GravRadPartile3D for test
1 parent 88338ac commit ca9c9cd

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

inputs/GravRadParticle3D.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ quokka.proj.particles = CIC_particles # List of particle types to include in out
5353

5454
max_timesteps = 2
5555

56-
tiny_profiler.enabled = 0
56+
tiny_profiler.enabled = 0
57+
58+
density_floor = 1.0e-7
59+
density_floor_expr = "1.0e-7 * (3.0 - sqrt(x*x + y*y + z*z) / 2.0)"

src/problems/GravRadParticle3D/testGravRadParticle3D.cpp

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "particles/PhysicsParticles.hpp"
1818
#include "radiation/radiation_system.hpp"
1919
#include "util/BC.hpp"
20+
#include "util/fextract.hpp"
21+
#ifdef HAVE_PYTHON
22+
#include "util/matplotlibcpp.h"
23+
#endif
2024

2125
struct ParticleProblem {
2226
};
@@ -29,7 +33,7 @@ constexpr double initial_Egas = 1.0e-5;
2933
constexpr double c = 100.0; // speed of light
3034
constexpr double chat = 2.0; // reduced speed of light
3135
constexpr double kappa0 = 1.0e-20; // opacity
32-
constexpr double rho0 = 1.0e-8;
36+
constexpr double rho0 = 1.0e-9;
3337
constexpr double m_H = C::m_p + C::m_e;
3438

3539
const double lum1 = 1.0;
@@ -142,6 +146,12 @@ auto QuokkaSimulation<ParticleProblem>::ComputeProjections(const amrex::Directio
142146
return proj;
143147
}
144148

149+
auto localDensityFloor(amrex::Real x, amrex::Real y, amrex::Real z) -> amrex::Real
150+
{
151+
// density_floor_expr = "1.0e-7 * (3.0 - sqrt(x*x + y*y + z*z) / 2.0)"
152+
return std::max(1.0e-7, 1.0e-7 * (3.0 - std::sqrt(x * x + y * y + z * z) / 2.0));
153+
}
154+
145155
auto problem_main() -> int
146156
{
147157
// Problem parameters
@@ -156,11 +166,60 @@ auto problem_main() -> int
156166
// initialize
157167
sim.setInitialConditions();
158168

159-
// evolve
160-
sim.evolve();
169+
// read output variables
170+
auto [position, values] = fextract(sim.state_new_cc_[0], sim.Geom(0), 2, 0.0, true); // z direction
171+
const int nz = static_cast<int>(position.size());
161172

162173
int status = 0;
163174

175+
// extract density and check floor
176+
std::vector<double> zs(nz);
177+
std::vector<double> rho_z(nz);
178+
std::vector<double> custom_floor_z(nz);
179+
amrex::Real min_density = std::numeric_limits<amrex::Real>::max();
180+
amrex::Real min_density_ratio = std::numeric_limits<amrex::Real>::max();
181+
for (int i = 0; i < nz; ++i) {
182+
amrex::Real const z = position[i];
183+
custom_floor_z.at(i) = localDensityFloor(0.0, 0.0, z); // note that the real x and y are 0.5 * delta_x
184+
amrex::Real const rho = values.at(RadSystem<ParticleProblem>::gasDensity_index)[i];
185+
zs.at(i) = z;
186+
rho_z.at(i) = rho;
187+
min_density = std::min(min_density, rho);
188+
min_density_ratio = std::min(min_density_ratio, rho / custom_floor_z.at(i));
189+
}
190+
191+
// Check that custom floor is working: min_density_ratio should not be smaller than 0.98
192+
if (min_density_ratio > 0.99) {
193+
amrex::Print() << "Custom density floor test PASSED: min density ratio = " << min_density_ratio << " > 0.99\n";
194+
} else {
195+
amrex::Print() << "Custom density floor test FAILED: min density ratio = " << min_density_ratio << " < 0.99\n";
196+
status = 1;
197+
}
198+
199+
#ifdef HAVE_PYTHON
200+
// Plot results
201+
matplotlibcpp::clf();
202+
matplotlibcpp::ylim(0.9e-7, 3.1e-7);
203+
204+
std::map<std::string, std::string> rho_args;
205+
std::map<std::string, std::string> custom_floor_args;
206+
rho_args["label"] = "rho";
207+
rho_args["linestyle"] = "-";
208+
rho_args["color"] = "C0";
209+
custom_floor_args["label"] = "custom floor";
210+
custom_floor_args["linestyle"] = "--";
211+
custom_floor_args["color"] = "C1";
212+
matplotlibcpp::plot(zs, rho_z, rho_args);
213+
matplotlibcpp::plot(zs, custom_floor_z, custom_floor_args);
214+
215+
matplotlibcpp::legend();
216+
matplotlibcpp::title("Custom density floor: 1.0e-7*(3-r/2)");
217+
matplotlibcpp::save("./grav_rad_particle_3d_density_floor_z.pdf");
218+
#endif // HAVE_PYTHON
219+
220+
// evolve
221+
sim.evolve();
222+
164223
// compute total radiation energy
165224
const double total_Erad_over_vol = sim.state_new_cc_[0].sum(RadSystem<ParticleProblem>::radEnergy_index);
166225
const double dx = sim.Geom(0).CellSize(0);
@@ -266,11 +325,12 @@ auto problem_main() -> int
266325

267326
const double rel_err_tol = 1.0e-7;
268327
const double rel_position_error_tol = t_sim < 1.0 ? 2.0e-4 : 2.0e-3;
269-
status = 1;
270328
if (rel_err < rel_err_tol && rel_position_error_cicrad < rel_position_error_tol && rel_position_error_cic < rel_position_error_tol &&
271329
rel_position_error_rad < rel_position_error_tol) {
272-
status = 0;
273330
amrex::Print() << "Relative error within tolerance.\n";
331+
} else {
332+
status = 1;
333+
amrex::Print() << "Relative error beyond tolerance: rel_err = " << rel_err << ", rel_position_error_cicrad = " << rel_position_error_cicrad << ", rel_position_error_cic = " << rel_position_error_cic << ", rel_position_error_rad = " << rel_position_error_rad << "\n";
274334
}
275335

276336
amrex::Print() << "Exact positions of the CICRad particles should be: " << exact_x << ", " << exact_y << ", " << exact_z << "\n";
@@ -294,8 +354,7 @@ auto problem_main() -> int
294354
amrex::Print() << "Relative L1 norm on Rad particle positions = " << rel_position_error_rad << "\n";
295355

296356
// Cleanup and exit
297-
amrex::Print() << "Finished."
298-
<< "\n";
357+
amrex::Print() << "Finished.\n";
299358
}
300359

301360
return status;

0 commit comments

Comments
 (0)