Simple Fields (Code Examples)¶
Refer to the Simple Fields format specification while reading these examples and writing your own code.
Write a Simple Fields¶
% write_simple_fields.m: Template for writing your own Simple Fields
%% Construct object
fld = SimpleFields;
%% Create some 3D test matrices
nx = 100;
ny = 150;
nz = 125;
x = linspace(-10.0, 10.0, nx);
y = linspace(-15.0, 15.0, ny);
z = linspace(-12.5, 12.5, nz);
[X,Y,Z] = meshgrid(x,y,z);
%% Set datasets and attributes
fld.X = X; % required | X values | meters
fld.Y = Y; % required | Y values | meters
fld.Z = Z; % required | Z values | meters
fld.Ex = X.^2 + Y.^2; % required | Electric field, x-component | Volts/meter
fld.Ey = 0.0; % required | Electric field, y-component | Volts/meter
fld.Ez = 5.823 * X.^2 + Z.^2; % required | Electric field, z-component | Volts/meter
fld.Bx = X.^4 + Z.^2; % required | Magnetic field, x-component | Tesla
fld.By = 2.191 + Y + Z.^2; % required | Magnetic field, y-component | Tesla
fld.Bz = 8.123; % required | Magnetic field, z-component | Tesla
fld.rho = X + Y + Z; % optional | Mass density | kg / m**3
% disp(fld.object_type) % already-set | Specification of the HDF5 object type | "fields" (always this value)
% disp(fld.fields_type) % already-set | Specification of the fields sub-type | "simple" (always this value)
% disp(fld.pradformat_version) % already-set | HDF5 pradformat file format version followed | e.g. "0.1.0"
fld.rho_description = "Six-ionized CH plasma and chamber walls";
fld.label = "Fields_10"; % optional | Short, identifying label for this file (with no spaces or crazy characters). This can be stamped onto plots, etc.
fld.description = "Fields test example with lots of X, Y, and Z dependence."; % optional | Longer description of this file. This can be read by people trying to figure out where this file came from.
% disp(fld.file_date); % automatically-set | Date the (future) file will be created, in the format "YYYY-MM-DD" | You don't need to set this, it will be set automatically
% fld.raw_data_filename = "SimulationMain/DISC_OMEGA/chk0013"; % optional | Filename of the raw data file (e.g. simulation output) from which this derivative file was created, if applicable.
%% Pretty print your newly-minted fields object
disp(fld)
%% Save to file
[status, msg, msgID] = mkdir('outs');
h5filename = fullfile('outs', 'myfields.h5');
prad_save(fld, h5filename);
# write_simple_fields.py: Template for writing your own Simple Fields
import os
import numpy as np
import pradformat as prf
## Construct object
fld = prf.SimpleFields()
## Create some 3D test matrices
nx = 100
ny = 150
nz = 125
x = np.linspace(-10.0, 10.0, nx)
y = np.linspace(-15.0, 15.0, ny)
z = np.linspace(-12.5, 12.5, nz)
[X,Y,Z] = np.meshgrid(x,y,z)
## Set datasets and attributes
fld.X = X # required | X values | meters
fld.Y = Y # required | Y values | meters
fld.Z = Z # required | Z values | meters
fld.Ex = X**2 + Y**2 # required | Electric field, x-component | Volts/meter
fld.Ey = 0.0 # required | Electric field, y-component | Volts/meter
fld.Ez = 5.823 * X**2 + Z**2 # required | Electric field, z-component | Volts/meter
fld.Bx = X**4 + Z**2 # required | Magnetic field, x-component | Tesla
fld.By = 2.191 + Y + Z**2 # required | Magnetic field, y-component | Tesla
fld.Bz = 8.123 # required | Magnetic field, z-component | Tesla
fld.rho = X + Y + Z # optional | Mass density | kg / m**3
# print(fld.object_type) # already-set | Specification of the HDF5 object type | "fields" (always this value)
# print(fld.fields_type) # already-set | Specification of the fields sub-type | "simple" (always this value)
# print(fld.pradformat_version) # already-set | HDF5 pradformat file format version followed | e.g. "0.1.0"
fld.rho_description = "Six-ionized CH plasma and chamber walls"
fld.label = "Fields_10" # optional | Short, identifying label for this file (with no spaces or crazy characters). This can be stamped onto plots, etc.
fld.description = "Fields test example with lots of X, Y, and Z dependence." # optional | Longer description of this file. This can be read by people trying to figure out where this file came from.
# print(fld.file_date) # automatically-set | Date the (future) file will be created, in the format "YYYY-MM-DD" | You don't need to set this, it will be set automatically
# fld.raw_data_filename = "SimulationMain/DISC_OMEGA/chk0013" # optional | Filename of the raw data file (e.g. simulation output) from which this derivative file was created, if applicable.
## Pretty print your newly-minted fields object
print(fld)
## Save to file
if not os.path.isdir("outs"):
os.mkdir("outs")
h5filename = os.path.join('outs', 'myfields.h5')
fld.save(h5filename)
Read a Simple Fields¶
% read_simple_fields.m: Template for reading Simple Fields
%% Load object from pradformat file
h5filename = fullfile('outs', 'myfields.h5');
fld = prad_load(h5filename);
%% Examine your newly loaded object
disp(fld)
%% Utilize object's datasets and attributes in your own scripts
assert(isa(fld, 'SimpleFields'));
% fld.X; % required | X values | meters
% fld.Y; % required | Y values | meters
% fld.Z; % required | Z values | meters
% fld.Ex; % required | Electric field, x-component | Volts/meter
% fld.Ey; % required | Electric field, y-component | Volts/meter
% fld.Ez; % required | Electric field, z-component | Volts/meter
% fld.Bx; % required | Magnetic field, x-component | Tesla
% fld.By; % required | Magnetic field, y-component | Tesla
% fld.Bz; % required | Magnetic field, z-component | Tesla
% fld.rho; % optional | Mass density | kg / m**3
%
% fld.object_type; % required | Specification of the HDF5 object type | "fields" (always this value)
% fld.fields_type; % required | Specification of the fields sub-type | "simple" (always this value)
% fld.pradformat_version; % required | HDF5 pradformat file format version followed | e.g. "0.1.0"
%
% fld.rho_description; % optional | A qualitative description of the material represented by rho.
% fld.label; % optional | Short, identifying label for this file (with no spaces or crazy characters). This can be stamped onto plots, etc.
% fld.description; % optional | Longer description of this file. This can be read by people trying to figure out where this file came from.
% fld.file_date; % optional | Date the (future) file will be created, in the format "YYYY-MM-DD" | You don't need to set this, it will be set automatically
% fld.raw_data_filename; % optional | Filename of the raw data file (e.g. simulation output) from which this derivative file was created, if applicable.
% Dealing with required attributes/datasets
disp(fld.pradformat_version)
disp(mean(fld.Ex(:)))
% Dealing with optional attributes/datasets
if ~isempty(fld.label)
disp(fld.label)
else
disp("Well, I'd like to show you the label attribute, but I guess it wasn't set. Oh well.")
end
# read_simple_fields.py: Template for reading Simple Fields
import os
import numpy as np
import pradformat as prf
## Load object from pradformat file
h5filename = os.path.join('outs', 'myfields.h5')
fld = prf.prad_load(h5filename)
## Examine your newly loaded object
print(fld)
## Utilize object's datasets and attributes in your own scripts
assert isinstance(fld, prf.SimpleFields)
# fld.X # required | X values | meters
# fld.Y # required | Y values | meters
# fld.Z # required | Z values | meters
# fld.Ex # required | Electric field, x-component | Volts/meter
# fld.Ey # required | Electric field, y-component | Volts/meter
# fld.Ez # required | Electric field, z-component | Volts/meter
# fld.Bx # required | Magnetic field, x-component | Tesla
# fld.By # required | Magnetic field, y-component | Tesla
# fld.Bz # required | Magnetic field, z-component | Tesla
# fld.rho # optional | Mass density | kg / m**3
#
# fld.object_type # required | Specification of the HDF5 object type | "fields" (always this value)
# fld.fields_type # required | Specification of the fields sub-type | "simple" (always this value)
# fld.pradformat_version # required | HDF5 pradformat file format version followed | e.g. "0.1.0"
#
# fld.rho_description # optional | A qualitative description of the material represented by rho.
# fld.label # optional | Short, identifying label for this file (with no spaces or crazy characters). This can be stamped onto plots, etc.
# fld.description # optional | Longer description of this file. This can be read by people trying to figure out where this file came from.
# fld.file_date # optional | Date the (future) file will be created, in the format "YYYY-MM-DD" | You don't need to set this, it will be set automatically
# fld.raw_data_filename # optional | Filename of the raw data file (e.g. simulation output) from which this derivative file was created, if applicable.
# Dealing with required attributes/datasets
print(fld.pradformat_version)
print(np.mean(fld.Ex))
# Dealing with optional attributes/datasets
if not isinstance(fld.label, type(None)):
print(fld.label)
else:
print("Well, I'd like to show you the label attribute, but I guess it wasn't set. Oh well.")