pycalphad.codegen package

Submodules

pycalphad.codegen.phase_record_factory module

class pycalphad.codegen.phase_record_factory.PhaseRecordFactory(dbf, comps, state_variables, models, parameters=None)[source]

Bases: object

get(phase_name)[source]
get_phase_constraints(phase_name)[source]
get_phase_formula_energy(phase_name)[source]
get_phase_formula_moles_element(phase_name, element_name, per_formula_unit=True)[source]
get_phase_property(phase_name, property_name, include_grad=True, include_hess=True)[source]
items()[source]
keys()[source]
update_parameters(parameters)[source]
values()[source]

pycalphad.codegen.sympydiff_utils module

This module defines functions for compiling symbolic SymEngine expressions into fast callable functions.

The SymEngine lambdify function is used to compile the functions using a particular backend, with or without common subexpression elimination (CSE).

By default, the LLVM backend is used with the opt_level=0 option and common subexpression elimination is on, as defined by the module constants:

LAMBDIFY_DEFAULT_BACKEND = 'llvm'
LAMBDIFY_DEFAULT_CSE = True
LAMBDIFY_DEFAULT_LLVM_OPT_LEVEL = 0

Note that as of February 2020, SymEngine only supports using 'lambda' or 'llvm' backends. The LLVM backend uses the LLVM compiler to compile the expressions, and is slower to build the callable functions than the Lambda backend, though in principle the LLVM runtime performance can be better as LLVM can optimize the functions.

Callables produced with the Lambda backend cannot be pickled, since SymEngine does not define how its objects should be serialized. This is important to packages that may want to parallelize pycalphad calls and pickle the callable functions or PhaseRecords. The following issues track this behavior:

class pycalphad.codegen.sympydiff_utils.BuildFunctionsResult(func, grad, hess)

Bases: tuple

func

Alias for field number 0

grad

Alias for field number 1

hess

Alias for field number 2

class pycalphad.codegen.sympydiff_utils.ConstraintFunctions(cons_func, cons_jac, cons_hess)

Bases: tuple

cons_func

Alias for field number 0

cons_hess

Alias for field number 2

cons_jac

Alias for field number 1

pycalphad.codegen.sympydiff_utils.build_constraint_functions(variables, constraints, parameters=None, func_options=None, jac_options=None, hess_options=None)[source]

Build callables functions for the constraints, constraint Jacobian, and constraint Hessian.

Parameters:
  • variables (List[symengine.Symbol]) – Free variables in the symengine_graph. By convention these are usually all instances of StateVariables.

  • constraints (List[symengine.Basic]) – List of SymEngine expression to compile

  • parameters (Optional[List[symengine.Symbol]]) – Free variables in the symengine_graph. These are typically external parameters that are controlled by the user.

  • func_options (None, optional) – Options to pass to lambdify when compiling the function.

  • jac_options (Optional[Dict[str, str]]) – Options to pass to lambdify when compiling the Jacobian.

  • hess_options (Optional[Dict[str, str]]) – Options to pass to lambdify when compiling Hessian.

Return type:

ConstraintFunctions

Notes

Default options for compiling the function, gradient and Hessian are defined by _get_lambdify_options.

pycalphad.codegen.sympydiff_utils.build_functions(symengine_graph, variables, parameters=None, wrt=None, include_obj=True, include_grad=False, include_hess=False, func_options=None, grad_options=None, hess_options=None)[source]

Build function, gradient, and Hessian callables of the symengine_graph.

Parameters:
  • symengine_graph (symengine.Basic) – symengine expression to compile, \(f(x) : \mathbb{R}^{n} \rightarrow \mathbb{R}\), which will corresponds to symengine_graph(variables+parameters)

  • variables (List[symengine.Symbol]) – Free variables in the symengine_graph. By convention these are usually all instances of StateVariables.

  • parameters (Optional[List[symengine.Symbol]]) – Free variables in the symengine_graph. These are typically external parameters that are controlled by the user.

  • wrt (Optional[List[symengine.Symbol]]) – Variables to differentiate with respect to for the gradient and Hessian callables. If None, will fall back to variables.

  • include_obj (Optional[bool]) – Whether to build the symengine_graph callable, \(f(x) : \mathbb{R}^{n} \rightarrow \mathbb{R}\)

  • include_grad (Optional[bool]) – Whether to build the gradient callable, \(\pmb{g}(x) = \nabla f(x) : \mathbb{R}^{n} \rightarrow \mathbb{R}^{n}\)

  • include_hess (Optional[bool]) – Whether to build the Hessian callable, \(\mathbb{H}(x) = \nabla^2 f(x) : \mathbb{R}^{n} \rightarrow \mathbb{R}^{n \times n}\)

  • func_options (Optional[Dict[str, str]]) – Options to pass to lambdify when compiling the function.

  • grad_options (Optional[Dict[str, str]]) – Options to pass to lambdify when compiling the gradient.

  • hess_options (Optional[Dict[str, str]]) – Options to pass to lambdify when compiling Hessian.

Return type:

BuildFunctionsResult

Notes

Default options for compiling the function, gradient and Hessian are defined by _get_lambdify_options.

Module contents