WarpX icon indicating copy to clipboard operation
WarpX copied to clipboard

Add External Fields from Data File

Open Yin-YinjianZhao opened this issue 2 years ago • 16 comments

This PR aims to solve the issue #3048.

Yin-YinjianZhao avatar Apr 18 '22 22:04 Yin-YinjianZhao

@RemiLehe @ax3l Now, the loaded external field (1st plot) looks different from the provided external field (2nd plot). I'm stuck at debugging. Maybe you guys can help when reviewing this PR.

Screen Shot 2022-05-26 at 1 46 24 PM Screen Shot 2022-05-26 at 1 46 17 PM

Yin-YinjianZhao avatar May 26 '22 20:05 Yin-YinjianZhao

What is the reason that this requires creating new multifabs, Efield_fp_external and Bfield_fp_external instead of using E/Bfield_fp, E/Bfield_aux and E/Bfield_cp as is currently done with input like Ex_external_grid_function(x,y,z)? Basically I'm asking if there is a reason that the 'read external field from a data file' cannot be treated exactly like the parsed input parameter Ex_external_grid_function(x,y,z) (obviously with adding the read and interpolate functionality to WarpXInitData)?

roelof-groenewald avatar Jun 05 '22 20:06 roelof-groenewald

What is the reason that this requires creating new multifabs, Efield_fp_external and Bfield_fp_external instead of using E/Bfield_fp, E/Bfield_aux and E/Bfield_cp as is currently done with input like Ex_external_grid_function(x,y,z)? Basically I'm asking if there is a reason that the 'read external field from a data file' cannot be treated exactly like the parsed input parameter Ex_external_grid_function(x,y,z) (obviously with adding the read and interpolate functionality to WarpXInitData)?

Hi @roelof-groenewald, thanks for looking at this PR and asking this question. The reason we cannot easily use the existing (E/Bfield_fp, E/Bfield_aux and E/Bfield_cp) field arrays is that we have to make sure the external field data satisfy the Maxwell equation very well as well as the boundary conditions. If we want the external field data to be a bit more flexible, we would like to add them during the gather phase rather than adding to the existing field arrays in the code.

@RemiLehe Please add more comments if needed here.

Yin-YinjianZhao avatar Jun 06 '22 21:06 Yin-YinjianZhao

Now, the loaded external field (1st plot) looks different from the provided external field (2nd plot). I'm stuck at debugging. Maybe you guys can help when reviewing this PR.

I would try these things:

  • plot also the other slices of the simulation
  • try to invert the read (or write) indices

ax3l avatar Jun 07 '22 20:06 ax3l

Record a working test!

constant_B.txt This (Jupyter Notebook) python script generates a constant Bz field and other zero fields into txt files. Then, this version of the PR will read those txt files as external fields.

The WarpX input file is: input_constant_B.txt Compile using: cmake -S . -B build -DWarpX_EXTERNAL_FIELD=TRUE -DWarpX_openpmd_branch=0.14.5 -DWarpX_DIMS=3 cmake --build build -j 4

Then, the above constant_B.txt script can be used again to read the outputted diags data and plot the particle trajectory, as shown below.

Screen Shot 2022-06-28 at 2 09 34 PM

Yin-YinjianZhao avatar Jun 28 '22 21:06 Yin-YinjianZhao

Record a working test in RZ!

constant_B_rz.txt This (Jupyter Notebook) python script generates a constant Bz field and other zero fields into txt files. Then, this version of the PR (Make rz workiong: 03c3d00) will read those txt files as external fields.

The WarpX input file is: input_constant_B_rz.txt Compile using: cmake -S . -B build -DWarpX_EXTERNAL_FIELD=TRUE -DWarpX_openpmd_branch=0.14.5 -DWarpX_DIMS=RZ cmake --build build -j 4

Then, the above constant_B_rz.txt script can be used again to read the outputted diags data and plot the particle trajectory, as shown below. Screen Shot 2022-07-29 at 9 26 04 AM

Yin-YinjianZhao avatar Jul 29 '22 16:07 Yin-YinjianZhao

Record a working test in XZ!

constant_B_xz.txt This (Jupyter Notebook) python script generates a constant By field and other zero fields into txt files. Then, this version of the PR (Make xz workiong: 5db4e86) will read those txt files as external fields.

The WarpX input file is: input_constant_B_xz.txt Compile using: cmake -S . -B build -DWarpX_EXTERNAL_FIELD=TRUE -DWarpX_openpmd_branch=0.14.5 -DWarpX_DIMS=2 cmake --build build -j 4

Then, the above constant_B_xz.txt script can be used again to read the outputted diags data and plot the particle trajectory, as shown below. Screen Shot 2022-07-29 at 10 31 20 AM

Yin-YinjianZhao avatar Jul 29 '22 17:07 Yin-YinjianZhao

Record a working test in 1D!

constant_E_z.txt This (Jupyter Notebook) python script generates a constant Ez field and other zero fields into txt files. Then, this version of the PR (Make 1d work: 774e591) will read those txt files as external fields.

The WarpX input file is: input_constant_E_z.txt Compile using: cmake -S . -B build -DWarpX_EXTERNAL_FIELD=TRUE -DWarpX_openpmd_branch=0.14.5 -DWarpX_DIMS=1 cmake --build build -j 4

Then, the above constant_E_z.txt script can be used again to read the outputted diags data and plot the particle trajectory (z vs time step), as shown below. Screen Shot 2022-07-29 at 11 16 39 AM

Yin-YinjianZhao avatar Jul 29 '22 18:07 Yin-YinjianZhao

