I’m struggling with how to include @structural_parameters in the new “MTK-model”.
Here is a semi-realistic model [simulating 2 nitrogen atoms interacting via a harmonic and a Morse potential…].
My old/working model:
@mtkmodel TwoAtoms begin
#
# Model parameters
# -- structure parameters
@structural_parameters begin
# typeP, :harmonic -- harmonic potential
# typeP, :Morse -- Morse potential
typeP = :harmonic
end
#
# -- parameters
#
# Model parameters
@parameters begin
# Constant
mb = 14, [description = "Nitrogen atom mass, atom mass unit u"]
D_e = 945, [description = "Morse parameter D_e, unit u*nm^2/ps^2"]
a = 27, [description = "Morse parameter a, unit 1/nm"]
r_e = 0.1, [description = "Potential equilibrium distance, unit nm"]
k_e = 2a^2*D_e, [description = "Harmonic parameter, u/ps^2"]
r_0 = 1.5r_e, [description = "Initial distance between particles, nm"]
#
end
#
# Model variables, with initial values needed
@variables begin
# Variables
x_1(t)=0, [description = "Particle 1, position unit nm"]
v_1(t)=0, [description = "Particle 1, velocity unit nm/ps"]
x_2(t)=r_0, [description = "Particle 2, position unit nm"]
v_2(t)=0, [description = "Particle 2, velocity unit nm/ps"]
r(t), [description = "Distance between particles, nm"]
P(t), [description = "Potential energy, u*nm^2/ps^2"]
K(t), [description = "Kinetic energy, u*nm^2/ps^2"]
E(t), [description = "Total energy, u*nm^2/ps^2"]
end
#
# Model equations
@equations begin
# Basic algebraics
K ~ mb*(v_1^2 + v_2^2)/2
r ~ abs(x_1 - x_2)
#
# Velocity definitions
Dt(x_1) ~ v_1
Dt(x_2) ~ v_2
# Dynamics based on typeP
if typeP == :harmonic
Dt(v_1) ~ -k_e*(r - r_e)*sign(x_1-x_2)/mb
Dt(v_2) ~ k_e*(r - r_e)*sign(x_1-x_2)/mb
P ~ k_e/2*(r-r_e)^2
elseif typeP == :Morse
Dt(v_1) ~ -2*D_e*a*(1-exp(-a*(r-r_e)))*exp(-a*(r-r_e))*sign(x_1-x_2)/mb
Dt(v_2) ~ 2*D_e*a*(1-exp(-a*(r-r_e)))*exp(-a*(r-r_e))*sign(x_1-x_2)/mb
P ~ D_e*(1 - exp(-a*(r-r_e)))^2
end
E ~ K + P
end
end
and here is how I instantiate two models:
@mtkcompile tp_h = TwoAtoms()
@mtkcompile tp_m = TwoAtoms(;typeP=:Morse)
Next, I am trying to use the @component macro, but do not understand how to handle structural_parameters. Here is the same code as above, where I have had to comment out the structural parameter typeP in order to make it work:
@component function TwoAtomsNew(; name)
#
# Model parameters
# -- structure parameters
#=
struct_params = @structural_parameters begin
# typeP, :harmonic -- harmonic potential
# typeP, :Morse -- Morse potential
typeP = :harmonic
end
=#
#
# -- parameters
#
# Model parameters
params = @parameters begin
# Constant
mb = 14, [description = "Nitrogen atom mass, atom mass unit u"]
D_e = 945, [description = "Morse parameter D_e, unit u*nm^2/ps^2"]
a = 27, [description = "Morse parameter a, unit 1/nm"]
r_e = 0.1, [description = "Potential equilibrium distance, unit nm"]
k_e = 2a^2*D_e, [description = "Harmonic parameter, u/ps^2"]
r_0 = 1.5r_e, [description = "Initial distance between particles, nm"]
#
end
#
# Model variables, with initial values needed
vars = @variables begin
# Variables
x_1(t)=0, [description = "Particle 1, position unit nm"]
v_1(t)=0, [description = "Particle 1, velocity unit nm/ps"]
x_2(t)=r_0, [description = "Particle 2, position unit nm"]
v_2(t)=0, [description = "Particle 2, velocity unit nm/ps"]
r(t), [description = "Distance between particles, nm"]
P(t), [description = "Potential energy, u*nm^2/ps^2"]
K(t), [description = "Kinetic energy, u*nm^2/ps^2"]
E(t), [description = "Total energy, u*nm^2/ps^2"]
end
#
# Model equations
eqs = [
# Basic algebraics
K ~ mb*(v_1^2 + v_2^2)/2
r ~ abs(x_1 - x_2)
#
# Velocity definitions
Dt(x_1) ~ v_1
Dt(x_2) ~ v_2
# Dynamics based on typeP
#if typeP == :harmonic
Dt(v_1) ~ -k_e*(r - r_e)*sign(x_1-x_2)/mb
Dt(v_2) ~ k_e*(r - r_e)*sign(x_1-x_2)/mb
P ~ k_e/2*(r-r_e)^2
#=elseif typeP == :Morse
Dt(v_1) ~ -2*D_e*a*(1-exp(-a*(r-r_e)))*exp(-a*(r-r_e))*sign(x_1-x_2)/mb
Dt(v_2) ~ 2*D_e*a*(1-exp(-a*(r-r_e)))*exp(-a*(r-r_e))*sign(x_1-x_2)/mb
P ~ D_e*(1 - exp(-a*(r-r_e)))^2
end
E ~ K + P =#
]
#
System(eqs, t, vars, params; name)
end
Instantiating this new version works by doing:
@mtkcompile tp_h = TwoAtomsNew()
Question: How can I extend TwoAtomsNew with the structural parameter?