29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.lang.reflect.Array;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
36 import org.apache.commons.math.complex.Complex;
37 import org.apache.commons.math.linear.Array2DRowFieldMatrix;
38 import org.apache.commons.math.linear.ArrayFieldVector;
39 import org.apache.commons.math.linear.FieldMatrix;
40 import org.apache.commons.math.linear.FieldVector;
51 private static final String DEBUG_TAG =
"ComplexRBSystem";
79 BufferedReader reader = m.getBufReader(
"n_bfs.dat");
86 Log.d(DEBUG_TAG,
"Finished reading n_bfs.dat");
96 String[] dual_norms_tokens;
98 BufferedReader reader = m
99 .getBufReader(
"output_" + String.format(
"%03d", i) +
"_dual_norms.dat");
101 dual_norms_tokens = reader.readLine().split(
" ");
108 for (
int q = 0; q < Q_l_hat; q++) {
110 Double.parseDouble(dual_norms_tokens[Q_l_hat + q]));
116 String[] output_i_tokens;
117 for (
int q_l = 0; q_l <
getQl(i); q_l++) {
120 BufferedReader reader_i = m.getBufReader(
"output_" + String.format(
"%03d", i) +
"_"
121 + String.format(
"%03d", q_l) +
".dat");
123 output_i_tokens = reader_i.readLine().split(
" ");
128 for (
int j = 0; j <
getNBF(); j++) {
131 new Complex(Double.parseDouble(output_i_tokens[j]), Double
132 .parseDouble(output_i_tokens[
getNBF() + j])));
138 Log.d(DEBUG_TAG,
"Finished reading output data");
140 Log.d(DEBUG_TAG,
"No output data set. (get_n_outputs() == 0)");
170 for (
int q_f = 0; q_f <
getQf(); q_f++) {
172 BufferedReader reader = m.getBufReader(
"RB_F_" + String.format(
"%03d", q_f) +
".dat");
174 tokens = reader.readLine().split(
" ");
182 for (
int i = 0; i <
getNBF(); i++) {
184 new Complex(Double.parseDouble(tokens[i]), Double.parseDouble(tokens[
getNBF() + i])));
187 Log.d(DEBUG_TAG,
"Finished reading RB_F_q data");
192 RB_A_q_vector = (FieldMatrix<Complex>[]) Array.newInstance(FieldMatrix.class,
getQa());
196 for (
int q_a = 0; q_a <
getQa(); q_a++) {
198 BufferedReader reader = m.getBufReader(
"RB_A_" + String.format(
"%03d", q_a) +
".dat");
199 tokens = reader.readLine().split(
" ");
204 RB_A_q_vector[q_a] =
new Array2DRowFieldMatrix<Complex>((
new Complex(0, 0)).getField(),
getNBF(),
209 for (
int i = 0; i <
getNBF(); i++)
210 for (
int j = 0; j <
getNBF(); j++) {
214 new Complex(Double.parseDouble(tokens[count]), Double.parseDouble(tokens[count
219 Log.d(DEBUG_TAG,
"Finished reading RB_A_q data");
224 BufferedReader reader = m.getBufReader(
"Fq_norms.dat");
226 String[] tokens = reader.readLine().split(
" ");
234 for (
int i = 0; i < Q_f_hat; i++) {
236 Double.parseDouble(tokens[i * 2 + 1]));
239 Log.d(DEBUG_TAG,
"Finished reading Fq_norms.dat");
244 BufferedReader reader = m.getBufReader(
"Fq_Aq_norms.dat");
246 String[] tokens = reader.readLine().split(
" ");
257 for (
int q_f = 0; q_f <
getQf(); q_f++)
258 for (
int q_a = 0; q_a <
getQa(); q_a++) {
259 for (
int i = 0; i <
getNBF(); i++) {
260 Rdata[q_f][q_a][i] = Double.parseDouble(tokens[count]);
263 for (
int i = 0; i <
getNBF(); i++) {
264 Idata[q_f][q_a][i] = Double.parseDouble(tokens[count]);
269 for (
int q_f = 0; q_f <
getQf(); q_f++)
270 for (
int q_a = 0; q_a <
getQa(); q_a++)
271 for (
int i = 0; i <
getNBF(); i++)
274 Log.d(DEBUG_TAG,
"Finished reading Fq_Aq_norms.dat");
285 double[][] Rdata2 = null, Idata2 = null;
286 for (
int i = 0; i <
getQa(); i++)
287 for (
int j = i; j <
getQa(); j++) {
288 String file =
"Aq_Aq_" + String.format(
"%03d", i) +
"_" + String.format(
"%03d", j) +
"_norms.bin";
291 InputStream in = m.getInStream(file);
293 Rdata2 = mr.readRawDoubleMatrix(in, n, n);
294 Idata2 = mr.readRawDoubleMatrix(in, n, n);
295 }
catch (IOException io) {
296 Log.e(
"ComplexRBSystem",
"IOException with file " + file, io);
301 for (
int k = 0; k < n; k++)
302 for (
int l = 0; l < n; l++)
307 Log.d(DEBUG_TAG,
"Finished reading Aq_Aq_norms.dat");
329 float[] Rdata3, Idata3;
330 for (
int q_uL = 0; q_uL <
getQuL(); q_uL++) {
331 InputStream in = m.getInStream(
"uL_" + String.format(
"%03d", q_uL) +
".bin");
333 Rdata3 = mr.readRawFloatVector(in, n);
334 Idata3 = mr.readRawFloatVector(in, n);
338 for (
int i = 0; i < n; i++)
339 uL_vector[q_uL][i] =
new Complex(Rdata3[i], Idata3[i]);
342 Log.d(DEBUG_TAG,
"Finished reading uL.dat");
349 float[] Rdata3, Idata3;
351 for (
int inbfs = 0; inbfs <
getNBF(); inbfs++) {
352 InputStream in = m.getInStream(
"Z_" + String.format(
"%03d", imf) +
"_"
353 + String.format(
"%03d", inbfs) +
".bin");
355 Rdata3 = mr.readRawFloatVector(in, n);
356 Idata3 = mr.readRawFloatVector(in, n);
360 for (
int i = 0; i < n; i++)
361 Z_vector[imf][inbfs][i] =
new Complex(Rdata3[i], Idata3[i]);
364 Log.d(DEBUG_TAG,
"Finished reading Z.dat");
384 throw new RuntimeException(
"ERROR: N cannot be larger than the number " +
"of basis functions in RB_solve");
387 throw new RuntimeException(
"ERROR: N must be greater than 0 in RB_solve");
391 FieldMatrix<Complex> RB_system_matrix_N =
new Array2DRowFieldMatrix<Complex>((
new Complex(0, 0)).getField(), N,
394 for (
int q_a = 0; q_a <
getQa(); q_a++) {
395 RB_system_matrix_N = RB_system_matrix_N.add(
RB_A_q_vector[q_a].getSubMatrix(0, N - 1, 0, N - 1)
396 .scalarMultiply(
theta_a.getEntry(q_a)));
401 FieldVector<Complex> RB_rhs_N =
new ArrayFieldVector<Complex>(N,
new Complex(0d, 0d));
403 for (
int q_f = 0; q_f <
getQf(); q_f++) {
412 RB_solution =
new ArrayFieldVector<Complex>(N,
new Complex(0., 0.));
413 for (
int j = 1; j < N; j++)
414 for (
int i = j; i < N; i++) {
415 Complex m = RB_system_matrix_N.getEntry(i, j - 1).divide(RB_system_matrix_N.getEntry(j - 1, j - 1));
416 for (
int k = 0; k < N; k++)
417 RB_system_matrix_N.setEntry(
420 RB_system_matrix_N.getEntry(i, k).subtract(
421 RB_system_matrix_N.getEntry(j - 1, k).multiply(m)));
422 RB_rhs_N.setEntry(i, RB_rhs_N.getEntry(i).subtract(m.multiply(RB_rhs_N.getEntry(j - 1))));
424 RB_solution.setEntry(N - 1, RB_rhs_N.getEntry(N - 1).divide(RB_system_matrix_N.getEntry(N - 1, N - 1)));
425 for (
int j = N - 2; j >= 0; j--) {
426 Complex m =
new Complex(0., 0.);
427 for (
int i = j + 1; i < N; i++)
428 m = m.add(RB_system_matrix_N.getEntry(j, i).multiply(RB_solution.getEntry(i)));
429 RB_solution.setEntry(j, (RB_rhs_N.getEntry(j).subtract(m)).divide(RB_system_matrix_N.getEntry(j, j)));
452 double RB_solution_norm = 0.0d;
453 for (
int i = 0; i < N; i++)
454 RB_solution_norm += ((
RB_solution.getEntry(i)).multiply((
RB_solution.getEntry(i)).conjugate())).getReal();
455 RB_solution_norm = Math.sqrt(RB_solution_norm);
458 FieldVector<Complex> RB_output_vector_N =
new ArrayFieldVector<Complex>(N,
new Complex(0d, 0d));
463 for (
int q_l = 1; q_l <
getQl(i); q_l++)
464 RB_output_vector_N = RB_output_vector_N.add((
RB_output_vectors[i][q_l].getSubVector(0, N))
466 for (
int j = 0; j < N; j++)
467 RB_outputs[i] =
RB_outputs[i].add((RB_output_vector_N.getEntry(j).conjugate()).multiply((RB_solution
500 for (
int q_f1 = 0; q_f1 <
getQf(); q_f1++) {
501 for (
int q_f2 = q_f1; q_f2 <
getQf(); q_f2++) {
502 double delta = (q_f1 == q_f2) ? 1. : 2.;
510 for (
int q_f = 0; q_f <
getQf(); q_f++) {
511 for (
int q_a = 0; q_a <
getQa(); q_a++) {
512 for (
int i = 0; i < N; i++) {
522 for (
int q_a1 = 0; q_a1 <
getQa(); q_a1++) {
523 for (
int q_a2 = q_a1; q_a2 <
getQa(); q_a2++) {
524 for (
int i = 0; i < N; i++) {
525 for (
int j = 0; j < N; j++) {
526 double delta = (q_a1 == q_a2) ? 1. : 2.;
531 (theta_a.getEntry(q_a1).conjugate().multiply(theta_a.getEntry(q_a2))))
539 double residual_norm_sq = res_ff + res_af + res_aa;
541 if (residual_norm_sq < 0.) {
545 residual_norm_sq = Math.abs(residual_norm_sq);
548 return Math.sqrt(residual_norm_sq);
560 double output_norm_sq = 0.;
563 for (
int q_l1 = 0; q_l1 <
getQl(i); q_l1++) {
564 for (
int q_l2 = q_l1; q_l2 <
getQl(i); q_l2++) {
575 return Math.sqrt(output_norm_sq);
579 return RB_solution.getEntry(i);
584 Complex[] c = RB_solution.toArray();
585 double[][] d =
new double[2][c.length];
586 for (
int i = 0; i < c.length; i++) {
587 d[0][i] = c[i].getReal();
588 d[1][i] = c[i].getImaginary();
620 int N = RB_solution.getDimension();
626 throw new RuntimeException(
"Too many output fields used by current "
627 +
"SolutionFieldTypes set in RBSystem. Check your model.xml.");
629 int numDoF =
Z_vector[fnumcnt][0].length;
630 switch (sftype.Type) {
634 for (
int nodenr = 0; nodenr < numDoF; nodenr++) {
635 tmpval =
new Complex(0., 0.);
636 for (
int rbdim = 0; rbdim < N; rbdim++) {
639 f.setComplexValue(nodenr, tmpval);
642 for (
int q_uL = 0; q_uL <
getQuL(); q_uL++)
643 for (
int i = 0; i < numDoF; i++) {
644 f.addComplexValue(i, (float)
uL_vector[q_uL][i].getReal(),
645 (float)
uL_vector[q_uL][i].getImaginary());
652 throw new RuntimeException(
"Invalid/unimplemented solution field type '" + sftype.Type
653 +
"' for complex RB system");
659 fnumcnt += sftype.Type.requiredDoFFields;
670 int numSweep = RB_sweep_solution.length;
675 Complex[][] RB_sweep_sol =
new Complex[numSweep][N];
676 for (
int i = 0; i < numSweep; i++)
677 for (
int j = 0; j < N; j++)
683 throw new RuntimeException(
"Too many output fields used by current "
684 +
"SolutionFieldTypes set in RBSystem. Check your model.xml.");
686 int numDoF =
Z_vector[fnumcnt][0].length;
687 switch (sftype.Type) {
691 for (
int iSweep = 0; iSweep < numSweep; iSweep++) {
692 for (
int i = 0; i < numDoF; i++) {
693 tmpval =
new Complex(0., 0.);
694 for (
int rbdim = 0; rbdim < N; rbdim++) {
695 tmpval = tmpval.add(
Z_vector[fnumcnt][rbdim][i].multiply(RB_sweep_sol[iSweep][rbdim]));
697 f.setComplexValue(iSweep * numDoF + i, tmpval);
701 for (
int q_uL = 0; q_uL <
getQuL(); q_uL++)
702 for (
int iSweep = 0; iSweep < numSweep; iSweep++)
703 for (
int nodenr = 0; nodenr < numDoF; nodenr++) {
704 f.addComplexValue(iSweep * numDoF + nodenr,
uL_vector[q_uL][nodenr]);
711 throw new RuntimeException(
"Invalid/unimplemented solution field type '" + sftype.Type
712 +
"' for complex RB system sweep");
718 fnumcnt += sftype.Type.requiredDoFFields;
730 Class<?> partypes[] = null;
731 meth = oldAffFcnCl.getMethod(
"is_derived_output", partypes);
732 }
catch (NoSuchMethodException nsme) {
737 Object arglist[] = null;
739 boolean val = (Boolean) theta_obj;
741 }
catch (IllegalAccessException iae) {
742 throw new RuntimeException(iae);
743 }
catch (InvocationTargetException ite) {
744 throw new RuntimeException(ite.getCause());
754 Class<?> partypes[] =
new Class[1];
755 partypes[0] =
double[].class;
757 meth = oldAffFcnCl.getMethod(
"cal_derived_output", partypes);
758 }
catch (NoSuchMethodException nsme) {
759 throw new RuntimeException(
"getMethod for cal_derived_output failed", nsme);
764 Object arglist[] =
new Object[1];
765 double[] input =
new double[4];
775 double[] output = (
double[]) theta_obj;
776 RB_outputs[i] =
new Complex(output[0], output[1]);
779 }
catch (IllegalAccessException iae) {
780 throw new RuntimeException(iae);
781 }
catch (InvocationTargetException ite) {
782 throw new RuntimeException(ite.getCause());
Complex[][] output_dual_norms
double get_RB_output_error_bound(int n_output, boolean Rpart)
GeometryData getGeometry()
The model's geometry data.
Complex complex_eval_theta_q_f(int q)
int getNumDoFFields()
Returns the number of degree-of-freedom fields generated/computed by the model.
double[][] get_RBsolution()
FieldVector< Complex > theta_a
This class provides the Online stage for the reduced basis method for elliptic steady state problems...
Represents the results of a simulation.
FieldMatrix< Complex >[] RB_A_q_vector
Complex[] Fq_representor_norms
Reading matrices and vectors with a bunch of convenient overloads for different sources and output fo...
Complex[][][] Aq_Aq_representor_norms
double compute_output_dual_norm(int i, double t)
Complex[] RB_output_error_bounds
boolean is_derived_output()
boolean return_rel_error_bound
Boolean flag to indicate whether RB_solve returns an absolute or relative error bound.
FieldVector< Complex > complex_eval_theta_q_a()
void initialize_data_vectors()
void set_n_basis_functions(int _N)
This class serves as base class for accessing various types of models at different locations...
FieldVector< Complex > RB_solution
int getQf()
TODO: if all affine_functions implement the IAffineFunctions interface, just call the getQf method of...
FieldVector< Complex >[][] RB_output_vectors
FieldDescriptor[] logicalFieldTypes
The logical output fields of the model, each collecting one ore more model DoF's into a related unit...
FieldVector< Complex >[] RB_F_q_vector
SimulationResult getSimulationResults()
Returns the results of the complex RB simulation.
Provides a Log class imitating the Android Log class when not used on android systems.
double get_RB_output(int n_output, boolean Rpart)
RB system class for complex-valued fields.
Complex[][][] Fq_Aq_representor_norms
Complex get_complex_soln_coeff(int i)
double get_SCM_upper_bound()
double[][][] RB_sweep_solution
A solution field with complex values.
double residual_scaling_denom(double alpha_LB)
Specifies the residual scaling on the denominator to be used in the a posteriori error bound...
void loadOfflineData_rbappmit(AModelManager m)
Complex complex_eval_theta_q_l(int n, int q)
List< MeshTransform > transforms
SimulationResult getSweepSimResults()
Contains information about the logical solution fields.
double compute_residual_dual_norm(int N)
int getQl(int output_index)
TODO: if all affine_functions implement the IAffineFunctions interface, just call the getQl method of...
double get_SCM_lower_bound()
void cal_derived_output()