Version: dev

rename_variable

Stable function · Editing

Tier: stable · Kind: function · Topic: Editing

JuliaTypeScriptPythonRustGo
rename_variablerenameVariablerename_variable, rename_variable_in_modelnot exportedRenameVariable

Note: spelling differs from the §2 rule: python=rename_variable_in_model (expected rename_variable); multiple spellings exported by: python

Julia

rename_variable(model::Model, old_name::String, new_name::String)
rename_variable(model::Model, old_name::String, new_name::String) -> Model

Rename a variable throughout the model.

Updates the variable definition and all references in equations. Throws EditError if old_name does not exist.

Source

Example (rename_variable)

renamed = rename_variable(model, "lambda", "k")
@assert parameters(renamed) == ["k"]
@assert to_ascii(renamed.equations[1]) == "D(N)/Dt = (-k) * N"

# Drop the half_life equation, then the variable it defined.
trimmed = remove_variable(remove_equation(model, 2), "half_life")
@assert unknowns(trimmed) == ["N"]
@assert length(trimmed.equations) == 1

# Editing a variable that does not exist raises an EditError.
try
    rename_variable(model, "nope", "k")
    error("expected an EditError")
catch e
    @assert e isa EditError
end

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

TypeScript

function renameVariable(model: Model, oldName: string, newName: string): Model

Rename a variable throughout a model.

The rewrite covers exactly the expression sites removeVariable scans (via substituteInModel — equations, observed-variable expressions, event conditions/triggers/affect RHSs, and inline subsystems), so a rename never leaves a dangling reference in a site the removal guard would have flagged.

Parameters

  • model — Model to rename variable in
  • oldName — Current variable name
  • newName — New variable name

Returns

New model with variable renamed

Throws

  • EntityNotFoundError if variable doesn’t exist

Source

Python

Exported as rename_variable and rename_variable_in_model. Both names refer to the same operation.

rename_variable

rename_variable(model: Model, old_name: str, new_name: str, validate_after_edit: bool = True) -> EditResult

Rename a variable in a model.

Source

rename_variable_in_model

rename_variable_in_model(model: Model, old_name: str, new_name: str, validate_after_edit: bool = True) -> EditResult

Deprecated alias for rename_variable . Rename a variable in a model.

Source

Example (rename_variable)

renamed = earthsci_ast.rename_variable(doc.models["Decay"], "lambda", "k").modified_object
assert "k" in renamed.variables and "lambda" not in renamed.variables
assert earthsci_ast.to_ascii(renamed.equations[1]) == "half_life = log(2) / k"

trimmed = earthsci_ast.remove_variable(renamed, "N0")
assert "N0" not in trimmed.modified_object.variables

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

Rust

Rust does not export this function.

Go

func RenameVariable(model Model, oldName, newName string) (Model, error)

RenameVariableInFile when the document is available; it resolves scoped references and rewrites coupling endpoints too. TypeScript’s renameVariable has exactly this limitation, for exactly this reason: it calls substituteInModel without the esmFile context that its own scoped-reference resolution requires.

Source · Go docs