File: sensitivity_analysis.cpp

package info (click to toggle)
casadi 3.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,964 kB
  • sloc: cpp: 114,229; python: 35,462; xml: 1,946; ansic: 859; makefile: 257; sh: 114; f90: 63; perl: 9
file content (223 lines) | stat: -rw-r--r-- 8,079 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 *    MIT No Attribution
 *
 *    Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl, KU Leuven.
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy of this
 *    software and associated documentation files (the "Software"), to deal in the Software
 *    without restriction, including without limitation the rights to use, copy, modify,
 *    merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 *    permit persons to whom the Software is furnished to do so.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 *    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <casadi/casadi.hpp>

#include <iostream>
#include <iomanip>

using namespace casadi;

struct Test {
  SXDict dae;
  double tf;
  std::vector<double> x0;
  double u0;
  bool is_ode;
  std::string name;
};

/** \brief Generate a simple ODE */
Test simpleODE(){
  // Time
  SX t = SX::sym("t");

  // Parameter
  SX u = SX::sym("u");

  // Differential states
  SX s = SX::sym("s"), v = SX::sym("v"), m = SX::sym("m");
  SX x = vertcat(s, v, m);

  // Constants
  double alpha = 0.05; // friction
  double beta = 0.1;   // fuel consumption rate

  // Differential equation
  SX ode = vertcat(v, (u-alpha*v*v)/m, -beta*u*u);

  // Quadrature
  SX quad = pow(v,3) + pow((3-sin(t))-u,2);

  // Return test problem
  Test r;
  r.dae = decltype(r.dae){{"t", t}, {"x", x}, {"p", u}, {"ode", ode}, {"quad", quad}}; // decltype because MSVC bug
  r.tf = 0.5;
  r.x0 = {0, 0, 1};
  r.u0 = 0.4;
  r.is_ode = true;
  r.name = "simple_ode";
  return r;
}

/** \brief Generate a simple DAE */
Test simpleDAE(){
  // Parameter
  SX u = SX::sym("u");

  // Differential state
  SX x = SX::sym("x");

  // Algebraic variable
  SX z = SX::sym("z");

  // Differential equation
  SX ode = -x + 0.5*x*x + u + 0.5*z;

  // Algebraic constraint
  SX alg = z + exp(z) - 1. + x;

  // Quadrature
  SX quad = x*x + 3.0*u*u;

  // Return DAE
  Test r;
  r.dae = decltype(r.dae){{"x", x}, {"z", z}, {"p", u}, {"ode", ode}, {"alg", alg}, {"quad", quad}};
  r.tf = 5;
  r.x0 = {1};
  r.u0 = 0.4;
  r.is_ode = false;
  r.name = "simple_dae";
  return r;
}

struct Solver {
  std::string plugin;
  bool ode_only;
  Dict opts;
};

