KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Basics2_Parametrized.m
Go to the documentation of this file.
1 namespace demos{
2 
3 
4 /* (Autoinserted by mtoc++)
5  * This source code has been filtered by the mtoc++ executable,
6  * which generates code that can be processed by the doxygen documentation tool.
7  *
8  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
9  * Except for the comments, the function bodies of your M-file functions are untouched.
10  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
11  * attached source files that are highly readable by humans.
12  *
13  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
14  * the correct locations in the source code browser.
15  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
16  */
17 
19 
20 
21 /* % Initialization
22  * Please refer to demos.Basics1_Linear for elementary linear setup */
23 dim = 1000;
24 spfac = .2;
25 /* Get a RandStream for reproducable demo */
26 s = RandStream(" mt19937ar "," Seed ",0);
27 
28 /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29  *% %%%%%%%%%%%% Simple linear model %%%%%%%%%%%%
30  *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31  *% Initialize the base model */
32 m = models.BaseFullModel(" LinearModelWithParamsDemo ");
33 m.T= .5;
34 m.dt= 0.01;
35 m.System.MaxTimestep= m.dt;
36 /* Quick'n dirty. For different solvers see Basics1 */
37 m.ODESolver= solvers.ExplEuler;
38 
39 /* % Add some parameters to the system
40  * This is pretty random! This is for demonstration purposes only. */
41 m.System.addParam(" P1 (Diffswitch) ",0," Range ",[0 1]);
42 m.System.addParam(" P2 (Exp) ",1," Range ",[0 2]);
43 m.System.addParam(" P3 (SomeOtherName) ",0," Range ",[-1 1]);
44 
45 /* % Set the basic components
46  * Define the affine coefficient functions. They can be time- and parameter
47  * dependent, where the function strings (by convention) have the variables
48  * `t` and `\mu` available for inline use. */
49 funs = [" t*mu(1) ", " mu(1)*mu(2).^2 ", " exp(mu(2))-mu(1) ", " cos(mu(3))+sin(10*mu(1)) "];
50 /* Compose an affine-parametric initial value
51  * Notice: For affine initial values the time parameter will not be used
52  * (i.e. set to []). Hence, funs{1} would cause an error if used here. */
53 Px0 = dscomponents.AffineInitialValue;
54 Px0.addMatrix(funs[2],10*(s.rand(dim,1)-.5));
55 Px0.addMatrix(funs[3],10*(s.rand(dim,1)-.5));
56 m.System.x0= Px0;
57 
58 /* Compose an affine-parametric linear core function
59  * Here we use a diffusion matrix (with neumann boundary) for the first
60  * component, that will go into the system with `t*\mu(1)`. Hence, the
61  * diffusion will get stronger over time, and can be controlled via
62  * parameter one. */
63 Ap = dscomponents.AffLinCoreFun;
64 e = ones(dim,1);
65 diff = spdiags([e -2*e e], -1:1, dim, dim);
66 diff(1,2) = -1; diff(end,end-1) = -1;
67 Ap.addMatrix(funs[1], diff);
68 for k = [2 4]
69  Ap.addMatrix(funs[k], 0.1*(Utils.sprand(dim,dim,spfac,s)-.5));
70 end
71 m.System.A= Ap;
72 
73 /* Parameter-dependent output switch between DoFs 1 and 2 */
74 Cp = dscomponents.AffLinOutputConv;
75 Cp.addMatrix(" mu(1) ",[1; zeros(dim-1,1)]^t);
76 Cp.addMatrix(" 1-mu(1) ",[0; 1; zeros(dim-2,1)]^t);
77 m.System.C= Cp;
78 
79 /* % Run a simple simulation and plot the results */
80 mu = m.getRandomParam;
81 [t,y,ct,x] = m.simulate(mu);
82 fprintf(" Simulation time: %g\n ",ct);
83 m.plot(t,y);
84 m.plotState(t,x);
85 
86 /* % Add inputs */
87 u1 = @(t)-200*exp(-(t-.5).^2/3);
88 u2 = @(t)200*sin(10*t).*(t>.3);
89 m.System.Inputs[1] = u1;
90 m.System.Inputs[2] = u2;
91 Bp = dscomponents.AffLinInputConv;
92 Bp.addMatrix(" mu(2)/2 ",[1; zeros(dim-1,1)]);
93 Bp.addMatrix(" 1-mu(2)/2 ",[0; 1; zeros(dim-2,1)]);
94 m.System.B= Bp;
95 
96 /* % Plot new system */
97 mu = m.getRandomParam;
98 [~,y] = m.simulate(mu);
99 [t,y_u1] = m.simulate(mu,1);
100 m.plot(t,y);
101 m.plot(t,y_u1);
102 m.plot(t,y-y_u1);
103 
104 /* % Setup the model reduction
105  * Choose a parameter sampler */
106 s = sampling.RandomSampler;
107 s.Seed= 1;
108 s.Samples= 5;
109 m.Sampler= s;
110 
111 /* Now we want only the input 1 used as training input (more would result in
112  * cross-product of param samples and input indices) */
113 m.TrainingInputs= 1;
114 
115 /* Setup the subspace reduction scheme */
116 s = spacereduction.PODGreedy;
117 /* Just go to subspace size 20.
118  * Otherwise the error tolerance can be set via s.Eps */
119 s.MaxSubspaceSize= 20;
120 /* This causes the trajectory data `x` to be augmented by evaluations
121  * `A(t,mu)*x`. This can be handy in order to improve the projected A matrix.
122  * Please see the spacereduction.BaseSpaceReducer class for
123  * more details and alternatives. */
124 s.IncludeAxData= true;
125 
126 /* This causes the span of the B matrix to be the initial space
127  * Alternative an initial space can be set using s.InitialSpace */
128 s.IncludeBSpan= true;
129 m.SpaceReducer= s;
130 
131 /* % Run the offline phase
132  * All the steps from the offline phase can also be run by using
133  * m.offlineGenerations. */
134 m.off1_createParamSamples;
135 m.off2_genTrainingData;
136 m.off3_computeReducedSpace;
137 /* The following steps are not required in this setting:
138  * m.off4_genApproximationTrainData;
139  * m.off5_computeApproximation;
140  * m.off6_prepareErrorEstimator; */
141 save basics2_param;
142 
143 /* % Simulate & Plot the reduced model */
144 mu = m.getRandomParam;
145 /* This is the MAIN method to create a reduced model from a full model. */
146 r = m.buildReducedModel;
147 /* The models.ReducedModel inherits from BaseModel as well, so that the same
148  * interface for running simulations can be used. */
149 [~,yr] = r.simulate(mu,1);
150 [t,y] = m.simulate(mu,1);
151 m.plot(t,y);
152 m.plot(t,yr);
153 m.plot(t,yr-y);
154 
155 /* % Additional analysis tools */
156 ma = ModelAnalyzer(r);
157 /* Gives a short summary of all components of the reduced/full model that
158  * were involved in the reduction process */
159 ma.plotReductionOverview;
160 ma.compareRedFull(mu,1);
161 }
187 };
ModelAnalyzer: Analysis tools for reduced models and approximations.
Definition: ModelAnalyzer.m:17
Collection of generally useful functions.
Definition: Utils.m:17
function Basics2_Parametrized()
Basics2_Parametrized:
static function S = sprand(integer n,integer m,double dens,RandStream rs)
Creates a random sparse matrix with given density (approximately).
Definition: Utils.m:629