# Logging¶

magnum.fe provides classes for the convenient logging of the simulation state. Usually when performing dynamic simulations the logging of a number of scalar values like the time and the averaged magnetization is of interest. This task is performed by the ScalarLogger class. The values are written to simple tab separated file that can be read by a number of post processing and plotting tools like e.g. Gnuplot. Additionally one might be interested in the exact magnetization configuration at every time in order to get deeper insight into the dynamic process. The FieldLogger provides loggin features for arbitrary scalar and vector fields and writes the field data along with the current simulation time to a PVD file that can be read by programs using the VTK library, e.g. Paraview. The class Logger employs both ScalarLogger and FieldLogger to log scalar and field data and offers a convenient resume functionality to continue a simulation from intermediate logging results.

## Logger¶

class magnumfe.Logger(directory, scalars=[], fields=[], scalars_every=1, fields_every=1)

Logger class that is able to log both scalars and fields and that offers a simple resume mechanism to resume simulations from existing log files. Resume is currently only possible from time t and magnetization m and requires the respective field logger for the magnetization. Internally uses ScalarLogger and FieldLogger.

Arguments
directory (string)
The directoy where all log files are stored
scalars (tuple|:class:list)
List of scalars to be logged, e.g. (‘t’, ‘m’, ‘m[pinned]’)
fields (tuple|:class:list)
List of fields to be logged, e.g. (‘m’, ‘h[pinned]’)
scalars_every (int)
Log scalars every nth step
fields_every (int)
Log fields every nth step
Example
# Log the magnetization every 10th step
logger = Logger('data', scalars = ['t', 'm'], fields = ['m'])

# check if resume is possible
logger.is_resumable()

# try to resume from log files
logger.resume(state)

# log state
logger << state

is_resumable()

Returns True if logger can resume from log files.

Returns
bool
True if resumable, False otherwise
log(state)

Logs a field from the given state to the log file. This method is aliased with left-shift operator, so logger.log(state) can also be written as logger << state.

Arguments
state (State)
The state from which the logging row should be generated.
resume(state)

Tries to resume from existing log files. If resume is possible 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 modiefied and the simulations starts from the beginning.

Arguments
state (State)
The state to be resumed from the log data

## ScalarLogger¶

class magnumfe.ScalarLogger(filename, columns, every=1)

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

Arguments
filename (str)
The name of the log file
columns ([str | function])
The columns to be written to the log file
every (int)
Write row to log file every nth call
Example
# log time and average magnetization
logger = ScalarLogger('log.dat', ('t', 'm'))

# log time and average magnetization in the domains
# 'free' and 'fixed' separately
logger = ScalarLogger('log.dat', ('t', 'm[free]', 'm[fixed]'))

# log time and potential u averaged on the facet domain 'top'
logger = ScalarLogger('log.dat', ('t', 'u[facet:top]'))

# log time in ns and averaged magnetization
logger = ScalarLogger('log.dat', (('t', lambda state: state.t*1e9), 'm'))

# Actually log a row
state = State(mesh)
logger << state

add_column(column)

Add a column to the logger. The column can be given either by a str pointing to a (virtual) attribute in the state object, e.g. ‘t’ to log state.t or a function that receives a state object and returns the value to be logged, e.g. lambda state: state.t. Lambda attributes can be named by providing a tuple with name and lambda, e.g. ('t', lambda state: state.t).

Arguments
column (str | tuple | function)
the column to be logged.
log(state)

Logs a row to the log file generated from the given state. This method is aliased with left-shift operator, so logger.log(state) can also be written as logger << state.

Arguments
state (State)
The state from which the logging row should be generated.
reset()

Reset the counter that is used to log every nth row.

resumable_step()

Returns the last step the logger can resume from, e.g. if the logger logs every 10th step and the first (i = 0) step was already logged, the result is 10.

Returns
int
The step number the logger is able to resume from
resume(i)

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

Arguments
i (int)
The log step to resume from

## FieldLogger¶

class magnumfe.FieldLogger(filename, field, every=1, encoding='compressed')

Simple logger class to log field values into PVD files.

Arguments
filename (str)
The name of the log file
field (str)
The name of the field to be logged, e.g. “m”
every (int)
Write row to log file every nth call
encoding (string)
Encoding used to write VTU files (“ascii”, “compressed”)
Example
# Log the magnetization every 10th step
logger = FieldLogger('m.pvd', 'm', every = 10)

# Log the magnetization cropped to the 'free' domain
logger = FieldLogger('m.pvd', 'm[free]')

# Actually log a field
state = State(mesh)
logger << state

last_recorded_step()

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

Returns
int
Number of the last step recorded
log(state)

Logs a field from the given state to the log file. This method is aliased with left-shift operator, so logger.log(state) can also be written as logger << state.

Arguments
state (State)
The state from which the logging row should be generated.
reset()

Reset the counter that is used to log every nth row.

resumable_step()

Returns the last step the logger can resume from, e.g. if the logger logs every 10th step and the first (i = 0) step was already logged, the result is 10.

Returns
int
The step number the logger is able to resume from
resume(i)

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

Arguments
i (int)
The log step to resume from
step_data(i)

Returns field and time to a given step number.

Arguments
i (int)
The step number
Returns
(float, dolfin.Function)
Time and the field of step i