graintrace.taylor#

Uniaxial Taylor crystal-plasticity forward + calibration model (NEML2 v3).

A mixed-control NEML2 NonlinearSystem (cpfe_base/neml2_cpfe_calibration.i) is wrapped by neml2.pyzag.NEML2PyzagModel and driven through pyzag.nonlinear.solve_adjoint() for analytic parameter gradients.

class graintrace.taylor.UniaxialTaylorModel(factory, axial_index=2, nchunk=5, rtol=1e-06, atol=1e-08, linesearch_iter=5)[source]#

Bases: Module

Differentiable uniaxial Taylor forward via a NEML2 mixed-control eq_sys.

Wraps a neml2.pyzag.NEML2PyzagModel and integrates the strain history with pyzag.nonlinear.solve_adjoint. Uniaxial loading is expressed with NEML2’s MixedControlSetup: control selects the axial component as strain-controlled (prescribed = rate); the remaining components are stress-free, and a global constraint forces target_cauchy_stress to equal the grain-mean per-crystal stress.

Parameters:
  • factory (NEML2PyzagModel)

  • axial_index (int)

  • nchunk (int)

  • rtol (float)

  • atol (float)

  • linesearch_iter (int)

forward(orientations, strain, rate, initial_strains=None, return_state=False, return_orientation=False)[source]#

Integrate the uniaxial history and return the macro stress trajectory.

Parameters:
  • orientations (Tensor) – (n_grains, 3) NEML2 v3 MRP orientations (tan(theta/4)*axis).

  • strain (Tensor) – (ntime,) axial strain grid (mm/mm), typically starting at 0.

  • rate (float) – assumed strain rate (1/s); t = strain / rate.

  • initial_strains (Tensor | None) – optional (n_grains, 6) initial elastic strain.

  • return_state (bool) – also return per-grain elastic strain history.

  • return_orientation (bool) – also return the per-grain crystal orientation history (the model’s orientation state, which evolves with plastic spin).

Returns:

macro_stress alone when neither flag is set, otherwise a tuple in the fixed order (macro_stress[, elastic_strain][, orientation]):

  • macro_stress: (ntime, 6) macro Cauchy stress (SR2 Voigt: xx, yy, zz, yz, xz, xy).

  • elastic_strain (if return_state): (ntime, n_grains, 6).

  • orientation (if return_orientation): (ntime, n_grains, 3) neml2 v3 MRP, in the same convention and frame as the input orientations (no conversion is applied); step 0 equals the input.

class graintrace.taylor.TaylorModel(neml2_path, neml2_model_name='model_with_stress', axial_index=2, assumed_rate=0.0001, npoints=500, nchunk=5, equation_system='eq_sys', exclude_parameters=None, compile=False, device='cpu', slip_systems=None)[source]#

Bases: BaseMaterialApproximationModel

Taylor calibration model backed by the NEML2 v3 + pyzag adjoint engine.

  • Loads and preprocesses experimental data (per-stress-level CSVs).

  • Manages the six calibration parameters as NEML2/torch parameters.

  • Runs differentiable uniaxial stress-strain simulations for optimization.

slip_systems (constructor kwarg, default None = use neml2_path as written, which for the shipped neml2_cpfe_calibration.i is FCC <110>{111}) selects the crystal slip: a preset name from graintrace.slip_systems.SLIP_SYSTEM_PRESETS ("fcc", "bcc", "bcc_112", "bcc_110_112", "bcc_110_112_123"), a SlipSystems, or (direction, plane) Miller families. The deck’s # @SLIP lines are rewritten into a temporary copy, which is what gets loaded. Use the same value as CPFESimulation(slip_systems=...) so calibration and CPFE agree.

Parameters:
DEFAULT_EXCLUDE_PARAMETERS = ['slip_rule_gamma0', 'slip_rule_n']#
get_params()[source]#

Current values of the six calibration parameters (detached).

Return type:

Tensor

set_params(values)[source]#

Assign the six calibration parameters from a 1-D iterable.

Return type:

None

load_experiment_data(data_dir, strain_stress_file, full_field_strain_units=None, strain_stress_file_units=None, straintype='eFab', npoints=None, max_strain=None, max_stress=None, n_grains=None, seed=0, angle_identifier=('Eul0', 'Eul1', 'Eul2'), angle_convention='bunge', angle_type='auto')[source]#

Load the per-stress-level FF grain tables and the macro stress-strain curve.

Grain orientations come from the Euler columns (angle_identifier) via euler_to_mrp(), i.e. the neml2 MRP of the active R the Taylor model expects. Only when a table has no Euler columns are the O11..O33 columns used, and they must hold the active R (MIDAS Grains.csv O; see load_orientation_matrices()).

Parameters:
  • data_dir (str) – folder of per-stress-level CSVs named <stress>.csv.

  • strain_stress_file (str) – macro strain,stress CSV.

  • full_field_strain_units (str | None) – None or "microstrain" (grain strains).

  • strain_stress_file_units (str | None) – None or "microstrain" (macro strain).

  • straintype (str) – full-field strain column prefix ("eKen" or "eFab").

  • npoints (int | None) – resampled macro-curve points (defaults to the model’s).

  • max_strain (float | None) – cap the macro curve at this strain.

  • max_stress (float | None) – cap the macro curve at this stress (if no max_strain).

  • n_grains (int | None) – subsample this many grains per file (None = all).

  • seed (int) – RNG seed for the subsampling.

  • angle_identifier – the three Euler columns (standard Bunge triplet).

  • angle_convention (str) – "bunge", "kocks" or "roe".

  • angle_type (str) – "degrees", "radians" or "auto" (any |value| > 2*pi means degrees, MIDAS style), per file.

Returns:

dict with files, stress_levels, strain_stress, exp_strain, exp_texture (neml2 MRP per file), exp_weights, avg_exp_strain, avg_axial_strain, use_weights and cutoff_strain.

simulate(params, d=None, assumed_rate=None, experiment_data=None, return_state=False, initial_strains=None, return_orientation=False)[source]#

Run a differentiable uniaxial stress-strain simulation.

Parameters:
  • params – optional 1-D tensor of the six calibration values. If given, they are assigned to the factory (no grad) before the solve; use this for plotting a specific parameter set. Pass None to run with the factory’s current (possibly optimizer-owned) parameters so gradients flow (used during calibration).

  • d (Tensor | None) – ignored (kept for API compatibility; the axial direction is set by self.axial_index).

  • assumed_rate (float | None) – strain rate; defaults to self.assumed_rate.

  • experiment_data – dict from load_experiment_data().

  • return_state (bool) – also return the per-grain elastic strain history.

  • initial_strains – optional (n_grains, 6) initial elastic strain.

  • return_orientation (bool) – also return the per-grain orientation history.

Returns:

stress_hist (npoints, 6) alone when neither flag is set, otherwise a tuple in the fixed order (stress_hist[, state_hist][, ori_hist]): state_hist is (npoints, n_grains, 6) elastic strain (if return_state); ori_hist is (npoints, n_grains, 3) neml2 v3 MRP orientations (if return_orientation), in the same convention as experiment_data["exp_texture"][0] (no conversion), with ori_hist[0] equal to the input texture.