25#include "ReducedUnsteadyMSR.H"
28reducedusMSR::reducedusMSR() {}
32 problem = & FOMproblem;
33 N_BC = problem->inletIndex.rows();
34 N_BCt = problem->inletIndexT.rows();
35 Nphi_u = problem->B_matrix.rows();
36 Nphi_p = problem->K_matrix.cols();
37 Nphi_flux = problem->LF_matrix[0].cols();
38 Nphi_prec1 = problem->PS1_matrix.cols();
39 Nphi_prec2 = problem->PS2_matrix.cols();
40 Nphi_prec3 = problem->PS3_matrix.cols();
41 Nphi_prec4 = problem->PS4_matrix.cols();
42 Nphi_prec5 = problem->PS5_matrix.cols();
43 Nphi_prec6 = problem->PS6_matrix.cols();
44 Nphi_prec7 = problem->PS7_matrix.cols();
45 Nphi_prec8 = problem->PS8_matrix.cols();
46 Nphi_T = problem->LT_matrix.rows();
47 Nphi_dec1 = problem->LD1_matrix.rows();
48 Nphi_dec2 = problem->LD2_matrix.rows();
49 Nphi_dec3 = problem->LD3_matrix.rows();
50 Nphi_const = problem->PF_matrix[0].rows();
54 for (
int k = 0; k < problem->liftfield.size(); k++)
56 Umodes.append((problem->liftfield[k]).clone());
59 for (
int k = 0; k < problem->NUmodes; k++)
61 Umodes.append((problem->Umodes[k]).clone());
64 for (
int k = 0; k < problem->NPmodes; k++)
66 Pmodes.append((problem->Pmodes[k]).clone());
69 for (
int k = 0; k < problem->NFluxmodes; k++)
71 Fluxmodes.append((problem->Fluxmodes[k]).clone());
74 for (
int k = 0; k < Nphi_prec1; k++)
76 Prec1modes.append((problem->Prec1modes[k]).clone());
79 for (
int k = 0; k < Nphi_prec2; k++)
81 Prec2modes.append((problem->Prec2modes[k]).clone());
84 for (
int k = 0; k < Nphi_prec3; k++)
86 Prec3modes.append((problem->Prec3modes[k]).clone());
89 for (
int k = 0; k < Nphi_prec4; k++)
91 Prec4modes.append((problem->Prec4modes[k]).clone());
94 for (
int k = 0; k < Nphi_prec5; k++)
96 Prec5modes.append((problem->Prec5modes[k]).clone());
99 for (
int k = 0; k < Nphi_prec6; k++)
101 Prec6modes.append((problem->Prec6modes[k]).clone());
104 for (
int k = 0; k < Nphi_prec7; k++)
106 Prec7modes.append((problem->Prec7modes[k]).clone());
109 for (
int k = 0; k < Nphi_prec8; k++)
111 Prec8modes.append((problem->Prec8modes[k]).clone());
114 for (
int k = 0; k < problem->liftfieldT.size(); k++)
116 Tmodes.append((problem->liftfieldT[k]).clone());
119 for (
int k = 0; k < problem->NTmodes; k++)
121 Tmodes.append((problem->Tmodes[k]).clone());
124 for (
int k = 0; k < Nphi_dec1; k++)
126 Dec1modes.append((problem->Dec1modes[k]).clone());
129 for (
int k = 0; k < Nphi_dec2; k++)
131 Dec2modes.append((problem->Dec2modes[k]).clone());
134 for (
int k = 0; k < Nphi_dec3; k++)
136 Dec3modes.append((problem->Dec3modes[k]).clone());
139 for (
int k = 0; k < Nphi_const; k++)
141 vmodes.append((problem->vmodes[k]).clone());
142 Dmodes.append((problem->Dmodes[k]).clone());
143 NSFmodes.append((problem->NSFmodes[k]).clone());
144 Amodes.append((problem->Amodes[k]).clone());
145 SPmodes.append((problem->SPmodes[k]).clone());
146 TXSmodes.append((problem->TXSmodes[k]).clone());
150 for (
int k = 0; k < problem->Ufield.size(); k++)
152 Usnapshots.append((problem->Ufield[k]).clone());
153 Psnapshots.append((problem->Pfield[k]).clone());
154 Fluxsnapshots.append((problem->Fluxfield[k]).clone());
155 Prec1snapshots.append((problem->Prec1field[k]).clone());
156 Prec2snapshots.append((problem->Prec2field[k]).clone());
157 Prec3snapshots.append((problem->Prec3field[k]).clone());
158 Prec4snapshots.append((problem->Prec4field[k]).clone());
159 Prec5snapshots.append((problem->Prec5field[k]).clone());
160 Prec6snapshots.append((problem->Prec6field[k]).clone());
161 Prec7snapshots.append((problem->Prec7field[k]).clone());
162 Prec8snapshots.append((problem->Prec8field[k]).clone());
163 Tsnapshots.append((problem->Tfield[k]).clone());
164 Dec1snapshots.append((problem->Dec1field[k]).clone());
165 Dec2snapshots.append((problem->Dec2field[k]).clone());
166 Dec3snapshots.append((problem->Dec3field[k]).clone());
167 vsnapshots.append((problem->vFields[k]).clone());
168 Dsnapshots.append((problem->DFields[k]).clone());
169 NSFsnapshots.append((problem->NSFFields[k]).clone());
170 Asnapshots.append((problem->AFields[k]).clone());
171 SPsnapshots.append((problem->SPFields[k]).clone());
172 TXSsnapshots.append((problem->TXSFields[k]).clone());
175 newton_object_fd = newton_usmsr_fd(
Nphi_u + Nphi_p,
Nphi_u + Nphi_p,
177 newton_object_n = newton_usmsr_n(Nphi_flux + Nphi_prec1 + Nphi_prec2 +
178 Nphi_prec3 + Nphi_prec4 + Nphi_prec5 + Nphi_prec6 + Nphi_prec7 + Nphi_prec8,
179 Nphi_flux + Nphi_prec1 + Nphi_prec2 + Nphi_prec3 + Nphi_prec4 + Nphi_prec5 +
180 Nphi_prec6 + Nphi_prec7 + Nphi_prec8, FOMproblem);
181 newton_object_t = newton_usmsr_t(Nphi_T + Nphi_dec1 + Nphi_dec2 + Nphi_dec3,
182 Nphi_T + Nphi_dec1 + Nphi_dec2 + Nphi_dec3, FOMproblem);
186 Eigen::VectorXd& fvec)
const
188 Eigen::VectorXd a_tmp(Nphi_u);
189 Eigen::VectorXd b_tmp(Nphi_p);
190 a_tmp = x.head(Nphi_u);
191 b_tmp = x.tail(Nphi_p);
192 Eigen::VectorXd a_dot(Nphi_u);
193 a_dot = (x.head(Nphi_u) - y_old.head(Nphi_u)) / dt;
196 Eigen::MatrixXd cc(1, 1);
197 Eigen::MatrixXd gg(1, 1);
198 Eigen::MatrixXd bb(1, 1);
200 Eigen::VectorXd M1 = problem->B_matrix * a_tmp * nu;
202 Eigen::VectorXd M2 = problem->K_matrix * b_tmp;
204 Eigen::VectorXd M5 = problem->M_matrix * a_dot;
206 Eigen::VectorXd M3 = problem->D_matrix * b_tmp;
208 Eigen::VectorXd M6 = problem->BC1_matrix * a_tmp * nu;
210 Eigen::VectorXd M7 = problem->BC3_matrix * a_tmp * nu;
212 for (
int i = 0; i < Nphi_u; i++)
214 cc = a_tmp.transpose() * problem->C_matrix[i] * a_tmp;
215 fvec(i) = -M5(i) + M1(i) - cc(0, 0) - M2(i);
218 for (
int i = 0; i < Nphi_p; i++)
221 gg = a_tmp.transpose() * problem->G_matrix[i] * a_tmp;
223 fvec(k) = M3(i, 0) + gg(0, 0) - M7(i, 0);
226 for (
int j = 0; j < N_BC; j++)
228 fvec(j) = x(j) - BC(j);
234int newton_usmsr_fd::df(
const Eigen::VectorXd& x,
235 Eigen::MatrixXd& fjac)
const
237 Eigen::NumericalDiff<newton_usmsr_fd> numDiff(*
this);
244 Eigen::VectorXd& fvecn)
const
246 Eigen::VectorXd c_tmp(Nphi_flux);
247 Eigen::VectorXd d1_tmp(Nphi_prec1);
248 Eigen::VectorXd d2_tmp(Nphi_prec2);
249 Eigen::VectorXd d3_tmp(Nphi_prec3);
250 Eigen::VectorXd d4_tmp(Nphi_prec4);
251 Eigen::VectorXd d5_tmp(Nphi_prec5);
252 Eigen::VectorXd d6_tmp(Nphi_prec6);
253 Eigen::VectorXd d7_tmp(Nphi_prec7);
254 Eigen::VectorXd d8_tmp(Nphi_prec8);
255 c_tmp = n.head(Nphi_flux);
257 d1_tmp = n.segment(pos, Nphi_prec1);
258 pos = pos + Nphi_prec1;
259 d2_tmp = n.segment(pos, Nphi_prec2);
260 pos = pos + Nphi_prec2;
261 d3_tmp = n.segment(pos, Nphi_prec3);
262 pos = pos + Nphi_prec3;
263 d4_tmp = n.segment(pos, Nphi_prec4);
264 pos = pos + Nphi_prec4;
265 d5_tmp = n.segment(pos, Nphi_prec5);
266 pos = pos + Nphi_prec5;
267 d6_tmp = n.segment(pos, Nphi_prec6);
268 pos = pos + Nphi_prec6;
269 d7_tmp = n.segment(pos, Nphi_prec7);
270 pos = pos + Nphi_prec7;
271 d8_tmp = n.segment(pos, Nphi_prec8);
272 Eigen::VectorXd c_dot(Nphi_flux);
273 Eigen::VectorXd d1_dot(Nphi_prec1);
274 Eigen::VectorXd d2_dot(Nphi_prec2);
275 Eigen::VectorXd d3_dot(Nphi_prec3);
276 Eigen::VectorXd d4_dot(Nphi_prec4);
277 Eigen::VectorXd d5_dot(Nphi_prec5);
278 Eigen::VectorXd d6_dot(Nphi_prec6);
279 Eigen::VectorXd d7_dot(Nphi_prec7);
280 Eigen::VectorXd d8_dot(Nphi_prec8);
281 c_dot = (n.head(Nphi_flux) - w_old.head(Nphi_flux)) / dt;
283 d1_dot = (n.segment(pos, Nphi_prec1) - w_old.segment(pos, Nphi_prec1)) / dt;
285 d2_dot = (n.segment(pos, Nphi_prec2) - w_old.segment(pos, Nphi_prec2)) / dt;
287 d3_dot = (n.segment(pos, Nphi_prec3) - w_old.segment(pos, Nphi_prec3)) / dt;
289 d4_dot = (n.segment(pos, Nphi_prec4) - w_old.segment(pos, Nphi_prec4)) / dt;
291 d5_dot = (n.segment(pos, Nphi_prec5) - w_old.segment(pos, Nphi_prec5)) / dt;
293 d6_dot = (n.segment(pos, Nphi_prec6) - w_old.segment(pos, Nphi_prec6)) / dt;
295 d7_dot = (n.segment(pos, Nphi_prec7) - w_old.segment(pos, Nphi_prec7)) / dt;
297 d8_dot = (n.segment(pos, Nphi_prec8) - w_old.segment(pos, Nphi_prec8)) / dt;
300 Eigen::VectorXd F_dot = problem->MF_matrix * c_dot * iv;
302 Eigen::MatrixXd lf(1, 1);
304 Eigen::MatrixXd pf(1, 1);
306 Eigen::MatrixXd af(1, 1);
308 Eigen::VectorXd F3_1 = problem->PS1_matrix * d1_tmp * l1;
309 Eigen::VectorXd F3_2 = problem->PS2_matrix * d2_tmp * l2;
310 Eigen::VectorXd F3_3 = problem->PS3_matrix * d3_tmp * l3;
311 Eigen::VectorXd F3_4 = problem->PS4_matrix * d4_tmp * l4;
312 Eigen::VectorXd F3_5 = problem->PS5_matrix * d5_tmp * l5;
313 Eigen::VectorXd F3_6 = problem->PS6_matrix * d6_tmp * l6;
314 Eigen::VectorXd F3_7 = problem->PS7_matrix * d7_tmp * l7;
315 Eigen::VectorXd F3_8 = problem->PS8_matrix * d8_tmp * l8;
317 Eigen::VectorXd Pdot_1 = problem->MP1_matrix * d1_dot;
318 Eigen::VectorXd Pdot_2 = problem->MP2_matrix * d2_dot;
319 Eigen::VectorXd Pdot_3 = problem->MP3_matrix * d3_dot;
320 Eigen::VectorXd Pdot_4 = problem->MP4_matrix * d4_dot;
321 Eigen::VectorXd Pdot_5 = problem->MP5_matrix * d5_dot;
322 Eigen::VectorXd Pdot_6 = problem->MP6_matrix * d6_dot;
323 Eigen::VectorXd Pdot_7 = problem->MP7_matrix * d7_dot;
324 Eigen::VectorXd Pdot_8 = problem->MP8_matrix * d8_dot;
326 Eigen::MatrixXd pp1(1, 1);
327 Eigen::MatrixXd pp2(1, 1);
328 Eigen::MatrixXd pp3(1, 1);
329 Eigen::MatrixXd pp4(1, 1);
330 Eigen::MatrixXd pp5(1, 1);
331 Eigen::MatrixXd pp6(1, 1);
332 Eigen::MatrixXd pp7(1, 1);
333 Eigen::MatrixXd pp8(1, 1);
335 Eigen::VectorXd P1_1 = problem->LP1_matrix * d1_tmp * (nu / Sc);
336 Eigen::VectorXd P1_2 = problem->LP2_matrix * d2_tmp * (nu / Sc);
337 Eigen::VectorXd P1_3 = problem->LP3_matrix * d3_tmp * (nu / Sc);
338 Eigen::VectorXd P1_4 = problem->LP4_matrix * d4_tmp * (nu / Sc);
339 Eigen::VectorXd P1_5 = problem->LP5_matrix * d5_tmp * (nu / Sc);
340 Eigen::VectorXd P1_6 = problem->LP6_matrix * d6_tmp * (nu / Sc);
341 Eigen::VectorXd P1_7 = problem->LP7_matrix * d7_tmp * (nu / Sc);
342 Eigen::VectorXd P1_8 = problem->LP8_matrix * d8_tmp * (nu / Sc);
344 Eigen::VectorXd P2_1 = problem->MP1_matrix * d1_tmp * l1;
345 Eigen::VectorXd P2_2 = problem->MP2_matrix * d2_tmp * l2;
346 Eigen::VectorXd P2_3 = problem->MP3_matrix * d3_tmp * l3;
347 Eigen::VectorXd P2_4 = problem->MP4_matrix * d4_tmp * l4;
348 Eigen::VectorXd P2_5 = problem->MP5_matrix * d5_tmp * l5;
349 Eigen::VectorXd P2_6 = problem->MP6_matrix * d6_tmp * l6;
350 Eigen::VectorXd P2_7 = problem->MP7_matrix * d7_tmp * l7;
351 Eigen::VectorXd P2_8 = problem->MP8_matrix * d8_tmp * l8;
353 Eigen::MatrixXd fs1(1, 1);
354 Eigen::MatrixXd fs2(1, 1);
355 Eigen::MatrixXd fs3(1, 1);
356 Eigen::MatrixXd fs4(1, 1);
357 Eigen::MatrixXd fs5(1, 1);
358 Eigen::MatrixXd fs6(1, 1);
359 Eigen::MatrixXd fs7(1, 1);
360 Eigen::MatrixXd fs8(1, 1);
362 for (
int i = 0; i < Nphi_flux; i++)
364 lf = d_c.transpose() * problem->LF_matrix[i] * c_tmp;
365 pf = nsf_c.transpose() * problem->PF_matrix[i] * c_tmp * (1 - btot);
366 af = a_c.transpose() * problem->AF_matrix[i] * c_tmp;
367 fvecn(i) = -F_dot(i) + lf(0, 0) + pf(0, 0) - af(0,
368 0) + F3_1(i) + F3_2(i) + F3_3(i) + F3_4(i) + F3_5(i) + F3_6(i) + F3_7(i) + F3_8(
372 int pfvecn = Nphi_flux;
374 for (
int i = 0; i < Nphi_prec1; i++)
377 pp1 = a_tmp.transpose() * problem->ST1_matrix[i] * d1_tmp;
378 fs1 = nsf_c.transpose() * problem->FS1_matrix[i] * c_tmp * b1;
379 fvecn(k) = -Pdot_1(i) - pp1(0, 0) + P1_1(i) - P2_1(i) + fs1(0, 0);
382 pfvecn += Nphi_prec1;
384 for (
int i = 0; i < Nphi_prec2; i++)
387 pp2 = a_tmp.transpose() * problem->ST2_matrix[i] * d2_tmp;
388 fs2 = nsf_c.transpose() * problem->FS2_matrix[i] * c_tmp * b2;
389 fvecn(k) = -Pdot_2(i) - pp2(0, 0) + P1_2(i) - P2_2(i) + fs2(0, 0);
392 pfvecn += Nphi_prec2;
394 for (
int i = 0; i < Nphi_prec3; i++)
397 pp3 = a_tmp.transpose() * problem->ST3_matrix[i] * d3_tmp;
398 fs3 = nsf_c.transpose() * problem->FS3_matrix[i] * c_tmp * b3;
399 fvecn(k) = -Pdot_3(i) - pp3(0, 0) + P1_3(i) - P2_3(i) + fs3(0, 0);
402 pfvecn += Nphi_prec3;
404 for (
int i = 0; i < Nphi_prec4; i++)
407 pp4 = a_tmp.transpose() * problem->ST4_matrix[i] * d4_tmp;
408 fs4 = nsf_c.transpose() * problem->FS4_matrix[i] * c_tmp * b4;
409 fvecn(k) = -Pdot_4(i) - pp4(0, 0) + P1_4(i) - P2_4(i) + fs4(0, 0);
412 pfvecn += Nphi_prec4;
414 for (
int i = 0; i < Nphi_prec5; i++)
417 pp5 = a_tmp.transpose() * problem->ST5_matrix[i] * d5_tmp;
418 fs5 = nsf_c.transpose() * problem->FS5_matrix[i] * c_tmp * b5;
419 fvecn(k) = -Pdot_5(i) - pp5(0, 0) + P1_5(i) - P2_5(i) + fs5(0, 0);
422 pfvecn += Nphi_prec5;
424 for (
int i = 0; i < Nphi_prec6; i++)
427 pp6 = a_tmp.transpose() * problem->ST6_matrix[i] * d6_tmp;
428 fs6 = nsf_c.transpose() * problem->FS6_matrix[i] * c_tmp * b6;
429 fvecn(k) = -Pdot_6(i) - pp6(0, 0) + P1_6(i) - P2_6(i) + fs6(0, 0);
432 pfvecn += Nphi_prec6;
434 for (
int i = 0; i < Nphi_prec7; i++)
437 pp7 = a_tmp.transpose() * problem->ST7_matrix[i] * d7_tmp;
438 fs7 = nsf_c.transpose() * problem->FS7_matrix[i] * c_tmp * b7;
439 fvecn(k) = -Pdot_7(i) - pp7(0, 0) + P1_7(i) - P2_7(i) + fs7(0, 0);
442 pfvecn += Nphi_prec7;
444 for (
int i = 0; i < Nphi_prec8; i++)
447 pp8 = a_tmp.transpose() * problem->ST8_matrix[i] * d8_tmp;
448 fs8 = nsf_c.transpose() * problem->FS8_matrix[i] * c_tmp * b8;
449 fvecn(k) = -Pdot_8(i) - pp8(0, 0) + P1_8(i) - P2_8(i) + fs8(0, 0);
455int newton_usmsr_n::df(
const Eigen::VectorXd& n,
456 Eigen::MatrixXd& fjacn)
const
458 Eigen::NumericalDiff<newton_usmsr_n> numDiff(*
this);
459 numDiff.df(n, fjacn);
464 Eigen::VectorXd& fvect)
const
466 Eigen::VectorXd e_tmp(Nphi_T);
467 Eigen::VectorXd f1_tmp(Nphi_dec1);
468 Eigen::VectorXd f2_tmp(Nphi_dec2);
469 Eigen::VectorXd f3_tmp(Nphi_dec3);
470 e_tmp = t.head(Nphi_T);
472 f1_tmp = t.segment(pos, Nphi_dec1);
474 f2_tmp = t.segment(pos, Nphi_dec2);
476 f3_tmp = t.segment(pos, Nphi_dec3);
477 Eigen::VectorXd e_dot(Nphi_T);
478 Eigen::VectorXd f1_dot(Nphi_dec1);
479 Eigen::VectorXd f2_dot(Nphi_dec2);
480 Eigen::VectorXd f3_dot(Nphi_dec3);
481 e_dot = (t.head(Nphi_T) - z_old.head(Nphi_T)) / dt;
483 f1_dot = (t.segment(pos, Nphi_dec1) - z_old.segment(pos, Nphi_dec1)) / dt;
485 f2_dot = (t.segment(pos, Nphi_dec2) - z_old.segment(pos, Nphi_dec2)) / dt;
487 f3_dot = (t.segment(pos, Nphi_dec3) - z_old.segment(pos, Nphi_dec3)) / dt;
490 Eigen::VectorXd T_dot = problem->TM_matrix * e_dot;
492 Eigen::MatrixXd tt(1, 1);
494 Eigen::VectorXd T1 = problem->LT_matrix * e_tmp * nu / Pr;
496 Eigen::MatrixXd xsf(1, 1);
498 Eigen::MatrixXd dhs1(1, 1);
499 Eigen::MatrixXd dhs2(1, 1);
500 Eigen::MatrixXd dhs3(1, 1);
502 Eigen::VectorXd DHdot_1 = problem->MD1_matrix * f1_dot;
503 Eigen::VectorXd DHdot_2 = problem->MD2_matrix * f2_dot;
504 Eigen::VectorXd DHdot_3 = problem->MD3_matrix * f3_dot;
506 Eigen::MatrixXd dh1(1, 1);
507 Eigen::MatrixXd dh2(1, 1);
508 Eigen::MatrixXd dh3(1, 1);
510 Eigen::VectorXd DH1_1 = problem->LD1_matrix * f1_tmp * nu / Sc;
511 Eigen::VectorXd DH1_2 = problem->LD2_matrix * f2_tmp * nu / Sc;
512 Eigen::VectorXd DH1_3 = problem->LD3_matrix * f3_tmp * nu / Sc;
514 Eigen::VectorXd DH2_1 = problem->MD1_matrix * f1_tmp * dl1;
515 Eigen::VectorXd DH2_2 = problem->MD2_matrix * f2_tmp * dl2;
516 Eigen::VectorXd DH2_3 = problem->MD3_matrix * f3_tmp * dl3;
518 Eigen::MatrixXd dfs1(1, 1);
519 Eigen::MatrixXd dfs2(1, 1);
520 Eigen::MatrixXd dfs3(1, 1);
522 for (
int i = 0; i < Nphi_T; i++)
524 tt = a_tmp.transpose() * problem->TS_matrix[i] * e_tmp;
525 xsf = txs_c.transpose() * problem->TXS_matrix[i] * c_tmp * ((1 - dbtot) / cp);
526 dhs1 = v_c.transpose() * problem->THS1_matrix[i] * f1_tmp * (dl1 / cp);
527 dhs2 = v_c.transpose() * problem->THS2_matrix[i] * f2_tmp * (dl2 / cp);
528 dhs3 = v_c.transpose() * problem->THS3_matrix[i] * f3_tmp * (dl3 / cp);
529 fvect(i) = -T_dot(i) - tt(0, 0) + T1(i) + xsf(0, 0) + dhs1(0, 0) + dhs2(0,
535 for (
int i = 0; i < Nphi_dec1; i++)
538 dh1 = a_tmp.transpose() * problem->SD1_matrix[i] * f1_tmp;
539 dfs1 = sp_c.transpose() * problem->DFS1_matrix[i] * c_tmp * db1;
540 fvect(k) = -DHdot_1(i) - dh1(0, 0) + DH1_1(i) - DH2_1(i) + dfs1(0, 0);
545 for (
int i = 0; i < Nphi_dec2; i++)
548 dh2 = a_tmp.transpose() * problem->SD2_matrix[i] * f2_tmp;
549 dfs2 = sp_c.transpose() * problem->DFS2_matrix[i] * c_tmp * db2;
550 fvect(k) = -DHdot_2(i) - dh2(0, 0) + DH1_2(i) - DH2_2(i) + dfs2(0, 0);
555 for (
int i = 0; i < Nphi_dec3; i++)
558 dh3 = a_tmp.transpose() * problem->SD3_matrix[i] * f3_tmp;
559 dfs3 = sp_c.transpose() * problem->DFS3_matrix[i] * c_tmp * db3;
560 fvect(k) = -DHdot_3(i) - dh3(0, 0) + DH1_3(i) - DH2_3(i) + dfs3(0, 0);
563 for (
int i = 0; i < N_BCt; i++)
565 fvect(i) = t(i) - BCt(i);
571int newton_usmsr_t::df(
const Eigen::VectorXd& t,
572 Eigen::MatrixXd& fjact)
const
574 Eigen::NumericalDiff<newton_usmsr_t> numDiff(*
this);
575 numDiff.df(t, fjact);
581 Eigen::MatrixXd temp_now, Eigen::VectorXd mu_online,
int startSnap)
583 Info <<
"\n Starting online stage...\n" << endl;
586 w.resize(Nphi_flux + Nphi_prec1 + Nphi_prec2 + Nphi_prec3 + Nphi_prec4 +
587 Nphi_prec5 + Nphi_prec6 + Nphi_prec7 + Nphi_prec8, 1);
589 z.resize(Nphi_T + Nphi_dec1 + Nphi_dec2 + Nphi_dec3, 1);
596 for (
int j = 0; j <
N_BC; j++)
598 y(j) = vel_now(j, 0);
605 Prec1snapshots[startSnap], Prec1modes);
608 Prec2snapshots[startSnap], Prec2modes);
611 Prec3snapshots[startSnap], Prec3modes);
614 Prec4snapshots[startSnap], Prec4modes);
617 Prec5snapshots[startSnap], Prec5modes);
620 Prec6snapshots[startSnap], Prec6modes);
623 Prec7snapshots[startSnap], Prec7modes);
626 Prec8snapshots[startSnap], Prec8modes);
630 Dec1snapshots[startSnap], Dec1modes);
633 Dec2snapshots[startSnap], Dec2modes);
636 Dec3snapshots[startSnap], Dec3modes);
638 for (
int j = 0; j < N_BCt; j++)
640 z(j) = temp_now(j, 0);
643 newton_object_fd.nu =
nu;
644 newton_object_fd.y_old =
y;
645 newton_object_fd.dt = dt;
646 newton_object_fd.BC.resize(
N_BC);
648 for (
int j = 0; j <
N_BC; j++)
650 newton_object_fd.BC(j) = vel_now(j, 0);
653 newton_object_n.nu =
nu;
654 newton_object_n.d = d;
655 newton_object_n.m = m;
656 newton_object_n.nsf = nsf;
657 newton_object_n.Keff = Keff;
658 newton_object_n.iv = iv;
659 newton_object_n.l1 = l1;
660 newton_object_n.l2 = l2;
661 newton_object_n.l3 = l3;
662 newton_object_n.l4 = l4;
663 newton_object_n.l5 = l5;
664 newton_object_n.l6 = l6;
665 newton_object_n.l7 = l7;
666 newton_object_n.l8 = l8;
667 newton_object_n.b1 = b1;
668 newton_object_n.b2 = b2;
669 newton_object_n.b3 = b3;
670 newton_object_n.b4 = b4;
671 newton_object_n.b5 = b5;
672 newton_object_n.b6 = b6;
673 newton_object_n.b7 = b7;
674 newton_object_n.b8 = b8;
675 newton_object_n.btot = btot;
676 newton_object_n.Sc = Sc;
677 newton_object_n.w_old = w;
678 newton_object_n.dt = dt;
679 newton_object_t.nu =
nu;
680 newton_object_t.Keff = Keff;
681 newton_object_t.sp = sp;
682 newton_object_t.cp = cp;
683 newton_object_t.dl1 = dl1;
684 newton_object_t.dl2 = dl2;
685 newton_object_t.dl3 = dl3;
686 newton_object_t.db1 = db1;
687 newton_object_t.db2 = db2;
688 newton_object_t.db3 = db3;
689 newton_object_t.dbtot = dbtot;
690 newton_object_t.Sc = Sc;
691 newton_object_t.Pr = Pr;
692 newton_object_t.dt = dt;
693 newton_object_t.z_old = z;
694 newton_object_t.BCt.resize(N_BCt);
696 for (
int j = 0; j < N_BCt; j++)
698 newton_object_t.BCt(j) = temp_now(j, 0);
702 int Ntsteps =
static_cast<int>((finalTime - tstart) / dt);
704 online_solution_n.resize(Ntsteps);
705 online_solution_t.resize(Ntsteps);
706 online_solution_C.resize(Ntsteps);
712 Eigen::MatrixXd tmp_sol_fd(
Nphi_u + Nphi_p + 1, 1);
713 tmp_sol_fd(0) = time;
714 tmp_sol_fd.col(0).tail(
y.rows()) =
y;
715 Eigen::MatrixXd tmp_sol_n(Nphi_flux + Nphi_prec1 + Nphi_prec2 + Nphi_prec3 +
716 Nphi_prec4 + Nphi_prec5 + Nphi_prec6 + Nphi_prec7 + Nphi_prec8 + 1, 1);
718 tmp_sol_n.col(0).tail(w.rows()) = w;
719 Eigen::MatrixXd tmp_sol_t(Nphi_T + Nphi_dec1 + Nphi_dec2 + Nphi_dec3 + 1, 1);
721 tmp_sol_t.col(0).tail(z.rows()) = z;
722 Eigen::MatrixXd tmp_sol_C(6 * Nphi_const + 1, 1);
724 Eigen::VectorXd v_c0;
725 Eigen::VectorXd d_c0;
726 Eigen::VectorXd nsf_c0;
727 Eigen::VectorXd a_c0;
728 Eigen::VectorXd sp_c0;
729 Eigen::VectorXd txs_c0;
730 v_c0.resize(Nphi_const);
731 d_c0.resize(Nphi_const);
732 nsf_c0.resize(Nphi_const);
733 a_c0.resize(Nphi_const);
734 sp_c0.resize(Nphi_const);
735 txs_c0.resize(Nphi_const);
736 std::vector<double> tv0;
737 tv0.resize(mu_online.size() + 1);
740 for (
int k = 1; k < tv0.size(); k++)
742 tv0[k] = mu_online(k - 1);
745 for (
int i = 0; i < Nphi_const; i++)
747 v_c0(i) = problem->rbfsplines_v[i]->eval(tv0);
748 d_c0(i) = problem->rbfsplines_D[i]->eval(tv0);
749 nsf_c0(i) = problem->rbfsplines_NSF[i]->eval(tv0);
750 a_c0(i) = problem->rbfsplines_A[i]->eval(tv0);
751 sp_c0(i) = problem->rbfsplines_SP[i]->eval(tv0);
752 txs_c0(i) = problem->rbfsplines_TXS[i]->eval(tv0);
756 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = v_c0;
758 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = d_c0;
760 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = nsf_c0;
762 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = a_c0;
764 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = sp_c0;
766 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = txs_c0;
768 online_solution_n[counter] = tmp_sol_n;
769 online_solution_t[counter] = tmp_sol_t;
770 online_solution_C[counter] = tmp_sol_C;
773 Eigen::HybridNonLinearSolver<newton_usmsr_fd> hnls_fd(newton_object_fd);
774 Eigen::HybridNonLinearSolver<newton_usmsr_n> hnls_n(newton_object_n);
775 Eigen::HybridNonLinearSolver<newton_usmsr_t> hnls_t(newton_object_t);
776 Color::Modifier red(Color::FG_RED);
777 Color::Modifier green(Color::FG_GREEN);
778 Color::Modifier def(Color::FG_DEFAULT);
780 while (time < finalTime + dt)
783 std::vector<double> tv;
784 tv.resize(mu_online.size() + 1);
787 for (
int k = 1; k < tv.size(); k++)
789 tv[k] = mu_online(k - 1);
792 for (
int i = 0; i < Nphi_const; i++)
794 newton_object_n.d_c(i) = problem->rbfsplines_D[i]->eval(tv);
795 newton_object_n.nsf_c(i) = problem->rbfsplines_NSF[i]->eval(tv);
796 newton_object_n.a_c(i) = problem->rbfsplines_A[i]->eval(tv);
797 newton_object_t.v_c(i) = problem->rbfsplines_v[i]->eval(tv);
798 newton_object_t.sp_c(i) = problem->rbfsplines_SP[i]->eval(tv);
799 newton_object_t.txs_c(i) = problem->rbfsplines_TXS[i]->eval(tv);
802 Eigen::VectorXd res_fd(
y);
803 Eigen::VectorXd res_n(w);
804 Eigen::VectorXd res_t(z);
810 for (
int j = 0; j <
N_BC; j++)
812 y(j) = vel_now(j, 0);
815 newton_object_n.a_tmp =
y.head(
Nphi_u);
817 newton_object_t.a_tmp =
y.head(
Nphi_u);
818 newton_object_t.c_tmp = w.head(Nphi_flux);
821 for (
int j = 0; j < N_BCt; j++)
823 z(j) = temp_now(j, 0);
826 newton_object_fd.operator()(
y, res_fd);
827 newton_object_fd.y_old =
y;
828 newton_object_n.operator()(w, res_n);
829 newton_object_n.w_old = w;
830 newton_object_t.operator()(z, res_t);
831 newton_object_t.z_old = z;
833 " ##################" << endl;
834 Info <<
"Time = " << time << endl;
836 if (res_fd.norm() /
y.norm() < 1e-5)
838 Info << green <<
"|F_fd(x)| = " << res_fd.norm() /
y.norm() <<
839 " - Minimun reached in " << hnls_fd.iter <<
" iterations " << def << endl
844 Info << red <<
"|F_fd(x)| = " << res_fd.norm() /
y.norm() <<
845 " - Minimun reached in " << hnls_fd.iter <<
" iterations " << def << endl
849 if (res_n.norm() / w.norm() < 1e-5)
851 Info << green <<
"|F_n(x)| = " << res_n.norm() / w.norm() <<
852 " - Minimun reached in " << hnls_n.iter <<
" iterations " << def << endl <<
857 Info << red <<
"|F_n(x)| = " << res_n.norm() / w.norm() <<
858 " - Minimun reached in " << hnls_n.iter <<
" iterations " << def << endl <<
862 if (res_t.norm() / z.norm() < 1e-5)
864 Info << green <<
"|F_t(x)| = " << res_t.norm() / z.norm() <<
865 " - Minimun reached in " << hnls_t.iter <<
" iterations " << def << endl <<
870 Info << red <<
"|F_t(x)| = " << res_t.norm() / z.norm() <<
871 " - Minimun reached in " << hnls_t.iter <<
" iterations " << def << endl <<
876 tmp_sol_fd(0) = time;
877 tmp_sol_fd.col(0).tail(
y.rows()) =
y;
889 tmp_sol_n.col(0).tail(w.rows()) = w;
891 if (counter >= online_solution_n.size())
893 online_solution_n.append(tmp_sol_n);
897 online_solution_n[counter] = tmp_sol_n;
901 tmp_sol_t.col(0).tail(z.rows()) = z;
903 if (counter >= online_solution_t.size())
905 online_solution_t.append(tmp_sol_t);
909 online_solution_t[counter] = tmp_sol_t;
914 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = newton_object_t.v_c;
916 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = newton_object_n.d_c;
918 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = newton_object_n.nsf_c;
920 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = newton_object_n.a_c;
922 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = newton_object_t.sp_c;
924 tmp_sol_C.col(0).segment(pos_c, Nphi_const) = newton_object_t.txs_c;
926 if (counter >= online_solution_C.size())
928 online_solution_C.append(tmp_sol_C);
932 online_solution_C[counter] = tmp_sol_C;
939 "./ITHACAoutput/red_coeff_fd");
941 "./ITHACAoutput/red_coeff_n");
943 "./ITHACAoutput/red_coeff_t");
945 "./ITHACAoutput/red_coeff_C");
948void reducedusMSR::reconstructAP(fileName folder,
int printevery)
953 reconstruct_fd(folder, printevery);
954 reconstruct_n(folder, printevery);
955 reconstruct_C(folder, printevery);
956 reconstruct_t(folder, printevery);
959void reducedusMSR::reconstruct_fd(fileName folder,
int printevery)
967 Info <<
"Reconstructing online solution | fluid-dynamics" << endl;
974 if (counter == nextwrite)
976 volVectorField U_rec(
"U",
Umodes[0] * 0);
978 for (
int j = 0; j <
Nphi_u; j++)
984 volScalarField P_rec(
"p", Pmodes[0] * 0);
986 for (
int j = 0; j < Nphi_p; j++)
992 std::ofstream of(folder +
"/" + name(counter2) +
"/" + name(
994 nextwrite += printevery;
996 UREC.append(U_rec.clone());
997 PREC.append(P_rec.clone());
1003 Info <<
"End" << endl;
1006void reducedusMSR::reconstruct_n(fileName folder,
int printevery)
1008 if (recall ==
false)
1014 Info <<
"Reconstructing online solution | neutronics" << endl;
1019 for (
int i = 0; i < online_solution_n.size(); i++)
1021 if (counter == nextwrite)
1023 volScalarField Flux_rec(
"flux", Fluxmodes[0] * 0);
1025 for (
int j = 0; j < Nphi_flux; j++)
1027 Flux_rec += Fluxmodes[j] * online_solution_n[i](j + 1, 0);
1031 int pos = Nphi_flux;
1032 volScalarField Prec1_rec(
"prec1", Prec1modes[0] * 0);
1034 for (
int j = 0; j < Nphi_prec1; j++)
1036 Prec1_rec += Prec1modes[j] * online_solution_n[i](j + pos + 1, 0);
1041 volScalarField Prec2_rec(
"prec2", Prec2modes[0] * 0);
1043 for (
int j = 0; j < Nphi_prec2; j++)
1045 Prec2_rec += Prec2modes[j] * online_solution_n[i](j + pos + 1, 0);
1050 volScalarField Prec3_rec(
"prec3", Prec3modes[0] * 0);
1052 for (
int j = 0; j < Nphi_prec3; j++)
1054 Prec3_rec += Prec3modes[j] * online_solution_n[i](j + pos + 1, 0);
1059 volScalarField Prec4_rec(
"prec4", Prec4modes[0] * 0);
1061 for (
int j = 0; j < Nphi_prec4; j++)
1063 Prec4_rec += Prec4modes[j] * online_solution_n[i](j + pos + 1, 0);
1068 volScalarField Prec5_rec(
"prec5", Prec5modes[0] * 0);
1070 for (
int j = 0; j < Nphi_prec5; j++)
1072 Prec5_rec += Prec5modes[j] * online_solution_n[i](j + pos + 1, 0);
1077 volScalarField Prec6_rec(
"prec6", Prec6modes[0] * 0);
1079 for (
int j = 0; j < Nphi_prec6; j++)
1081 Prec6_rec += Prec6modes[j] * online_solution_n[i](j + pos + 1, 0);
1086 volScalarField Prec7_rec(
"prec7", Prec7modes[0] * 0);
1088 for (
int j = 0; j < Nphi_prec7; j++)
1090 Prec7_rec += Prec7modes[j] * online_solution_n[i](j + pos + 1, 0);
1095 volScalarField Prec8_rec(
"prec8", Prec8modes[0] * 0);
1097 for (
int j = 0; j < Nphi_prec8; j++)
1099 Prec8_rec += Prec8modes[j] * online_solution_n[i](j + pos + 1, 0);
1103 std::ofstream of(folder +
"/" + name(counter2) +
"/" + name(
1104 online_solution_n[i](0)));
1105 nextwrite += printevery;
1107 FLUXREC.append(Flux_rec.clone());
1108 PREC1REC.append(Prec1_rec.clone());
1109 PREC2REC.append(Prec2_rec.clone());
1110 PREC3REC.append(Prec3_rec.clone());
1111 PREC4REC.append(Prec4_rec.clone());
1112 PREC5REC.append(Prec5_rec.clone());
1113 PREC6REC.append(Prec6_rec.clone());
1114 PREC7REC.append(Prec7_rec.clone());
1115 PREC8REC.append(Prec8_rec.clone());
1121 Info <<
"End" << endl;
1124void reducedusMSR::reconstruct_t(fileName folder,
int printevery)
1126 if (recall ==
false)
1132 Info <<
"Reconstructing online solution | thermal" << endl;
1136 dimensionedScalar decLam1(
"decLam1", dimensionSet(0, 0, -1, 0, 0, 0, 0), dl1);
1137 dimensionedScalar decLam2(
"decLam2", dimensionSet(0, 0, -1, 0, 0, 0, 0), dl2);
1138 dimensionedScalar decLam3(
"decLam3", dimensionSet(0, 0, -1, 0, 0, 0, 0), dl3);
1140 for (
int i = 0; i < online_solution_t.size(); i++)
1142 if (counter == nextwrite)
1144 volScalarField T_rec(
"T", Tmodes[0] * 0);
1146 for (
int j = 0; j < Nphi_T; j++)
1148 T_rec += Tmodes[j] * online_solution_t[i](j + 1, 0);
1153 volScalarField PowerDens_rec(
"powerDens", Dec1modes[0] * 0 * decLam1);
1154 volScalarField Dec1_rec(
"dec1", Dec1modes[0] * 0);
1156 for (
int j = 0; j < Nphi_dec1; j++)
1158 Dec1_rec += Dec1modes[j] * online_solution_t[i](j + pos + 1, 0);
1159 PowerDens_rec += Dec1modes[j] * online_solution_t[i](j + pos + 1, 0) * decLam1;
1164 volScalarField Dec2_rec(
"dec2", Dec2modes[0] * 0);
1166 for (
int j = 0; j < Nphi_dec2; j++)
1168 Dec2_rec += Dec2modes[j] * online_solution_t[i](j + pos + 1, 0);
1169 PowerDens_rec += Dec2modes[j] * online_solution_t[i](j + pos + 1, 0) * decLam2;
1174 volScalarField Dec3_rec(
"dec3", Dec3modes[0] * 0);
1176 for (
int j = 0; j < Nphi_dec3; j++)
1178 Dec3_rec += Dec3modes[j] * online_solution_t[i](j + pos + 1, 0);
1179 PowerDens_rec += Dec3modes[j] * online_solution_t[i](j + pos + 1, 0) * decLam3;
1183 PowerDens_rec += (1 - dbtot) * SPREC[counter2 - 1] * FLUXREC[counter2 - 1];
1185 std::ofstream of(folder +
"/" + name(counter2) +
"/" + name(
1186 online_solution_t[i](0)));
1187 nextwrite += printevery;
1189 TREC.append(T_rec.clone());
1190 DEC1REC.append(Dec1_rec.clone());
1191 DEC2REC.append(Dec2_rec.clone());
1192 DEC3REC.append(Dec3_rec.clone());
1193 POWERDENSREC.append(PowerDens_rec.clone());
1199 Info <<
"End" << endl;
1202void reducedusMSR::reconstruct_C(fileName folder,
int printevery)
1204 if (recall ==
false)
1210 Info <<
"Reconstructing temperature changing constants" << endl;
1215 for (
int i = 0; i < online_solution_C.size(); i++)
1217 if (counter == nextwrite)
1219 volScalarField v_rec(
"v", vmodes[0] * 0);
1221 for (
int j = 0; j < Nphi_const; j++)
1223 v_rec += vmodes[j] * online_solution_C[i](j + 1, 0);
1227 int pos = Nphi_const;
1228 volScalarField D_rec(
"D", Dmodes[0] * 0);
1230 for (
int j = 0; j < Nphi_const; j++)
1232 D_rec += Dmodes[j] * online_solution_C[i](j + pos + 1, 0);
1237 volScalarField NSF_rec(
"NSF", NSFmodes[0] * 0);
1239 for (
int j = 0; j < Nphi_const; j++)
1241 NSF_rec += NSFmodes[j] * online_solution_C[i](j + pos + 1, 0);
1246 volScalarField A_rec(
"A", Amodes[0] * 0);
1248 for (
int j = 0; j < Nphi_const; j++)
1250 A_rec += Amodes[j] * online_solution_C[i](j + pos + 1, 0);
1255 volScalarField SP_rec(
"SP", SPmodes[0] * 0);
1257 for (
int j = 0; j < Nphi_const; j++)
1259 SP_rec += SPmodes[j] * online_solution_C[i](j + pos + 1, 0);
1264 volScalarField TXS_rec(
"TXS", TXSmodes[0] * 0);
1266 for (
int j = 0; j < Nphi_const; j++)
1268 TXS_rec += TXSmodes[j] * online_solution_C[i](j + pos + 1, 0);
1272 std::ofstream of(folder +
"/" + name(counter2) +
"/" + name(
1273 online_solution_C[i](0)));
1274 nextwrite += printevery;
1276 vREC.append(v_rec.clone());
1277 DREC.append(D_rec.clone());
1278 NSFREC.append(NSF_rec.clone());
1279 AREC.append(A_rec.clone());
1280 SPREC.append(SP_rec.clone());
1281 TXSREC.append(TXS_rec.clone());
1287 Info <<
"End" << endl;
PtrList< volVectorField > Usnapshots
List of pointers to store the snapshots for each field.
PtrList< volVectorField > Umodes
List of pointers to store the modes for each field.
scalar nu
characteristic constants of the problem
PtrList< volVectorField > UREC
Recontructed fields.
void loadConstants(msrProblem *problem)
Method to load all the constants needed in the ROM from ///the FOM.
int N_BC
Number of parametrized boundary conditions.
List< Eigen::MatrixXd > online_solution_fd
List of Eigen matrices to store the online solution.
int count_online_solve
Counter to count the online solutions.
int Nphi_u
Number of modes for each field.
Eigen::VectorXd y
Vector to store the solution during the Newton procedure.
virtual void solveOnline()
Virtual Method to perform and online Solve.
void exportSolution(GeometricField< Type, PatchField, GeoMesh > &s, fileName subfolder, fileName folder, word fieldName)
Export a field to file in a certain folder and subfolder.
void exportMatrix(Eigen::Matrix< T, -1, dim > &matrix, word Name, word type, word folder)
Export the reduced matrices in numpy (type=python), matlab (type=matlab) and txt (type=eigen) format ...
Eigen::VectorXd getCoeffs(GeometricField< Type, PatchField, GeoMesh > &snapshot, PtrList< GeometricField< Type, PatchField, GeoMesh > > &modes, label Nmodes, bool consider_volumes)
Projects a snapshot on a basis function and gets the coefficients of the projection.
void createSymLink(word folder)
Creates symbolic links to 0, system and constant.
int operator()(const Eigen::VectorXd &x, Eigen::VectorXd &fvec) const
int operator()(const Eigen::VectorXd &n, Eigen::VectorXd &fvecn) const
int operator()(const Eigen::VectorXd &t, Eigen::VectorXd &fvect) const