forked from ECP-WarpX/WarpX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement div(B) Cleaning With FDTD (ECP-WarpX#1829)
* Implement div(B) Cleaning With FDTD * Add CI Test * Clean Up
- Loading branch information
Showing
19 changed files
with
463 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#! /usr/bin/env python | ||
|
||
# Copyright 2019 | ||
# | ||
# This file is part of WarpX. | ||
# | ||
# License: BSD-3-Clause-LBNL | ||
|
||
import sys | ||
sys.path.insert(1, '../../../../warpx/Regression/Checksum/') | ||
import numpy as np | ||
import yt | ||
yt.funcs.mylog.setLevel(50) | ||
import re | ||
import checksumAPI | ||
|
||
# Name of the last plotfile | ||
fn = sys.argv[1] | ||
|
||
# Load yt data | ||
ds_old = yt.load('divb_cleaning_3d_plt00398') | ||
ds_mid = yt.load('divb_cleaning_3d_plt00399') | ||
ds_new = yt.load(fn) # this is the last plotfile | ||
|
||
ad_old = ds_old.covering_grid(level = 0, left_edge = ds_old.domain_left_edge, dims = ds_old.domain_dimensions) | ||
ad_mid = ds_mid.covering_grid(level = 0, left_edge = ds_mid.domain_left_edge, dims = ds_mid.domain_dimensions) | ||
ad_new = ds_new.covering_grid(level = 0, left_edge = ds_new.domain_left_edge, dims = ds_new.domain_dimensions) | ||
|
||
G_old = ad_old['boxlib', 'G'].v.squeeze() | ||
G_new = ad_new['boxlib', 'G'].v.squeeze() | ||
divB = ad_mid['boxlib', 'divB'].v.squeeze() | ||
|
||
# Check max norm of error on div(B) = dG/dt | ||
# (the time interval between old and new is 2*dt) | ||
dt = 1.504557189e-15 | ||
x = G_new - G_old | ||
y = divB * 2 * dt | ||
|
||
rel_error = np.amax(abs(x - y)) / np.amax(abs(y)) | ||
tolerance = 1e-1 | ||
|
||
assert(rel_error < tolerance) | ||
|
||
test_name = fn[:-9] # Could also be os.path.split(os.getcwd())[1] | ||
|
||
if re.search('single_precision', fn): | ||
checksumAPI.evaluate_checksum(test_name, fn, rtol=1.e-3) | ||
else: | ||
checksumAPI.evaluate_checksum(test_name, fn) |
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,54 @@ | ||
# Iterations | ||
max_step = 400 | ||
|
||
# Domain | ||
amr.n_cell = 32 32 32 | ||
amr.max_grid_size = 16 | ||
amr.max_level = 0 | ||
|
||
# Geometry | ||
geometry.coord_sys = 0 | ||
geometry.is_periodic = 0 0 0 | ||
geometry.prob_lo = -50.e-6 -50.e-6 -50.e-6 | ||
geometry.prob_hi = 50.e-6 50.e-6 50.e-6 | ||
|
||
# Shape factors | ||
interpolation.nox = 3 | ||
interpolation.noy = 3 | ||
interpolation.noz = 3 | ||
|
||
# Numerics | ||
warpx.cfl = 0.25 | ||
warpx.do_divb_cleaning = 1 | ||
warpx.use_filter = 1 | ||
|
||
# External magnetic field | ||
my_constants.qm = 1e-1 | ||
warpx.B_ext_grid_init_style = parse_B_ext_grid_function | ||
warpx.Bx_external_grid_function(x,y,z) = qm * x / (x*x + y*y + z*z) | ||
warpx.By_external_grid_function(x,y,z) = qm * y / (x*x + y*y + z*z) | ||
warpx.Bz_external_grid_function(x,y,z) = qm * z / (x*x + y*y + z*z) | ||
|
||
# Particle beam | ||
particles.species_names = beam | ||
beam.charge = -q_e | ||
beam.mass = 1.e30 | ||
beam.injection_style = "gaussian_beam" | ||
beam.x_rms = 2.e-6 | ||
beam.y_rms = 2.e-6 | ||
beam.z_rms = 2.e-6 | ||
beam.x_m = 0. | ||
beam.y_m = 0. | ||
beam.z_m = 0.e-6 | ||
beam.npart = 20000 | ||
beam.q_tot = -1.e-20 | ||
beam.momentum_distribution_type = "gaussian" | ||
beam.ux_m = 0.0 | ||
beam.uy_m = 0.0 | ||
beam.uz_m = 0.5 | ||
|
||
# Diagnostics | ||
diagnostics.diags_names = diag1 | ||
diag1.intervals = 0, 398:400:1 | ||
diag1.diag_type = Full | ||
diag1.fields_to_plot = Ex Ey Ez Bx By Bz divE divB G |
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,13 @@ | ||
{ | ||
"lev=0": { | ||
"Bx": 30110529.73244452, | ||
"By": 30110529.732444517, | ||
"Bz": 30110529.73244452, | ||
"Ex": 137103615680453.66, | ||
"Ey": 137103615680454.56, | ||
"Ez": 137103615680494.5, | ||
"G": 0.08248210392635753, | ||
"divB": 6944252335584.074, | ||
"divE": 60881293.88523142 | ||
} | ||
} |
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
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
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,95 @@ | ||
/* Copyright 2020 | ||
* | ||
* This file is part of WarpX. | ||
* | ||
* License: BSD-3-Clause-LBNL | ||
*/ | ||
|
||
#include "Utils/WarpXAlgorithmSelection.H" | ||
#include "FiniteDifferenceSolver.H" | ||
#ifdef WARPX_DIM_RZ | ||
# include "FiniteDifferenceAlgorithms/CylindricalYeeAlgorithm.H" | ||
#else | ||
# include "FiniteDifferenceAlgorithms/CartesianYeeAlgorithm.H" | ||
# include "FiniteDifferenceAlgorithms/CartesianCKCAlgorithm.H" | ||
# include "FiniteDifferenceAlgorithms/CartesianNodalAlgorithm.H" | ||
#endif | ||
#include "Utils/WarpXConst.H" | ||
#include "WarpX.H" | ||
#include <AMReX_Gpu.H> | ||
|
||
using namespace amrex; | ||
|
||
void FiniteDifferenceSolver::EvolveG ( | ||
std::unique_ptr<amrex::MultiFab>& Gfield, | ||
std::array<std::unique_ptr<amrex::MultiFab>,3> const& Bfield, | ||
amrex::Real const dt) | ||
{ | ||
#ifdef WARPX_DIM_RZ | ||
// TODO Implement G update equation in RZ geometry | ||
amrex::ignore_unused(Gfield, Bfield, dt); | ||
#else | ||
// Select algorithm | ||
if (m_do_nodal) | ||
{ | ||
EvolveGCartesian<CartesianNodalAlgorithm>(Gfield, Bfield, dt); | ||
} | ||
else if (m_fdtd_algo == MaxwellSolverAlgo::Yee) | ||
{ | ||
EvolveGCartesian<CartesianYeeAlgorithm>(Gfield, Bfield, dt); | ||
} | ||
else if (m_fdtd_algo == MaxwellSolverAlgo::CKC) | ||
{ | ||
EvolveGCartesian<CartesianCKCAlgorithm>(Gfield, Bfield, dt); | ||
} | ||
else | ||
{ | ||
amrex::Abort("EvolveG: unknown FDTD algorithm"); | ||
} | ||
#endif | ||
} | ||
|
||
#ifndef WARPX_DIM_RZ | ||
|
||
template<typename T_Algo> | ||
void FiniteDifferenceSolver::EvolveGCartesian ( | ||
std::unique_ptr<amrex::MultiFab>& Gfield, | ||
std::array<std::unique_ptr<amrex::MultiFab>,3> const& Bfield, | ||
amrex::Real const dt) | ||
{ | ||
#ifdef AMREX_USE_OMP | ||
#pragma omp parallel if (amrex::Gpu::notInLaunchRegion()) | ||
#endif | ||
|
||
// Loop over grids and over tiles within each grid | ||
for (amrex::MFIter mfi(*Gfield, TilingIfNotGPU()); mfi.isValid(); ++mfi) | ||
{ | ||
// Extract field data for this grid/tile | ||
amrex::Array4<amrex::Real> const& G = Gfield->array(mfi); | ||
amrex::Array4<amrex::Real> const& Bx = Bfield[0]->array(mfi); | ||
amrex::Array4<amrex::Real> const& By = Bfield[1]->array(mfi); | ||
amrex::Array4<amrex::Real> const& Bz = Bfield[2]->array(mfi); | ||
|
||
// Extract stencil coefficients | ||
amrex::Real const* const AMREX_RESTRICT coefs_x = m_stencil_coefs_x.dataPtr(); | ||
amrex::Real const* const AMREX_RESTRICT coefs_y = m_stencil_coefs_y.dataPtr(); | ||
amrex::Real const* const AMREX_RESTRICT coefs_z = m_stencil_coefs_z.dataPtr(); | ||
|
||
const int n_coefs_x = m_stencil_coefs_x.size(); | ||
const int n_coefs_y = m_stencil_coefs_y.size(); | ||
const int n_coefs_z = m_stencil_coefs_z.size(); | ||
|
||
// Extract tilebox to loop over | ||
amrex::Box const& tf = mfi.tilebox(Gfield->ixType().toIntVect()); | ||
|
||
// Loop over cells and update G | ||
amrex::ParallelFor(tf, [=] AMREX_GPU_DEVICE (int i, int j, int k) | ||
{ | ||
G(i,j,k) += dt * (T_Algo::UpwardDx(Bx, coefs_x, n_coefs_x, i, j, k) | ||
+ T_Algo::UpwardDy(By, coefs_y, n_coefs_y, i, j, k) | ||
+ T_Algo::UpwardDz(Bz, coefs_z, n_coefs_z, i, j, k)); | ||
}); | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.