JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
ReducedModel.java
Go to the documentation of this file.
1 package kermor;
2 
5 import jarmos.ModelBase;
6 import jarmos.Parameters;
8 import jarmos.Util;
14 
15 import java.io.IOException;
16 
17 import org.apache.commons.math.linear.Array2DRowRealMatrix;
18 import org.apache.commons.math.linear.RealMatrix;
19 import org.apache.commons.math.ode.DerivativeException;
20 import org.apache.commons.math.ode.FirstOrderIntegrator;
21 import org.apache.commons.math.ode.nonstiff.EulerIntegrator;
22 import org.apache.commons.math.ode.sampling.FixedStepHandler;
23 import org.apache.commons.math.ode.sampling.StepNormalizer;
24 
33 public class ReducedModel extends ModelBase implements FixedStepHandler {
34 
36 
37  public FirstOrderIntegrator integrator;
38 
39  public String name;
40 
42 
43  private IOutputToDoF converter;
44 
45  private double[] times;
46 
47  private int cnt;
48  private RealMatrix output;
49  private double[] mu;
50  private double dt = .1;
51  private double T = 1;
52 
53  public void simulate(double[] mu, int input) throws KerMorException {
54  if (mu == null && params != null) {
55  throw new KerMorException("Simulation without a parameter when parameters are configured are not allowed.");
56  }
57  cnt = 0;
58  double[] out = new double[system.getDimension()];
59  output = new Array2DRowRealMatrix(system.C.getOutputDimension(), times.length);
60  // output = new double[times.length][];
61  this.mu = mu;
62  this.system.setConfig(mu, input);
63 
64  try {
65  integrator.integrate(system, 0, system.x0.evaluate(mu), T, out);
66  } catch (Exception e) {
67  throw new KerMorException("Simulation failed due to an exception:" + e.getMessage(), e);
68  }
69  }
70 
71  public double[][] getOutput() {
72  return output.getData();
73  }
74 
76  double[][] dof_fields = converter.transformOutputToDoFs(output.getData());
77 
78  SimulationResult res = new SimulationResult(times.length);
79  // Add default transforms for each timestep (TODO improve; use only
80  // some transforms and an transform-index)
81  for (int i = 0; i < times.length; i++) {
82  res.addTransform(new DefaultTransform());
83  }
84 
85  int fnumcnt = 0;
86  for (FieldDescriptor sftype : logicalFieldTypes) {
87  if (fnumcnt + sftype.Type.requiredDoFFields > getNumDoFFields()) {
88  throw new RuntimeException("Too many output fields used by current "
89  + "SolutionFieldTypes set in RBSystem. Check your model.xml.");
90  }
91  int numDoF = dof_fields[fnumcnt].length;
92  switch (sftype.Type) {
93  case Displacement3D: {
94  DisplacementField d = new DisplacementField(sftype, numDoF);
95  for (int nodenr = 0; nodenr < numDoF; nodenr++) {
96  d.setDisplacement(nodenr, (float) dof_fields[fnumcnt][nodenr],
97  (float) dof_fields[fnumcnt + 1][nodenr], (float) dof_fields[fnumcnt + 2][nodenr]);
98  }
99  res.addField(d);
100  break;
101  }
102  case RealValue: {
103  DefaultSolutionField d = new DefaultSolutionField(sftype, numDoF);
104  for (int nodenr = 0; nodenr < numDoF; nodenr++) {
105  d.setValue(nodenr, (float) dof_fields[fnumcnt][nodenr]);
106  }
107  res.addField(d);
108  break;
109  }
110  default: {
111  throw new RuntimeException("Invalid/unimplemented solution field type '" + sftype + "'");
112  }
113  }
114  /*
115  * Increase field counter by the amount the current solution field
116  * used
117  */
118  fnumcnt += sftype.Type.requiredDoFFields;
119  }
120  return res;
121  }
122 
123  public double getT() {
124  return T;
125  }
126 
127  public void setT(double value) {
128  this.T = value;
129  this.times = Util.range(0, dt, T);
130  }
131 
132  public double getdt() {
133  return dt;
134  }
135 
136  public void setdt(double value) {
137  this.dt = value;
138  this.times = Util.range(0, dt, T);
139  }
140 
141  public double[] getTimes() {
142  return times;
143  }
144 
145  @Override
147  super.loadOfflineData(mng);
148 
149  String hlp = mng.getModelXMLTagValue("kermor_model.dt");
150  setdt(Double.parseDouble(hlp));
151  hlp = mng.getModelXMLTagValue("T");
152  setT(Double.parseDouble(hlp));
153  name = mng.getModelXMLAttribute("title");
154 
155  params = mng.getParameters();
156 
157  // Load the system
158  system = ReducedSystem.load(mng);
159 
160  // The ODE integrator
161  hlp = mng.getModelXMLTagValue("kermor_model.solvertype");
162  if ("implicit".equals(hlp)) {
164  // res.integrator = new AdamsMoultonIntegrator(3, 1e-10*res.dt,
165  // res.dt, 1e-4, 1e-3);
166  } else {
167  integrator = new EulerIntegrator(dt);
168  }
169  integrator.addStepHandler(new StepNormalizer(dt, this));
170 
171  // Check if a custom conversion of output data to simulation result
172  // has been implemented & attached
173  if (mng.xmlTagExists("kermor_model.outputtodof")) {
174  converter = (IOutputToDoF) mng.loadModelClass(mng.getModelXMLTagValue("kermor_model.outputtodof"));
175  } else {
176  converter = new DefaultOutputToDoFs();
177  }
178  }
179 
180  // //////// FixedStepHandler methods
181  @Override
182  public void handleStep(double t, double[] x, double[] xDot, boolean isLast) throws DerivativeException {
183  // output[cnt++] = system.C.evaluate(t, x, mu);
184  output.setColumn(cnt++, system.C.evaluate(t, x, mu));
185  }
186 
187  // ////////// StepHandler methods
188  // @Override
189  // public boolean requiresDenseOutput() {
190  // return false;
191  // }
192  //
193  // @Override
194  // public void reset() {
195  // cnt = 0;
196  // }
197  //
198  // @Override
199  // public void handleStep(StepInterpolator interpolator, boolean isLast)
200  // throws DerivativeException {
201  // double t = interpolator.getInterpolatedTime();
202  // double[] x = interpolator.getInterpolatedState();
203  // output.setColumn(cnt++, system.C.evaluate(t, x, mu));
204  // }
205 
206 }
The displacement field is a logical solution field describing displacements of geometry nodes/vertice...
int getNumDoFFields()
Returns the number of degree-of-freedom fields generated/computed by the model.
Definition: ModelBase.java:78
void setdt(double value)
The default solution field containing an array of real values.
Represents the results of a simulation.
void handleStep(double t, double[] x, double[] xDot, boolean isLast)
FirstOrderIntegrator integrator
void simulate(double[] mu, int input)
Main reduced model class.
This class serves as base class for accessing various types of models at different locations...
A class for model parameters.
Definition: Parameters.java:17
Custom exception for JKerMor related errors.
Custom implicit euler integrator class.
double[][] getOutput()
Interface for post-simulation output to DoF conversion.
void setT(double value)
void loadOfflineData(AModelManager mng)
FieldDescriptor[] logicalFieldTypes
The logical output fields of the model, each collecting one ore more model DoF&#39;s into a related unit...
Definition: ModelBase.java:26
SimulationResult getSimulationResult()
This Exception gets thrown when an error occurs regarding the functionality of the ModelManager...
Main reduced dynamical system class.
Default mesh transformation.
Default implementation for output to DoF conversion.
Contains information about the logical solution fields.
Base class for all JaRMoSBase models.
Definition: ModelBase.java:18
ReducedSystem system
Utility functions.
Definition: Util.java:15