Hi-PACE / hipace

Highly efficient Plasma Accelerator Emulation, quasistatic particle-in-cell code
https://hipace.readthedocs.io
Other
48 stars 14 forks source link

Generate h5 file after filtering by particle ID #1109

Open Zxy-1104 opened 2 months ago

Zxy-1104 commented 2 months ago

How to generate a new H5 file by filtering the particle ID in the H5 output file? That is, the particle ID is divided into two parts according to the particle energy in the original output file, and all the output information of the two parts of the particle is placed in two different H5 files according to the ID, and the particle information is read into the input file by using the from_file injection_type.

AlexanderSinn commented 2 months ago

Hello,

if I understand your question correctly, you are looking for a way to post process a H5 beam and write the result in a new H5 file to use it with another hipace simulation. Please correct me if that’s not the case.

For H5 post-processing, I suggest using a python script with openPMD-api to read and write the H5 beam data. There is such a script to write a beam at https://github.com/Hi-PACE/hipace/blob/development/tools/write_beam.py. It is based on the openPMD-api tutorial to write a file, https://openpmd-api.readthedocs.io/en/latest/usage/firstwrite.html. There is a similar tutorial to read data https://openpmd-api.readthedocs.io/en/latest/usage/firstread.html. Note that the tutorials are for fields (“meshes”) whereas the beam data would be stored under “particles”.

Once you have the particle data (position, momentum, weighting, ID) in python you can apply any transformation to them, including splitting particles into two groups based on ID or energy.

One thing to look out for is when you read in a file that was generated using the from_file injection_type, the ID from the file will not be read in and instead new IDs will be assigned to the particles.

Also make sure to use a recent development version of hipace as a bug was fixed not long ago where beam IDs could be repeated under certain conditions.

Zxy-1104 commented 2 months ago

Dear AlexanderSinn, Since I have been very busy these days and have been trying to solve this problem, I am very sorry for the delay in replying to you. Thank you so much for your advice! After several days of trying, I successfully solved this problem, thank you very much! However, when applying this method, I encountered another problem, which has not been solved, so I would like to ask you about it. The question is: when I use both from_file and fixed_ppc, that is, from_file to import the driver beam and fixed_ppc to import the accelerated beam. Why does the weighting of accelerated beam in the output file all are 0?(The input file and weighting is attached) At the same time, I also want to ask about a problem that I have encountered before but have not been solved. The problem is: When I set the proton Gaussian beam as the driver beam in the input file, that is, driver.element = proton and diagnostic.beam_data = driver, an error occurs: " terminate called after throwing an instance of 'std::runtime_error' what(): Chunks cannot be written for a constant RecordComponent. SIGABRT See Backtrace.23.0 file for details"(Backtrace.23.0 is attached) But when I set the positron Gaussian beam as the driver beam in the input file, i.e. driver.element = positron and diagnostic.beam_data = driver, no error occurs. I would like to know if this is because of my program installation problems or other reasons? Backtrace.23.0.txt inputs.txt weight

AlexanderSinn commented 2 months ago

Hello, one issue I see in the input script is that in

positron.density(x,y,z) = 1e22*exp(-x**2/(2*70e-6**2)-y**2/(2*70e-6**2)-(z+20)**2/(2*20e-6**2))

the mean value for z is at -20 not -20e-6, which causes all particles inside the domain to have a weight of zero.

The Chunks cannot be written for a constant RecordComponent. error can be caused by a beam that has no particles. Maybe they have all been depleted or were never initialized properly.

I opened PRs #1119, #1120 and #1121 to add more warnings for zero-particle/zero-weight beams and fix the crash in case the beam has no particles.