add_equation
Stable function · Editing
Tier: stable · Kind: function · Topic: Editing
| Julia | TypeScript | Python | Rust | Go |
|---|---|---|---|---|
add_equation | addEquation | add_equation, add_equation_to_model | add_equation | AddEquation |
Note: spelling differs from the §2 rule: python=add_equation_to_model (expected add_equation); multiple spellings exported by: python
Julia
add_equation(model::Model, equation::Equation)
add_equation(model::Model, equation::Equation) -> Model
Add a new equation to a model.
Appends the equation to the end of the equations list.
Example (edit)
doc = load_path(joinpath(FIXTURES, "decay.esm"))
model = doc.models["Decay"]
# Edits return a new Model; the original is left alone.
decayed = ModelVariable(UnknownVariable; default = 0.0, units = "mol",
description = "Amount that has decayed")
edited = add_variable(model, "decayed", decayed)
edited = add_equation(edited, parse_equation("D(decayed, t) = lambda * N"))
@assert ode_states(model) == ["N"]
@assert ode_states(edited) == ["N", "decayed"]
doc.models["Decay"] = edited
@assert validate(doc).is_valid
json = to_json(doc)
@assert occursin("\"decayed\"", json)
path = joinpath(mktempdir(), "decay_edited.esm")
write_path(doc, path)
reloaded = load_path(path)
@assert sort(ode_states(reloaded.models["Decay"])) == ["N", "decayed"]
TypeScript
function addEquation(model: Model, equation: Equation): Model
Add a new equation to a model
Parameters
model— Model to add equation toequation— Equation to add
Returns
New model with equation added
Example (edit)
const doc = loadPath(fixture('decay.esm'))
const model = doc.models!.Decay
assert.ok(!('ref' in model)) // narrows away an unresolved `{ ref }` mount
// Edits never mutate: each returns a new model.
const withVar = addVariable(model, 'fraction', {
type: 'unknown',
units: '1',
description: 'Fraction of the initial amount remaining',
})
const edited = addEquation(withVar, parseEquation('fraction = N / 100'))
assert.equal(model.equations.length, 2)
assert.equal(edited.equations.length, 3)
assert.deepEqual(observedUnknowns(edited), ['fraction', 'half_life'])
const newDoc = { ...doc, models: { ...doc.models, Decay: edited } }
// `toJson` returns the text; `writePath` writes it and returns nothing.
const json = toJson(newDoc)
const reloaded = loadString(json).models!.Decay
assert.match(json, /"fraction"/)
const outPath = join(outDir, 'decay_edited.esm')
writePath(newDoc, outPath)
assert.equal(readFileSync(outPath, 'utf8'), json)
Python
Exported as add_equation and add_equation_to_model. Both names refer to the same operation.
add_equation
add_equation(model: Model, equation: Equation, validate_after_edit: bool = True) -> EditResult
Add an equation to a model.
add_equation_to_model
add_equation_to_model(model: Model, equation: Equation, validate_after_edit: bool = True) -> EditResult
Deprecated alias for add_equation
. Add an equation to a model.
Example (edit)
# Edits return a new model in `modified_object`; the input is left unchanged.
edited = earthsci_ast.add_variable(
model, "N0", ModelVariable(type="parameter", units="mol", default=100.0)
).modified_object
edited = earthsci_ast.add_variable(
edited, "fraction", ModelVariable(type="unknown", units="1")
).modified_object
step = earthsci_ast.add_equation(
edited, Equation(lhs="fraction", rhs=ExprNode(op="/", args=["N", "N0"]))
)
assert step.success, step.errors
doc.models["Decay"] = step.modified_object
assert "fraction" not in model.variables # the original model is untouched
assert earthsci_ast.validate(doc).is_valid
assert earthsci_ast.observed_unknowns(doc.models["Decay"]) == ["fraction", "half_life"]
text = earthsci_ast.to_json(doc)
assert json.loads(text)["models"]["Decay"]["equations"][-1] == {
"lhs": "fraction",
"rhs": {"op": "/", "args": ["N", "N0"]},
}
with tempfile.TemporaryDirectory() as tmp:
out = Path(tmp) / "decay_edited.esm"
earthsci_ast.write_path(doc, out)
reread = earthsci_ast.load_path(out)
assert list(reread.models["Decay"].variables) == ["N", "lambda", "half_life", "N0", "fraction"]
Rust
pub fn add_equation(model: &Model, equation: Equation) -> EditResult<Model>
Add an equation to a model
Arguments
model- The model to modifyequation- The equation to add
Returns
EditResult<Model>- New model with the added equation
Example (edit)
let mut doc = load_path(fixture("decay.esm"))?;
let model = &doc.models.as_ref().unwrap()["Decay"];
// Track the decay product M. Edits return a new model; the input is
// left unchanged.
let product = ModelVariable {
var_type: VariableType::Unknown,
units: Some("mol".to_string()),
default: Some(0.0.into()),
description: Some("Amount produced".to_string()),
default_units: None,
element_type: None,
shape: None,
location: None,
distribution: None,
update: None,
};
let model = add_variable(model, "M", product)?;
let model = add_equation(&model, parse_equation("D(M)/Dt = lambda * N")?)?;
assert_eq!(model.variables.len(), 4);
assert_eq!(model.equations.len(), 3);
// Adding a name that is already declared is an error.
assert!(add_variable(&model, "N", model.variables["N"].clone()).is_err());
doc.models.as_mut().unwrap().insert("Decay".to_string(), model);
let json = to_json(&doc)?;
assert!(json.contains("\"M\""));
let out = std::env::temp_dir().join("decay_with_product.esm");
write_path(&doc, &out)?;
let reread = load_string(&std::fs::read_to_string(&out)?)?;
assert_eq!(reread.models.as_ref().unwrap()["Decay"].variables.len(), 4);
Go
func AddEquation(model Model, equation Equation) Model
AddEquation returns a copy of model with equation appended.
Example (edit)
doc, err := esm.LoadPath(fixture("decay.esm"))
if err != nil {
log.Fatal(err)
}
units, desc := "mol", "Amount decayed so far"
model := esm.AddVariable(doc.Models["Decay"], "decayed", esm.ModelVariable{
Type: "unknown", Units: &units, Description: &desc,
})
eq, err := esm.ParseEquation("decayed = 100 - N")
if err != nil {
log.Fatal(err)
}
model = esm.AddEquation(model, *eq)
doc.Models["Decay"] = model
fmt.Println(len(model.Equations), esm.Validate(doc).IsValid)
text, err := esm.ToJSON(doc)
if err != nil {
log.Fatal(err)
}
fmt.Println(strings.Contains(text, `"decayed"`))
path := filepath.Join(dir, "decay-edited.esm")
if err := esm.WritePath(doc, path); err != nil {
log.Fatal(err)
}
reread, err := esm.LoadPath(path)
if err != nil {
log.Fatal(err)
}
fmt.Println(sortedKeys(reread.Models["Decay"].Variables))