1 |
#LyX 1.4.1 created this file. For more info see http://www.lyx.org/ |
2 |
\lyxformat 245 |
3 |
\begin_document |
4 |
\begin_header |
5 |
\textclass book |
6 |
\language english |
7 |
\inputencoding auto |
8 |
\fontscheme default |
9 |
\graphics default |
10 |
\paperfontsize default |
11 |
\spacing single |
12 |
\papersize default |
13 |
\use_geometry false |
14 |
\use_amsmath 2 |
15 |
\cite_engine basic |
16 |
\use_bibtopic false |
17 |
\paperorientation portrait |
18 |
\secnumdepth 3 |
19 |
\tocdepth 3 |
20 |
\paragraph_separation indent |
21 |
\defskip medskip |
22 |
\quotes_language english |
23 |
\papercolumns 1 |
24 |
\papersides 1 |
25 |
\paperpagestyle default |
26 |
\tracking_changes false |
27 |
\output_changes true |
28 |
\end_header |
29 |
|
30 |
\begin_body |
31 |
|
32 |
\begin_layout Chapter |
33 |
The modeling of a simple dynamic tank |
34 |
\begin_inset LatexCommand \index{dynamic tank} |
35 |
|
36 |
\end_inset |
37 |
|
38 |
|
39 |
\begin_inset LatexCommand \label{cha:ivp} |
40 |
|
41 |
\end_inset |
42 |
|
43 |
|
44 |
\end_layout |
45 |
|
46 |
\begin_layout Standard |
47 |
This chapter assumes you have read Chapter |
48 |
\color black |
49 |
\InsetSpace ~ |
50 |
|
51 |
\begin_inset LatexCommand \vref{cha:model1} |
52 |
|
53 |
\end_inset |
54 |
|
55 |
and Chapter\InsetSpace ~ |
56 |
|
57 |
\begin_inset LatexCommand \vref{cha:model2} |
58 |
|
59 |
\end_inset |
60 |
|
61 |
, which introduce you to ASCEND modeling concepts. |
62 |
\end_layout |
63 |
|
64 |
\begin_layout Standard |
65 |
The purpose of this chapter is to be a good first step along the path to |
66 |
learning how to use ASCEND for dynamic simulations |
67 |
\begin_inset LatexCommand \index{dynamic simulation} |
68 |
|
69 |
\end_inset |
70 |
|
71 |
. |
72 |
We shall lead you through the steps for creating a simple model. |
73 |
You will also learn the standard methods that we employ for our dynamic |
74 |
libraries |
75 |
\begin_inset LatexCommand \index{dynamic libraries} |
76 |
|
77 |
\end_inset |
78 |
|
79 |
. |
80 |
We will present our reasons for the steps we take. |
81 |
|
82 |
\end_layout |
83 |
|
84 |
\begin_layout Subsubsection* |
85 |
The problem |
86 |
\end_layout |
87 |
|
88 |
\begin_layout List |
89 |
\labelwidthstring 00.00.0000 |
90 |
Step\InsetSpace ~ |
91 |
1: We would like to create a dynamic model of a simple tank. |
92 |
\end_layout |
93 |
|
94 |
\begin_layout Subsubsection* |
95 |
Topics covered in this chapter are: |
96 |
\end_layout |
97 |
|
98 |
\begin_layout Itemize |
99 |
Converting the word description to an ASCEND model. |
100 |
\end_layout |
101 |
|
102 |
\begin_layout Itemize |
103 |
Solving the model. |
104 |
\end_layout |
105 |
|
106 |
\begin_layout Itemize |
107 |
Creating a script to load and execute an instance of the model. |
108 |
\end_layout |
109 |
|
110 |
\begin_layout Itemize |
111 |
Integrating |
112 |
\begin_inset LatexCommand \index{integrating} |
113 |
|
114 |
\end_inset |
115 |
|
116 |
the model. |
117 |
\end_layout |
118 |
|
119 |
\begin_layout Itemize |
120 |
View Integration Results. |
121 |
\end_layout |
122 |
|
123 |
\begin_layout Section |
124 |
Converting the word description |
125 |
\newline |
126 |
into an ASCEND model |
127 |
\end_layout |
128 |
|
129 |
\begin_layout Standard |
130 |
As stated in Section\InsetSpace ~ |
131 |
|
132 |
\begin_inset LatexCommand \vref{sec:model1.converting-the-word} |
133 |
|
134 |
\end_inset |
135 |
|
136 |
|
137 |
\color none |
138 |
, we need to make an instance of a type and solve the instance. |
139 |
So we shall start by creating a tank type definition. |
140 |
We will have to create our type definition as a text file using a text |
141 |
editor. |
142 |
(Possible text editors |
143 |
\begin_inset LatexCommand \index{text editor} |
144 |
|
145 |
\end_inset |
146 |
|
147 |
are Word |
148 |
\begin_inset LatexCommand \index{Word} |
149 |
|
150 |
\end_inset |
151 |
|
152 |
, Emacs |
153 |
\begin_inset LatexCommand \index{Emacs} |
154 |
|
155 |
\end_inset |
156 |
|
157 |
, Notepad |
158 |
\begin_inset LatexCommand \index{Notepad} |
159 |
|
160 |
\end_inset |
161 |
|
162 |
, pico |
163 |
\begin_inset LatexCommand \index{pico} |
164 |
|
165 |
\end_inset |
166 |
|
167 |
, vi |
168 |
\begin_inset LatexCommand \index{vi} |
169 |
|
170 |
\end_inset |
171 |
|
172 |
, etc. |
173 |
We shall discuss editors shortly.) |
174 |
\end_layout |
175 |
|
176 |
\begin_layout Standard |
177 |
We need first to decide the parts to our model. |
178 |
In this case we know that we need the variables listed in Table\InsetSpace ~ |
179 |
|
180 |
\begin_inset LatexCommand \eqref{cap:Variables-required-for} |
181 |
|
182 |
\end_inset |
183 |
|
184 |
We readily fill in the first three columns in this table, and we can also |
185 |
fill out the fourth column if we know the units that are associated with |
186 |
each of the parts. |
187 |
To find the ASCEND variable type needed for the fourth column use the find |
188 |
menu on the library window and select ATOM by units |
189 |
\begin_inset LatexCommand \index{atom, select by units} |
190 |
|
191 |
\end_inset |
192 |
|
193 |
. |
194 |
The result of this search will be all the ASCEND variable type that have |
195 |
the units you entered. |
196 |
\end_layout |
197 |
|
198 |
\begin_layout Standard |
199 |
\begin_inset Float table |
200 |
wide false |
201 |
sideways false |
202 |
status open |
203 |
|
204 |
\begin_layout Caption |
205 |
\begin_inset LatexCommand \label{cap:Variables-required-for} |
206 |
|
207 |
\end_inset |
208 |
|
209 |
Variables required for model |
210 |
\end_layout |
211 |
|
212 |
\begin_layout Standard |
213 |
\begin_inset Tabular |
214 |
<lyxtabular version="3" rows="8" columns="4"> |
215 |
<features> |
216 |
<column alignment="center" valignment="top" leftline="true" width="0"> |
217 |
<column alignment="center" valignment="top" leftline="true" width="1.5in"> |
218 |
<column alignment="center" valignment="top" leftline="true" width="1.5in"> |
219 |
<column alignment="center" valignment="top" leftline="true" rightline="true" width="1in"> |
220 |
<row topline="true" bottomline="true"> |
221 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
222 |
\begin_inset Text |
223 |
|
224 |
\begin_layout Standard |
225 |
Symbol |
226 |
\end_layout |
227 |
|
228 |
\end_inset |
229 |
</cell> |
230 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
231 |
\begin_inset Text |
232 |
|
233 |
\begin_layout Standard |
234 |
Meaning |
235 |
\end_layout |
236 |
|
237 |
\end_inset |
238 |
</cell> |
239 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
240 |
\begin_inset Text |
241 |
|
242 |
\begin_layout Standard |
243 |
Typical Units |
244 |
\end_layout |
245 |
|
246 |
\end_inset |
247 |
</cell> |
248 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
249 |
\begin_inset Text |
250 |
|
251 |
\begin_layout Standard |
252 |
ASCEND variable type |
253 |
\end_layout |
254 |
|
255 |
\end_inset |
256 |
</cell> |
257 |
</row> |
258 |
<row topline="true"> |
259 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
260 |
\begin_inset Text |
261 |
|
262 |
\begin_layout Standard |
263 |
|
264 |
\family typewriter |
265 |
M |
266 |
\end_layout |
267 |
|
268 |
\end_inset |
269 |
</cell> |
270 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
271 |
\begin_inset Text |
272 |
|
273 |
\begin_layout Standard |
274 |
Moles in Tank |
275 |
\end_layout |
276 |
|
277 |
\end_inset |
278 |
</cell> |
279 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
280 |
\begin_inset Text |
281 |
|
282 |
\begin_layout Standard |
283 |
mol, kmol |
284 |
\end_layout |
285 |
|
286 |
\end_inset |
287 |
</cell> |
288 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
289 |
\begin_inset Text |
290 |
|
291 |
\begin_layout Standard |
292 |
mole |
293 |
\end_layout |
294 |
|
295 |
\end_inset |
296 |
</cell> |
297 |
</row> |
298 |
<row topline="true"> |
299 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
300 |
\begin_inset Text |
301 |
|
302 |
\begin_layout Standard |
303 |
|
304 |
\family typewriter |
305 |
dM_dt |
306 |
\end_layout |
307 |
|
308 |
\end_inset |
309 |
</cell> |
310 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
311 |
\begin_inset Text |
312 |
|
313 |
\begin_layout Standard |
314 |
Rate of change of Moles in tank (derivative) |
315 |
\end_layout |
316 |
|
317 |
\end_inset |
318 |
</cell> |
319 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
320 |
\begin_inset Text |
321 |
|
322 |
\begin_layout Standard |
323 |
mol/sec, kmol/sec |
324 |
\end_layout |
325 |
|
326 |
\end_inset |
327 |
</cell> |
328 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
329 |
\begin_inset Text |
330 |
|
331 |
\begin_layout Standard |
332 |
molar_rate |
333 |
\end_layout |
334 |
|
335 |
\end_inset |
336 |
</cell> |
337 |
</row> |
338 |
<row topline="true"> |
339 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
340 |
\begin_inset Text |
341 |
|
342 |
\begin_layout Standard |
343 |
|
344 |
\family typewriter |
345 |
input |
346 |
\end_layout |
347 |
|
348 |
\end_inset |
349 |
</cell> |
350 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
351 |
\begin_inset Text |
352 |
|
353 |
\begin_layout Standard |
354 |
Feed flow rate |
355 |
\end_layout |
356 |
|
357 |
\end_inset |
358 |
</cell> |
359 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
360 |
\begin_inset Text |
361 |
|
362 |
\begin_layout Standard |
363 |
mol/sec, kmol/sec |
364 |
\end_layout |
365 |
|
366 |
\end_inset |
367 |
</cell> |
368 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
369 |
\begin_inset Text |
370 |
|
371 |
\begin_layout Standard |
372 |
molar_rate |
373 |
\end_layout |
374 |
|
375 |
\end_inset |
376 |
</cell> |
377 |
</row> |
378 |
<row topline="true"> |
379 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
380 |
\begin_inset Text |
381 |
|
382 |
\begin_layout Standard |
383 |
|
384 |
\family typewriter |
385 |
output |
386 |
\end_layout |
387 |
|
388 |
\end_inset |
389 |
</cell> |
390 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
391 |
\begin_inset Text |
392 |
|
393 |
\begin_layout Standard |
394 |
Output flow rate |
395 |
\end_layout |
396 |
|
397 |
\end_inset |
398 |
</cell> |
399 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
400 |
\begin_inset Text |
401 |
|
402 |
\begin_layout Standard |
403 |
mol/sec, kmol/sec |
404 |
\end_layout |
405 |
|
406 |
\end_inset |
407 |
</cell> |
408 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
409 |
\begin_inset Text |
410 |
|
411 |
\begin_layout Standard |
412 |
molar_rate |
413 |
\end_layout |
414 |
|
415 |
\end_inset |
416 |
</cell> |
417 |
</row> |
418 |
<row topline="true"> |
419 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
420 |
\begin_inset Text |
421 |
|
422 |
\begin_layout Standard |
423 |
|
424 |
\family typewriter |
425 |
Volume |
426 |
\end_layout |
427 |
|
428 |
\end_inset |
429 |
</cell> |
430 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
431 |
\begin_inset Text |
432 |
|
433 |
\begin_layout Standard |
434 |
Volume of liquid in the tank |
435 |
\end_layout |
436 |
|
437 |
\end_inset |
438 |
</cell> |
439 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
440 |
\begin_inset Text |
441 |
|
442 |
\begin_layout Standard |
443 |
m^3,ft^3 |
444 |
\end_layout |
445 |
|
446 |
\end_inset |
447 |
</cell> |
448 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
449 |
\begin_inset Text |
450 |
|
451 |
\begin_layout Standard |
452 |
volume |
453 |
\end_layout |
454 |
|
455 |
\end_inset |
456 |
</cell> |
457 |
</row> |
458 |
<row topline="true"> |
459 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
460 |
\begin_inset Text |
461 |
|
462 |
\begin_layout Standard |
463 |
|
464 |
\family typewriter |
465 |
density |
466 |
\end_layout |
467 |
|
468 |
\end_inset |
469 |
</cell> |
470 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
471 |
\begin_inset Text |
472 |
|
473 |
\begin_layout Standard |
474 |
Molar density of tank fluid |
475 |
\end_layout |
476 |
|
477 |
\end_inset |
478 |
</cell> |
479 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
480 |
\begin_inset Text |
481 |
|
482 |
\begin_layout Standard |
483 |
mol/m^3,mol/ft^3 |
484 |
\end_layout |
485 |
|
486 |
\end_inset |
487 |
</cell> |
488 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
489 |
\begin_inset Text |
490 |
|
491 |
\begin_layout Standard |
492 |
molar_density |
493 |
\end_layout |
494 |
|
495 |
\end_inset |
496 |
</cell> |
497 |
</row> |
498 |
<row topline="true" bottomline="true"> |
499 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
500 |
\begin_inset Text |
501 |
|
502 |
\begin_layout Standard |
503 |
|
504 |
\family typewriter |
505 |
dynamic |
506 |
\end_layout |
507 |
|
508 |
\end_inset |
509 |
</cell> |
510 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
511 |
\begin_inset Text |
512 |
|
513 |
\begin_layout Standard |
514 |
Boolean for switching between dynamic and steady state simulations |
515 |
\end_layout |
516 |
|
517 |
\end_inset |
518 |
</cell> |
519 |
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none"> |
520 |
\begin_inset Text |
521 |
|
522 |
\begin_layout Standard |
523 |
N/A |
524 |
\end_layout |
525 |
|
526 |
\end_inset |
527 |
</cell> |
528 |
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none"> |
529 |
\begin_inset Text |
530 |
|
531 |
\begin_layout Standard |
532 |
boolean |
533 |
\end_layout |
534 |
|
535 |
\end_inset |
536 |
</cell> |
537 |
</row> |
538 |
</lyxtabular> |
539 |
|
540 |
\end_inset |
541 |
|
542 |
|
543 |
\end_layout |
544 |
|
545 |
\end_inset |
546 |
|
547 |
|
548 |
\end_layout |
549 |
|
550 |
\begin_layout Standard |
551 |
We would like to be able to compute the number of moles in the tank for |
552 |
a given volume assuming steady state |
553 |
\begin_inset LatexCommand \index{steady state} |
554 |
|
555 |
\end_inset |
556 |
|
557 |
(dM_dt = 0). |
558 |
We would also like to be able to calculate how the volume changes if we |
559 |
are not at steady state. |
560 |
The following equations describe the simple tank system. |
561 |
\end_layout |
562 |
|
563 |
\begin_layout Standard |
564 |
The first equation is the differential equation that relates the input and |
565 |
output flows to the accumulation in the tank. |
566 |
The second equation is the relation of the moles in the tank to the volume |
567 |
of liquid and should be rearranged to avoid division |
568 |
\begin_inset LatexCommand \index{division, avoid} |
569 |
|
570 |
\end_inset |
571 |
|
572 |
. |
573 |
These equations are all that is need for a simple tank. |
574 |
\end_layout |
575 |
|
576 |
\begin_layout Standard |
577 |
\begin_inset Formula \begin{equation} |
578 |
dM\_dt=input-output\label{eq:ivp.dMdt}\end{equation} |
579 |
|
580 |
\end_inset |
581 |
|
582 |
|
583 |
\end_layout |
584 |
|
585 |
\begin_layout Standard |
586 |
\begin_inset Formula \begin{equation} |
587 |
Volume=\frac{M}{density}\label{eq:ivp.Volume}\end{equation} |
588 |
|
589 |
\end_inset |
590 |
|
591 |
|
592 |
\end_layout |
593 |
|
594 |
\begin_layout Subsubsection* |
595 |
The first version of the code for tank |
596 |
\end_layout |
597 |
|
598 |
\begin_layout LyX-Code |
599 |
REQUIRE "ivpsystem.a4l"; |
600 |
\end_layout |
601 |
|
602 |
\begin_layout LyX-Code |
603 |
REQUIRE "atoms.a4l"; |
604 |
\end_layout |
605 |
|
606 |
\begin_layout LyX-Code |
607 |
MODEL tank |
608 |
\begin_inset LatexCommand \index{tank, dynamic MODEL} |
609 |
|
610 |
\end_inset |
611 |
|
612 |
; |
613 |
\end_layout |
614 |
|
615 |
\begin_layout LyX-Code |
616 |
(* List of Variables *) |
617 |
\end_layout |
618 |
|
619 |
\begin_layout LyX-Code |
620 |
dM_dt IS_A molar_rate; |
621 |
\end_layout |
622 |
|
623 |
\begin_layout LyX-Code |
624 |
M IS_A mole; |
625 |
\end_layout |
626 |
|
627 |
\begin_layout LyX-Code |
628 |
input IS_A molar_rate; |
629 |
\end_layout |
630 |
|
631 |
\begin_layout LyX-Code |
632 |
output IS_A molar_rate; |
633 |
\end_layout |
634 |
|
635 |
\begin_layout LyX-Code |
636 |
Volume IS_A volume; |
637 |
\end_layout |
638 |
|
639 |
\begin_layout LyX-Code |
640 |
density IS_A real_constant; |
641 |
\end_layout |
642 |
|
643 |
\begin_layout LyX-Code |
644 |
dynamic IS_A boolean; |
645 |
\end_layout |
646 |
|
647 |
\begin_layout LyX-Code |
648 |
t IS_A time; |
649 |
\end_layout |
650 |
|
651 |
\begin_layout LyX-Code |
652 |
(* Equations *) |
653 |
\end_layout |
654 |
|
655 |
\begin_layout LyX-Code |
656 |
dM_dt = input - output; |
657 |
\end_layout |
658 |
|
659 |
\begin_layout LyX-Code |
660 |
M = Volume * density; |
661 |
\end_layout |
662 |
|
663 |
\begin_layout LyX-Code |
664 |
(* Assignment of values to Constants *) |
665 |
\end_layout |
666 |
|
667 |
\begin_layout LyX-Code |
668 |
density :==10 {mol/m^3}; |
669 |
\end_layout |
670 |
|
671 |
\begin_layout LyX-Code |
672 |
|
673 |
\end_layout |
674 |
|
675 |
\begin_layout LyX-Code |
676 |
METHODS |
677 |
\end_layout |
678 |
|
679 |
\begin_layout LyX-Code |
680 |
|
681 |
\end_layout |
682 |
|
683 |
\begin_layout LyX-Code |
684 |
METHOD check_self; |
685 |
\end_layout |
686 |
|
687 |
\begin_layout LyX-Code |
688 |
IF (input < 1e-4 {mole/s}) THEN |
689 |
\end_layout |
690 |
|
691 |
\begin_layout LyX-Code |
692 |
STOP {Input dried up in tank}; |
693 |
\end_layout |
694 |
|
695 |
\begin_layout LyX-Code |
696 |
END IF; |
697 |
\end_layout |
698 |
|
699 |
\begin_layout LyX-Code |
700 |
IF (output < 1e-4 {mole/s}) THEN |
701 |
\end_layout |
702 |
|
703 |
\begin_layout LyX-Code |
704 |
STOP {Output dried up in tank}; |
705 |
\end_layout |
706 |
|
707 |
\begin_layout LyX-Code |
708 |
END IF; |
709 |
\end_layout |
710 |
|
711 |
\begin_layout LyX-Code |
712 |
END check_self; |
713 |
\end_layout |
714 |
|
715 |
\begin_layout LyX-Code |
716 |
METHOD check_all; |
717 |
\end_layout |
718 |
|
719 |
\begin_layout LyX-Code |
720 |
RUN check_self; |
721 |
\end_layout |
722 |
|
723 |
\begin_layout LyX-Code |
724 |
END check_all; |
725 |
\end_layout |
726 |
|
727 |
\begin_layout LyX-Code |
728 |
|
729 |
\end_layout |
730 |
|
731 |
\begin_layout LyX-Code |
732 |
METHOD default_self; |
733 |
\end_layout |
734 |
|
735 |
\begin_layout LyX-Code |
736 |
dynamic := FALSE; |
737 |
\end_layout |
738 |
|
739 |
\begin_layout LyX-Code |
740 |
t :=0 {sec}; |
741 |
\end_layout |
742 |
|
743 |
\begin_layout LyX-Code |
744 |
dM_dt :=0 {mol/sec}; |
745 |
\end_layout |
746 |
|
747 |
\begin_layout LyX-Code |
748 |
dM_dt.lower_bound := -1e49 {mol/sec}; |
749 |
\end_layout |
750 |
|
751 |
\begin_layout LyX-Code |
752 |
END default_self; |
753 |
\end_layout |
754 |
|
755 |
\begin_layout LyX-Code |
756 |
|
757 |
\end_layout |
758 |
|
759 |
\begin_layout LyX-Code |
760 |
METHOD default_all; |
761 |
\end_layout |
762 |
|
763 |
\begin_layout LyX-Code |
764 |
RUN default_self; |
765 |
\end_layout |
766 |
|
767 |
\begin_layout LyX-Code |
768 |
END default_all; |
769 |
\end_layout |
770 |
|
771 |
\begin_layout LyX-Code |
772 |
|
773 |
\end_layout |
774 |
|
775 |
\begin_layout LyX-Code |
776 |
METHOD bound_self; |
777 |
\end_layout |
778 |
|
779 |
\begin_layout LyX-Code |
780 |
END bound_self; |
781 |
\end_layout |
782 |
|
783 |
\begin_layout LyX-Code |
784 |
|
785 |
\end_layout |
786 |
|
787 |
\begin_layout LyX-Code |
788 |
METHOD bound_all; |
789 |
\end_layout |
790 |
|
791 |
\begin_layout LyX-Code |
792 |
RUN bound_self; |
793 |
\end_layout |
794 |
|
795 |
\begin_layout LyX-Code |
796 |
END bound_all; |
797 |
\end_layout |
798 |
|
799 |
\begin_layout LyX-Code |
800 |
|
801 |
\end_layout |
802 |
|
803 |
\begin_layout LyX-Code |
804 |
METHOD scale_self; |
805 |
\end_layout |
806 |
|
807 |
\begin_layout LyX-Code |
808 |
END scale_self; |
809 |
\end_layout |
810 |
|
811 |
\begin_layout LyX-Code |
812 |
|
813 |
\end_layout |
814 |
|
815 |
\begin_layout LyX-Code |
816 |
METHOD scale_all; |
817 |
\end_layout |
818 |
|
819 |
\begin_layout LyX-Code |
820 |
RUN scale_self; |
821 |
\end_layout |
822 |
|
823 |
\begin_layout LyX-Code |
824 |
END scale_all; |
825 |
\end_layout |
826 |
|
827 |
\begin_layout LyX-Code |
828 |
|
829 |
\end_layout |
830 |
|
831 |
\begin_layout LyX-Code |
832 |
METHOD seqmod; |
833 |
\end_layout |
834 |
|
835 |
\begin_layout LyX-Code |
836 |
dM_dt.fixed :=TRUE; |
837 |
\end_layout |
838 |
|
839 |
\begin_layout LyX-Code |
840 |
M.fixed :=FALSE; |
841 |
\end_layout |
842 |
|
843 |
\begin_layout LyX-Code |
844 |
Volume.fixed :=TRUE; |
845 |
\end_layout |
846 |
|
847 |
\begin_layout LyX-Code |
848 |
input.fixed :=TRUE; |
849 |
\end_layout |
850 |
|
851 |
\begin_layout LyX-Code |
852 |
output.fixed :=FALSE; |
853 |
\end_layout |
854 |
|
855 |
\begin_layout LyX-Code |
856 |
IF dynamic THEN |
857 |
\end_layout |
858 |
|
859 |
\begin_layout LyX-Code |
860 |
dM_dt.fixed :=FALSE; |
861 |
\end_layout |
862 |
|
863 |
\begin_layout LyX-Code |
864 |
M.fixed :=TRUE; |
865 |
\end_layout |
866 |
|
867 |
\begin_layout LyX-Code |
868 |
Volume.fixed :=FALSE; |
869 |
\end_layout |
870 |
|
871 |
\begin_layout LyX-Code |
872 |
output.fixed :=TRUE; |
873 |
\end_layout |
874 |
|
875 |
\begin_layout LyX-Code |
876 |
END IF; |
877 |
\end_layout |
878 |
|
879 |
\begin_layout LyX-Code |
880 |
END seqmod; |
881 |
\end_layout |
882 |
|
883 |
\begin_layout LyX-Code |
884 |
|
885 |
\end_layout |
886 |
|
887 |
\begin_layout LyX-Code |
888 |
METHOD specify; |
889 |
\end_layout |
890 |
|
891 |
\begin_layout LyX-Code |
892 |
input.fixed :=TRUE; |
893 |
\end_layout |
894 |
|
895 |
\begin_layout LyX-Code |
896 |
RUN seqmod; |
897 |
\end_layout |
898 |
|
899 |
\begin_layout LyX-Code |
900 |
END specify; |
901 |
\end_layout |
902 |
|
903 |
\begin_layout LyX-Code |
904 |
|
905 |
\end_layout |
906 |
|
907 |
\begin_layout LyX-Code |
908 |
METHOD set_ode; |
909 |
\end_layout |
910 |
|
911 |
\begin_layout LyX-Code |
912 |
(* set ODE_TYPE -1=independent variable, |
913 |
\end_layout |
914 |
|
915 |
\begin_layout LyX-Code |
916 |
0=algebraic variable, 1=state variable, |
917 |
\end_layout |
918 |
|
919 |
\begin_layout LyX-Code |
920 |
2=derivative *) |
921 |
\end_layout |
922 |
|
923 |
\begin_layout LyX-Code |
924 |
t.ode_type :=-1; |
925 |
\end_layout |
926 |
|
927 |
\begin_layout LyX-Code |
928 |
dM_dt.ode_type :=2; |
929 |
\end_layout |
930 |
|
931 |
\begin_layout LyX-Code |
932 |
M.ode_type :=1; |
933 |
\end_layout |
934 |
|
935 |
\begin_layout LyX-Code |
936 |
(* Set ODE_ID *) |
937 |
\end_layout |
938 |
|
939 |
\begin_layout LyX-Code |
940 |
dM_dt.ode_id :=1; |
941 |
\end_layout |
942 |
|
943 |
\begin_layout LyX-Code |
944 |
M.ode_id :=1; |
945 |
\end_layout |
946 |
|
947 |
\begin_layout LyX-Code |
948 |
END set_ode; |
949 |
\end_layout |
950 |
|
951 |
\begin_layout LyX-Code |
952 |
|
953 |
\end_layout |
954 |
|
955 |
\begin_layout LyX-Code |
956 |
METHOD set_obs; |
957 |
\end_layout |
958 |
|
959 |
\begin_layout LyX-Code |
960 |
(* Set OBS_ID to any integer value greater |
961 |
\end_layout |
962 |
|
963 |
\begin_layout LyX-Code |
964 |
than 0, the variable will be recorded |
965 |
\end_layout |
966 |
|
967 |
\begin_layout LyX-Code |
968 |
(i.e., observed) *) |
969 |
\end_layout |
970 |
|
971 |
\begin_layout LyX-Code |
972 |
M.obs_id :=1; |
973 |
\end_layout |
974 |
|
975 |
\begin_layout LyX-Code |
976 |
Volume.obs_id :=2; |
977 |
\end_layout |
978 |
|
979 |
\begin_layout LyX-Code |
980 |
input.obs_id :=3; |
981 |
\end_layout |
982 |
|
983 |
\begin_layout LyX-Code |
984 |
output.obs_id :=4; |
985 |
\end_layout |
986 |
|
987 |
\begin_layout LyX-Code |
988 |
END set_obs; |
989 |
\end_layout |
990 |
|
991 |
\begin_layout LyX-Code |
992 |
|
993 |
\end_layout |
994 |
|
995 |
\begin_layout LyX-Code |
996 |
METHOD values; |
997 |
\end_layout |
998 |
|
999 |
\begin_layout LyX-Code |
1000 |
Volume :=5 {m^3}; |
1001 |
\end_layout |
1002 |
|
1003 |
\begin_layout LyX-Code |
1004 |
input :=100 {mole/s}; |
1005 |
\end_layout |
1006 |
|
1007 |
\begin_layout LyX-Code |
1008 |
END values; |
1009 |
\end_layout |
1010 |
|
1011 |
\begin_layout LyX-Code |
1012 |
END tank; |
1013 |
\end_layout |
1014 |
|
1015 |
\begin_layout Standard |
1016 |
Our model definition has the following structure for it so far: |
1017 |
\end_layout |
1018 |
|
1019 |
\begin_layout Itemize |
1020 |
MODEL statement |
1021 |
\end_layout |
1022 |
|
1023 |
\begin_layout Itemize |
1024 |
list of variables we intend to use in the type definition |
1025 |
\end_layout |
1026 |
|
1027 |
\begin_layout Itemize |
1028 |
equations |
1029 |
\end_layout |
1030 |
|
1031 |
\begin_layout Itemize |
1032 |
METHODS |
1033 |
\end_layout |
1034 |
|
1035 |
\begin_layout Itemize |
1036 |
END statement |
1037 |
\end_layout |
1038 |
|
1039 |
\begin_layout Standard |
1040 |
While we have put the statements in this order, we could mix them up and |
1041 |
intermix the middle two types of statements, even going to the extreme |
1042 |
of defining the variables after we first use them. |
1043 |
Once the |
1044 |
\family typewriter |
1045 |
METHODS |
1046 |
\family default |
1047 |
section is started no new equations or variables can be declared. |
1048 |
The |
1049 |
\family typewriter |
1050 |
MODEL |
1051 |
\family default |
1052 |
and |
1053 |
\family typewriter |
1054 |
END |
1055 |
\family default |
1056 |
statements begin and end the type definition. |
1057 |
\end_layout |
1058 |
|
1059 |
\begin_layout Standard |
1060 |
There are two new methods added to a dynamic model that you would not see |
1061 |
in a steady state model, and they are the |
1062 |
\family typewriter |
1063 |
set_ode |
1064 |
\family default |
1065 |
and |
1066 |
\family typewriter |
1067 |
set_obs |
1068 |
\family default |
1069 |
methods. |
1070 |
The |
1071 |
\family typewriter |
1072 |
set_ode |
1073 |
\family default |
1074 |
method is used to setup the model for integration. |
1075 |
The |
1076 |
\family typewriter |
1077 |
set_obs |
1078 |
\family default |
1079 |
method is used to tell ASCEND which variables you would like to observe |
1080 |
in the output of the integration. |
1081 |
\end_layout |
1082 |
|
1083 |
\begin_layout Standard |
1084 |
Now we need to discuss the how and why of the two new methods. |
1085 |
The |
1086 |
\family typewriter |
1087 |
set_ode |
1088 |
\family default |
1089 |
method is used to set up the equations and variables described in the model |
1090 |
for integration by LSODE |
1091 |
\begin_inset LatexCommand \index{LSODE} |
1092 |
|
1093 |
\end_inset |
1094 |
|
1095 |
. |
1096 |
In order for LSODE to be able to integrate the model, it needs to know |
1097 |
which variable is the independent variable - in this case |
1098 |
\family typewriter |
1099 |
t |
1100 |
\family default |
1101 |
(time), which variables are the derivatives, and which are the states. |
1102 |
The way we do this is we have to add a few extra attributes to each variable. |
1103 |
In Section\InsetSpace ~ |
1104 |
|
1105 |
\begin_inset LatexCommand \ref{sec:model1.converting-the-word} |
1106 |
|
1107 |
\end_inset |
1108 |
|
1109 |
|
1110 |
\color none |
1111 |
, the idea of an atom was discussed with its units, default value, bounds |
1112 |
etc. |
1113 |
We need to add 5 more of this type of parameter. |
1114 |
These attributes |
1115 |
\begin_inset LatexCommand \index{attributes, ODE variables} |
1116 |
|
1117 |
\end_inset |
1118 |
|
1119 |
are |
1120 |
\family typewriter |
1121 |
\color black |
1122 |
ode_type |
1123 |
\family default |
1124 |
\color none |
1125 |
, |
1126 |
\family typewriter |
1127 |
ode_id |
1128 |
\family default |
1129 |
, |
1130 |
\family typewriter |
1131 |
obs_id |
1132 |
\family default |
1133 |
, |
1134 |
\family typewriter |
1135 |
ode_rtol |
1136 |
\family default |
1137 |
and |
1138 |
\family typewriter |
1139 |
ode_atol |
1140 |
\family default |
1141 |
. |
1142 |
|
1143 |
\end_layout |
1144 |
|
1145 |
\begin_layout Standard |
1146 |
This now brings us to the reason there is a |
1147 |
\family typewriter |
1148 |
system.a4l |
1149 |
\family default |
1150 |
and an |
1151 |
\family typewriter |
1152 |
ivpsystem.a4l |
1153 |
\family default |
1154 |
|
1155 |
\begin_inset LatexCommand \index{ivpsystem.a4l} |
1156 |
|
1157 |
\end_inset |
1158 |
|
1159 |
. |
1160 |
For a steady state model the new attributes discussed above are not needed, |
1161 |
and would take up memory and introduce confusion; therefore, they are excluded |
1162 |
for the system library. |
1163 |
If a dynamic simulations is to be loaded and solved, the ivpsystem library |
1164 |
needs to be loaded instead of the system library so the extra attributes |
1165 |
will be present with each part. |
1166 |
\end_layout |
1167 |
|
1168 |
\begin_layout Standard |
1169 |
We will now go through the purpose of each of these attributes. |
1170 |
First |
1171 |
\family typewriter |
1172 |
ode_type |
1173 |
\family default |
1174 |
is to tell the system what type of variable it is. |
1175 |
A value of -1 for |
1176 |
\family typewriter |
1177 |
ode_type |
1178 |
\family default |
1179 |
means the variable is the independent variable, 0 means it is an algebraic |
1180 |
variable (default), 1 means it is a state variable, and finally 2 means |
1181 |
it is a derivative. |
1182 |
|
1183 |
\end_layout |
1184 |
|
1185 |
\begin_layout Standard |
1186 |
The attribute |
1187 |
\family typewriter |
1188 |
ode_id |
1189 |
\family default |
1190 |
is used to match the state variables with their derivatives and only needs |
1191 |
to be used if the variable is a state or derivative. |
1192 |
In the example |
1193 |
\family typewriter |
1194 |
M |
1195 |
\family default |
1196 |
is a state and |
1197 |
\family typewriter |
1198 |
dM_dt |
1199 |
\family default |
1200 |
is the derivative. |
1201 |
Therefore they both need to have the same |
1202 |
\family typewriter |
1203 |
ode_id |
1204 |
\family default |
1205 |
so ASCEND will know that they belong together. |
1206 |
Each state and derivative pair needs to have a different |
1207 |
\family typewriter |
1208 |
ode_id |
1209 |
\family default |
1210 |
; however, it does not matter what the number is as long as it is a positive |
1211 |
integer and no other state and derivative pair has the same number. |
1212 |
|
1213 |
\end_layout |
1214 |
|
1215 |
\begin_layout Standard |
1216 |
Next |
1217 |
\family typewriter |
1218 |
obs_id |
1219 |
\family default |
1220 |
is used by the user to flag a variable for observation while integrating. |
1221 |
For any integer value of |
1222 |
\family typewriter |
1223 |
obs_id |
1224 |
\family default |
1225 |
greater then 0 the variable will be observed. |
1226 |
The result of flagging a variable for observation is that its values will |
1227 |
be in a data column in one of two output files. |
1228 |
One of the files of data produced with each integration contains the values |
1229 |
of the states and the second the values of the variables flagged for observatio |
1230 |
n. |
1231 |
The default file names are |
1232 |
\family typewriter |
1233 |
y.dat |
1234 |
\family default |
1235 |
and |
1236 |
\family typewriter |
1237 |
obs.dat |
1238 |
\family default |
1239 |
respectfully; however, they can be changed in the solver options |
1240 |
\begin_inset LatexCommand \index{options, solver} |
1241 |
|
1242 |
\end_inset |
1243 |
|
1244 |
general menu. |
1245 |
|
1246 |
\end_layout |
1247 |
|
1248 |
\begin_layout Standard |
1249 |
Last, but not least, are the error control attributes for LSODE: |
1250 |
\family typewriter |
1251 |
ode_rtol |
1252 |
\family default |
1253 |
and |
1254 |
\family typewriter |
1255 |
ode_atol |
1256 |
\family default |
1257 |
. |
1258 |
Both of these come directly from the LSODE attributes rtol and atol which |
1259 |
are the local relative and absolute error tolerances for the variable respectiv |
1260 |
ely. |
1261 |
\end_layout |
1262 |
|
1263 |
\begin_layout Standard |
1264 |
There is one other thing about methods that we need to discuss before moving |
1265 |
on and that is the |
1266 |
\family typewriter |
1267 |
seqmod |
1268 |
\family default |
1269 |
method. |
1270 |
If you have not already noticed, it is a little different from the other |
1271 |
examples as it has an IF statement in it. |
1272 |
This is an important part of the dynamic simulation. |
1273 |
It switches the degrees of freedom depending on if we are computing an |
1274 |
initial condition or performing an integration step. |
1275 |
We use the boolean |
1276 |
\family typewriter |
1277 |
dynamic |
1278 |
\family default |
1279 |
|
1280 |
\begin_inset LatexCommand \index{dynamic, boolean variable} |
1281 |
|
1282 |
\end_inset |
1283 |
|
1284 |
to control whether we are going to solve the model as a steady state model |
1285 |
( |
1286 |
\family typewriter |
1287 |
dynamic := FALSE; |
1288 |
\family default |
1289 |
) or as a dynamic model ( |
1290 |
\family typewriter |
1291 |
dynamic := TRUE; |
1292 |
\family default |
1293 |
). |
1294 |
For the current example, we have a simple tank and, for steady state, we |
1295 |
would like to calculate the number of moles and output flow rate for a |
1296 |
fixed tank volume and input flow rate. |
1297 |
Also, for the model to be at steady state, we have to fix the derivative |
1298 |
and set it equal to zero, |
1299 |
\family typewriter |
1300 |
(dM_dt.fixed :=TRUE; dM_dt :=0 {mole/s};. |
1301 |
|
1302 |
\family default |
1303 |
The derivative is normally set to zero in the |
1304 |
\family typewriter |
1305 |
default_self |
1306 |
\family default |
1307 |
method to prepare the model to solve for initial steady-state conditions.) |
1308 |
If we then want to integrate this model for a fixed output flow (as when |
1309 |
pumping the liquid out under flow control), we would free up the volume |
1310 |
and fix the output flow rate. |
1311 |
The model will then compute how the liquid volume will change with time. |
1312 |
|
1313 |
\end_layout |
1314 |
|
1315 |
\begin_layout Standard |
1316 |
In dynamic simulation, an initial value integration package, such as LSODE, |
1317 |
repeatedly asks the model to compute the time derivatives for the state |
1318 |
variables, given fixed values for the states. |
1319 |
Using values for |
1320 |
\family typewriter |
1321 |
dM_dt |
1322 |
\family default |
1323 |
computed by the model, the integration package will then update the state |
1324 |
variable, |
1325 |
\family typewriter |
1326 |
M |
1327 |
\family default |
1328 |
, to its new value. |
1329 |
To accommodate this calculation, we therefore fix the state variable, |
1330 |
\family typewriter |
1331 |
M |
1332 |
\family default |
1333 |
, and free up the derivative, |
1334 |
\family typewriter |
1335 |
dM_dt |
1336 |
\family default |
1337 |
. |
1338 |
\end_layout |
1339 |
|
1340 |
\begin_layout Section |
1341 |
Solving an ASCEND instance |
1342 |
\end_layout |
1343 |
|
1344 |
\begin_layout Standard |
1345 |
We are now ready to read in and compile an instance of our tank model. |
1346 |
We are assuming that you understand how to use the scripting window, and |
1347 |
we will show how to go about reading, compiling, solving and integrating |
1348 |
a dynamic model using the following script. |
1349 |
\end_layout |
1350 |
|
1351 |
\begin_layout Subsubsection* |
1352 |
Script code |
1353 |
\begin_inset LatexCommand \index{script, solve dynamic model} |
1354 |
|
1355 |
\end_inset |
1356 |
|
1357 |
|
1358 |
\end_layout |
1359 |
|
1360 |
\begin_layout LyX-Code |
1361 |
DELETE TYPES; |
1362 |
\end_layout |
1363 |
|
1364 |
\begin_layout LyX-Code |
1365 |
READ FILE "example.a4c"; |
1366 |
\end_layout |
1367 |
|
1368 |
\begin_layout LyX-Code |
1369 |
COMPILE ex OF tank; |
1370 |
\end_layout |
1371 |
|
1372 |
\begin_layout LyX-Code |
1373 |
BROWSE ex; |
1374 |
\end_layout |
1375 |
|
1376 |
\begin_layout LyX-Code |
1377 |
RUN {ex.default_self}; |
1378 |
\end_layout |
1379 |
|
1380 |
\begin_layout LyX-Code |
1381 |
RUN {ex.reset}; |
1382 |
\end_layout |
1383 |
|
1384 |
\begin_layout LyX-Code |
1385 |
RUN {ex.values}; |
1386 |
\end_layout |
1387 |
|
1388 |
\begin_layout LyX-Code |
1389 |
SOLVE ex WITH QRSlv; |
1390 |
\end_layout |
1391 |
|
1392 |
\begin_layout LyX-Code |
1393 |
RUN {ex.check_all}; |
1394 |
\end_layout |
1395 |
|
1396 |
\begin_layout LyX-Code |
1397 |
ASSIGN {ex.dynamic} TRUE; |
1398 |
\end_layout |
1399 |
|
1400 |
\begin_layout LyX-Code |
1401 |
RUN {ex.reset}; |
1402 |
\end_layout |
1403 |
|
1404 |
\begin_layout LyX-Code |
1405 |
RUN {ex.set_ode}; |
1406 |
\end_layout |
1407 |
|
1408 |
\begin_layout LyX-Code |
1409 |
RUN {ex.set_obs}; |
1410 |
\end_layout |
1411 |
|
1412 |
\begin_layout LyX-Code |
1413 |
# User will need to edit the next line to correct path |
1414 |
\end_layout |
1415 |
|
1416 |
\begin_layout LyX-Code |
1417 |
# to the models directory |
1418 |
\end_layout |
1419 |
|
1420 |
\begin_layout LyX-Code |
1421 |
source "$env(ASCENDDIST)/models/set_intervals.tcl"; |
1422 |
\end_layout |
1423 |
|
1424 |
\begin_layout LyX-Code |
1425 |
set_int 500 10 {s}; |
1426 |
\end_layout |
1427 |
|
1428 |
\begin_layout LyX-Code |
1429 |
INTEGRATE ex FROM 0 TO 50 WITH BLSODE; |
1430 |
\end_layout |
1431 |
|
1432 |
\begin_layout LyX-Code |
1433 |
ASSIGN {ex.input} 120 {mole/s}; |
1434 |
\end_layout |
1435 |
|
1436 |
\begin_layout LyX-Code |
1437 |
INTEGRATE ex FROM 50 TO 499 WITH BLSODE; |
1438 |
\end_layout |
1439 |
|
1440 |
\begin_layout LyX-Code |
1441 |
# In order to view integration results for both the |
1442 |
\end_layout |
1443 |
|
1444 |
\begin_layout LyX-Code |
1445 |
# integrations the user will have to go to the solver |
1446 |
\end_layout |
1447 |
|
1448 |
\begin_layout LyX-Code |
1449 |
# window, select options, general and turn off the |
1450 |
\end_layout |
1451 |
|
1452 |
\begin_layout LyX-Code |
1453 |
# overwrite integrator logs toggle. |
1454 |
\end_layout |
1455 |
|
1456 |
\begin_layout LyX-Code |
1457 |
# (NOTE: If you were then to run a different model or this |
1458 |
\end_layout |
1459 |
|
1460 |
\begin_layout LyX-Code |
1461 |
# same simulation again it would still write to the same |
1462 |
\end_layout |
1463 |
|
1464 |
\begin_layout LyX-Code |
1465 |
# files) |
1466 |
\end_layout |
1467 |
|
1468 |
\begin_layout LyX-Code |
1469 |
# In order to see both sets of data at the same time on |
1470 |
\end_layout |
1471 |
|
1472 |
\begin_layout LyX-Code |
1473 |
# one plot you will have to merge the two sets of data in |
1474 |
\end_layout |
1475 |
|
1476 |
\begin_layout LyX-Code |
1477 |
# the file. |
1478 |
This is done with following command. |
1479 |
\end_layout |
1480 |
|
1481 |
\begin_layout LyX-Code |
1482 |
asc_merge_data_file ascend new_obs.dat obs.dat; |
1483 |
\end_layout |
1484 |
|
1485 |
\begin_layout LyX-Code |
1486 |
# This command can also be used to convert data into a |
1487 |
\end_layout |
1488 |
|
1489 |
\begin_layout LyX-Code |
1490 |
# format that can be loaded into matlab for further work. |
1491 |
\end_layout |
1492 |
|
1493 |
\begin_layout LyX-Code |
1494 |
asc_merge_data_file matlab matlab_obs.m obs.dat; |
1495 |
\end_layout |
1496 |
|
1497 |
\begin_layout LyX-Code |
1498 |
# This command can also be used to convert data into a |
1499 |
\end_layout |
1500 |
|
1501 |
\begin_layout LyX-Code |
1502 |
# format that can be loaded into excel as a tab delimited |
1503 |
\end_layout |
1504 |
|
1505 |
\begin_layout LyX-Code |
1506 |
# text file. |
1507 |
\end_layout |
1508 |
|
1509 |
\begin_layout LyX-Code |
1510 |
asc_merge_data_file excel excel_obs.txt obs.dat; |
1511 |
\end_layout |
1512 |
|
1513 |
\begin_layout Standard |
1514 |
First of all reading and compiling an instance of a dynamic model is the |
1515 |
same as a steady state model except, as stated earlier, we must load |
1516 |
\family typewriter |
1517 |
ivpsystem.a4l |
1518 |
\family default |
1519 |
instead of |
1520 |
\family typewriter |
1521 |
system.a4l |
1522 |
\family default |
1523 |
. |
1524 |
The file containing |
1525 |
\family typewriter |
1526 |
example.a4c |
1527 |
\family default |
1528 |
in the first version of the code has |
1529 |
\family typewriter |
1530 |
REQUIRE |
1531 |
\family default |
1532 |
statements to load the right system file and the file |
1533 |
\family typewriter |
1534 |
atoms.a4l |
1535 |
\family default |
1536 |
. |
1537 |
|
1538 |
\end_layout |
1539 |
|
1540 |
\begin_layout Standard |
1541 |
Now it is time to solve the model, and this is where things start to change. |
1542 |
We must first solve the model for its initial conditions. |
1543 |
We set the boolean variable |
1544 |
\family typewriter |
1545 |
dynamic |
1546 |
\family default |
1547 |
to |
1548 |
\family typewriter |
1549 |
FALSE |
1550 |
\family default |
1551 |
(in the |
1552 |
\family typewriter |
1553 |
default_self |
1554 |
\family default |
1555 |
method) and run the |
1556 |
\family typewriter |
1557 |
reset |
1558 |
\family default |
1559 |
method to get a well-posed steady-state model. |
1560 |
We also need to run the |
1561 |
\family typewriter |
1562 |
values |
1563 |
\family default |
1564 |
method to set the fixed values of the initial conditions. |
1565 |
Finally we are solve, getting as the solution the initial conditions for |
1566 |
our model. |
1567 |
\end_layout |
1568 |
|
1569 |
\begin_layout Standard |
1570 |
After solving for the initial conditions |
1571 |
\begin_inset LatexCommand \index{initial conditions} |
1572 |
|
1573 |
\end_inset |
1574 |
|
1575 |
, we set things up for the dynamic simulation. |
1576 |
We set the boolean variable |
1577 |
\family typewriter |
1578 |
dynamic |
1579 |
\family default |
1580 |
to |
1581 |
\family typewriter |
1582 |
TRUE |
1583 |
\family default |
1584 |
and then run the |
1585 |
\family typewriter |
1586 |
seqmod |
1587 |
\family default |
1588 |
method to give a well-posed dynamic model. |
1589 |
We now have to establish which variables are the independent variables, |
1590 |
the state variables and their corresponding derivatives, and tell which |
1591 |
variables we would like to observe; we run |
1592 |
\family typewriter |
1593 |
set_ode |
1594 |
\family default |
1595 |
and |
1596 |
\family typewriter |
1597 |
set_obs |
1598 |
\family default |
1599 |
methods described above. |
1600 |
\end_layout |
1601 |
|
1602 |
\begin_layout Standard |
1603 |
In order for ASCEND and LSODE to know what step size and how many steps |
1604 |
we want to observe, we must load a Tcl file that defines a new script command. |
1605 |
The file we need to load is called |
1606 |
\family typewriter |
1607 |
set_intervals.tcl |
1608 |
\family default |
1609 |
, and it is found in the models subdirectory of the ASCEND distribution. |
1610 |
The command source comes from Tcl and is used to read and execute the a |
1611 |
set of commands in a file. |
1612 |
The file in this case is |
1613 |
\family typewriter |
1614 |
set_intervals.tcl |
1615 |
\family default |
1616 |
and the commands within it setup a new script command |
1617 |
\family typewriter |
1618 |
set_int |
1619 |
\family default |
1620 |
. |
1621 |
Once we have loaded this file, we can use the new command |
1622 |
\family typewriter |
1623 |
set_int |
1624 |
\family default |
1625 |
to set up the number of possible steps and their maximum size. |
1626 |
Now we are ready to integrate. |
1627 |
The way we do this is to use the |
1628 |
\family typewriter |
1629 |
INTEGRATE |
1630 |
\family default |
1631 |
command in the script. |
1632 |
The syntax for these command is as follows. |
1633 |
\end_layout |
1634 |
|
1635 |
\begin_layout Subsubsection* |
1636 |
Syntax for |
1637 |
\family typewriter |
1638 |
set_int |
1639 |
\end_layout |
1640 |
|
1641 |
\begin_layout LyX-Code |
1642 |
set_int number_of_steps step_size |
1643 |
\end_layout |
1644 |
|
1645 |
\begin_layout LyX-Code |
1646 |
{units of step size(time)}; |
1647 |
\end_layout |
1648 |
|
1649 |
\begin_layout Subsubsection* |
1650 |
Syntax for |
1651 |
\family typewriter |
1652 |
INTEGRATE |
1653 |
\family default |
1654 |
|
1655 |
\end_layout |
1656 |
|
1657 |
\begin_layout LyX-Code |
1658 |
INTGRATE compiled_model_name |
1659 |
\end_layout |
1660 |
|
1661 |
\begin_layout LyX-Code |
1662 |
FROM initial_step |
1663 |
\end_layout |
1664 |
|
1665 |
\begin_layout LyX-Code |
1666 |
TO final_step |
1667 |
\end_layout |
1668 |
|
1669 |
\begin_layout LyX-Code |
1670 |
WITH BLSODE; |
1671 |
\end_layout |
1672 |
|
1673 |
\begin_layout Standard |
1674 |
The command is set up with the initial and final step so that you can integrate |
1675 |
for a number of steps, then make step changes, and then continue to integrate |
1676 |
another number of steps. |
1677 |
\end_layout |
1678 |
|
1679 |
\begin_layout Section |
1680 |
Viewing Simulation Results |
1681 |
\end_layout |
1682 |
|
1683 |
\begin_layout Standard |
1684 |
To view the simulation results, open the ASCPLOT |
1685 |
\begin_inset LatexCommand \index{ASCPLOT} |
1686 |
|
1687 |
\end_inset |
1688 |
|
1689 |
window using the Tools menu on the Script window. |
1690 |
To view a plot, first use the File menu to load the data using Load data |
1691 |
set. |
1692 |
Depending on what you want to look at, you can load the file containing |
1693 |
the states or the file containing the variables you flagged for observation. |
1694 |
Once the data file is loaded, you can double click on the file name in |
1695 |
the top window to get a list of the variables in the file. |
1696 |
This list will appear in the left window named Unused variables |
1697 |
\begin_inset LatexCommand \index{variables, unused} |
1698 |
|
1699 |
\end_inset |
1700 |
|
1701 |
below where you just double clicked. |
1702 |
As you will notice on the line below, the independent variable has already |
1703 |
been set to time. |
1704 |
The way we select the variables we want to plot vs. |
1705 |
time is to highlight them from the list in the left window and, using the |
1706 |
top arrow button, move them over to the plotted variables window on the |
1707 |
right. |
1708 |
We then use the View plot file command from the Execute menu to view the |
1709 |
plot. |
1710 |
|
1711 |
\end_layout |
1712 |
|
1713 |
\begin_layout Standard |
1714 |
If we now want to plot something else, we simply highlight those variables |
1715 |
that we do not want to plot in the plotted variables window, use the other |
1716 |
arrow to move them back to the unused variable window and then move new |
1717 |
variables to the plotted variables window. |
1718 |
|
1719 |
\end_layout |
1720 |
|
1721 |
\begin_layout Standard |
1722 |
If we want to change the independent variable, we select the variable we |
1723 |
want to be the new independent variable from the list in either the unused |
1724 |
variable window or the plotted variable window and then use the appropriate |
1725 |
down arrow to move that variable down to become the independent variable. |
1726 |
\end_layout |
1727 |
|
1728 |
\begin_layout Subsection |
1729 |
Graphing options |
1730 |
\end_layout |
1731 |
|
1732 |
\begin_layout Standard |
1733 |
Now that you are able to view a plot, you might want to add titles or change |
1734 |
the axis scale, line colors, and so forth. |
1735 |
Adding titles can be done by selecting set |
1736 |
\shape italic |
1737 |
titles |
1738 |
\shape default |
1739 |
under the |
1740 |
\shape italic |
1741 |
Display |
1742 |
\shape default |
1743 |
menu, a new window will open in which you will have the option to add a |
1744 |
plot title and axis labels. |
1745 |
To change the axis scale, line color and many other features select see |
1746 |
options from the |
1747 |
\shape italic |
1748 |
Options |
1749 |
\shape default |
1750 |
menu. |
1751 |
\end_layout |
1752 |
|
1753 |
\begin_layout Subsubsection* |
1754 |
Graphing in Windows |
1755 |
\end_layout |
1756 |
|
1757 |
\begin_layout Standard |
1758 |
Under MS Windows |
1759 |
\begin_inset LatexCommand \index{MS Windows} |
1760 |
|
1761 |
\end_inset |
1762 |
|
1763 |
the default graph program Tkxgraph |
1764 |
\begin_inset LatexCommand \index{Tkxgraph} |
1765 |
|
1766 |
\end_inset |
1767 |
|
1768 |
gives you full control of the options without having to go through the |
1769 |
ASCPLOT Options menu. |
1770 |
Tkxgraph is also available for UNIX, but xgraph |
1771 |
\begin_inset LatexCommand \index{xgraph} |
1772 |
|
1773 |
\end_inset |
1774 |
|
1775 |
does a much better job drawing dashed lines with X11 than Tkxgraph does. |
1776 |
\end_layout |
1777 |
|
1778 |
\begin_layout Standard |
1779 |
If you decide you do not like the plotting tools described above, you have |
1780 |
two more options, and they are to convert the ASCEND output data files |
1781 |
so that they can be loaded by Matlab or a spreadsheet. |
1782 |
To convert the data files a new script command needs to be introduced and |
1783 |
the command is |
1784 |
\family typewriter |
1785 |
asc_merge_data_file |
1786 |
\family default |
1787 |
|
1788 |
\begin_inset LatexCommand \index{asc\_merge\_data\_file} |
1789 |
|
1790 |
\end_inset |
1791 |
|
1792 |
. |
1793 |
|
1794 |
\end_layout |
1795 |
|
1796 |
\begin_layout Subsubsection* |
1797 |
Syntax for |
1798 |
\family typewriter |
1799 |
asc_merge_data_file |
1800 |
\family default |
1801 |
command |
1802 |
\end_layout |
1803 |
|
1804 |
\begin_layout LyX-Code |
1805 |
asc_merge_data_file convert_to |
1806 |
\backslash |
1807 |
|
1808 |
\end_layout |
1809 |
|
1810 |
\begin_layout LyX-Code |
1811 |
output_file_name input_file_names |
1812 |
\end_layout |
1813 |
|
1814 |
\begin_layout Standard |
1815 |
The syntax for the |
1816 |
\family typewriter |
1817 |
asc_merge_data_file |
1818 |
\family default |
1819 |
command is as follows. |
1820 |
First of all the |
1821 |
\family typewriter |
1822 |
convert_to |
1823 |
\family default |
1824 |
is the format you want the data converted to. |
1825 |
There are three options matlab, excel or ascend. |
1826 |
The |
1827 |
\family typewriter |
1828 |
output_file_name |
1829 |
\family default |
1830 |
is exactly that, the name of the file in which you want the converted data |
1831 |
to be put. |
1832 |
The |
1833 |
\family typewriter |
1834 |
input_file_names |
1835 |
\family default |
1836 |
is also exactly that, the file name or names that you want converted. |
1837 |
If more than one input file is given the data is combined into one output |
1838 |
file. |
1839 |
\end_layout |
1840 |
|
1841 |
\begin_layout Standard |
1842 |
If the matlab |
1843 |
\begin_inset LatexCommand \index{matlab} |
1844 |
|
1845 |
\end_inset |
1846 |
|
1847 |
option is used the output file extension should be |
1848 |
\family typewriter |
1849 |
m |
1850 |
\family default |
1851 |
, if excel is used the extension should be |
1852 |
\family typewriter |
1853 |
txt |
1854 |
\family default |
1855 |
as it is a tab delimited text file and for ascend the extension should |
1856 |
be |
1857 |
\family typewriter |
1858 |
dat |
1859 |
\family default |
1860 |
for use with ASCPLOT. |
1861 |
\end_layout |
1862 |
|
1863 |
\begin_layout Standard |
1864 |
You maybe wondering what exactly is this |
1865 |
\family typewriter |
1866 |
asc_merge_data_file |
1867 |
\family default |
1868 |
command doing. |
1869 |
In the next three paragraphs we will give a brief explanation of each of |
1870 |
the options. |
1871 |
\end_layout |
1872 |
|
1873 |
\begin_layout Subsubsection* |
1874 |
Matlab conversion |
1875 |
\end_layout |
1876 |
|
1877 |
\begin_layout Standard |
1878 |
When the data is converted to be used in matlab the first thing that is |
1879 |
done is the header of the ascend data file is placed in the output file |
1880 |
but is commented out. |
1881 |
This is so the user can still tell when the data was created. |
1882 |
The next thing is does is put all the data into a matrix that has the same |
1883 |
name as the output file with var added to the end. |
1884 |
All variable names from the ascend data file are then converted to matlab |
1885 |
legal names by replacing the all dots and brackets with underscores(_). |
1886 |
The new variable names are then set equal to there corresponding column |
1887 |
of data in the matrix. |
1888 |
Each variable then becomes a vector. |
1889 |
When the file is run all the data is loaded and set equal to the new variable |
1890 |
names and can easily be plotted using matlab commands. |
1891 |
\end_layout |
1892 |
|
1893 |
\begin_layout Subsubsection* |
1894 |
Excel |
1895 |
\begin_inset LatexCommand \index{Excel} |
1896 |
|
1897 |
\end_inset |
1898 |
|
1899 |
conversion |
1900 |
\end_layout |
1901 |
|
1902 |
\begin_layout Standard |
1903 |
When the data is converted to be used in Excel the only thing that happens |
1904 |
is instead of the list of variables and units being a column it is turn |
1905 |
into rows. |
1906 |
When the data is loaded into Excel as a tab delimited text file all the |
1907 |
data will be in columns with the first row being the units of the data |
1908 |
and the second being the ascend variable name. |
1909 |
The data is then easily plotted using the Excel graphing package. |
1910 |
\end_layout |
1911 |
|
1912 |
\begin_layout Subsubsection* |
1913 |
Ascend conversion |
1914 |
\end_layout |
1915 |
|
1916 |
\begin_layout Standard |
1917 |
This is not so much a conversion as a merge and is the origin of the command. |
1918 |
It is only useful if there are multiple headers in a file or more than |
1919 |
one input file is given. |
1920 |
Multiple headers in the file occur when stopping and starting integrations |
1921 |
with the overwrite option turned off. |
1922 |
This conversion removes all subsequent headers that are the same as the |
1923 |
first, whether in one file or multiple, to leave one output file with what |
1924 |
looks like one data set for plotting. |
1925 |
If the headers are different the data will just be combined into one file |
1926 |
and when loaded in ASCPLOT will still look like different data sets. |
1927 |
\end_layout |
1928 |
|
1929 |
\begin_layout Section |
1930 |
Preparing a model for reuse |
1931 |
\begin_inset LatexCommand \index{reuse, model} |
1932 |
|
1933 |
\end_inset |
1934 |
|
1935 |
|
1936 |
\end_layout |
1937 |
|
1938 |
\begin_layout Standard |
1939 |
There are four major ways to prepare a model for reuse as described in Chapter\InsetSpace ~ |
1940 |
|
1941 |
\begin_inset LatexCommand \ref{cha:model2} |
1942 |
|
1943 |
\end_inset |
1944 |
|
1945 |
. |
1946 |
All of what is said there about reusable models applies to dynamic models. |
1947 |
However, there is one thing that we think should be repeated to make clear |
1948 |
for dynamic models, and that is parameterizing a model. |
1949 |
\end_layout |
1950 |
|
1951 |
\begin_layout Subsection |
1952 |
Parameterizing |
1953 |
\begin_inset LatexCommand \index{parameterizing} |
1954 |
|
1955 |
\end_inset |
1956 |
|
1957 |
the tank model |
1958 |
\end_layout |
1959 |
|
1960 |
\begin_layout Standard |
1961 |
As stated in Section\InsetSpace ~ |
1962 |
|
1963 |
\begin_inset LatexCommand \vref{sec:model2.parameterizingVessel} |
1964 |
|
1965 |
\end_inset |
1966 |
|
1967 |
|
1968 |
\color none |
1969 |
, parameterizing a model type definition alerts a future user as to which |
1970 |
parts of this model you deem to be the most likely to be shared. |
1971 |
An instance of a parameterized model is then created from previously defined |
1972 |
types. |
1973 |
\end_layout |
1974 |
|
1975 |
\begin_layout Standard |
1976 |
The new thing that needs to be repeated is that the |
1977 |
\family typewriter |
1978 |
ode_id |
1979 |
\family default |
1980 |
's of derivative and state pairs must be different even if they are in different |
1981 |
part of a larger model. |
1982 |
If for instance we wanted to have two tanks in series we could parameterize |
1983 |
the tank model and connect the two tanks together with the outlet of the |
1984 |
first tank being the feed to the second tank. |
1985 |
However, with the |
1986 |
\family typewriter |
1987 |
set_ode |
1988 |
\family default |
1989 |
|
1990 |
\begin_inset LatexCommand \index{set\_ode} |
1991 |
|
1992 |
\end_inset |
1993 |
|
1994 |
method, as we have currently written it, the derivative and state pairs |
1995 |
for both tanks would have the same |
1996 |
\family typewriter |
1997 |
ode_id |
1998 |
\family default |
1999 |
's. |
2000 |
Our way around this is to introduce an |
2001 |
\family typewriter |
2002 |
ode_counter |
2003 |
\family default |
2004 |
that is used to set the |
2005 |
\family typewriter |
2006 |
ode_id |
2007 |
\family default |
2008 |
's and is incremented after each derivative and state pair is set. |
2009 |
The |
2010 |
\family typewriter |
2011 |
ode_counter |
2012 |
\family default |
2013 |
becomes one of the model parameters and is, therefore, the same in all |
2014 |
models. |
2015 |
We will now give an example of this to help explain. |
2016 |
\end_layout |
2017 |
|
2018 |
\begin_layout Subsubsection* |
2019 |
|
2020 |
\family typewriter |
2021 |
set_ode |
2022 |
\family default |
2023 |
method for parameterized tank model |
2024 |
\end_layout |
2025 |
|
2026 |
\begin_layout LyX-Code |
2027 |
METHOD set_ode; |
2028 |
\end_layout |
2029 |
|
2030 |
\begin_layout LyX-Code |
2031 |
(* set ODE_TYPE -1=independent variable, |
2032 |
\end_layout |
2033 |
|
2034 |
\begin_layout LyX-Code |
2035 |
0=algebraic variable, 1=state variable, |
2036 |
\end_layout |
2037 |
|
2038 |
\begin_layout LyX-Code |
2039 |
2=derivative *) |
2040 |
\end_layout |
2041 |
|
2042 |
\begin_layout LyX-Code |
2043 |
t.ode_type :=-1; |
2044 |
\end_layout |
2045 |
|
2046 |
\begin_layout LyX-Code |
2047 |
dM_dt.ode_type :=2; |
2048 |
\end_layout |
2049 |
|
2050 |
\begin_layout LyX-Code |
2051 |
M.ode_type :=1; |
2052 |
\end_layout |
2053 |
|
2054 |
\begin_layout LyX-Code |
2055 |
(* Set ODE_ID *) |
2056 |
\end_layout |
2057 |
|
2058 |
\begin_layout LyX-Code |
2059 |
dM_dt.ode_id := ode_offset; |
2060 |
\end_layout |
2061 |
|
2062 |
\begin_layout LyX-Code |
2063 |
M.ode_id := ode_offset; |
2064 |
\end_layout |
2065 |
|
2066 |
\begin_layout LyX-Code |
2067 |
ode_offset := ode_offset+1; |
2068 |
\end_layout |
2069 |
|
2070 |
\begin_layout LyX-Code |
2071 |
END set_ode; |
2072 |
\end_layout |
2073 |
|
2074 |
\begin_layout LyX-Code |
2075 |
|
2076 |
\end_layout |
2077 |
|
2078 |
\begin_layout Subsubsection* |
2079 |
|
2080 |
\family typewriter |
2081 |
set_ode |
2082 |
\family default |
2083 |
method for larger model with two tank models being used as parts |
2084 |
\end_layout |
2085 |
|
2086 |
\begin_layout LyX-Code |
2087 |
|
2088 |
\end_layout |
2089 |
|
2090 |
\begin_layout LyX-Code |
2091 |
METHOD set_ode; |
2092 |
\end_layout |
2093 |
|
2094 |
\begin_layout LyX-Code |
2095 |
RUN tank_1.set_ode; |
2096 |
\end_layout |
2097 |
|
2098 |
\begin_layout LyX-Code |
2099 |
RUN tank_2.set_ode; |
2100 |
\end_layout |
2101 |
|
2102 |
\begin_layout LyX-Code |
2103 |
END set_ode; |
2104 |
\end_layout |
2105 |
|
2106 |
\begin_layout Standard |
2107 |
The parameterized tank |
2108 |
\family typewriter |
2109 |
set_ode |
2110 |
\family default |
2111 |
method is almost the same as the original one we wrote except it now uses |
2112 |
|
2113 |
\family typewriter |
2114 |
ode_offset |
2115 |
\family default |
2116 |
, an |
2117 |
\family typewriter |
2118 |
ode_counter |
2119 |
\family default |
2120 |
, to set the |
2121 |
\family typewriter |
2122 |
ode_id |
2123 |
\family default |
2124 |
s. |
2125 |
It may be obvious, but this is how it works. |
2126 |
When the larger model |
2127 |
\family typewriter |
2128 |
set_ode |
2129 |
\family default |
2130 |
is run, the |
2131 |
\family typewriter |
2132 |
set_ode |
2133 |
\family default |
2134 |
for |
2135 |
\family typewriter |
2136 |
tank_1 |
2137 |
\family default |
2138 |
is run, the |
2139 |
\family typewriter |
2140 |
ode_id |
2141 |
\family default |
2142 |
s are set to the current value of |
2143 |
\family typewriter |
2144 |
ode_offset |
2145 |
\family default |
2146 |
, the counter is then incremented and |
2147 |
\family typewriter |
2148 |
set_ode |
2149 |
\family default |
2150 |
is run for |
2151 |
\family typewriter |
2152 |
tank_2 |
2153 |
\family default |
2154 |
which then gets the incremented |
2155 |
\family typewriter |
2156 |
ode_offset |
2157 |
\family default |
2158 |
so the values are now different. |
2159 |
You can now hopefully see that we can string as may tanks together as we |
2160 |
like, and all the derivative and state pairs |
2161 |
\family typewriter |
2162 |
ode_id |
2163 |
\family default |
2164 |
will be different. |
2165 |
\end_layout |
2166 |
|
2167 |
\begin_layout Standard |
2168 |
This same idea can be applies to setting the observed variables. |
2169 |
The reason this is a good idea is that the variables are placed in the |
2170 |
output files in order of there |
2171 |
\family typewriter |
2172 |
obs_id |
2173 |
\family default |
2174 |
value. |
2175 |
This way we can keep all variables flagged for observation from one part |
2176 |
of a model together. |
2177 |
\end_layout |
2178 |
|
2179 |
\begin_layout Standard |
2180 |
The important thing that needs to be stressed for a dynamic system is that |
2181 |
the time variable, dynamic boolean, and ode and obs counters must be in |
2182 |
the parameter list. |
2183 |
All these variable need to be the same in each model to be consistent and |
2184 |
to make sure the model gets setup correctly when the |
2185 |
\family typewriter |
2186 |
set_ode |
2187 |
\family default |
2188 |
method is executed. |
2189 |
\end_layout |
2190 |
|
2191 |
\begin_layout Section |
2192 |
In conclusion |
2193 |
\end_layout |
2194 |
|
2195 |
\begin_layout Standard |
2196 |
We have just led you step by step through the process of creating a small |
2197 |
dynamic ASCEND model and the basics on how to view the results. |
2198 |
\end_layout |
2199 |
|
2200 |
\end_body |
2201 |
\end_document |