JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
QNTransientSCMSystem.java
Go to the documentation of this file.
1 package rb;
2 
3 import jarmos.Log;
5 
6 import java.io.BufferedReader;
7 import java.io.IOException;
8 import java.lang.reflect.InvocationTargetException;
9 import java.lang.reflect.Method;
10 
11 import org.apache.commons.math.linear.RealVector;
12 
22 public class QNTransientSCMSystem extends RBSCMSystem {
23 
25  super(sys);
26  }
27 
28  // Logging tag
29  private static final String DEBUG_TAG = "QNTransientSCMSystem";
30 
34  private int n_bfs;
35 
39  private double[][] C_J_RB_coeffs;
40 
44  private RealVector current_RB_coeffs;
45 
46  // We also need to save the RB coefficients during LB calculations
47  private RealVector saved_RB_coeffs;
48 
49  // We may need to scale the theta_c function for the sake of the SCM!
50  private double SCM_theta_c_scaling;
51 
56  return n_bfs;
57  }
58 
59  void set_n_basis_functions(int n_bfs_in) {
60  n_bfs = n_bfs_in;
61  }
62 
66  void set_current_RB_coeffs(RealVector RB_coeffs) {
67  current_RB_coeffs = RB_coeffs;
68  }
69 
73  public double eval_theta_c() {
74 
75  Method meth;
76 
77  try {
78  Class<?> partypes[] = new Class[1];
79  partypes[0] = double[].class;
80 
81  meth = sys.oldAffFcnCl.getMethod("evaluateC", partypes);
82  } catch (NoSuchMethodException nsme) {
83  throw new RuntimeException("getMethod for evaluateC failed", nsme);
84  }
85 
86  Double theta_val;
87  try {
88  Object arglist[] = new Object[1];
89  arglist[0] = sys.getParams().getCurrent();
90 
91  Object theta_obj = meth.invoke(sys.oldAffFcnObj, arglist);
92  theta_val = (Double) theta_obj;
93  } catch (IllegalAccessException iae) {
94  throw new RuntimeException(iae);
95  } catch (InvocationTargetException ite) {
96  throw new RuntimeException(ite.getCause());
97  }
98 
99  return SCM_theta_c_scaling * theta_val.doubleValue();
100  }
101 
105  @Override
106  public int getQa() {
107  return super.getQa() + get_n_basis_functions();
108  }
109 
113  @Override
114  public double thetaQa(int q) {
115 
116  if (q < get_n_basis_functions()) {
117  double theta_c_value = eval_theta_c();
118  return current_RB_coeffs.getEntry(q) * theta_c_value;
119  } else {
120 
121  Method meth;
122 
123  try {
124  Class<?> partypes[] = new Class[2];
125  partypes[0] = Integer.TYPE;
126  partypes[1] = double[].class;
127 
128  meth = sys.oldAffFcnCl.getMethod("evaluateA", partypes);
129  } catch (NoSuchMethodException nsme) {
130  throw new RuntimeException("getMethod for evaluateA failed", nsme);
131  }
132 
133  Double theta_val;
134  try {
135  Object arglist[] = new Object[2];
136  arglist[0] = new Integer(q - get_n_basis_functions());
137  arglist[1] = sys.getParams().getCurrent();
138 
139  Object theta_obj = meth.invoke(sys.oldAffFcnObj, arglist);
140  theta_val = (Double) theta_obj;
141  } catch (IllegalAccessException iae) {
142  throw new RuntimeException(iae);
143  } catch (InvocationTargetException ite) {
144  throw new RuntimeException(ite.getCause());
145  }
146 
147  return theta_val.doubleValue();
148  }
149  }
150 
154  @Override
155  protected void get_current_parameters_from_C_J(int index) {
156  super.get_current_parameters_from_C_J(index);
157 
158  for (int i = 0; i < get_n_basis_functions(); i++)
159  current_RB_coeffs.setEntry(i, C_J_RB_coeffs[index][i]);
160  }
161 
165  @Override
166  protected void save_current_parameters() {
167  super.save_current_parameters();
168 
169  saved_RB_coeffs = current_RB_coeffs.copy();
170  }
171 
175  @Override
176  protected void reload_current_parameters() {
177  super.reload_current_parameters();
178 
179  set_current_RB_coeffs(saved_RB_coeffs);
180  }
181 
185  @Override
186  public void loadOfflineData(AModelManager m) throws IOException, InconsistentStateException {
187 
188  // Initially set number of basis functions from n_bfs.dat
189  {
190  BufferedReader reader = m.getBufReader("n_bfs.dat");
191 
192  String line = reader.readLine();
193  reader.close();
194  reader = null;
195 
196  n_bfs = Integer.parseInt(line);
197 
198  Log.d(DEBUG_TAG, "Finished reading n_bfs.dat");
199  }
200 
201  super.loadOfflineData(m);
202 
203  // Read C_J_RB_coeffs
204  {
205  C_J_RB_coeffs = new double[C_J_stability_vector.length][get_n_basis_functions()];
206  if (C_J_stability_vector != null) {
207  String[] tokens;
208  {
209  BufferedReader reader = m.getBufReader("C_J_RB_coeffs.dat");
210  String line = reader.readLine();
211  reader.close();
212  reader = null;
213  tokens = line.split(" ");
214  }
215 
216  int count = 0;
217  for (int i = 0; i < C_J_stability_vector.length; i++) {
218  for (int j = 0; j < get_n_basis_functions(); j++) {
219  C_J_RB_coeffs[i][j] = Double.parseDouble(tokens[count]);
220  count++;
221  }
222  }
223  }
224  Log.d(DEBUG_TAG, "Finished reading C_J_RB_coeffs.dat");
225  }
226  }
227 
233  @Override
234  public void readConfiguration(AModelManager m) throws IOException {
235  super.readConfiguration(m);
236  GetPot infile = new GetPot(m.getInStream(Const.parameters_filename), Const.parameters_filename);
237  SCM_theta_c_scaling = infile.call("SCM_theta_c_scaling", 1.);
238  Log.d(DEBUG_TAG, "SCM_theta_c_scaling = " + SCM_theta_c_scaling);
239  }
240 
241 }
void save_current_parameters()
Override to also save the RB coefficients.
void set_n_basis_functions(int n_bfs_in)
Base class for quadratically nonlinear RB systems including the SCM method for error bound computatio...
void loadOfflineData(AModelManager m)
Override read_offline_data in order to read in the extra data in the QNTransient case.
Utility helper class from rbAppMIT included for compatibility of rbAppMIT model loading.
Definition: GetPot.java:31
double eval_theta_c()
Evaluate theta_c (for the quadratic nonlinearity) at the current parameter.
This class provides the Online stage for the reduced basis method for elliptic steady state problems...
Definition: RBSystem.java:54
void set_current_RB_coeffs(RealVector RB_coeffs)
Set the current RB coefficients.
int get_n_basis_functions()
Get/set the number of basis functions.
void reload_current_parameters()
Override to also load the RB coefficients.
This class implements the Online stage of the Successive Constraint Method for coercive problems...
This class serves as base class for accessing various types of models at different locations...
void readConfiguration(AModelManager m)
Provides a Log class imitating the Android Log class when not used on android systems.
Definition: Log.java:11
double[] C_J_stability_vector
The values of the stability factor at the greedily selected parameters.
static final String parameters_filename
Inherited from the rbAppMIT models to read the model parameters.
Definition: Const.java:13
double thetaQa(int q)
Override eval_theta_q_a in order to account for the affine terms related to basis functions...
int getQa()
Override get_Q_a since we have n_bfs extra terms.
Exception imported from rbappmit.
Class with constants used throughout JRB.
Definition: Const.java:9
void get_current_parameters_from_C_J(int index)
Override to also load the RB coefficients.