fbpic / fbpic

Spectral, quasi-3D Particle-In-Cell code, for CPU and GPU
http://fbpic.github.io
Other
175 stars 71 forks source link

Plasma density profile from file #608

Closed dmargarone closed 2 years ago

dmargarone commented 2 years ago

Hi guys !!

Like external electron beam or field, is it possible to read the plasma density profile from file ?

For example, I have a density profile measured experimentally, I could fit this profile and make a similar density profile numerically. However, if we can read the experimentally measured profile directly in to the code it may be more realistic and accurate.

Thanks in advance. DM

hightower8083 commented 2 years ago

dear @dmargarone

the question concerns rather python than FBPIC itself. The code accepts any function dens_func that can be 2D in (z,r) space or 3D in a full Cartesian space (x,y,z). In a simplest case, if you have 1D longitudinal profile, i.e. some array z_coord in microns and corresponding density n_p normalized to 1 (measured or from CFD simulation), you may define it with something like this:

dens_func = lambda z, r : np.interp(z, z_coord, n_p, left=0, right=0)

For 3D profiles, depending on your data, you may either consider combinations of 1D interpolators (like np.interp above), or a full 3D interpolator of you taste (see scipy.interpolate for more options)

dmargarone commented 2 years ago

Thank you so much @hightower8083 for answering this question.

Exactly as you mentioned, I have 1D longitudinal profile (data of 2 columns ). 1st column z-coordinate and 2nd column plasma density.

Sorry, but I have one query here ....

(1) why you wrote "lambda" in the dens_func ?

Thank you so so much. DM

hightower8083 commented 2 years ago

lambda is a standard inline function definition in python, but if you are not familiar with it you may prefer to use a more common way:

import numpy as np

def dens_func(z, r):
    value = np.interp(z, z_coord, n_p, left=0, right=0)
    return value

or even better consider a scipy method (avoids reinititalization at each call, but make sure there's no NaN values in the input arrays)

from scipy.interpolate import interp1d

dens_func_interp = interp1d(z_coord, n_p, bounds_error=False, fill_value=0.0)

def dens_func(z, r):
    value = dens_func_interp(z)
    return value
dmargarone commented 2 years ago

Thank you Thank you Thank you @hightower8083

You are great teacher. You realized correctly that I am not good in Python. Actually, I started Python only after becoming a user of FBPIC. Basically, I am a C and Matlab user. Nowadays, I am trying to learn Python in parallel to FBPIC.

You all guys are so wonderful and kind that I have no words to praise you .... simply great !!

Thank you for helping on this issue. It is now really clear.

Lovingly, DM