int main(){

  // Test problems
  std::vector<Test> tests = {simpleODE(), simpleDAE()};

  // ODE/DAE integrators
  std::vector<Solver> solvers;
  solvers.push_back({"cvodes", true, Dict()});
  solvers.push_back({"idas", false, Dict()});
  solvers.push_back({"rk", true, Dict()});
  Dict kinsol_options = {{"linear_solver_type", "dense"}};

  Dict coll_opts = {{"rootfinder", "kinsol"},
                    {"collocation_scheme", "legendre"},
                    {"rootfinder_options", kinsol_options}};
  solvers.push_back({"collocation", false, coll_opts});

  // Loop over all problems
  for (auto&& test : tests) {
    // Loop over all solvers
    for (auto&& solver : solvers) {
      // Test with or without multiple output times
      for (int ntout : {1, 3}) {
        // Skip if problem cannot be handled
        if (solver.ode_only && !test.is_ode) continue;
        // Printout
        std::cout << "Solving \"" << test.name << "\" using \"" << solver.plugin << "\", " << ntout << " output times" << std::endl;
        // Output time grid
        std::vector<double> tout;
        if (ntout == 1) {
          tout = {test.tf};
        } else {
          tout = {0, 0.5 * test.tf, test.tf};
        }
        // Create integrator instance
        Function I = integrator("I", solver.plugin, test.dae, 0, tout, solver.opts);

        // Buffers for evaluation
        std::map<std::string, DM> arg, res;

        // Integrate to get results
        arg = decltype(arg){{"x0", test.x0},
                            {"p", test.u0}};
        res = I(arg);
        DM xf(res.at("xf"));
        DM qf(res.at("qf"));
        std::cout << std::setw(50) << "Unperturbed solution: " << "xf  = " << xf.nonzeros() <<  ", qf  = " << qf.nonzeros() << std::endl;

        // Perturb solution to get a finite difference approximation
        double h = 0.001;
        arg["p"] = test.u0+h;
        res = I(arg);
        DM fd_xf = (res.at("xf")-xf)/h;
        DM fd_qf = (res.at("qf")-qf)/h;
        std::cout << std::setw(50) << "Finite difference approximation: " << "d(xf)/d(p) = " << fd_xf.nonzeros() << ", d(qf)/d(p) = " << fd_qf.nonzeros() << std::endl;

        // Calculate once, forward
        Function I_fwd = I.factory("I_fwd", {"x0", "p", "fwd:x0", "fwd:p"},
                                            {"fwd:xf", "fwd:qf"});
        arg = decltype(arg){{"x0", test.x0},
                            {"p", test.u0},
                            {"fwd_x0", 0},
                            {"fwd_p", 1}};
        res = I_fwd(arg);
        DM fwd_xf = res.at("fwd_xf");
        DM fwd_qf = res.at("fwd_qf");
        std::cout << std::setw(50) << "Forward sensitivities: " << "d(xf)/d(p) = " << fwd_xf.nonzeros() << ", d(qf)/d(p) = " << fwd_qf.nonzeros() << std::endl;

        // Calculate once, adjoint
        std::vector<double> adj_qf(ntout, 0);
        adj_qf.back() = 1;
        Function I_adj = I.factory("I_adj", {"x0", "p", "adj:xf", "adj:qf"},
                                            {"adj:p", "adj:x0"});
        arg = decltype(arg){{"x0", test.x0}, {"p", test.u0}, {"adj_xf", 0}, {"adj_qf", adj_qf}};
        res = I_adj(arg);
        DM adj_x0 = res.at("adj_x0");
        DM adj_p = res.at("adj_p");
        std::cout << std::setw(50) << "Adjoint sensitivities: " << "d(qf)/d(x0) = " << adj_x0.nonzeros() << ", d(qf)/d(p) = " << adj_p.nonzeros() << std::endl;

        // Perturb adjoint solution to get a finite difference approximation of the second order sensitivities
        arg["p"] = test.u0+h;
        res = I_adj(arg);
        DM fd_adj_x0 = (res.at("adj_x0")-adj_x0)/h;
        DM fd_adj_p = (res.at("adj_p")-adj_p)/h;
        std::cout << std::setw(50) << "FD of adjoint sensitivities: " << "d2(qf)/d(x0)d(p) = " << fd_adj_x0.nonzeros() << ", d2(qf)/d(p)d(p) = " << fd_adj_p.nonzeros() << std::endl;

        // Forward over adjoint to get the second order sensitivities
        Function I_foa = I_adj.factory("I_foa",
                                      {"x0", "p", "fwd:p", "adj_xf", "adj_qf"},
                                      {"fwd:adj_x0", "fwd:adj_p"});
        arg = decltype(arg){{"x0", test.x0},
                            {"p", test.u0},
                            {"fwd_p", 1},
                            {"adj_xf", 0},
                            {"adj_qf", adj_qf}};
        res = I_foa(arg);
        DM fwd_adj_x0 = res.at("fwd_adj_x0");
        DM fwd_adj_p = res.at("fwd_adj_p");
        std::cout << std::setw(50) << "Forward over adjoint sensitivities: " << "d2(qf)/d(x0)d(p) = " << fwd_adj_x0.nonzeros() << ", d2(qf)/d(p)d(p) = " << fwd_adj_p.nonzeros() << std::endl;

        // Adjoint over adjoint to get the second order sensitivities
        Function I_aoa = I_adj.factory("I_aoa", {"x0", "p", "adj_xf", "adj_qf", "adj:adj_p"},
                                                {"adj:x0", "adj:p"});
        arg = decltype(arg){{"x0", test.x0},
                            {"p", test.u0},
                            {"adj_xf", 0},
                            {"adj_qf", adj_qf},
                            {"adj_adj_p", 1}};
        res = I_aoa(arg);
        DM adj_adj_x0 = res.at("adj_x0");
        DM adj_adj_p = res.at("adj_p");
        std::cout << std::setw(50) << "Adjoint over adjoint sensitivities: " << "d2(qf)/d(x0)d(p) = " << adj_adj_x0.nonzeros() << ", d2(qf)/d(p)d(p) = " << adj_adj_p.nonzeros() << std::endl;
      }
    }
  }
  return 0;
}