simba.Modules package

Subpackages

Submodules

simba.Modules.MathParser module

class MathParser(vars, math=True)[source]

Bases: object

Basic parser with local variable and math functions

Parameters:
  • vars (mapping) – mapping object where obj[name] -> numerical value

  • math (bool, optional) – if True (default) all math function are added in the same name space

Example

data = {‘r’: 3.4, ‘theta’: 3.141592653589793} parser = MathParser(data) assert parser.parse(‘r*cos(theta)’) == -3.4 data[‘theta’] =0.0 assert parser.parse(‘r*cos(theta)’) == 3.4

eval_(node)[source]
parse(expr)[source]

simba.Modules.SDDSFile module

class MyEnumMeta(cls, bases, classdict, **kwds)[source]

Bases: EnumMeta

class SDDSArray(columns)[source]

Bases: Munch

class SDDSColumn(name=None, data=[], unit='', type=2, symbol='', formatstring='', fieldlength=0, description='')[source]

Bases: SDDSObject

property data
length()[source]
class SDDSFile(index=1, ascii=False)[source]

Bases: object

add_column(name, data, type=2, unit='', symbol='', formatstring='', fieldlength=0, description='')[source]
add_columns(name, data, type, unit, symbol, formatstring=None, fieldlength=None, description=None)[source]
add_parameter(name, data, type=2, unit='', symbol='', formatstring='', fieldlength=0, description='')[source]
add_parameters(name, data, type, unit, symbol, formatstring=None, fieldlength=None, description=None)[source]
clear()[source]
column_names()[source]
columns()[source]
property data
dimensions(list)[source]
property index
load(*args, **kwargs)[source]
parameter_names()[source]
parameters()[source]
read_file(filename, page=-1)[source]
save(*args, **kwargs)[source]
write_file(filename)[source]
class SDDSObject(index=1, name=None, data=[], unit='', type=2, symbol='', formatstring='', fieldlength=0, description='')[source]

Bases: Munch

property data
property description
property fieldlength
property formatstring
property name
property symbol
property type
property unit
class SDDSParameter(name=None, data=[], unit='', type=2, symbol='', formatstring='', fieldlength=0, description='')[source]

Bases: SDDSObject

property data
property fieldlength
class SDDS_Types(value)[source]

Bases: IntEnum

An enumeration.

SDDS_ASCII = 2
SDDS_BINARY = 1
SDDS_CHARACTER = 9
SDDS_DOUBLE = 2
SDDS_FLOAT = 3
SDDS_INT16 = 6
SDDS_INT32 = 4
SDDS_LONG = 4
SDDS_LONGDOUBLE = 1
SDDS_NUM_TYPES = 9
SDDS_REAL32 = 3
SDDS_REAL64 = 2
SDDS_SHORT = 6
SDDS_STRING = 8
SDDS_UINT16 = 7
SDDS_UINT32 = 5
SDDS_ULONG = 5
SDDS_USHORT = 7
read_sdds_file(filename, ascii=False, object=None)[source]

simba.Modules.constants module

simba.Modules.gdf_beam module

class gdf_beam(filename)[source]

Bases: Munch

get_position(position)[source]
Parameters:

position (float)

Return type:

dict | None

get_time(time)[source]
Parameters:

time (float)

Return type:

dict | None

property positions: dict
property screens: dict
single_position_data()[source]
Return type:

None

sort_screens(**kwargs)[source]
Return type:

None

sort_touts(**kwargs)[source]
Return type:

None

sorted_nicely(unsorted_list, dict_key=None)[source]

Sort the given iterable in the way that humans expect.

Parameters:
  • unsorted_list (list)

  • dict_key (Optional[str])

Return type:

list

property times: dict
property touts: dict

simba.Modules.gdf_emit module

class gdf_emit(filename)[source]

Bases: Munch

get_property(property)[source]

simba.Modules.id_number module

class zmqClient(port=5557, host='localhost')[source]

Bases: object

get_id()[source]
request(msg='get_number')[source]
reset_id()[source]

simba.Modules.id_number_server module

class zmqServer(port=5557)[source]

Bases: Thread

run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

stop()[source]

simba.Modules.merge_two_dicts module

merge_dicts(*dicts)[source]

Combine multiple dictionaries: first dictionary overwrites keys in the second dictionary and so on

Parameters:

dicts (Dict of dicts) – Dictionary, ordered by priority

Returns:

Merged single dictionary

Return type:

Dict

merge_two_dicts(y, x)[source]

Combine two dictionaries: first dictionary overwrites keys in the second dictionary

Parameters:
  • y (Dict) – First dictionary (overwrites second dictionary if keys match)

  • x (Dict) – Second dictionary

Returns:

Merged single dictionary

Return type:

Dict

simba.Modules.pmd_units module

Simple units functionality for the openPMD beamphysics records.

For more advanced units, use a package like Pint:

https://pint.readthedocs.io/

dimension(name)[source]
dimension_name(dim_array)[source]
divide_units(u1, u2)[source]

Divides two pmd_unit symbols : u1/u2

is_identity(u)[source]

Checks if the unit is equivalent to 1

multiply_units(u1, u2)[source]

Multiplies two pmd_unit symbols

nice_array(a)[source]

Returns a scaled array, the scaling, and a unit prefix

Example

nice_array( np.array([2e-10, 3e-10]) )

Returns:

(array([200., 300.]), 1e-12, ‘p’)

nice_scale_prefix(scale)[source]

Returns a nice factor and a SI prefix string

Example

scale = 2e-10

f, u = nice_scale_prefix(scale)

pg_units(key)[source]

Returns a str representing the units of any attribute

