Loggers#

NeuralMag provides several helper classes for the logging of simulation results. These classes mainly differ in the kind of data that is logged. While the FieldLogger class is able to write VTK files for arbitrary field data such as the magnetization field or material parameters, the ScalarLogger class writes scalar data such as the averaged magnetization to as CSV file. The Logger class offers both scalar and field logging and has the additional functionality to resume simulations from existing log files.

Class-Reference#

class neuralmag.FieldLogger(filename, fields, every=1, scale=1.0)#

Logger class for fields using PVD/VTI files from the visualization toolkit (VTK). The logger creates a single PVD file referencing the VTI snapshots for different simulation times saving spatially resolved field data.

Parameters:
  • filename (str) – The name of the log file.

  • fields (list) – The fields to be written to the log file as a list of attribute names of the state class.

  • every (int, optional) – Write field to log file every nth call.

  • scale (float, optional) – Factor applied to mesh spacing and origin before writing VTI files, e.g. use 1e9 to store coordinates in nanometres when the mesh is defined in SI metres.

Examples

# provide key strings which are available in state
logger = FieldLogger("data/m.pvd", ["m", "h_demag"])

# Actually log fields
state = State(mesh)
logger.log(state)
last_recorded_step()#

Returns the number of the last step logged and None if no step was yet logged.

Returns:

Number of the last step recorded.

Return type:

int or None

log(state)#

Log simulation step.

Parameters:

state (State) – The state to be logged.

resumable_step()#

Returns the first step that can be written when resuming, e.g. if the logger logs every 10th step and the first (i = 0) step was already logged, the result is 10.

Returns:

The step number.

Return type:

int

resume(i)#

Try to resume existing log file from log step i. The log file is truncated accordingly.

Parameters:

i (int) – The log step to resume from.

step_data(i, field, state)#

Returns field and time to a given step number.

Parameters:
  • i (int) – The step number.

  • field (str) – The name of the field to be read.

  • state (State) – The state used for the read.

Returns:

The field as a function.

Return type:

Function

class neuralmag.ScalarLogger(filename, columns, every=1)#

Simple logger class to log scalar values into a tab separated file.

Parameters:
  • filename (str) – The name of the log file.

  • columns (list) – List of attribute names to be logged.

  • every (int) – Write row to log file every nth call.

Examples

# provide key strings which are available in state
logger = ScalarLogger("log.dat", ["t","m"])

# provide func(state) or tuple (name, func(state))
logger = ScalarLogger("log.dat", [("t[ns]", lambda state: state.t*1e9)])

# Actually log a row
state = State(mesh)
logger.log(state)
add_column(column)#

Add column to log file.

This method is automatically called for all columns on initialization of the logger. The column can be provided either as attribute name or as a Callable that takes the state as the only argument. If the column is a Function, the functional is averaged over the whole mesh before logging.

Note

This method can only be called before the first line is logged

Parameters:

column (str or Callable) – The column to be logged.

log(state)#

Log simulation step.

Parameters:

state (State) – The state to be logged.

resumable_step()#

Returns the first step that can be written when resuming, e.g. if the logger logs every 10th step and the first (i = 0) step was already logged, the result is 10.

Returns:

The step number.

Return type:

int

resume(i)#

Try to resume existing log file from log step i. The log file is truncated accordingly.

Parameters:

i (int) – The log step to resume from.

class neuralmag.Logger(directory, scalars=[], fields=[], scalars_every=1, fields_every=1, scale=1.0)#

Combined Scalar- and Field-Logger class.

Parameters:
  • directory (str) – The name of the log file.

  • scalars (list) – List of state-attribute names of the scalars to be logged.

  • fields (list) – List of state-attribute names of the fields to be logged.

  • scalars_every (int) – Write scalars every nth step.

  • fields_every (int) – Write fields every nth step.

  • scale (float) – Factor applied to mesh spacing and origin before writing VTI files, e.g. use 1e9 to store coordinates in nanometres when the mesh is defined in SI metres.

Examples

# provide key strings which are available in state
logger = Logger("data", ["m", "h_demag"], ["m"], fields_every=100)

# Actually log fields
state = State(mesh)
logger.log(state)
is_resumable()#

Returns True if logger can resume from log files.

Returns:

True if resumable, False otherwise.

Return type:

bool

log(state)#

Log simulation step.

Parameters:

state (State) – The state to be logged.

resume(state)#

Tries to resume from existing log files. If resume is possible, i.e. if at least one magnetization field has been logged before, the state object is updated with latest possible values of t and m and the different log files are aligned and resumed. If resume is not possible the state is not modified and the simulations starts from the beginning.

Parameters:

state (State) – The state to be resumed from.