ElectromagneticFields.jl – Modules

ElectromagneticFields.codeFunction
code(equ, pert = ZeroPerturbation(); export_parameters = true, escape = false, output = 0, cse = true)

Generate code for evaluating analytic equilibria: an Expr defining the field functions of equ, which @code splices into the calling module and load_equilibrium evaluates into one.

  • export_parameters also emits the equilibrium's scalar parameters as constants.
  • escape escapes the generated names, which a macro splicing this into another module needs.
  • output is 0, 1 or 2: silent, one line per function, or the generated body as well.
  • cse names each repeated subexpression once instead of emitting it in full every time it occurs. This is on by default and is value-preserving to the last bit — subexpressions are named, never rewritten — but the generated code is easier to read against a paper with it off. It matters: SymEngine shares nothing, so db₁dx₁ of the ITER Solov'ev equilibrium with an X-point contains 108 separate evaluations of log(ξ₁) without it, and is 6.8 times slower.

The @code macros take these as key = value arguments, after the equilibrium's parameters: ThetaPinch.@code(B₀, cse = false), or just ThetaPinch.@code cse = false for the defaults.

source
ElectromagneticFields.code_argumentsMethod
code_arguments(args)

Split the arguments a @code macro was called with into the equilibrium's parameters and the options destined for code, returned as (parameters, options).

Macros cannot take keyword arguments, so an option is written as key = value among the arguments — @code cse = false or @code(R₀, B₀, q₀, cse = false). A ; key = value tail is accepted too.

source
ElectromagneticFields.eliminate_common_subexpressionsMethod
eliminate_common_subexpressions(body::Expr; prefix = "_cse")

Rewrite a generated function body so that every repeated subexpression is evaluated once and read from a local afterwards. Returns a begin ... end block whose value is that of body, and whose locals are named prefix * n.

Value-preserving to the last bit: subexpressions are named, not rewritten.

source
ElectromagneticFields.load_equilibriumFunction
load_equilibrium(equ, pert = ZeroPerturbation(); target_module = Main, output = 0, cse = true)

Evaluate functions for evaluating analytic equilibria: generate the field functions of equ with code and evaluate them into target_module.

output and cse are passed on to code — in particular cse = false emits every repeated subexpression in full, which is slower but easier to read against a paper.

source