Tested a more complicated 3D scenario: magnetic mirror effect.

  1. A 2D RZ magnetic mirror field is generated by the FEMM software. The field is shown as this figure. Capture Then, the 3D Bx, By, Bz fields are saved using a Lua script into txt files named B0.txt, B1.txt, B2.txt. Corresponding E0.txt, E1.txt, E2.txt (same size and format as B files) are generated manually with all zeros. An information file is also generated by the Lua script named info.txt, indicating the number of grid points, the cell sizes, and the minimal x,y,z values of the external field domain. The FEMM file and the Lua script are posted here in this FEMM.zip. FEMM.zip

  2. Then, a corresponding WarpX input file is generated and given here, which contains only one test particle. input.txt

  3. WarpX (the current version of this PR) can be compiled using: cmake -S . -B build -DWarpX_EXTERNAL_FIELD=TRUE -DWarpX_openpmd_branch=0.14.5 -DWarpX_DIMS=3 cmake --build build -j 4

  4. The output can be read by a python script: read.txt

  5. The result makes sense, as a particle can be reflected by the magnetic mirror. Screen Shot 2022-08-02 at 3 21 27 PM Screen Shot 2022-08-02 at 3 21 42 PM Screen Shot 2022-08-02 at 3 21 50 PM

Yin-YinjianZhao avatar Aug 02 '22 22:08 Yin-YinjianZhao

Now, the mirror 3D test is applied using OpenPMD.

openpmd_create.txt reads the previous B*.txt and E*.txt txt data files and converts them into OpenPMD format file openpmd_data.h5. B0.txt B1.txt B2.txt E0.txt E1.txt E2.txt

WarpX can be compiled using: cmake -S . -B build -DWarpX_EXTERNAL_FIELD=TRUE -DWarpX_openpmd_branch=0.14.5 -DWarpX_DIMS=3 cmake --build build -j 4

The input file is input.txt.

The results can be read and plotted using read.txt.

Yin-YinjianZhao avatar Aug 05 '22 20:08 Yin-YinjianZhao

This pull request introduces 3 alerts when merging b32e6efdd766f5986d66b213479d7c84587fca7e into 4f88c5b85e6de10ea51c3bd146f5ba5aaa6d5cec - view on LGTM.com

new alerts:

  • 3 for Unsigned comparison to zero

lgtm-com[bot] avatar Aug 05 '22 21:08 lgtm-com[bot]

This pull request introduces 4 alerts when merging c21244d103e3eb245ebb1d3b9d2c4b016afd8671 into ee837089bf2264feb412e6351c22658dfe7599d2 - view on LGTM.com

new alerts:

  • 4 for Unused import

lgtm-com[bot] avatar Aug 17 '22 21:08 lgtm-com[bot]

This pull request introduces 5 alerts when merging caf213cdff02213cf0ff7389b399898bc76951b5 into 642f6c0f4ea5a524e6f73f2123aebbdef70c94fd - view on LGTM.com

new alerts:

  • 5 for Unused import

lgtm-com[bot] avatar Aug 18 '22 19:08 lgtm-com[bot]

This pull request introduces 4 alerts when merging a96a3eb3a181d6e1a649a43bc9eb2e00e4e0ef57 into 642f6c0f4ea5a524e6f73f2123aebbdef70c94fd - view on LGTM.com

new alerts:

  • 4 for Unused import

lgtm-com[bot] avatar Aug 18 '22 23:08 lgtm-com[bot]

This pull request introduces 4 alerts when merging 3394416cc0f7dc1e4742249b469957bedbfd5e84 into 4876446dff60411cbdd4892a044643f5fcc9dd00 - view on LGTM.com

new alerts:

  • 4 for Unused import

lgtm-com[bot] avatar Aug 24 '22 21:08 lgtm-com[bot]

This pull request introduces 4 alerts when merging 5452b0a818e952fc8ac989a306b0ea83738243f1 into 90b72e804c96eea345896125b7eb7d3c3272711b - view on LGTM.com

new alerts:

  • 4 for Unused import

lgtm-com[bot] avatar Sep 23 '22 21:09 lgtm-com[bot]

This pull request introduces 4 alerts when merging 2ff7eafd1ccf0aef287401fa2675cc51285dd518 into 5d635b996feff9fac6065930df7b87b83cceb311 - view on LGTM.com

new alerts:

  • 4 for Unused import

Heads-up: LGTM.com's PR analysis will be disabled on the 5th of December, and LGTM.com will be shut down ⏻ completely on the 16th of December 2022. Please enable GitHub code scanning, which uses the same CodeQL engine :gear: that powers LGTM.com. For more information, please check out our post on the GitHub blog.

lgtm-com[bot] avatar Nov 14 '22 17:11 lgtm-com[bot]

After discussion with @Yin-YinjianZhao, the current status is that the code is almost ready. We just need to:

  • fix CI
  • implement/test for GPU

RemiLehe avatar Dec 07 '22 22:12 RemiLehe

It seems a little bit odd to me to push a distinction between internal and external fields all the way down to the particle containers, and make them consider those. I'll look further, but naively, it seems like the fields should be summed outside any particle container, and the particles should only see the summed effective field. Is there a use case for some particles to be influenced by an external field, and others not?

PhilMiller avatar Dec 08 '22 19:12 PhilMiller

Given my current contract tasking, I may be able to pick up work on this from @Yin-YinjianZhao entirely, if that would be preferable.

PhilMiller avatar Dec 08 '22 19:12 PhilMiller

Just to update - I will be getting this refactored and cleaned up ASAP as part of a contracted project

PhilMiller avatar Dec 20 '22 23:12 PhilMiller

To be superseded by #3584

PhilMiller avatar Dec 20 '22 23:12 PhilMiller