Normalize_IR
allow composite if_statments and normalize
allow event conditions to be composite (contain &&, || ).
Example
if u[1]+u[2] > u[3] && u < 10 || u > 5 && u < 15 || u > 20
a = 0
end
Solution
case1) && ; without else
if A>0 && B>0
X
end
<–>
if A>0
if B>0
X
end
end
if B>0
if A>0
X
end
end
case2) && ; with else
if A>0 && B>0
X
else
Y
end
<–>
if A>0
if B>0
X
end
else
Y
end
if B>0
if A>0
X
end
else
Y
end
case3) || ; without else
if A>0 || B>0
X
end
<–>
if A>0
X
end
if B>0
X
end
case4) || ; with else
if A>0 || B>0
X
else
Y
end
<–>
if A>0
X
else
if !(B>0)
Y
end
end
if B>0
X
else
if !(A>0)
Y
end
end
case5) multiple || && ;with or without else
if A>0 || B>0 && C>0 #.....
#(with or without else : the logic is the same)
end
<–>
if A>0
#whole user if-statment and its body
end
if B>0
#whole user if-statment and its body
end
if C>0
#whole user if-statment and its body
end
QuantizedSystemSolver.SimpleModelIR.changeVarNames_params
— MethodchangeVarNames_params(ex::Expr,stateVarName::Symbol,discrParamName::Symbol,muteVar::Symbol,param::Dict{Symbol,Union{Float64,Int64,Expr,Symbol}},helperFunSymSet::Set{Symbol})
As the name suggests, this changes the continuous variables names to :q and the discrete variable name to :p and any mute variables to :i. It also plugs the parameters values from a parameter dictionary into the differential equations. The function changeVarNames_params has three methods. One for RHS of equations, one for if-statements when RHS is an expression, and one for if-statements when RHS is a symbol. This is method one. It has an additional symDict::Dict{Symbol,Expr} to collect the translation of symbols of continous and discrete variables (q[i] <-> qi).
arguments:
ex::Expr
: the expression to be changedstateVarName::Symbol
: the name of the state variablemuteVar::Symbol
: the name of the mute variableparam::Dict{Symbol,Union{Float64,Int64,Expr,Symbol}}
: the dictionary of parameters
Example:
using QuantizedSystemSolver
(ex, stateVarName, discrParamName,muteVar, param) = (:(du[k] = u[k] * u[k - 1] * coef2), :u,:p, :k, Dict{Symbol, Union{Float64, Int64,Expr,Symbol}}(:coef1 => 2.0, :coef2 => 1.5))
newEx=QuantizedSystemSolver.changeVarNames_params(ex, stateVarName,discrParamName, muteVar, param,Set([:f]))
(newEx, stateVarName, muteVar, param)
# output
(:(du[i] = q[i] * q[i - 1] * 1.5), :u, :k, Dict{Symbol, Union{Float64, Int64, Expr, Symbol}}(:coef1 => 2.0, :coef2 => 1.5))
QuantizedSystemSolver.SimpleModelIR.changeVarNames_params
— MethodchangeVarNames_params(element::Symbol,stateVarName::Symbol,discrParamName::Symbol,muteVar::Symbol,param::Dict{Symbol,Union{Float64,Int64,Expr,Symbol}},helperFunSymSet::Set{Symbol})
This is method three of the function changeVarNames_params. It is for if-statements when RHS is a symbol. Again, it changes the symbol to :q if it is a continuous variable, to :p if it is a discrete variable, to :i if it is a mute variable, and to its corresponding value if it is a parameter.
QuantizedSystemSolver.SimpleModelIR.recurse
— Methodrecurse(e::Expr,flattened::Vector{Expr})
Break down a compound condition into basic components. This function recursively decomposes a condition expression into its basic components, flattening nested logical operators (&&, ||) into a list of expressions. It returns a vector of flattened expressions.
Example:
using QuantizedSystemSolver
ex=:(u < 1 || u > 10)
flattened = Expr[]
QuantizedSystemSolver.recurse(ex, flattened)
flattened
# output
2-element Vector{Expr}:
:(u < 1)
:(u > 10)
QuantizedSystemSolver.SimpleModelIR.decompose_condition
— Methoddecompose_condition(cond::Expr)
Break down a compound condition into basic components. Returns a tuple (kind, normalized conditions). where kind is :or or :and depending on the original condition.
Example:
using QuantizedSystemSolver
ex=:(u < 1 || u > 10)
(kind,flattened)=QuantizedSystemSolver.decompose_condition(ex)
(kind,flattened)
# output
(:or, Expr[:(u < 1), :(u > 10)])
QuantizedSystemSolver.SimpleModelIR.to_zcf
— Methodto_zcf(expr::Expr)
Convert a condition expression to zero-crossing form. This function transforms a condition expression into a zero-crossing form (ZCF) by rearranging the terms. For example, it converts expressions like A < B
to B - A
and A > B
to A - B
. It is used to prepare conditions for further processing in the normalization of IRs.
Example:
```jldoctest using QuantizedSystemSolver ex1 = :(u > 10) ex2 = :(u < 1) zcf1 = QuantizedSystemSolver.tozcf(ex1) zcf2 = QuantizedSystemSolver.tozcf(ex2) (zcf1, zcf2)
output
(:(u - 10), :(1 - u))
QuantizedSystemSolver.SimpleModelIR.process_if_condition
— Methodprocess_if_condition(cond, stateVarName, discrParamName, param, helperFunSymSet)
Processes an if
condition within the intermediate representation (IR).
Arguments
cond
: The condition expression to be processed.stateVarName
: The name of the state variable involved in the condition.discrParamName
: The name of the discrete parameter relevant to the condition.param
: Additional parameters required for processing.helperFunSymSet
: A set of helper function symbols used during processing.
Returns
Returns the processed representation of the if
condition, potentially transformed for normalization within the IR.
Notes
This function is intended for internal use in the normalization of IR.
QuantizedSystemSolver.SimpleModelIR.process_if_block
— Methodprocess_if_block(block_expr::Expr, stateVarName, discrParamName, param, helperFunSymSet)
Processes an if
block (body) expression within the intermediate representation (IR).
Arguments
block_expr::Expr
: The Julia expression representing theif
block to be processed.stateVarName
: The name of the state variable involved in the block.discrParamName
: The name of the discrete variable.param
: Additional parameter(s) required for processing the block.helperFunSymSet
: A set of helper function symbols used during processing.
Returns
- The processed
if
block body (change of variable names).
Notes
- This function is intended for internal use within the IR normalization pipeline.
QuantizedSystemSolver.SimpleModelIR.process_if_expr
— Methodprocess_if_expr(statement,stateVarName,discrParamName,param,helperFunSymSet)
Processes an IfStatement
within the intermediate representation (IR) of a simple model. This function normalizes the given IfStatement
according to the provided state variable name, discrete variable name, additional parameters, and a set of helper function symbols: - Applies variable substitution and body block processing. - Decomposes composite conditions (&&, ||).
Arguments
statement::IfStatement
: TheIfStatement
node to be processed.stateVarName
: The name of the state variable relevant to the normalization.discrParamName
: The name of the discretization parameter.param
: Additional parameter(s) required for normalization.helperFunSymSet
: A set of symbols representing helper functions used during normalization.
Returns
Vector{IfStatement}
: A vector of normalizedIfStatement
objects resulting from the processing.
Notes
This function is typically used as part of the IR normalization pipeline in the SimpleModelIR
module.
QuantizedSystemSolver.SimpleModelIR.normalize_ir
— Methodnormalize_ir(ir, stateVarName::Symbol, discrParamName::Symbol)
Normalizes the intermediate representation (IR) of an ODE function.
Arguments
ir::ODEFunctionIR
: The intermediate representation of the ODE function to be normalized.stateVarName::Symbol
: The symbol representing the state variable in the IR.discrParamName::Symbol
: The symbol representing the name of the discrete variable.
Returns
- A normalized version of the input
ODEFunctionIR
.
Description
This function processes the given ODE function IR, normalizing its structure with respect to the specified state variable and discretization parameter. The normalization includes renaming variables, swapping parameters, and decomposing composite if-statements.