-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strang implicit spectral em redo (#5027)
This implements use of the PSATD field advance coupled with the implicit solver, using a Strang split advance. - Advect Maxwell using PSATD with no J, ½ step - Advance particles along with dE/dt = -J implicitly, full step, iterating - Advect Maxwell using PSATD with no J, ½ step This requires the input parameter psatd.update_with_rho = 0. With psatd.periodic_single_box_fft = 1, exact energy conservation is obtained. Otherwise good conservation is seen, but not exact (will depend on parameters). Convergence is found for wpedt <= 1.9 (compared to wpedt < 0.25 for FDTD). This PR replaces PR #4662. A task for a future PR would be to implement specialized source free spectral advance routines (as noted in source comments).
- Loading branch information
Showing
16 changed files
with
512 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 Justin Angus, David Grote | ||
# | ||
# | ||
# This file is part of WarpX. | ||
# | ||
# License: BSD-3-Clause-LBNL | ||
# | ||
# This is a script that analyses the simulation results from the script `inputs_vandb_2d`. | ||
# This simulates a 2D periodic plasma using the implicit solver | ||
# with the Villasenor deposition using shape factor 2. | ||
import os | ||
import sys | ||
|
||
import numpy as np | ||
|
||
sys.path.insert(1, "../../../../warpx/Regression/Checksum/") | ||
import checksumAPI | ||
|
||
# this will be the name of the plot file | ||
fn = sys.argv[1] | ||
|
||
field_energy = np.loadtxt("diags/reducedfiles/field_energy.txt", skiprows=1) | ||
particle_energy = np.loadtxt("diags/reducedfiles/particle_energy.txt", skiprows=1) | ||
|
||
total_energy = field_energy[:, 2] + particle_energy[:, 2] | ||
|
||
delta_E = (total_energy - total_energy[0]) / total_energy[0] | ||
max_delta_E = np.abs(delta_E).max() | ||
|
||
# This case should have near machine precision conservation of energy | ||
tolerance_rel_energy = 2.1e-14 | ||
|
||
print(f"max change in energy: {max_delta_E}") | ||
print(f"tolerance: {tolerance_rel_energy}") | ||
|
||
assert max_delta_E < tolerance_rel_energy | ||
|
||
test_name = os.path.split(os.getcwd())[1] | ||
checksumAPI.evaluate_checksum(test_name, fn) |
98 changes: 98 additions & 0 deletions
98
Examples/Tests/implicit/inputs_test_2d_theta_implicit_strang_psatd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
################################# | ||
########## CONSTANTS ############ | ||
################################# | ||
|
||
my_constants.n0 = 1.e30 # m^-3 | ||
my_constants.nz = 40 | ||
my_constants.Ti = 100. # eV | ||
my_constants.Te = 100. # eV | ||
my_constants.wpe = q_e*sqrt(n0/(m_e*epsilon0)) | ||
my_constants.de0 = clight/wpe | ||
my_constants.nppcz = 10 # number of particles/cell in z | ||
my_constants.dt = 0.1/wpe # s | ||
|
||
################################# | ||
####### GENERAL PARAMETERS ###### | ||
################################# | ||
max_step = 20 | ||
amr.n_cell = nz nz | ||
amr.max_grid_size = nz | ||
amr.max_level = 0 | ||
geometry.dims = 2 | ||
geometry.prob_lo = 0.0 0.0 # physical domain | ||
geometry.prob_hi = 10.0*de0 10.0*de0 | ||
|
||
################################# | ||
####### Boundary condition ###### | ||
################################# | ||
boundary.field_lo = periodic periodic | ||
boundary.field_hi = periodic periodic | ||
|
||
################################# | ||
############ NUMERICS ########### | ||
################################# | ||
warpx.serialize_initial_conditions = 1 | ||
warpx.verbose = 1 | ||
warpx.const_dt = dt | ||
#warpx.cfl = 0.5656 | ||
warpx.use_filter = 0 | ||
|
||
algo.maxwell_solver = psatd | ||
algo.evolve_scheme = strang_implicit_spectral_em | ||
implicit_evolve.nonlinear_solver = "picard" | ||
|
||
picard.verbose = true | ||
picard.max_iterations = 9 | ||
picard.relative_tolerance = 0.0 | ||
picard.absolute_tolerance = 0.0 | ||
picard.require_convergence = false | ||
|
||
algo.particle_pusher = "boris" | ||
|
||
algo.particle_shape = 2 | ||
algo.current_deposition = direct | ||
algo.charge_deposition = standard | ||
algo.field_gathering = energy-conserving | ||
interpolation.galerkin_scheme = 0 | ||
|
||
psatd.periodic_single_box_fft = 1 | ||
psatd.update_with_rho = 0 | ||
|
||
################################# | ||
############ PLASMA ############# | ||
################################# | ||
particles.species_names = electrons protons | ||
|
||
electrons.species_type = electron | ||
electrons.injection_style = "NUniformPerCell" | ||
electrons.num_particles_per_cell_each_dim = nppcz nppcz | ||
electrons.profile = constant | ||
electrons.density = n0 | ||
electrons.momentum_distribution_type = gaussian | ||
electrons.ux_th = sqrt(Te*q_e/m_e)/clight | ||
electrons.uy_th = sqrt(Te*q_e/m_e)/clight | ||
electrons.uz_th = sqrt(Te*q_e/m_e)/clight | ||
|
||
protons.species_type = proton | ||
protons.injection_style = "NUniformPerCell" | ||
protons.num_particles_per_cell_each_dim = nppcz nppcz | ||
protons.profile = constant | ||
protons.density = n0 | ||
protons.momentum_distribution_type = gaussian | ||
protons.ux_th = sqrt(Ti*q_e/m_p)/clight | ||
protons.uy_th = sqrt(Ti*q_e/m_p)/clight | ||
protons.uz_th = sqrt(Ti*q_e/m_p)/clight | ||
|
||
# Diagnostics | ||
diagnostics.diags_names = diag1 | ||
diag1.intervals = 20 | ||
diag1.diag_type = Full | ||
diag1.fields_to_plot = Ex Ey Ez Bx By Bz jx jy jz rho divE | ||
diag1.electrons.variables = x z w ux uy uz | ||
diag1.protons.variables = x z w ux uy uz | ||
|
||
warpx.reduced_diags_names = particle_energy field_energy | ||
particle_energy.type = ParticleEnergy | ||
particle_energy.intervals = 1 | ||
field_energy.type = FieldEnergy | ||
field_energy.intervals = 1 |
31 changes: 31 additions & 0 deletions
31
Regression/Checksum/benchmarks_json/test_2d_theta_implicit_strang_psatd.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"lev=0": { | ||
"Bx": 60642.062637340816, | ||
"By": 89855.09371265332, | ||
"Bz": 54561.47120738846, | ||
"Ex": 81536346169528.28, | ||
"Ey": 13888711042388.54, | ||
"Ez": 86853122458391.0, | ||
"divE": 9.492653438830812e+22, | ||
"jx": 2.5941826848709296e+19, | ||
"jy": 2.9929071160915993e+19, | ||
"jz": 2.692985701872205e+19, | ||
"rho": 851978517887.51 | ||
}, | ||
"electrons": { | ||
"particle_momentum_x": 4.864385990952573e-19, | ||
"particle_momentum_y": 4.879723483907468e-19, | ||
"particle_momentum_z": 4.865564630727981e-19, | ||
"particle_position_x": 0.004250851253052539, | ||
"particle_position_y": 0.0042513622554793, | ||
"particle_weight": 2823958719279159.5 | ||
}, | ||
"protons": { | ||
"particle_momentum_x": 2.0934469726422704e-17, | ||
"particle_momentum_y": 2.0929630794865952e-17, | ||
"particle_momentum_z": 2.093085625201003e-17, | ||
"particle_position_x": 0.004251276208274589, | ||
"particle_position_y": 0.004251274670600805, | ||
"particle_weight": 2823958719279159.5 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
Source/FieldSolver/ImplicitSolvers/StrangImplicitSpectralEM.H
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* Copyright 2024 David Grote | ||
* | ||
* This file is part of WarpX. | ||
* | ||
* License: BSD-3-Clause-LBNL | ||
*/ | ||
#ifndef STRANG_IMPLICIT_SPECTRALEM_H_ | ||
#define STRANG_IMPLICIT_SPECTRALEM_H_ | ||
|
||
#include "FieldSolver/ImplicitSolvers/WarpXSolverVec.H" | ||
|
||
#include <AMReX_Array.H> | ||
#include <AMReX_MultiFab.H> | ||
#include <AMReX_REAL.H> | ||
|
||
#include "ImplicitSolver.H" | ||
|
||
/** @file | ||
* Implicit spectral electromagnetic time solver class. This is a fully implicit | ||
* algorithm where both the fields and particles are treated implicitly. | ||
* | ||
* The time stencil is | ||
* Advance (Eg^n, Bg^n) -> (Eg^{n+1/2}, Bg^{n+1/2}) source free // E transverse | ||
* Iterate: | ||
* Eg^{n+1} = Eg^n + c^2*dt*( - mu0*Jg^{n+1/2} ) // E longitudinal | ||
* xp^{n+1} = xp^n + dt*up^{n+1/2}/(0.5*(gammap^n + gammap^{n+1})) | ||
* up^{n+1} = up^n + dt*qp/mp*(Ep^{n+1/2} + up^{n+1/2}/gammap^{n+1/2} x Bp^{n+1/2}) | ||
* Advance (Eg^n+1/2, Bg^n+1/2) -> (Eg^{n+1}, Bg^{n+1}) source free // E transverse | ||
* | ||
* The algorithm is exactly energy conserving only with a single box, periodic fft (psatd.periodic_single_box_fft = 1). | ||
* With multiple boxes, energy is not conserved since the ffts in each box assumes periodic in the box which | ||
* is not consistent with the current. | ||
* The algorithm is numerially stable for any time step. | ||
* I.e., the CFL condition for light waves does not | ||
* have to be satisifed and the time step is not limited by the plasma period. However, how | ||
* efficiently the algorithm can use large time steps depends strongly on the nonlinear solver. | ||
* Furthermore, the time step should always be such that particles do not travel outside the | ||
* ghost region of the box they live in, which is an MPI-related limitation. The time step | ||
* is always limited by the need to resolve the appropriate physics. | ||
* | ||
*/ | ||
|
||
class StrangImplicitSpectralEM : public ImplicitSolver | ||
{ | ||
public: | ||
|
||
StrangImplicitSpectralEM() = default; | ||
|
||
~StrangImplicitSpectralEM() override = default; | ||
|
||
// Prohibit Move and Copy operations | ||
StrangImplicitSpectralEM(const StrangImplicitSpectralEM&) = delete; | ||
StrangImplicitSpectralEM& operator=(const StrangImplicitSpectralEM&) = delete; | ||
StrangImplicitSpectralEM(StrangImplicitSpectralEM&&) = delete; | ||
StrangImplicitSpectralEM& operator=(StrangImplicitSpectralEM&&) = delete; | ||
|
||
void Define ( WarpX* a_WarpX ) override; | ||
|
||
void PrintParameters () const override; | ||
|
||
void OneStep ( amrex::Real a_time, | ||
amrex::Real a_dt, | ||
int a_step ) override; | ||
|
||
void ComputeRHS ( WarpXSolverVec& a_RHS, | ||
const WarpXSolverVec& a_E, | ||
amrex::Real a_time, | ||
amrex::Real a_dt, | ||
int a_nl_iter, | ||
bool a_from_jacobian ) override; | ||
|
||
private: | ||
|
||
/** | ||
* \brief Solver vectors to be used in the nonlinear solver to solve for the | ||
* electric field E. The main logic for determining which variables should be | ||
* WarpXSolverVec type is that it must have the same size and have the same | ||
* centering of the data as the variable being solved for, which is E here. | ||
* For example, if using a Yee grid then a container for curlB could be a | ||
* WarpXSovlerVec, but magnetic field B should not be. | ||
*/ | ||
WarpXSolverVec m_E, m_Eold; | ||
|
||
/** | ||
* \brief B is a derived variable from E. Need to save Bold to update B during | ||
* the iterative nonlinear solve for E. Bold is owned here, but only used by WarpX. | ||
* It is not used directly by the nonlinear solver, nor is it the same size as the | ||
* solver vector (size E), and so it should not be WarpXSolverVec type. | ||
*/ | ||
amrex::Vector<std::array< std::unique_ptr<amrex::MultiFab>, 3 > > m_Bold; | ||
|
||
/** | ||
* \brief Update the E and B fields owned by WarpX | ||
*/ | ||
void UpdateWarpXFields ( WarpXSolverVec const& a_E, | ||
amrex::Real a_time, | ||
amrex::Real a_dt ); | ||
|
||
/** | ||
* \brief Nonlinear solver is for the time-centered values of E. After | ||
* the solver, need to use m_E and m_Eold to compute E^{n+1} | ||
*/ | ||
void FinishFieldUpdate ( amrex::Real a_new_time ); | ||
|
||
}; | ||
|
||
#endif |
Oops, something went wrong.