wakeSolver moduleο
- class wakis.wakeSolver.WakeSolver(wakelength=None, q=1e-9, sigmaz=1e-3, beta=1.0, xsource=0.0, ysource=0.0, xtest=0.0, ytest=0.0, chargedist=None, ti=None, fmax=None, compute_plane='both', skip_cells=0, add_space=None, Ez_file='Ez.h5', save=True, results_folder='results/', verbose=0, counter_moving=False)[source]ο
Bases:
objectClass for wake potential and impedance calculation from 3D time domain E fields
- static calc_impedance_from_wake(wake, s=None, t=None, fmax=None, samples=None, verbose=True)[source]ο
Calculate impedance from wake function using FFT.
- Parameters
wake (array_like or list) β Wake function or [t, wake] list.
s (ndarray, optional) β Distance array [m].
t (ndarray, optional) β Time array [s].
fmax (float, optional) β Maximum frequency.
samples (int, optional) β Number of FFT samples.
verbose (bool, optional) β If True, print information.
- Returns
f (ndarray) β Frequency array.
Z (ndarray) β Impedance array.
- calc_lambdas(**kwargs)[source]ο
Obtain normalized charge distribution in terms of s Ξ»(s) for impedance calculation.
- Parameters
s (ndarray) β Wakelength vector s=c*t-z.
chargedist (ndarray, optional) β Charge distribution Ξ»(z).
q (float, optional) β Total beam charge in [C].
z (ndarray, optional) β Vector containing z-coordinates of the domain [m].
zf (ndarray, optional) β Vector containing z-coordinates of the field monitor [m].
**kwargs β Additional parameters to set as attributes.
- calc_lambdas_analytic(**kwargs)[source]ο
Obtain normalized charge distribution in s Ξ»(z) as an analytical gaussian.
- Parameters
s (ndarray) β Wakelength vector s=c*t-z.
sigmaz (float) β Beam sigma in the longitudinal direction [m].
**kwargs β Additional parameters to set as attributes.
- calc_long_WP(Ezt=None, **kwargs)[source]ο
Obtain the wake potential from the pre-computed longitudinal Ez(z,t) field.
- Parameters
t (ndarray) β Vector containing time values [s].
z (ndarray) β Vector containing z-coordinates [m].
sigmaz (float) β Beam longitudinal sigma, to calculate injection time [m].
q (float) β Beam charge, to normalize wake potential.
ti (float, optional) β Injection time needed to set the negative part of s vector and wakelength.
Ezt (ndarray, optional) β Matrix (nz x nt) containing Ez(x_test, y_test, z, t).
Ez_file (str, optional) β HDF5 file containing the Ez(x, y, z) field data for every timestep.
**kwargs β Additional parameters to set as attributes.
- calc_long_WP_3d(**kwargs)[source]ο
Obtain the 3D wake potential from the pre-computed Ez(x,y,z) field.
- Parameters
Ez_file (str, optional) β HDF5 file containing the Ez(x,y,z) field data for every timestep.
t (ndarray) β Vector containing time values [s].
z (ndarray) β Vector containing z-coordinates [m].
q (float) β Total beam charge in [C].
n_transverse_cells (int, optional) β Number of transverse cells used for the 3d calculation: 2*n+1.
**kwargs β Additional parameters to set as attributes.
- calc_long_Z(samples=1001, fmax=None, **kwargs)[source]ο
Obtain the longitudinal impedance from the longitudinal wake potential and the beam charge distribution using a single-sided DFT.
- Parameters
WP (ndarray) β Longitudinal wake potential WP(s).
s (ndarray) β Wakelength vector s=c*t-z.
lambdas (ndarray) β Charge distribution Ξ»(s) interpolated to s axis, normalized by the beam charge.
chargedist (ndarray, optional) β Charge distribution Ξ»(z).
q (float, optional) β Total beam charge in [C].
z (ndarray) β Vector containing z-coordinates [m].
sigmaz (float) β Beam sigma in the longitudinal direction [m].
samples (int, optional) β Number of DFT samples. Default is 1001.
fmax (float, optional) β Maximum frequency of interest.
**kwargs β Additional parameters to set as attributes.
- calc_trans_WP(**kwargs)[source]ο
Obtain the transverse wake potential from the longitudinal wake potential in 3d using the Panofsky-Wenzel theorem.
- Parameters
WP_3d (ndarray) β Longitudinal wake potential in 3d WP(x,y,s).
s (ndarray) β Wakelength vector s=c*t-z.
dx (float) β Ez field mesh step in transverse plane, x-dir [m].
dy (float) β Ez field mesh step in transverse plane, y-dir [m].
x (ndarray, optional) β Vector containing x-coordinates [m].
y (ndarray, optional) β Vector containing y-coordinates [m].
n_transverse_cells (int, optional) β Number of transverse cells used for the 3d calculation: 2*n+1.
**kwargs β Additional parameters to set as attributes.
- calc_trans_Z(samples=1001, fmax=None)[source]ο
Obtain the transverse impedance from the transverse wake potential and the beam charge distribution using a single-sided DFT.
- Parameters
samples (int, optional) β Number of DFT samples. Default is 1001.
fmax (float, optional) β Maximum frequency of interest.
- static calc_wake_from_impedance(impedance, f=None, tmax=None, samples=None, pad=0, verbose=True)[source]ο
Calculate wake function from impedance using inverse FFT.
- Parameters
impedance (array_like or list) β Impedance or [f, Z] list.
f (ndarray, optional) β Frequency array.
tmax (float, optional) β Maximum time.
samples (int, optional) β Number of FFT samples.
pad (int, optional) β Padding for FFT.
verbose (bool, optional) β If True, print information.
- Returns
t (ndarray) β Time array [s].
wake (ndarray) β Wake function.
- copy()[source]ο
Return a shallow copy of the WakeSolver object.
- Returns
obj β Shallow copy of the WakeSolver.
- Return type
- get_DEmodel_fitting(freq_data=None, impedance_data=None, plane='longitudinal', dim='z', parameterBounds=None, N_resonators=None, DE_kernel='DE', maxiter=1e5, cmaes_sigma=0.01, popsize=150, tol=1e-3, use_minimization=True, minimization_margin=[0.3, 0.2, 0.01], minimum_peak_height=1.0, distance=3, inspect_bounds=False, Rs_bounds=[0.8, 10], Q_bounds=[0.5, 5], fres_bounds=[- 0.01e9, + 0.01e9], verbose=False)[source]ο
Fit the impedance using Differential Evolution or CMA-ES.
- Parameters
freq_data (ndarray, optional) β Frequency data.
impedance_data (ndarray, optional) β Impedance data.
plane (str, optional) β βlongitudinalβ or βtransverseβ.
dim (str, optional) β βzβ, βxβ, or βyβ.
parameterBounds (object, optional) β Parameter bounds for fitting.
N_resonators (int, optional) β Number of resonators.
DE_kernel (str, optional) β βDEβ or βCMAESβ.
maxiter (int, optional) β Maximum number of iterations.
cmaes_sigma (float, optional) β Sigma for CMA-ES.
popsize (int, optional) β Population size.
tol (float, optional) β Tolerance for convergence.
use_minimization (bool, optional) β Whether to run minimization after fitting.
minimization_margin (list, optional) β Margin for minimization.
minimum_peak_height (float, optional) β Minimum peak height for SmartBounds.
distance (int, optional) β Minimum distance between peaks for SmartBounds.
inspect_bounds (bool, optional) β If True, inspect and display bounds.
Rs_bounds (list, optional) β Bounds for Rs, Q, and fres.
Q_bounds (list, optional) β Bounds for Rs, Q, and fres.
fres_bounds (list, optional) β Bounds for Rs, Q, and fres.
- Returns
DE_model β Fitted DE model.
- Return type
object
- get_SmartBounds(freq_data=None, impedance_data=None, minimum_peak_height=1.0, distance=3, inspect_bounds=True, Rs_bounds=[0.8, 10], Q_bounds=[0.5, 5], fres_bounds=[- 0.01e9, + 0.01e9])[source]ο
Use Smart Bound Determination to estimate bounds for resonator fitting.
- Parameters
freq_data (ndarray) β Frequency data.
impedance_data (ndarray) β Impedance data.
minimum_peak_height (float, optional) β Minimum peak height for peak finding.
distance (int, optional) β Minimum distance between peaks.
inspect_bounds (bool, optional) β If True, inspect and display bounds.
Rs_bounds (list, optional) β Bounds for Rs, Q, and fres.
Q_bounds (list, optional) β Bounds for Rs, Q, and fres.
fres_bounds (list, optional) β Bounds for Rs, Q, and fres.
- Returns
bounds β SmartBoundDetermination result.
- Return type
object
- get_extrapolated_impedance(f=None, use_minimization=True, wakelength=None)[source]ο
Get the extrapolated impedance [Ohm] from the DE model.
- Parameters
f (ndarray, optional) β Frequency array.
use_minimization (bool, optional) β Whether to use minimization in the DE model.
wakelength (float, optional) β Wakelength for the DE model.
- Returns
f (ndarray) β Frequency array.
impedance (ndarray) β Extrapolated impedance [Ohm].
- get_extrapolated_wake(wakelength, sigma=None, use_minimization=True)[source]ο
Get the extrapolated wake potential [V/pC] from the DE model.
- Parameters
wakelength (float) β Wakelength to extrapolate to.
sigma (float, optional) β Sigma for the Gaussian. Default is self.sigmaz / c_light.
use_minimization (bool, optional) β Whether to use minimization in the DE model.
- Returns
s (ndarray) β Distance array [m].
wake_potential (ndarray) β Extrapolated wake potential [V/pC].
- get_extrapolated_wake_function(wakelength, use_minimization=True)[source]ο
Get the extrapolated wake function (Green function) from the DE model.
- Parameters
wakelength (float) β Wakelength to extrapolate to.
use_minimization (bool, optional) β Whether to use minimization in the DE model.
- Returns
t (ndarray) β Time array [s].
wake_function (ndarray) β Extrapolated wake function.
- load_results(folder=None)[source]ο
Load all txt results from a given folder.
- Parameters
folder (str, optional) β Folder path containing result txt files. If None, uses self.folder.
Notes
The txt files are generated when the attribute save = True is used.
- log(txt)[source]ο
Print a log message if verbose is enabled.
- Parameters
txt (str) β Message to print.
- read_Ez(filename=None, return_value=False)[source]ο
Read the Ez.h5 file containing the Ez field information.
- Parameters
filename (str, optional) β Path to the Ez.h5 file. If None, uses self.Ez_file.
return_value (bool, optional) β If True, return the h5py.File object.
- Returns
hf β Returned if return_value is True.
- Return type
h5py.File, optional
- read_cst_3d(path=None, folder='3d', filename='Ez.h5', units=1e-3)[source]ο
Read CST 3D exports folder and store the Ez field information into a matrix Ez(x,y,z) for every timestep into a single .h5 file compatible with wakis.
- Parameters
path (str, optional) β Path to the field data. Default is None.
folder (str, optional) β Folder containing the CST field data .txt files. Default is β3dβ.
filename (str, optional) β Name of the h5 file that will be generated. Default is βEz.h5β.
units (float, optional) β Unit conversion factor for coordinates. Default is 1e-3.
- read_txt(txt, skiprows=2, delimiter=None, usecols=None)[source]ο
Read variables from ASCII .txt files and return data in a dictionary.
- Parameters
txt (str) β Path to the .txt file.
skiprows (int, optional) β Number of rows to skip. Default is 2.
delimiter (str, optional) β Delimiter for columns.
usecols (list, optional) β Columns to read.
- Returns
d β Dictionary with keys as header names or integer indices.
- Return type
dict
- save_txt(f_name, x_data=None, y_data=None, x_name='X [-]', y_name='Y [-]')[source]ο
Save x and y data to a text file in a two-column format.
- Parameters
f_name (str) β Name of the output file (without the .txt extension).
x_data (ndarray, optional) β Array containing x-axis data.
y_data (ndarray, optional) β Array containing y-axis data.
x_name (str, optional) β Label for the x-axis column in the output file.
y_name (str, optional) β Label for the y-axis column in the output file.
Notes
The data is saved in a two-column format where x_data and y_data
are combined column-wise. - If x_data or y_data is missing, the function prints a warning and does not save a file.
Examples
Save two NumPy arrays to data.txt:
>>> x = np.linspace(0, 10, 5) >>> y = np.sin(x) >>> save_txt("data", x, y, x_name="Time [s]", y_name="Amplitude")
The saved file will look like:
0.00 0.00 2.50 0.59 5.00 -0.99 7.50 0.94 10.00 -0.54
- solve(compute_plane=None, **kwargs)[source]ο
Perform the wake potential and impedance calculation for longitudinal and transverse planes.
- Parameters
compute_plane (str, optional) β Which planes to compute: βbothβ, βlongitudinalβ, or βtransverseβ. Default is None (uses self.compute_plane).
**kwargs β Additional parameters to set as attributes.