Version: v0.4.0

sysview

Stable function · Differentiate a model

Tier: stable · Kind: function · Topic: Differentiate a model

JuliaPythonRust
sysviewsysviewsysview

Note: Julia and Python take a document’s model or a flattened system through one sysview; Rust views a flattened system through sysview_flat. The view holds the document’s data_sources (a document’s model takes its own; a flattened system has none, so Julia and Python take a data_sources keyword and Rust SysView::with_data_sources), which the derived documents copy for their data-fed parameters. Not given (nothing, None, Rust with_data_sources(None)) keeps the view’s own in every binding.

Julia

sysview(file::EarthSciAST.EsmFile, model_name; data_sources)
sysview(flat::EarthSciAST.FlattenedSystem; data_sources)
sysview(file::EsmFile, model_name; data_sources = file.data_sources) -> SysView
sysview(flat::FlattenedSystem; data_sources = nothing)              -> SysView

data_sources (a document’s data_sources, name → DataSource) is what the derived documents copy for each data-fed parameter (esm-jacobian-spec.md §8.1). A FlattenedSystem does not carry them, so pass the flattened document’s own when its parameters read data.

Source

Example (bands)

file = EarthSciAST.load_path(joinpath(FIXTURES, "bd_chem.esm"))
view = sysview(file, "Chem")
entries = jacobian_bands(view)
# One entry per (row variable, column variable) pair that occurs in the model.
@assert sort(unique([(en.u, en.v) for en in entries])) ==
    [("A", "A"), ("A", "B"), ("A", "C"), ("B", "A"), ("B", "B"), ("C", "B")]

From docs/api/examples/julia/jacobians.jl

Python

sysview(obj, model_name: str | None = None, data_sources=None) -> SysView

View a model of an EsmFile, or a FlattenedSystem, for differentiation.

Parameters

  • obj — an EsmFile, a flattened system, or a SysView (returned as is, model_name ignored).
  • model_name — which model of an EsmFile; may be omitted when the file has exactly one.
  • data_sources — a document’s data_sources (name → DataSource), the entries the derived documents copy for each data-fed parameter (spec §8.1). None keeps the input’s own: an EsmFile’s, none for a flattened system, which does not carry them, so pass the flattened document’s when its parameters read data.

Source

Example (bands)

file = earthsci_ast.load_path(FIXTURES / "bd_chem.esm")
entries = earthsci_astdiff.jacobian_bands(file)
# One entry per (row variable, column variable) pair that occurs in the model.
assert sorted({(en.u, en.v) for en in entries}) == [
    ("A", "A"),
    ("A", "B"),
    ("A", "C"),
    ("B", "A"),
    ("B", "B"),
    ("C", "B"),
]
view = earthsci_astdiff.sysview(file)

From docs/api/examples/python/jacobians.py

Rust

pub fn sysview<'a>(file: &'a EsmFile, model_name: Option<&str>) -> Result<SysView<'a>, BandError>

View a model of file for differentiation. model_name may be None when the file has exactly one model.

Source · Rust docs

Example (bands)

let file = load_path(fixture("bd_chem.esm"))?;
let entries = jacobian_bands(&file, None, Wrt::States)?;
// One entry per (row variable, column variable) pair that occurs in the model.
let mut pairs: Vec<(&str, &str)> =
    entries.iter().map(|en| (en.u.as_str(), en.v.as_str())).collect();
pairs.sort();
pairs.dedup();
assert_eq!(
    pairs,
    [("A", "A"), ("A", "B"), ("A", "C"), ("B", "A"), ("B", "B"), ("C", "B")]
);
let view = sysview(&file, None)?;

From docs/api/examples/rust/jacobians.rs