SUBMODELs are an experimental feature of the modeling language used in Magnolia.  One or more SUBMODEL sections may be specified in the model file before the main MODEL section.  SUBMODELs are intended to be used when a set of equations representing identically modeled subsystems in a model can be “factored” out of the main model code.  For example, one might imagine a model of an automobile which involves modeling four wheels.  The wheels might each use the exact same model code, albeit with perhaps different parameters and coupled in a different way to the rest of the model.  In Magnolia, the automobile model might be accomplished using a “Wheel” submodel, which includes four “Wheel” instances declared via a DIMENSION statement in main model, which might be called “Car.”

• One or more SUBMODEL sections can be introduced prior to the main MODEL section.  Each SUBMODEL must have a unique name.
• One or more “instances” of each SUBMODEL are created in the main model using the DIMENSION statement, much in the same way one might declare an integer variable, or and array variable.  Each “instance” is associated with a unique variable name.
• Variables belonging to each SUBMODEL are referenced from the main MODEL using “dot” notation.  For example, to reference a variable called “omega” in a “Wheel” submodel from the main model, one would used the following syntax (assume the associated variable name in the main model is “wheel2”: foo = wheel2.omega.
• To override the values of constants defined in a SUBMODEL, additional CONSTANT statements are declared in the main model class which reference those constants using the “dot” notation.  For example, if the “Wheel” model contains a constant named “radius”, the main model code might include a line like: CONSTANT wheel2.radius = 15.0
• When translating the model, Magnolia combines and sorts equations from the main model and all required submodels as necessary


! Model which clips output to fall within
! prescribed lower and upper bounds
submodel Limiter
constant lb = -1.0, ub = 1.0
function y = clip(x)
dimension x
if(x >= ub)
y = ub
else if(x <= lb)
y = lb
else
y = x
end
end
end

! Simple exponential decay model
submodel Exponential
derivative
constant k   = 1.0
constant xic = 1.0
xd = -k*x
x  = integ(xd, xic)
end
end

! Harmonic oscillator model
submodel Harmonic
derivative
constant k = 1.0
constant xdic = 0.0, xic = 1.0
xdd = -k*x
xd  = integ(xdd, xdic)
x   = integ(xd, xic)
end
end

! Combined model
model Combined
initial
constant kexp = 2.0, kharm = 3.0
m1.k = kexp
m2.k = kharm
end

derivative
dimension Exponential m1
dimension Harmonic m2
dimension Limiter m3

constant m1.k = 2.0, m2.k = 3.0
constant m3.lb = -0.5, m3.ub = 0.5

x1 = m1.x * m2.x
x2 = m3.clip(x1)

constant tstop = 10.0
termt(t >= tstop, 'Stopped on time limit')
end
end