CasADi tutorial

advertisement
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/integration/temp.py
September 28, 2016
CasADi tutorial
0
1
2
3
4
5
6
11
12
13
14
#
#
#
#
#
#
#
1
45
46
47
48
49
50
51
o p t s [ " r e l t o l " ] = 1 e -6
opts [ " grid " ] = ts
opts [ " o u t p u t _ t 0 " ] = True
s i m = i n t e g r a t o r (" sim ", " c v o d e s ", dae , o p t s )
s o l = s i m ( x0 = [ x0 , y0 ] , p= 0)
54
55
56
57
58
59
f i g u r e ()
p l o t ( s o l [ : ,0 ] , s o l [ : ,1 ] )
t i t l e ( ’ Van d e r P o l p h a s e s p a c e ’)
x l a b e l ( ’ x ’)
y l a b e l ( ’ y ’)
show ()
s o l = s o l [ ’ x f ’ ] . f u l l ().T
Plot the trajectory
from numpy i m p o r t *
i m p o r t numpy
from c a s a d i i m p o r t *
from p y l a b i m p o r t *
Error:
Van der To
Pol phase
space
/usr/lib/pymodules/python2.7/matplotlib/__init__.py:758: UserWarning: Found matplotlib configuration
conform
with the XDG base directory standard,
2.0 in ~/.matplotlib/.
_get_xdg_config_dir())
1.5
ODE integration
1.0
Let’s construct a simple Van der Pol oscillator.
0.5
u =
x =
y =
ode
23
dae
SX. sym (" u ")
SX. sym (" x ")
SX. sym (" y ")
= v e r t c a t ((1- y * y )* x - y + u , x )
0.0
y
18
19
20
21
0.5
DAE problem formulation as expected by CasADi’s integrators:
1.0
= { ’ x ’ : v e r t c a t ( x , y ), ’ p ’ : u , ’ ode ’ : ode }
1.5
The whole series of sundials options are available for the user
25
26
27
28
29
30
31
32
2.0
o p t s = {}
opts [ " f s e n s _ e r r _ c o n " ] = True
opts [ " quad_err_con " ] = True
o p t s [ " a b s t o l " ] = 1 e -6
o p t s [ " r e l t o l " ] = 1 e -6
t e n d = 10
opts [ " t0 " ] = 0
opts [ " t f " ] = tend
2.5
F = i n t e g r a t o r (" F ", " c v o d e s ", dae , o p t s )
p r i n t "%d −> %d " % (F. n _ i n (),F. n _ o u t ())
6 -> 6
Setup the Integrator to integrate from 0 to t=tend, starting at [x0,y0] The output of Integrator is the state at the
end of integration. To obtain the whole trajectory of states, use Simulator:
39
40
41
42
43
44
t s =numpy . l i n s p a c e (0, t e n d ,100)
x0 = 0; y0 = 1
o p t s = {}
opts [ " f s e n s _ e r r _ c o n " ] = True
opts [ " quad_err_con " ] = True
o p t s [ " a b s t o l " ] = 1 e -6
2
1
0
x
Sensitivity for initial conditions
Create the Integrator
34
35
3
66
67
68
69
70
71
72
73
74
75
76
77
d e f o u t ( dx0 ) :
r e s = F( x0 = [ x0 + dx0 , y0 ] )
r e t u r n r e s [ " x f " ] . f u l l ()
dx0 =numpy . l i n s p a c e (-2,2,100)
o u t = array( [ o u t ( dx ) f o r dx i n dx0 ] ). s q u e e z e ()
d x t e n d = o u t [ : ,0 ] - s o l [ -1,0 ]
f i g u r e ()
p l o t ( dx0 , d x t e n d )
g r i d ()
t i t l e ( ’ I n i t i a l p e r t u r b a t i o n map ’)
x l a b e l ( ’ dx ( 0 ) ’)
1
2
3
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/integration/temp.py
78
79
September 28, 2016
2
y l a b e l ( ’ dx ( t e n d ) ’)
show ()
Initial perturbation map
True sensitivity
Linearised sensitivity
1.0
Initial perturbation map
0.5
0.5
0.0
0.0
0.5
dx(tend)
1.0
1.0
1.5
1.5
2.0
2.0
2.5
2.5
3.0
2.0
3.0
2.0
76
77
78
79
80
81
82
83
84
85
86
87
dx(tend)
0.5
1.5
1.0
0.5
0.0
dx(0)
0.5
1.0
1.5
1.5
1.0
0.5
0.0
dx(0)
0.5
1.0
1.5
2.0
2.0
#
d i n t e g r a t o r = F. d e r i v a t i v e (1,0)
r e s = d i n t e g r a t o r ( d e r _ x 0 = [ x0 , y0 ] , f w d 0 _ x 0 = [ 1,0 ] )
A = r e s [ " fwd0_xf " ] [ 0 ]
A = float(A) # FIXME
p l o t ( dx0 ,A* dx0 )
l e g e n d (( ’ T r u e s e n s i t i v i t y ’, ’ L i n e a r i s e d
p l o t (0,0, ’ o ’)
show ()
The interpetation is that a small initial circular patch of phase space evolves into ellipsoid patches at later stages.
s e n s i t i v i t y ’))
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def out( t ) :
r e s = d i n t e g r a t o r ( d e r _ x 0 = [ x0 , y0 ] , f w d 0 _ x 0 = [ 1,0 ] )
A= r e s [ " f w d 0 _ x f " ] . f u l l ()
r e s = d i n t e g r a t o r ( d e r _ x 0 = [ x0 , y0 ] , f w d 0 _ x 0 = [ 0,1 ] )
B= r e s [ " f w d 0 _ x f " ] . f u l l ()
r e t u r n array( [ A,B ] ). s q u e e z e ().T
c i r c l e = array( [ [ s i n ( x ), c o s ( x ) ]
f o r x i n numpy . l i n s p a c e (0,2* p i ,100) ] ).T
f i g u r e ()
p l o t ( s o l [ : ,0 ] , s o l [ : ,1 ] )
g r i d ()
f o r i i n range(10) :
J = o u t ( t s [ 10* i ] )
e= 0.1* numpy . d o t ( J , c i r c l e ).T+ s o l [ 10* i , : ]
p l o t ( e [ : ,0 ] , e [ : ,1 ] , c o l o r = ’ r e d ’)
show ()
/home/travis/build/casadi/binaries/casadi/docs/tutorials/python/src/integration/temp.py
September 28, 2016
140
141
142
143
144
145
146
147
3
2
1
3
f i g u r e ()
p l o t ( uv , o u t )
g r i d ()
t i t l e ( ’ Dependence o f
x l a b e l ( ’ u ’)
y l a b e l ( ’ s t a t e ’)
l e g e n d (( ’ x ’, ’ y ’))
show ()
2
0
2
1
0
1
2
3
Symbolic intergator results
Since Integrator is just another Function, the usual CasADi rules for symbolic evaluation are active.
We create an MX ’w’ that contains the result of a time integration with: - a fixed integration start time , t=0s
- a fixed integration end time, t=10s - a fixed initial condition (1,0) - a free symbolic input, held constant during
integration interval
123
124
u=MX. sym (" u ")
w = F( x0 =MX( [ 1,0 ] ), p=u ) [ " x f " ]
127
128
129
130
131
132
133
f = F u n c t i o n ( ’ f ’, [ u ] , [ w ] )
We construct an MXfunction and a python help function ’out’
d e f o u t (u) :
w0 = f ( u )
r e t u r n w0. f u l l ()
o u t (0)
[ [ -2.54394406 ]
[ -0.43934198 ] ]
print
o u t (1)
[ [ -0.25398164 ]
[ 1.3963845 ] ]
Let’s plot the results
137
138
139
uv =numpy . l i n s p a c e (-1,1,100)
o u t = array( [ o u t ( i ) f o r
i
i n uv ] ). s q u e e z e ()
state
3
The figure reveals that perturbations perpendicular to the phase space trajectory shrink.
134
x
y
0
2
print
Dependence of final state on input u
1
1
3
f i n a l s t a t e on i n p u t u ’)
1
2
3
4
1.0
0.5
0.0
u
0.5
1.0
Download