JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
ReducedSystem.java
Go to the documentation of this file.
1 package kermor;
2 
3 import jarmos.Util;
10 
11 import java.io.IOException;
12 
30 
31 import org.apache.commons.math.ode.DerivativeException;
32 import org.apache.commons.math.ode.FirstOrderDifferentialEquations;
33 
42 public class ReducedSystem implements FirstOrderDifferentialEquations {
43 
44  private int dim = -1;
45 
46  public IOutputConv C = null;
47 
48  public IInitialValue x0 = null;
49 
50  public IInputConv B = null;
51 
52  public IMassMatrix M = null;
53 
54  public IInputFunctions u = null;
55 
56  public ICoreFun f = null;
57 
58  private double[] mu = null;
59  private int inidx = -1;
60 
61  public void setConfig(double[] mu, int InputIdx) {
62  this.mu = mu;
63  this.inidx = InputIdx;
64  }
65 
66  public double[] currentMu() {
67  return mu;
68  }
69 
70  public int currentInput() {
71  return inidx;
72  }
73 
74  @Override
75  public int getDimension() {
76  return dim;
77  }
78 
79  @Override
80  public void computeDerivatives(double t, double[] x, double[] xDot) throws DerivativeException {
81  Util.vecAdd(xDot, f.evaluate(t, x, mu));
82  if (B != null) {
83  Util.vecAdd(xDot, B.evaluate(t, mu).operate(u.evaluate(t, inidx)));
84  }
85  }
86 
98  public static ReducedSystem load(AModelManager mng) throws MathReaderException, ModelManagerException, IOException {
99  ReducedSystem res = new ReducedSystem();
100 
101  res.dim = Integer.parseInt(mng.getModelXMLTagValue("dim"));
102 
103  MathObjectReader r = mng.getMathObjReader();
104 
105  loadCoreFun(mng, res);
106 
107  loadInputs(mng, res);
108 
109  loadMassMatrix(mng, res);
110 
111  if ("dscomponents.LinearOutputConv".equals(mng.getModelXMLTagValue("outputconvtype"))) {
112  res.C = new LinearOutputConv(r.readMatrix(mng.getInStream("C.bin")));
113  }
114 
115  if ("dscomponents.ConstInitialValue".equals(mng.getModelXMLTagValue("initialvaluetype"))) {
116  res.x0 = new ConstInitialValue(r.readRawDoubleVector(mng.getInStream("x0.bin")));
117  }
118 
119  return res;
120  }
121 
122  private static void loadInputs(AModelManager mng, ReducedSystem res) throws IOException, ModelManagerException {
123  // Inputs
124  MathObjectReader r = mng.getMathObjReader();
125  String hlp;
126  if (mng.xmlTagExists("kermor_model.inputconv")) {
127  res.u = (IInputFunctions) mng.loadModelClass("Inputs");
128  hlp = mng.getModelXMLAttribute("type", "kermor_model.inputconv");
129  if ("dscomponents.LinearInputConv".equals(hlp)) {
130  res.B = new LinearInputConv(r.readMatrix(mng.getInStream("B.bin")));
131  } else if ("dscomponents.AffLinInputConv".equals(hlp)) {
132  IAffineCoefficients coeff = null;
133  coeff = (IAffineCoefficients) mng.loadModelClass(mng.getModelXMLTagValue("inputconv.coeffclass"));
134  AffParamMatrix a = new AffParamMatrix(r.readMatrix(mng.getInStream("B.bin")), res.dim, coeff);
135  res.B = new AffineInputConv(a);
136  }
137  }
138  }
139 
140  private static void loadCoreFun(AModelManager mng, ReducedSystem res) throws IOException, MathReaderException,
141  ModelManagerException {
142  MathObjectReader r = mng.getMathObjReader();
143  String type = mng.getModelXMLAttribute("type", "corefun");
144  if ("dscomponents.ParamTimeKernelCoreFun".equals(type)) {
145  KernelExpansion k = new KernelExpansion();
146  k.ma = r.readMatrix(mng.getInStream("Ma.bin"));
147 
148  k.StateKernel = loadKernel(mng, "statekernel", "kernel.bin");
149  k.xi = r.readMatrix(mng.getInStream("xi.bin"));
150  // System.out.println(Util.realMatToString(res.f.xi));
151 
152  k.TimeKernel = loadKernel(mng, "timekernel", "timekernel.bin");
153  k.ti = r.readVector(mng.getInStream("ti.bin"));
154  // System.out.println(Util.realVecToString(res.f.ti));
155 
156  k.ParamKernel = loadKernel(mng, "paramkernel", "paramkernel.bin");
157  k.mui = r.readMatrix(mng.getInStream("mui.bin"));
158  // System.out.println(Util.realMatToString(res.f.mui));
159 
160  res.f = k;
161  } else if ("dscomponents.LinearCoreFun".equals(type)) {
162  res.f = new LinearCoreFun(r.readMatrix(mng.getInStream("A.bin")));
163  } else if ("dscomponents.AffLinCoreFun".equals(type)) {
164  IAffineCoefficients coeff = null;
165  coeff = (IAffineCoefficients) mng.loadModelClass(mng.getModelXMLTagValue("corefun.coeffclass"));
166  AffParamMatrix a = new AffParamMatrix(r.readMatrix(mng.getInStream("A.bin")), res.dim, coeff);
167  res.f = new AffParamTimeCoreFun(a);
168  } else {
169  throw new RuntimeException("Unknown core function type: " + type);
170  }
171  }
172 
173  private static void loadMassMatrix(AModelManager mng, ReducedSystem res) throws IOException, ModelManagerException {
174  // Inputs
175  MathObjectReader r = mng.getMathObjReader();
176  String hlp;
177  if (mng.xmlTagExists("kermor_model.massmatrix")) {
178  hlp = mng.getModelXMLAttribute("type", "kermor_model.massmatrix");
179  if ("dscomponents.ConstMassMatrix".equals(hlp)) {
180  res.M = new ConstMassMatrix(r.readMatrix(mng.getInStream("M.bin")));
181  } else if ("dscomponents.AffLinMassMatrix".equals(hlp)) {
182  IAffineCoefficients coeff = (IAffineCoefficients) mng.loadModelClass(mng
183  .getModelXMLTagValue("massmatrix.coeffclass"));
184  AffParamMatrix a = new AffParamMatrix(r.readMatrix(mng.getInStream("B.bin")), res.dim, coeff);
185  res.B = new AffineInputConv(a);
186  throw new RuntimeException("Not yet fully implemented.");
187  }
188  }
189  }
190 
191  private static IKernel loadKernel(AModelManager mng, String typestr, String datafile) throws MathReaderException,
192  IOException {
193  IKernel res = null;
194  MathObjectReader r = new MathObjectReader();
195  String type = mng.getModelXMLTagValue(typestr);
196  if ("kernels.GaussKernel".equals(type)) {
197  double[] g = r.readRawDoubleVector(mng.getInStream(datafile));
198  res = new GaussKernel(g[0]);
199  } else if ("kernels.LinearKernel".equals(type)) {
200  res = new LinearKernel();
201  }
202  type = null;
203  r = null;
204  return res;
205  }
206 }
void setConfig(double[] mu, int InputIdx)
IInputFunctions u
Interface for input conversion matrices .
Definition: IInputConv.java:13
Implements a constant mass matrix of the dynamical system.
Interface for kernel implementations in JKerMor.
Definition: IKernel.java:14
Constant linear output conversion .
Base class for implementation of a series of time/parameter-dependent affine coefficients.
double[] evaluate(double t, int idx)
Reading matrices and vectors with a bunch of convenient overloads for different sources and output fo...
Implementation of the Gaussian kernel .
Interface for dynamical system input functions .
Interface for initial values of dynamical systems.
Constant linear dynamical system core function .
Interface for dynamical system mass matrices .
This class serves as base class for accessing various types of models at different locations...
RealMatrix readMatrix(InputStream in)
Reads a matrix from an InputStream, pointing to a binary file.
Represents a constant initial value for dynamical systems.
Class for time/parameter-affine dynamical system inputs .
This Exception gets thrown when an error occurs regarding the functionality of the ModelManager...
Implements a kernel expansion with optional time kernel and parameter kernel .
Interface for any dynamical system core function.
Definition: ICoreFun.java:11
static ReducedSystem load(AModelManager mng)
Affine parametric matrix class.
This class implements a time/parameter-affine linear core function .
Interface for dynamical system output conversion matrices .
Main reduced dynamical system class.
Constant linear input conversion .
Implements a linear scalar product kernel .
void computeDerivatives(double t, double[] x, double[] xDot)
Utility functions.
Definition: Util.java:15