OpenImpala
Image-based parallel transport property solver
Loading...
Searching...
No Matches
PhysicsConfig.H
Go to the documentation of this file.
1
4// --- PhysicsConfig.H ---
5//
6// Lightweight configuration for different Laplacian-type physics problems.
7//
8// The TortuosityHypre / EffectiveDiffusivityHypre solvers compute an effective
9// transport ratio (D_eff / D_phase). This struct maps that raw output to
10// the appropriate physical quantity: effective conductivity, formation factor,
11// thermal conductivity, etc.
12//
13// Usage:
14// auto physics = OpenImpala::PhysicsConfig::fromParmParse();
15// amrex::Real sigma_eff = physics.effectiveProperty(D_eff_ratio);
16//
17// Input file (all optional — defaults to Diffusion with unit bulk property):
18// physics.type = electrical_conductivity
19// physics.bulk_property = 1.5e7
20//
21// Unit testing:
22// Define OPENIMPALA_UNIT_TEST before including this header to compile
23// without AMReX dependencies. In that mode, fromParmParse() and print()
24// are unavailable; use fromTypeString() and direct construction instead.
25
26#ifndef PHYSICSCONFIG_H
27#define PHYSICSCONFIG_H
28
29#ifndef OPENIMPALA_UNIT_TEST
30#include <AMReX_REAL.H>
31#include <AMReX_ParmParse.H>
32#include <AMReX_Print.H>
33#include <AMReX_ParallelDescriptor.H>
34#endif
35
36#include <string>
37#include <algorithm>
38#include <cmath>
39#include <iomanip>
40#include <limits>
41#include <ostream>
42
43namespace OpenImpala {
44
45// When compiling for unit tests without AMReX, use double directly
46#ifdef OPENIMPALA_UNIT_TEST
47using Real = double;
48#else
49using Real = amrex::Real;
50#endif
51
59
62
63 // Human-readable labels for output
64 std::string name;
65 std::string coeff_label;
66 std::string field_label;
67 std::string eff_property_label;
68 std::string ratio_label;
69
70 // User-supplied bulk/reference property value (e.g. sigma_bulk)
72
73 // --- Interpret raw solver output ---
74
75 Real effectiveProperty(Real D_eff_ratio) const {
76 return D_eff_ratio * bulk_property;
77 }
78
79 Real tortuosityFactor(Real D_eff_ratio, Real vf) const {
80 if (D_eff_ratio > 0.0)
81 return vf / D_eff_ratio;
82 return std::numeric_limits<Real>::infinity();
83 }
84
85 Real formationFactor(Real D_eff_ratio) const {
86 if (D_eff_ratio > 0.0)
87 return 1.0 / D_eff_ratio;
88 return std::numeric_limits<Real>::infinity();
89 }
90
91 // --- Factory: build from a type string (no AMReX dependency) ---
92 // Returns true on success, false if type_str is unrecognised.
93 static bool fromTypeString(const std::string& type_str_in, PhysicsConfig& cfg) {
94 std::string type_str = type_str_in;
95 std::transform(type_str.begin(), type_str.end(), type_str.begin(),
96 [](unsigned char c) { return std::tolower(c); });
97
98 if (type_str == "diffusion") {
100 cfg.name = "Diffusion";
101 cfg.coeff_label = "D";
102 cfg.field_label = "concentration";
103 cfg.eff_property_label = "EffectiveDiffusivity";
104 cfg.ratio_label = "Deff_ratio";
105 } else if (type_str == "electrical_conductivity") {
107 cfg.name = "Electrical Conductivity";
108 cfg.coeff_label = "sigma";
109 cfg.field_label = "voltage";
110 cfg.eff_property_label = "EffectiveConductivity";
111 cfg.ratio_label = "sigma_eff_ratio";
112 } else if (type_str == "thermal_conductivity") {
114 cfg.name = "Thermal Conductivity";
115 cfg.coeff_label = "k";
116 cfg.field_label = "temperature";
117 cfg.eff_property_label = "EffectiveThermalConductivity";
118 cfg.ratio_label = "k_eff_ratio";
119 } else if (type_str == "dielectric_permittivity") {
121 cfg.name = "Dielectric Permittivity";
122 cfg.coeff_label = "epsilon";
123 cfg.field_label = "potential";
124 cfg.eff_property_label = "EffectivePermittivity";
125 cfg.ratio_label = "eps_eff_ratio";
126 } else if (type_str == "magnetic_permeability") {
128 cfg.name = "Magnetic Permeability";
129 cfg.coeff_label = "mu";
130 cfg.field_label = "potential";
131 cfg.eff_property_label = "EffectivePermeability";
132 cfg.ratio_label = "mu_eff_ratio";
133 } else {
134 return false;
135 }
136 return true;
137 }
138
139#ifndef OPENIMPALA_UNIT_TEST
140 // --- Factory: build from input file parameters ---
142 PhysicsConfig cfg;
143
144 amrex::ParmParse pp("physics");
145 std::string type_str = "diffusion";
146 pp.query("type", type_str);
147 pp.query("bulk_property", cfg.bulk_property);
148
149 if (!fromTypeString(type_str, cfg)) {
150 amrex::Abort("Unknown physics.type: '" + type_str +
151 "'. Valid options: diffusion, electrical_conductivity, "
152 "thermal_conductivity, dielectric_permittivity, magnetic_permeability");
153 }
154
155 return cfg;
156 }
157
158 // --- Print configuration summary ---
159 void print(int verbose = 1) const {
160 if (verbose >= 1 && amrex::ParallelDescriptor::IOProcessor()) {
161 amrex::Print() << " Physics Type: " << name << "\n";
162 amrex::Print() << " Coefficient: " << coeff_label << "\n";
163 amrex::Print() << " Field: " << field_label << "\n";
164 amrex::Print() << " Bulk Property: " << bulk_property << "\n";
165 }
166 }
167#endif
168
169 // --- Write results for a single direction ---
170 void writeDirectionResults(std::ostream& os, const std::string& dir_label, Real D_eff_ratio,
171 Real vf) const {
172 os << ratio_label << "_" << dir_label << ": " << std::scientific << D_eff_ratio << "\n";
173 os << "Tortuosity_" << dir_label << ": " << std::fixed << std::setprecision(9)
174 << tortuosityFactor(D_eff_ratio, vf) << "\n";
175 if (bulk_property != 1.0) {
176 os << eff_property_label << "_" << dir_label << ": " << std::scientific
177 << effectiveProperty(D_eff_ratio) << "\n";
178 }
180 os << "FormationFactor_" << dir_label << ": " << std::fixed << std::setprecision(9)
181 << formationFactor(D_eff_ratio) << "\n";
182 }
183 }
184
185 // --- Write file header ---
186 void writeHeader(std::ostream& os, const std::string& input_file, int phase_id) const {
187 os << "# Transport Property Calculation Results\n";
188 os << "# Physics Type: " << name << "\n";
189 os << "# Input File: " << input_file << "\n";
190 os << "# Analysis Phase ID: " << phase_id << "\n";
191 if (bulk_property != 1.0) {
192 os << "# Bulk Property (" << coeff_label << "_bulk): " << std::scientific
193 << bulk_property << "\n";
194 }
195 os << "# -----------------------------\n";
196 }
197};
198
199} // namespace OpenImpala
200
201#endif // PHYSICSCONFIG_H
Definition CathodeWrite.cpp:12
amrex::Real Real
Definition MacroGeometry.H:17
PhysicsType
Definition PhysicsConfig.H:52
@ ElectricalConductivity
Definition PhysicsConfig.H:54
@ ThermalConductivity
Definition PhysicsConfig.H:55
@ MagneticPermeability
Definition PhysicsConfig.H:57
@ DielectricPermittivity
Definition PhysicsConfig.H:56
@ Diffusion
Definition PhysicsConfig.H:53
Definition PhysicsConfig.H:60
void print(int verbose=1) const
Definition PhysicsConfig.H:159
Real tortuosityFactor(Real D_eff_ratio, Real vf) const
Definition PhysicsConfig.H:79
std::string field_label
Definition PhysicsConfig.H:66
Real bulk_property
Definition PhysicsConfig.H:71
PhysicsType type
Definition PhysicsConfig.H:61
std::string coeff_label
Definition PhysicsConfig.H:65
void writeDirectionResults(std::ostream &os, const std::string &dir_label, Real D_eff_ratio, Real vf) const
Definition PhysicsConfig.H:170
static bool fromTypeString(const std::string &type_str_in, PhysicsConfig &cfg)
Definition PhysicsConfig.H:93
Real effectiveProperty(Real D_eff_ratio) const
Definition PhysicsConfig.H:75
void writeHeader(std::ostream &os, const std::string &input_file, int phase_id) const
Definition PhysicsConfig.H:186
std::string name
Definition PhysicsConfig.H:64
std::string ratio_label
Definition PhysicsConfig.H:68
Real formationFactor(Real D_eff_ratio) const
Definition PhysicsConfig.H:85
std::string eff_property_label
Definition PhysicsConfig.H:67
static PhysicsConfig fromParmParse()
Definition PhysicsConfig.H:141