class pmd_unit(unitSymbol='', unitSI=0, unitDimension=(0, 0, 0, 0, 0, 0, 0))[source]

Bases: object

Params

unitSymbol: Native units name unitSI: Conversion factor to the the correspontign SI unit unitDimension: SI Base Exponents

Base unit dimensions are defined as:

Base dimension | exponents. | SI unit —————- —————– ——- length : (1,0,0,0,0,0,0) m mass : (0,1,0,0,0,0,0) kg time : (0,0,1,0,0,0,0) s current : (0,0,0,1,0,0,0) A temperture : (0,0,0,0,1,0,0) K mol : (0,0,0,0,0,1,0) mol luminous : (0,0,0,0,0,0,1) cd

Example

pmd_unit(‘eV’, 1.602176634e-19, (2, 1, -2, 0, 0, 0, 0)) defines that an eV is 1.602176634e-19 of base units m^2 kg/s^2, which is a Joule (J)

If unitSI=0 (default), init with a known symbol:

pmd_unit(‘T’)

returns:

pmd_unit(‘T’, 1, (0, 1, -2, -1, 0, 0, 0))

Simple equalities are provided:

u1 == u2

Returns True if the params are all the same.

property unitDimension
property unitSI
property unitSymbol
read_dataset_and_unit_h5(h5, expected_unit=None, convert=True)[source]

Reads a dataset that has openPMD unit attributes.

expected_unit can be a pmd_unit object, or a known unit str. Examples: ‘kg’, ‘J’, ‘eV’

If expected_unit is given, will check that the units are compatible.

If convert, the data will be returned with the expected_units.

Returns a tuple:

np.array, pmd_unit

read_unit_h5(h5)[source]

Reads unit data from an h5 handle and returns a pmd_unit object

sqrt_unit(u)[source]

Returns the sqrt of a unit

unit(symbol)[source]

Returns a pmd_unit from a known symbol.

  • is allowed between two known symbols:

write_dataset_and_unit_h5(h5, name, data, unit=None)[source]

Writes data and pmd_unit to h5[name]

See: read_dataset_and_unit_h5

write_unit_h5(h5, u)[source]

Writes an pmd_unit to an h5 handle

simba.Modules.units module

class UnitValue(input_array, units=None, dtype=None)[source]

Bases: ndarray

Subclass of ndarray MUST be initialized with a numpy array as first argument.

in_units_of(prefix)[source]
mean(axis=None, dtype=None, out=None, keepdims=False, *, where=True)[source]

Returns the average of the array elements along given axis.

Refer to numpy.mean for full documentation.

See also

numpy.mean

equivalent function

property nice
nounits = ['sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan', 'arctan2']
sqrt(*args, **kwargs)[source]
std(axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True)[source]

Returns the standard deviation of the array elements along given axis.

Refer to numpy.std for full documentation.

See also

numpy.std

equivalent function

sum(axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True)[source]

Return the sum of the array elements over the given axis.

Refer to numpy.sum for full documentation.

See also

numpy.sum

equivalent function

property val
var(axis=None, dtype=None, out=None, ddof=0, keepdims=False, *, where=True)[source]

Returns the variance of the array elements, along given axis.

Refer to numpy.var for full documentation.

See also

numpy.var

equivalent function

are_units_equal(string1, string2)[source]
collect_units(unit_powers)[source]

Collects units into numerator ands denominator and uses ‘*’ and ‘/’ correctly

expand_units(unit_list, power_factor=1)[source]

Takes a list of units (normally numerator or denominator) and collects units and powers Returns units (‘a^x’) and unitnames ((‘a’,x))

get_base_units(string)[source]
nice_array(a)[source]

Returns a scaled array, the scaling, and a unit prefix

Example

nice_array( np.array([2e-10, 3e-10]) )

Returns:

(array([200., 300.]), 1e-12, ‘p’)

nice_scale_prefix(scale)[source]

Returns a nice factor and a SI prefix string

Example

scale = 2e-10

f, u = nice_scale_prefix(scale)

set_nice_array(a, prefix)[source]

Returns a scaled array, the scaling, for a unit prefix

Example

set_nice_array( np.array([2e-3, 3e-3]), prefix=”m”)

Returns:

(array([2., 3.]), 1e-3, ‘m’)

unit_fraction(string)[source]

split a tring into numerator and denominator taking into account () - input is string in form ‘a^x*b/c^y’

unit_multiply(string1, string2=False, divide=False)[source]

multiply/divide two unit strings

unit_power(string, power_factor=1)[source]

# Takes a string in the form ‘a^x’ and returns (‘a’, x)

unit_power_multiply(string1, string2, power_factor=1)[source]

Takes two strings in the form (‘a’, x) and (‘a’, y) and returns (‘a’, (x+y))

unit_power_string(power_list, power_factor=1)[source]

Returns a unit in (‘a’, x) format and makes a unit string

unit_powers(string, power_factor=1)[source]
unit_to_the_power(string1, power=1)[source]

raise units to a power

Module contents

Simframe Modules

Modules to handle particle beams, electromagnetic fields, matrices, plotting, optimisation and Twiss parameters, along with various utility functions.

Classes:
  • beam: Handles particle distributions, including

various analysis functions and the loading and writing of files to and from various formats.

  • field: Handles electromagnetic field distributions,

including the loading and writing of files to and from various formats.

  • matrices: Handles particle tracking matrices of

various orders.

  • twiss: Handles beam twiss parameters produced by

simulations and joins them together.

  • optimiser: Generic optimiser class.

  • UnitValue: Class for storing arrays, floats and integers

with units attached; used in many of these modules.

Other classes are defined in this submodule, but most of them are for expert use only.