|
15 | 15 | # include "FieldSolver/FiniteDifferenceSolver/FiniteDifferenceAlgorithms/CartesianNodalAlgorithm.H" |
16 | 16 | # include "FieldSolver/FiniteDifferenceSolver/FiniteDifferenceAlgorithms/CartesianYeeAlgorithm.H" |
17 | 17 | #endif |
| 18 | +#include "Fields.H" |
18 | 19 | #include "Particles/MultiParticleContainer.H" |
19 | 20 | #include "Utils/TextMsg.H" |
20 | 21 | #include "Utils/WarpXAlgorithmSelection.H" |
21 | 22 | #include "Utils/WarpXConst.H" |
| 23 | +#include "Utils/Parser/ParserUtils.H" |
| 24 | + |
| 25 | +#include <ablastr/coarsen/sample.H> |
22 | 26 |
|
23 | 27 | #include <AMReX.H> |
24 | 28 | #include <AMReX_Geometry.H> |
25 | 29 | #include <AMReX_IntVect.H> |
| 30 | +#include <AMReX_MFIter.H> |
26 | 31 | #include <AMReX_Print.H> |
27 | 32 | #include <AMReX_REAL.H> |
28 | 33 | #include <AMReX_Vector.H> |
29 | 34 |
|
30 | 35 | #include <algorithm> |
31 | 36 | #include <memory> |
| 37 | +#include <filesystem> |
32 | 38 |
|
33 | 39 | /** |
34 | 40 | * Compute the minimum of array x, where x has dimension AMREX_SPACEDIM |
@@ -108,35 +114,229 @@ WarpX::ComputeDt () |
108 | 114 | } |
109 | 115 |
|
110 | 116 | /** |
111 | | - * Determine the simulation timestep from the maximum speed of all particles |
112 | | - * Sets timestep so that a particle can only cross cfl*dx cells per timestep. |
| 117 | + * Used to determine the simulation timestep from the maximum speed of all particles |
| 118 | + * Timestep will be set so that a particle can cross at most cfl*dx cells per timestep. |
113 | 119 | */ |
114 | | -void |
115 | | -WarpX::UpdateDtFromParticleSpeeds () |
| 120 | +amrex::Real |
| 121 | +WarpX::ParticleGridSpeedMax () |
116 | 122 | { |
117 | 123 | const amrex::Real* dx = geom[max_level].CellSize(); |
118 | 124 | const amrex::Real dx_min = minDim(dx); |
119 | 125 |
|
120 | 126 | const amrex::ParticleReal max_v = mypc->maxParticleVelocity(); |
121 | | - amrex::Real deltat_new = 0.; |
122 | 127 |
|
123 | | - // Protections from overly-large timesteps |
124 | | - if (max_v == 0) { |
125 | | - WARPX_ALWAYS_ASSERT_WITH_MESSAGE(m_max_dt.has_value(), "Particles at rest and no constant or maximum timestep specified. Aborting."); |
126 | | - deltat_new = m_max_dt.value(); |
127 | | - } else { |
128 | | - deltat_new = cfl * dx_min / max_v; |
| 128 | + return max_v/dx_min; |
| 129 | +} |
| 130 | + |
| 131 | +amrex::Real |
| 132 | +WarpX::GlobalPlasmaFrequencyMax () |
| 133 | +{ |
| 134 | + const std::unique_ptr<amrex::MultiFab> global_plasma_frequency = mypc->GetGlobalPlasmaFrequency(0); |
| 135 | + const amrex::Real global_plasma_frequency_max = global_plasma_frequency->max(0); |
| 136 | + return global_plasma_frequency_max; |
| 137 | +} |
| 138 | + |
| 139 | +amrex::Real |
| 140 | +WarpX::GlobalCyclotronFrequencyMax () |
| 141 | +{ |
| 142 | + using ablastr::fields::Direction; |
| 143 | + using warpx::fields::FieldType; |
| 144 | + |
| 145 | + const amrex::ParmParse pp_particles("particles"); |
| 146 | + amrex::Vector<amrex::Real> B_external_particle(3, 0.); |
| 147 | + utils::parser::queryArrWithParser(pp_particles, "B_external_particle", B_external_particle); |
| 148 | + |
| 149 | + const amrex::Real Bx_external = B_external_particle[0]; |
| 150 | + const amrex::Real By_external = B_external_particle[1]; |
| 151 | + const amrex::Real Bz_external = B_external_particle[2]; |
| 152 | + |
| 153 | + amrex::Real B_max = 0.; |
| 154 | + |
| 155 | + // loop over refinement levels |
| 156 | + for (int lev = 0; lev <= finestLevel(); ++lev) |
| 157 | + { |
| 158 | + // get MultiFab data at lev |
| 159 | + const amrex::MultiFab & Bx = *m_fields.get(FieldType::Bfield_aux, Direction{0}, lev); |
| 160 | + const amrex::MultiFab & By = *m_fields.get(FieldType::Bfield_aux, Direction{1}, lev); |
| 161 | + const amrex::MultiFab & Bz = *m_fields.get(FieldType::Bfield_aux, Direction{2}, lev); |
| 162 | + |
| 163 | + // Prepare interpolation of field components to cell center |
| 164 | + // The arrays below store the index type (staggering) of each MultiFab, with the third |
| 165 | + // component set to zero in the two-dimensional case. |
| 166 | + auto Bxtype = amrex::GpuArray<int,3>{0, 0, 0}; |
| 167 | + auto Bytype = amrex::GpuArray<int,3>{0, 0, 0}; |
| 168 | + auto Bztype = amrex::GpuArray<int,3>{0, 0, 0}; |
| 169 | + for (int i = 0; i < AMREX_SPACEDIM; ++i){ |
| 170 | + Bxtype[i] = Bx.ixType()[i]; |
| 171 | + Bytype[i] = By.ixType()[i]; |
| 172 | + Bztype[i] = Bz.ixType()[i]; |
| 173 | + } |
| 174 | + |
| 175 | + // General preparation of interpolation and reduction operations |
| 176 | + const amrex::GpuArray<int,3> cellCenteredtype{0,0,0}; |
| 177 | + const amrex::GpuArray<int,3> reduction_coarsening_ratio{1,1,1}; |
| 178 | + constexpr int reduction_comp = 0; |
| 179 | + |
| 180 | + amrex::ReduceOps<amrex::ReduceOpMax> reduce_op; |
| 181 | + amrex::ReduceData<amrex::Real> reduce_data(reduce_op); |
| 182 | + using ReduceTuple = typename decltype(reduce_data)::Type; |
| 183 | + |
| 184 | + // MFIter loop to interpolate fields to cell center and get maximum value |
| 185 | +#ifdef AMREX_USE_OMP |
| 186 | +#pragma omp parallel if (amrex::Gpu::notInLaunchRegion()) |
| 187 | +#endif |
| 188 | + for (amrex::MFIter mfi(Bx, amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi) |
| 189 | + { |
| 190 | + // Make the box cell centered in preparation for the interpolation (and to avoid |
| 191 | + // including ghost cells in the calculation) |
| 192 | + const amrex::Box & box = enclosedCells(mfi.nodaltilebox()); |
| 193 | + const auto& arrBx = Bx[mfi].array(); |
| 194 | + const auto& arrBy = By[mfi].array(); |
| 195 | + const auto& arrBz = Bz[mfi].array(); |
| 196 | + |
| 197 | + reduce_op.eval(box, reduce_data, |
| 198 | + [=] AMREX_GPU_DEVICE (int i, int j, int k) -> ReduceTuple |
| 199 | + { |
| 200 | + const amrex::Real Bx_interp = ablastr::coarsen::sample::Interp(arrBx, Bxtype, cellCenteredtype, |
| 201 | + reduction_coarsening_ratio, i, j, k, reduction_comp); |
| 202 | + const amrex::Real By_interp = ablastr::coarsen::sample::Interp(arrBy, Bytype, cellCenteredtype, |
| 203 | + reduction_coarsening_ratio, i, j, k, reduction_comp); |
| 204 | + const amrex::Real Bz_interp = ablastr::coarsen::sample::Interp(arrBz, Bztype, cellCenteredtype, |
| 205 | + reduction_coarsening_ratio, i, j, k, reduction_comp); |
| 206 | + return {amrex::Math::powi<2>(Bx_interp + Bx_external) + |
| 207 | + amrex::Math::powi<2>(By_interp + By_external) + |
| 208 | + amrex::Math::powi<2>(Bz_interp + Bz_external)}; |
| 209 | + }); |
| 210 | + } |
| 211 | + |
| 212 | + const amrex::Real hv_Bsq = amrex::get<0>(reduce_data.value()); // highest value of |B|**2 |
| 213 | + |
| 214 | + B_max = std::max(B_max, std::sqrt(hv_Bsq)); |
| 215 | + |
| 216 | + } |
| 217 | + |
| 218 | + // MPI reduce |
| 219 | + amrex::ParallelDescriptor::ReduceRealMax({B_max}); |
| 220 | + |
| 221 | + amrex::Real omegac_max = 0.; |
| 222 | + |
| 223 | + const int n_containers = mypc->nContainers(); |
| 224 | + for (int i = 0; i < n_containers; i++) |
| 225 | + { |
| 226 | + const WarpXParticleContainer& pc = mypc->GetParticleContainer(i); |
| 227 | + if (pc.getMass() > 0.) { |
| 228 | + const amrex::Real pc_omegac = pc.getCharge()*B_max/pc.getMass(); |
| 229 | + omegac_max = std::max(omegac_max, pc_omegac); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + return omegac_max; |
| 234 | +} |
| 235 | + |
| 236 | +void |
| 237 | +WarpX::ApplyDtLimiters () |
| 238 | +{ |
| 239 | + using namespace amrex::literals; |
| 240 | + |
| 241 | + // Calculate limiting values from the simulation conditions |
| 242 | + const amrex::Real vmax_o_dx = ParticleGridSpeedMax(); |
| 243 | + const amrex::Real omegap_max = m_max_omegap_dt.has_value() ? GlobalPlasmaFrequencyMax() : 0._rt; |
| 244 | + const amrex::Real omegac_max = m_max_omegac_dt.has_value() ? GlobalCyclotronFrequencyMax() : 0._rt; |
| 245 | + |
| 246 | + // Ensure that a valid time step value exists, either from the simulation conditions or from max_dt |
| 247 | + if (vmax_o_dx == 0._rt && |
| 248 | + (!m_max_omegap_dt.has_value() || omegap_max == 0._rt) && |
| 249 | + (!m_max_omegac_dt.has_value() || omegac_max == 0._rt)) { |
| 250 | + WARPX_ALWAYS_ASSERT_WITH_MESSAGE(m_max_dt.has_value(), |
| 251 | + "No valid time step size limit found, warpx.max_dt must be specified"); |
| 252 | + } |
| 253 | + |
| 254 | + amrex::Real dt_new = std::numeric_limits<amrex::Real>::max(); |
| 255 | + |
| 256 | + if (vmax_o_dx > 0._rt) { |
| 257 | + dt_new = std::min(dt_new, cfl/vmax_o_dx); |
| 258 | + } |
| 259 | + if (m_max_omegap_dt.has_value() && omegap_max > 0._rt) { |
| 260 | + dt_new = std::min(dt_new, m_max_omegap_dt.value()/omegap_max); |
| 261 | + } |
| 262 | + if (m_max_omegac_dt.has_value() && omegac_max > 0._rt) { |
| 263 | + dt_new = std::min(dt_new, m_max_omegac_dt.value()/omegac_max); |
129 | 264 | } |
130 | 265 |
|
131 | | - // Restrict to be less than user-specified maximum timestep, if present |
132 | 266 | if (m_max_dt.has_value()) { |
133 | | - deltat_new = std::min(deltat_new, m_max_dt.value()); |
| 267 | + dt_new = std::min(dt_new, m_max_dt.value()); |
134 | 268 | } |
135 | 269 |
|
136 | 270 | // Update dt |
137 | | - dt[max_level] = deltat_new; |
| 271 | + dt[max_level] = dt_new; |
138 | 272 |
|
139 | 273 | for (int lev = max_level-1; lev >= 0; --lev) { |
140 | 274 | dt[lev] = dt[lev+1] * refRatio(lev)[0]; |
141 | 275 | } |
| 276 | + |
| 277 | + // Write diagnostics if requested |
| 278 | + if (amrex::ParallelDescriptor::IOProcessor() |
| 279 | + && !m_dt_update_diagnostic_file.empty() |
| 280 | + && !amrex::FileExists(m_dt_update_diagnostic_file)) { |
| 281 | + |
| 282 | + std::filesystem::path const diagnostic_path(m_dt_update_diagnostic_file); |
| 283 | + std::filesystem::path const diagnostic_dir = diagnostic_path.parent_path(); |
| 284 | + if (!diagnostic_dir.empty()) { |
| 285 | + std::filesystem::create_directories(diagnostic_dir); |
| 286 | + } |
| 287 | + |
| 288 | + std::ofstream diagnostic_file{m_dt_update_diagnostic_file, std::ofstream::out | std::ofstream::trunc}; |
| 289 | + if (!diagnostic_file.is_open()) { |
| 290 | + amrex::Abort("Failed to open file: " + m_dt_update_diagnostic_file); |
| 291 | + } |
| 292 | + |
| 293 | + int c = 0; |
| 294 | + diagnostic_file << "#"; |
| 295 | + diagnostic_file << "[" << c++ << "]step()"; |
| 296 | + diagnostic_file << " "; |
| 297 | + diagnostic_file << "[" << c++ << "]time(s)"; |
| 298 | + diagnostic_file << " "; |
| 299 | + diagnostic_file << "[" << c++ << "]new_dt"; |
| 300 | + diagnostic_file << " "; |
| 301 | + diagnostic_file << "[" << c++ << "]vmax_dt"; |
| 302 | + if (m_max_omegap_dt.has_value()) { |
| 303 | + diagnostic_file << " "; |
| 304 | + diagnostic_file << "[" << c++ << "]omegap_dt"; |
| 305 | + } |
| 306 | + if (m_max_omegac_dt.has_value()) { |
| 307 | + diagnostic_file << " "; |
| 308 | + diagnostic_file << "[" << c++ << "]omegac_dt"; |
| 309 | + } |
| 310 | + diagnostic_file << "\n"; |
| 311 | + diagnostic_file.close(); |
| 312 | + } |
| 313 | + |
| 314 | + if (amrex::ParallelDescriptor::IOProcessor() |
| 315 | + && !m_dt_update_diagnostic_file.empty()) { |
| 316 | + |
| 317 | + std::ofstream diagnostic_file{m_dt_update_diagnostic_file, std::ofstream::out | std::ofstream::app}; |
| 318 | + if (!diagnostic_file.is_open()) { |
| 319 | + amrex::Abort("Failed to open file: " + m_dt_update_diagnostic_file); |
| 320 | + } |
| 321 | + |
| 322 | + diagnostic_file << std::setprecision(14); |
| 323 | + diagnostic_file << istep[0] + 1; |
| 324 | + diagnostic_file << " "; |
| 325 | + diagnostic_file << t_new[0]; |
| 326 | + diagnostic_file << " "; |
| 327 | + diagnostic_file << dt_new; |
| 328 | + diagnostic_file << " "; |
| 329 | + diagnostic_file << vmax_o_dx*dt_new; |
| 330 | + |
| 331 | + if (m_max_omegap_dt.has_value()) { |
| 332 | + diagnostic_file << " "; |
| 333 | + diagnostic_file << omegap_max*dt_new; |
| 334 | + } |
| 335 | + if (m_max_omegac_dt.has_value()) { |
| 336 | + diagnostic_file << " "; |
| 337 | + diagnostic_file << omegac_max*dt_new; |
| 338 | + } |
| 339 | + diagnostic_file << "\n"; |
| 340 | + diagnostic_file.close(); |
| 341 | + } |
142 | 342 | } |
0 commit comments