-
Notifications
You must be signed in to change notification settings - Fork 374
/
thermodynamics_example.py
196 lines (162 loc) · 5.55 KB
/
thermodynamics_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#
# This file is part of CasADi.
#
# CasADi -- A symbolic framework for dynamic optimization.
# Copyright (C) 2010-2014 Joel Andersson, Joris Gillis, Moritz Diehl,
# K.U. Leuven. All rights reserved.
# Copyright (C) 2011-2014 Greg Horn
#
# CasADi is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# CasADi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with CasADi; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#
# -*- coding: utf-8 -*-
import os
import sys
import numpy as NP
from numpy import *
import matplotlib.pyplot as plt
import zipfile
import time
import shutil
try:
# JModelica
from pymodelica import compile_jmu
from pyjmi import JMUModel
import pymodelica
use_precompiled = False
except:
print("No jmodelica installation, falling back to precompiled XML-files")
use_precompiled = True
# CasADi
from casadi import *
# Matplotlib interactive mode
#plt.ion()
# Compile Modelica code to XML
def comp(name):
curr_dir = os.path.dirname(os.path.abspath(__file__))
if use_precompiled:
shutil.copy(curr_dir + '/precompiled_' + name + '.xml', name + '.xml')
else:
jmu_name = compile_jmu(name, curr_dir+"/thermodynamics_example.mo",'modelica','ipopt',{'generate_xml_equations':True, 'generate_fmi_me_xml':False})
modname = name.replace('.','_')
sfile = zipfile.ZipFile(curr_dir+'/'+modname+'.jmu','r')
mfile = sfile.extract('modelDescription.xml','.')
os.remove(modname+'.jmu')
os.rename('modelDescription.xml',modname+'.xml')
# Compile the simplemost example (conservation of mass in control volume)
comp("BasicVolumeMassConservation")
# Read a model from XML
ivp = DaeBuilder()
ivp.parse_fmi('BasicVolumeMassConservation.xml')
# Transform into an explicit ODE
ivp.make_explicit()
# Create an integrator
dae = {'t': ivp.t, 'x': vertcat(*ivp.x), 'p': vertcat(*ivp.p), 'ode': vertcat(*ivp.ode)}
grid = NP.linspace(0,1,100)
F = integrator('F', 'cvodes', dae, {'grid':grid, 'output_t0':True})
# Integrate
x0 = ivp.start(vertcat(*ivp.x))
res = F(x0=x0)
# Output function
output_fcn_out = substitute([ivp("m"),ivp("P")], ivp.d, ivp.ddef)
output_fcn_in = [ivp.t, vertcat(*ivp.x), vertcat(*ivp.z)]
output_fcn = Function("output", output_fcn_in, output_fcn_out)
output_fcn = output_fcn.map(len(grid))
m_out, P_out = output_fcn(grid, res["xf"], res["zf"])
# Plot
plt.figure(1)
plt.subplot(1,2,1)
plt.plot(grid, m_out.T)
plt.xlabel("t")
plt.ylabel("m(t)")
plt.title("c.f. Fritzson figure 15-6 (left)")
plt.subplot(1,2,2)
plt.plot(grid, P_out.T)
plt.xlabel("t")
plt.ylabel("P(t)")
plt.title("c.f. Fritzson figure 15-6 (right)")
plt.draw()
# Compile the next example (conservation of energy in control volume)
comp("BasicVolumeEnergyConservation")
# Allocate a parser and load the xml
ivp = DaeBuilder()
ivp.parse_fmi('BasicVolumeEnergyConservation.xml')
# Transform into an explicit ODE
ivp.make_explicit()
# Create an integrator
dae = {'t': ivp.t, 'x': vertcat(*ivp.x), 'p': vertcat(*ivp.p), 'ode': vertcat(*ivp.ode)}
grid = NP.linspace(0,10,100)
F = integrator('F', 'cvodes', dae, {'grid':grid, 'output_t0':True})
# Integrate
x0 = ivp.start(vertcat(*ivp.x))
res = F(x0=x0)
# Output function
output_fcn_out = substitute([ivp("T")], ivp.d, ivp.ddef)
output_fcn_in = [ivp.t, vertcat(*ivp.x), vertcat(*ivp.z)]
output_fcn = Function("output", output_fcn_in, output_fcn_out)
output_fcn = output_fcn.map(len(grid))
T_out = output_fcn(grid, res["xf"], res["zf"])
# Plot
plt.figure(2)
plt.plot(grid, T_out.T)
plt.xlabel("t")
plt.ylabel("T(t)")
plt.title("c.f. Fritzson figure 15-9")
plt.draw()
# Compile the next example (Heat transfer and work)
comp("BasicVolumeTest")
# Allocate a parser and load the xml
ivp = DaeBuilder()
ivp.parse_fmi('BasicVolumeTest.xml')
# Transform into an explicit ODE
ivp.make_explicit()
# Create an integrator
dae = {'t': ivp.t, 'x': vertcat(*ivp.x), 'p': vertcat(*ivp.p), 'ode': densify(vertcat(*ivp.ode))}
grid = NP.linspace(0,2,100)
F = integrator('F', 'cvodes', dae, {'grid':grid, 'output_t0':True})
# Integrate
x0 = ivp.start(vertcat(*ivp.x))
res = F(x0=x0)
# Output function
output_fcn_out = substitute([ivp("T"),ivp("U"),ivp("V")], ivp.d, ivp.ddef)
output_fcn_in = [ivp.t, vertcat(*ivp.x), vertcat(*ivp.z)]
output_fcn = Function("output", output_fcn_in, output_fcn_out)
output_fcn = output_fcn.map(len(grid))
T_out, U_out, V_out = output_fcn(grid, res["xf"], res["zf"])
# Plot
plt.figure(3)
p1, = plt.plot(grid, T_out.T)
p2, = plt.plot(grid, U_out.T)
plt.xlabel("t")
plt.ylabel("T(t)")
plt.legend([p2, p1], ["T", "U"])
plt.title("c.f. Fritzson figure 15-14")
plt.figure(4)
plt.plot(grid, V_out.T)
plt.xlabel("t")
plt.ylabel("V(t)")
plt.title("Approximation of V")
plt.draw()
# Compile the next example (conservation of energy in control volume)
comp("CtrlFlowSystem")
# Allocate a parser and load the xml
ivp = DaeBuilder()
ivp.parse_fmi('CtrlFlowSystem.xml')
# Transform into a semi-explicit ODE
ivp.make_semi_explicit()
# Print the ivp
print(ivp)
# The problem has no differential states, so instead of integrating, we just solve for mdot...
plt.show()