import numpy as np
from adios2 import Stream
[docs]
class DiffusionCoefficients:
"""
Stores the profiles of diffusion model coefficients defined as functions
of the poloidal magnetic flux ψ.
The four diffusion coefficients are:
- ptl_diffusivity (m^2/s)
- momentum_diffusivity (m^2/s)
- heat_conductivity (m^2/s)
- ptl_pinch_velocity (m/s)
The data layout of each coefficient is (nspecies, npsi) where the second dimension
(the ψ-grid) is contiguous in memory. Also the ψ–grid is stored as a 1D NumPy array of length npsi.
Parameters:
nspecies: Number of particle species (default: 2).
npsi: Number of points in the ψ–grid.
"""
def __init__(self, nspecies=2, npsi=100):
self.nspecies = nspecies
self.npsi = npsi
# Allocate the 4 coefficient arrays and the psi–grid.
self.ptl_diffusivity = np.zeros((nspecies, npsi), order='C', dtype=np.float64)
self.momentum_diffusivity = np.zeros((nspecies, npsi), order='C', dtype=np.float64)
self.heat_conductivity = np.zeros((nspecies, npsi), order='C', dtype=np.float64)
self.ptl_pinch_velocity = np.zeros((nspecies, npsi), order='C', dtype=np.float64)
self.psi = np.zeros(npsi, dtype=np.float64)
[docs]
def write_to_file(self, filename="xgc.diffusion_coeff.bp"):
"""
Writes the diffusion coefficient profiles and the ψ–grid to an Adios BP file as a new step.
For each diffusion coefficient, the data for each species is split into an individual variable
using a species suffix. The species suffixes (for species indices 0 through 6) are:
0: _elec
1: _ion
2: _imp1
3: _imp2
4: _imp3
5: _imp4
6: _imp5
In addition, the file will store:
- n_species: a scalar integer (int64).
- psi: a 1D array (of length npsi).
The method opens the specified file (filename) using the Adios2 FileWriter in append mode,
writes all the individual diffusion profiles, and then ends the step.
"""
# Define species suffixes.
species_suffixes = ["_elec", "_ion", "_imp1", "_imp2", "_imp3", "_imp4", "_imp5"]
# Open the file for writing (appending a new step)
with Stream(filename, "a") as writer:
writer.BeginStep()
# Define a list of tuples: (coefficient base name, corresponding array)
coeffs = [
("ptl_diffusivity", self.ptl_diffusivity),
("momentum_diffusivity", self.momentum_diffusivity),
("heat_conductivity", self.heat_conductivity),
("ptl_pinch_velocity", self.ptl_pinch_velocity)
]
# Loop over coefficients and species.
for coeff_name, coeff_array in coeffs:
for i in range(self.nspecies):
# Form the variable name using the base name and species suffix.
var_name = coeff_name + species_suffixes[i]
# Extract the data for this species (a 1D array of length npsi).
data = coeff_array[i, :]
writer.write(var_name, data)
# Write out the number of species as an int64 scalar.
writer.write("n_species", np.int64(self.nspecies))
# Write out the psi–grid as a 1D double array.
writer.write("psi", self.psi)
writer.EndStep()