MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
QmomKinetics.cpp
Go to the documentation of this file.
1 
44 #include <iostream>
45 #include <string>
46 #include <fstream>
47 #include <iomanip>
48 #include <math.h>
49 #include <vector>
50 #include <boost/array.hpp>
51 #include <boost/numeric/odeint.hpp>
52 #include <boost/range/numeric.hpp>
53 #include "modena.h"
54 
55 extern "C"{void dsteqr_(char &, int *, double *, double *, double *, int *, double *, int *); }
56 
57 #include "experimentalInputs.h"
58 #include "readParameters.h"
59 #include "initializeMoments.h"
60 #include "pda.h"
61 #include "growth.h"
62 #include "coalescence.h"
63 #include "liquidBA.h"
64 
65 using namespace std;
66 using namespace boost::numeric::odeint;
73 typedef std::vector< double > state_type;
74 typedef runge_kutta_cash_karp54< state_type > error_stepper_type;
75 typedef controlled_runge_kutta< error_stepper_type > controlled_stepper_type;
80 controlled_stepper_type controlled_stepper;
81 // #include "bubbleRadius.h"
82 #include "partialPressure.h"
90 double dpdt[2] = {};
91 double pOld[2] = {};
92 
93 #include "modenaData.h"
94 #include "momentsConverter.h"
95 #include "write_kinetics.h"
96 #include "determinant.h"
97 #include "HankelHadamard.h"
98 #include "differenceTable.h"
99 #include "McGrawCorrection.h"
100 #include "WrightCorrection.h"
109 void QmomKinetics( const state_type &y , state_type &dydt , double t )
110 {
111  // dydt[0] : XW
112  // dydt[1] : XOH
113  // dydt[2] : T
114  // dydt[3] : L_l
115  // dydt[4] : L_g
116  // dydt[5] : CO2_l
117  // dydt[6] : CO2_g
118  // dydt[7] : m0
119  // dydt[8] : m1
120  // dydt[9] : m2
121  // dydt[10]: m3
122  // ---RF-1-private---
123  // dydt[11]: Catalyst_1
124  // dydt[12]: CE_A0
125  // dydt[13]: CE_A1
126  // dydt[14]: CE_B
127  // dydt[15]: CE_B2
128  // dydt[16]: CE_I0
129  // dydt[17]: CE_I1
130  // dydt[18]: CE_I2
131  // dydt[19]: CE_PBA
132  // dydt[20]: CE_Breac
133  // dydt[21]: CE_Areac0
134  // dydt[22]: CE_Areac1
135  // dydt[23]: CE_Ireac0
136  // dydt[24]: CE_Ireac1
137  // dydt[25]: CE_Ireac2
138  // dydt[26]: Bulk
139  // dydt[27]: R_1
140  // dydt[28]: R_1_mass
141  // dydt[29]: R_1_temp
142  // dydt[30]: R_1_vol
143 
144  int nNodes = 2;
145  int mOrder[6] = {0, 1, 2, 3, 4, 5};
146  double mom[2*nNodes], we[nNodes], vi[nNodes], sgBA[2*nNodes], sgCO2[2*nNodes], sc[2*nNodes];
147  double L_l, L_g, CO2_l, CO2_g, T, Lm, c1;
148  double rhoPolySurrgate;
149  double beta0 = 0.0;
150  double XW, XOH;
151 
152  // RF-1 variables
153  double Catalyst_1, CE_A0, CE_A1, CE_B, CE_B2,
154  CE_I0, CE_I1, CE_I2, CE_PBA, CE_Breac,
155  CE_Areac0, CE_Areac1, CE_Ireac0, CE_Ireac1,
156  CE_Ireac2, Bulk, R_1, R_1_mass, R_1_temp, R_1_vol;
157 
158  XW = y[0];
159  XOH = y[1];
160  T = y[2];
161  L_l = y[3];
162  L_g = y[4];
163  CO2_l = y[5];
164  CO2_g = y[6];
165  mom[0] = y[7];
166  mom[1] = y[8];
167  mom[2] = y[9];
168  mom[3] = y[10];
169  Catalyst_1 = y[11];
170  CE_A0 = y[12];
171  CE_A1 = y[13];
172  CE_B = y[14];
173  CE_B2 = y[15];
174  CE_I0 = y[16];
175  CE_I1 = y[17];
176  CE_I2 = y[18];
177  CE_PBA = y[19];
178  CE_Breac = y[20];
179  CE_Areac0 = y[21];
180  CE_Areac1 = y[22];
181  CE_Ireac0 = y[23];
182  CE_Ireac1 = y[24];
183  CE_Ireac2 = y[25];
184  Bulk = y[26];
185  R_1 = y[27];
186  R_1_mass = y[28];
187  R_1_temp = y[29];
188  R_1_vol = y[30];
189 
190  double Rx;
191  // Calling the RF-1 model
192  switch (kinMod)
193  {
194  case 1:
195  Rx=1;
196  break;
197  case 2:
198  if (y[1]<X_gel) {
199  Rx=1;
200  } else if (y[1]<0.87) {
201  Rx=-2.027*y[1]+2.013;
202  } else {
203  Rx=3.461*y[1]-2.761;
204  }
205  break;
206  case 3:
207  // set input vector
208  modena_inputs_set(inputs_kinetics, kineticTime_Pos, t);
209  modena_inputs_set(inputs_kinetics, Catalyst_1_Pos, Catalyst_1);
210  modena_inputs_set(inputs_kinetics, CE_A0_Pos, CE_A0);
211  modena_inputs_set(inputs_kinetics, CE_A1_Pos, CE_A1);
212  modena_inputs_set(inputs_kinetics, CE_B_Pos, CE_B);
213  modena_inputs_set(inputs_kinetics, CE_B2_Pos, CE_B2);
214  modena_inputs_set(inputs_kinetics, CE_I0_Pos, CE_I0);
215  modena_inputs_set(inputs_kinetics, CE_I1_Pos, CE_I1);
216  modena_inputs_set(inputs_kinetics, CE_I2_Pos, CE_I2);
217  modena_inputs_set(inputs_kinetics, CE_PBA_Pos, CE_PBA);
218  modena_inputs_set(inputs_kinetics, CE_Breac_Pos, CE_Breac);
219  modena_inputs_set(inputs_kinetics, CE_Areac0_Pos, CE_Areac0);
220  modena_inputs_set(inputs_kinetics, CE_Areac1_Pos, CE_Areac1);
221  modena_inputs_set(inputs_kinetics, CE_Ireac0_Pos, CE_Ireac0);
222  modena_inputs_set(inputs_kinetics, CE_Ireac1_Pos, CE_Ireac1);
223  modena_inputs_set(inputs_kinetics, CE_Ireac2_Pos, CE_Ireac2);
224  modena_inputs_set(inputs_kinetics, Bulk_Pos, Bulk);
225  modena_inputs_set(inputs_kinetics, R_1_Pos, R_1);
226  modena_inputs_set(inputs_kinetics, R_1_mass_Pos, R_1_mass);
227  modena_inputs_set(inputs_kinetics, R_1_temp_Pos, (T-273.15));
228  modena_inputs_set(inputs_kinetics, R_1_vol_Pos, R_1_vol);
229 
230  // call the model
231  int ret_kinetics = modena_model_call(kinetics, inputs_kinetics, outputs_kinetics);
232 
233  // terminate, if requested
234  if(modena_error_occurred())
235  {
236  modena_inputs_destroy (inputs_kinetics);
237  modena_outputs_destroy (outputs_kinetics);
238  modena_model_destroy (kinetics);
239  cout << "Modena Error:" << (modena_error()) << endl;
240  }
241 
242  // get the source terms for simpleKinetics
243  dydt[11] = modena_outputs_get(outputs_kinetics, source_Catalyst_1_Pos);
244  dydt[12] = modena_outputs_get(outputs_kinetics, source_CE_A0_Pos);
245  dydt[13] = modena_outputs_get(outputs_kinetics, source_CE_A1_Pos);
246  dydt[14] = modena_outputs_get(outputs_kinetics, source_CE_B_Pos);
247  dydt[15] = modena_outputs_get(outputs_kinetics, source_CE_B2_Pos);
248  dydt[16] = modena_outputs_get(outputs_kinetics, source_CE_I0_Pos);
249  dydt[17] = modena_outputs_get(outputs_kinetics, source_CE_I1_Pos);
250  dydt[18] = modena_outputs_get(outputs_kinetics, source_CE_I2_Pos);
251  dydt[19] = modena_outputs_get(outputs_kinetics, source_CE_PBA_Pos);
252  dydt[20] = modena_outputs_get(outputs_kinetics, source_CE_Breac_Pos);
253  dydt[21] = modena_outputs_get(outputs_kinetics, source_CE_Areac0_Pos);
254  dydt[22] = modena_outputs_get(outputs_kinetics, source_CE_Areac1_Pos);
255  dydt[23] = modena_outputs_get(outputs_kinetics, source_CE_Ireac0_Pos);
256  dydt[24] = modena_outputs_get(outputs_kinetics, source_CE_Ireac1_Pos);
257  dydt[25] = modena_outputs_get(outputs_kinetics, source_CE_Ireac2_Pos);
258  dydt[26] = modena_outputs_get(outputs_kinetics, source_Bulk_Pos);
259  dydt[27] = modena_outputs_get(outputs_kinetics, source_R_1_Pos);
260  dydt[28] = modena_outputs_get(outputs_kinetics, source_R_1_mass_Pos);
261  dydt[29] = modena_outputs_get(outputs_kinetics, source_R_1_temp_Pos);
262  dydt[30] = modena_outputs_get(outputs_kinetics, source_R_1_vol_Pos);
263 
264  if (W_0 > 1e-8) {
265  dydt[0] = -dydt[15]/(W_0/1000);
266  } else {
267  dydt[0] = 0;
268  }
269  dydt[1] = -(dydt[12] + dydt[13])/(OH_0)*1000;
270  break;
271  }
272 
273  switch (denMod)
274  {
275  case 1:
276  {
277  // Calling the model for density reaction mixture
278  size_t T_denpos = modena_model_inputs_argPos(density_reaction_mixturemodel, "T");
279  size_t XOH_denpos = modena_model_inputs_argPos(density_reaction_mixturemodel, "XOH");
280  modena_model_argPos_check(density_reaction_mixturemodel);
281 
282  // // set input vector
283  modena_inputs_set(inputs_den, T_denpos, T);
284  modena_inputs_set(inputs_den, XOH_denpos, XOH);
285 
286  // // call the model
287  int ret_den = modena_model_call (density_reaction_mixturemodel, inputs_den, outputs_den);
288 
289  if (ret_den != 0)
290  {
291  modena_inputs_destroy (inputs_den);
292  modena_outputs_destroy (outputs_den);
293  modena_model_destroy (density_reaction_mixturemodel);
294  exit(ret_den);
295  }
296 
297  rhoPolySurrgate = modena_outputs_get(outputs_den, 0);
298  break;
299  }
300  case 2:
301  rhoPolySurrgate = rhoPoly;
302  break;
303  case 3:
304  {
305  // Calling the PCSAFT model for density reaction mixture
306  size_t T_denpos = modena_model_inputs_argPos(density_reaction_mixturemodel, "T");
307  modena_model_argPos_check(density_reaction_mixturemodel);
308  modena_inputs_set(inputs_den, T_denpos, T);
309  // // call the model
310  int ret_den = modena_model_call (density_reaction_mixturemodel, inputs_den, outputs_den);
311  if (ret_den != 0)
312  {
313  modena_inputs_destroy (inputs_den);
314  modena_outputs_destroy (outputs_den);
315  modena_model_destroy (density_reaction_mixturemodel);
316  exit(ret_den);
317  }
318  rhoPolySurrgate = modena_outputs_get(outputs_den, 0);
319  break;
320  }
321  }
322  if (kinMod == 1 || kinMod == 2)
323  {
324  // ODEs
325  dydt[0] = A_W*exp(-E_W/(RR*y[2]))*(1-y[0]);
326  if(dydt[0] < 0.0)
327  {
328  dydt[0] = 0.0;
329  }
330  if(W_0 < 0.0)
331  {
332  dydt[0] = 0.0;
333  }
334 
335  dydt[1] = Rx*A_OH*exp(-E_OH/(RR*y[2]))*OH_0*(1-y[1])*(NCO_0/OH_0 - 2.0*y[0]*W_0/OH_0 - y[1]);
336  if(dydt[1] < 0.0)
337  {
338  dydt[1] = 0.0;
339  }
340  if (dilution) {
341  dydt[0]=dydt[0]/(1+y[3]*rhoPolySurrgate/rhoBL);
342  dydt[1]=dydt[1]/(1+y[3]*rhoPolySurrgate/rhoBL);
343  }
344  }
345 
346  // call the surrogate model for rheology
347  if (apparentViscosity)
348  {
349  double shearRate = 0.05;
350  // // set input vector
351  modena_inputs_set(inputs_rheo, temp_rheopos, T);
352  modena_inputs_set(inputs_rheo, conv_rheopos, XOH);
353  modena_inputs_set(inputs_rheo, shear_rheopos, shearRate);
354  modena_inputs_set(inputs_rheo, m0_rheopos, mom[0]);
355  modena_inputs_set(inputs_rheo, m1_rheopos, mom[1]);
356  // // call the model
357  int ret_rheo = modena_model_call (rheologymodel, inputs_rheo, outputs_rheo);
358  // // terminate, if requested
359  if(modena_error_occurred())
360  {
361  modena_inputs_destroy (inputs_rheo);
362  modena_outputs_destroy (outputs_rheo);
363  modena_model_destroy (rheologymodel);
364  cout << "Modena Error: " << (modena_error()) << endl;
365  }
366  double mu_app = modena_outputs_get(outputs_rheo, 0);
367  // cout << "apparent viscosity: " << mu_app << endl;
368  }
369 
370  // Gelling point representation
371  if(y[1] > X_gel)
372  {
373  beta0 = 0.0;
374  }
375  else
376  {
377  beta0 = beta0;
378  }
379 
380  if (realizabilityCheck)
381  {
382  cout << "----Step 1----" << endl;
383  cout << "moments before check realizability: " << endl;
384  printMoms(mom,nNodes);
385 
386  // Check realizability
387  cout << "----Step 2----" << endl;
388  cout << "check positivity" << endl;
389  if (!momentsPositivite(mom,nNodes))
390  {
391  double initial_mom[2*nNodes];
392  int nMoms = 2*nNodes;
393  mom_init(initial_mom, init_size, nMoms, sig, NN);
394 
395  for (int i = 0; i < 2*nNodes; i++)
396  {
397  if (mom[i] < 0.0)
398  {
399  mom[i] = initial_mom[i];
400  }
401  }
402  }
403  printMoms(mom,nNodes);
404 
405 
406  int realizable = 0;
407  cout << "----Step 3----" << endl;
408  cout << "first Hankel-Hadamard check" << endl;
409  realizable = HankelHadamard(mom, nNodes);
410  cout << "Hankel-Hadamard check (0:realizable, 1:unrealizable) --> " << realizable << endl;
411 
412  if (realizable == 1)
413  {
414  // double M0 = mom[0];
415  // normalizeMom(mom, nNodes);
416  cout << "----Step 4----" << endl;
417  cout << "McGrawCorrection" << endl;
418  McGrawCorrection(mom, nNodes);
419  cout << "moments after McGrawCorrection: " << endl;
420  printMoms(mom,nNodes);
421  // denormalizeMom(mom, M0, nNodes);
422  }
423  cout << "----Step 5----" << endl;
424  cout << "second Hankel-Hadamard check" << endl;
425  realizable = HankelHadamard(mom, nNodes);
426  cout << "Hankel-Hadamard check (0:realizable, 1:unrealizable) --> " << realizable << endl;
427 
428  if (realizable == 1)
429  {
430  cout << "----Step 6----" << endl;
431  cout << "WrightCorrection" << endl;
432  WrightCorrection(mom, nNodes);
433  cout << "moments after WrightCorrection: " << endl;
434  printMoms(mom,nNodes);
435  }
436  // cout << "moments after check realizability: " << endl;
437  // printMoms(mom,nNodes);
438  }
439 
440  PDA(we, vi, mom, nNodes);
441 
442  Lm = LMax(T);
443  // calling the surogate models for bubble growth rates.
444 
445  // partial pressure within bubbles due to the evaporation of physical blowing agent
446  double p_1 = partialPressureBA(y);
447  // // partial pressure within bubbles due to the generation of CO2
448  double p_2 = partialPressureCO2(y);
449  double c_1 = L_l*rhoPolySurrgate*1000.0/M_B;
450  double c_2 = CO2_l*rhoPolySurrgate*1000.0/M_CO2;
451 
452  if (bubbleMode == "two nodes")
453  {
454  double radiusGrowthBA[nNodes],
455  radiusGrowthCO2[nNodes],
456  volumeGrowthBA[nNodes],
457  volumeGrowthCO2[nNodes],
458  nodeRadii[nNodes];
459  int ret_bblgr1[nNodes], ret_bblgr2[nNodes];
460  for (int i = 0; i < nNodes; i++)
461  {
462  nodeRadii[i] = nodeRadius(vi[i]);
463  // set input vector
464  modena_inputs_set(inputs_bblgr1, Tbblgr1pos, T);
465  modena_inputs_set(inputs_bblgr1, Rbblgr1pos, nodeRadii[i]);
466  // modena_inputs_set(inputs_bblgr1, KH1bblgr1pos, KH1);
467  modena_inputs_set(inputs_bblgr1, c_1bblgr1pos, c_1);
468  modena_inputs_set(inputs_bblgr1, p_1bblgr1pos, p_1);
469  // call the bblgr1 model
470  ret_bblgr1[i] = modena_model_call (bblgr1, inputs_bblgr1, outputs_bblgr1);
471  radiusGrowthBA[i] = modena_outputs_get(outputs_bblgr1, 0);
472  // set input vector
473  modena_inputs_set(inputs_bblgr2, Tbblgr2pos, T);
474  modena_inputs_set(inputs_bblgr2, Rbblgr2pos, nodeRadii[i]);
475  // modena_inputs_set(inputs_bblgr2, KH2bblgr2pos, KH2);
476  modena_inputs_set(inputs_bblgr2, c_2bblgr2pos, c_2);
477  modena_inputs_set(inputs_bblgr2, p_2bblgr2pos, p_2);
478  // call the bblgr2 model
479  ret_bblgr2[i] = modena_model_call (bblgr2, inputs_bblgr2, outputs_bblgr2);
480  // cout << "ret_bblgr2[" << i << "] = " << ret_bblgr2[i] << endl;
481  radiusGrowthCO2[i] = modena_outputs_get(outputs_bblgr2, 0);
482  // cout << "radiusGrowthCO2[" << i << "] = " << radiusGrowthCO2[i] << endl;
483 
484  if(modena_error_occurred())
485  {
486  cout << modena_error() << endl;
487  }
488  volumeGrowthBA[i] = (radiusGrowthBA[i]*RR*T)/(p_1);
489  volumeGrowthCO2[i] = (radiusGrowthCO2[i]*RR*T)/(p_2);
490 
491  if (volumeGrowthBA[i] < 0.0 || radiusGrowthBA[i] < 0.0 || L0 < 1.0e-8 || y[1] > X_gel)
492  {
493  volumeGrowthBA[i] = 0.0;
494  }
495  if (volumeGrowthCO2[i] < 0.0 || radiusGrowthCO2[i] < 0.0 || W_0 < 1.0e-8 || y[1] > X_gel)
496  {
497  volumeGrowthCO2[i] = 0.0;
498  }
499  }
500 
501  growthSource(sgBA, sgCO2, we, vi, nNodes, mOrder, CO2_l, L_l, T, volumeGrowthBA, volumeGrowthCO2);
502  }
503  else if (bubbleMode == "mean radius")
504  {
505  // bubble radius for bblgr1 and bblgr2 model
506  double R = bubbleRadius(mom[0], mom[1]);
507  // set input vector
508  modena_inputs_set(inputs_bblgr1, Tbblgr1pos, T);
509  modena_inputs_set(inputs_bblgr1, Rbblgr1pos, R);
510  // modena_inputs_set(inputs_bblgr1, KH1bblgr1pos, KH1);
511  modena_inputs_set(inputs_bblgr1, c_1bblgr1pos, c_1);
512  modena_inputs_set(inputs_bblgr1, p_1bblgr1pos, p_1);
513  // set input vector
514  modena_inputs_set(inputs_bblgr2, Tbblgr2pos, T);
515  modena_inputs_set(inputs_bblgr2, Rbblgr2pos, R);
516  // modena_inputs_set(inputs_bblgr2, KH2bblgr2pos, KH2);
517  modena_inputs_set(inputs_bblgr2, c_2bblgr2pos, c_2);
518  modena_inputs_set(inputs_bblgr2, p_2bblgr2pos, p_2);
519 
520  // call the bblgr1 model
521  int ret_bblgr_1 = modena_model_call (bblgr1, inputs_bblgr1, outputs_bblgr1);
522  // call the bblgr2 model
523  int ret_bblgr_2 = modena_model_call (bblgr2, inputs_bblgr2, outputs_bblgr2);
524 
525  double G1, G2;
526  double dVdt_1[nNodes], dVdt_2[nNodes];
527  for (int i = 0; i < nNodes; i++)
528  {
529  dVdt_1[i] = 0.0;
530  dVdt_2[i] = 0.0;
531  }
532  G1 = modena_outputs_get(outputs_bblgr1, 0);
533  G2 = modena_outputs_get(outputs_bblgr2, 0);
534  dVdt_1[0] = (G1*RR*T)/(p_1);
535  if (dVdt_1[0] < 0.0 || G1 < 0.0 || L0<1e-8 || y[1]>X_gel)
536  {
537  dVdt_1[0] = 0.0;
538  }
539  dVdt_1[1] = dVdt_1[0];
540  dVdt_2[0] = (G2*RR*T)/(p_2);
541  if (dVdt_2[0] < 0.0 || G2 < 0.0 || W_0<1e-8 || y[1]>X_gel)
542  {
543  dVdt_2[0] = 0.0;
544  }
545  dVdt_2[1] = dVdt_2[0];
546 
547  growthSource(sgBA, sgCO2, we, vi, nNodes, mOrder, CO2_l, L_l, T, dVdt_1, dVdt_2);
548  }
549  else
550  {
551  cerr << "Invalid choice of bubbleMode!" << endl;
552  exit(1);
553  }
554 
555  coalescenceSource(sc, we, vi, nNodes, mOrder, beta0);
556 
557  dydt[3] = -sgBA[1]*(p_1/(RR*y[2]))*(M_B/1000.0)*(1.0/rhoPolySurrgate);
558  dydt[4] = sgBA[1]*(p_1/(RR*y[2]))*(M_B/1000.0)*(1.0/rhoPolySurrgate);
559  dydt[6] = sgCO2[1]*(p_2/(RR*y[2]))*(M_CO2/1000.0)*(1.0/rhoPolySurrgate);
560  dydt[5] = -sgCO2[1]*(p_2/(RR*y[2]))*(M_CO2/1000.0)*(1.0/rhoPolySurrgate) +\
561  W_0*dydt[0]*(M_CO2/1000.0)*(1.0/rhoPolySurrgate);
562 
563  dydt[7] = sgBA[0] + sgCO2[0] + sc[0];
564  dydt[8] = sgBA[1] + sgCO2[1] + sc[1];
565  dydt[9] = sgBA[2] + sgCO2[2] + sc[2];
566  dydt[10] = sgBA[3] + sgCO2[3] + sc[3];
567  // temperature
568  C_TOT = C_Poly + CO2_g*C_CO2 + L_g*C_BG + L_l*C_BL;
569  dydt[2] = (-DH_OH*OH_0)/(rhoPolySurrgate*C_TOT)*dydt[1]+\
570  (-DH_W*W_0)/(rhoPolySurrgate*C_TOT)*dydt[0]+\
571  lambda/C_TOT*dydt[3];
572 }
578 int main(int argc, char **argv)
579 {
580  readParams();
581  #include "modenaCalls.h"
582 
583  // initial conditions
584  state_type y(31);
585  y[0] = 0.0;
586  y[1] = 0.0;
587  y[2] = Temp0;
588  y[3] = L0;
589  y[4] = 1.0e-14;
590  y[5] = 0.0;
591  y[6] = 1.0e-14;
592 
593  // moments initialization
594  int nOfmom = 4;
595  double momz[nOfmom];
596  double pBA, pCO2, bubble_radius, rho_foam;
597  mom_init(momz, init_size, nOfmom, sig, NN);
598 
599  if(momentsPositivite(momz,nOfmom))
600  {
601  y[7] = momz[0];
602  y[8] = momz[1];
603  y[9] = momz[2];
604  y[10] = momz[3];
605  }
606  double R = bubbleRadius(y[7], y[8]);
607  air_g=y[8]/(1+y[8])*M_air*1e-3*(Pr+2*surfaceTension/R)/(RR*Temp0*rhoPoly);
608 
609  // initialize RF-1 variables
610  y[11] = catalyst*1e-3;
611  y[12] = polyol1_ini*1e-3;
612  y[13] = polyol2_ini*1e-3;
613  y[14] = amine_ini*1e-3;
614  y[15] = max(W_0*1e-3,0.0);
615  y[16] = isocyanate1_ini*1e-3;
616  y[17] = isocyanate2_ini*1e-3;
617  y[18] = isocyanate3_ini*1e-3;
618  y[19] = 0.0;
619  y[20] = 0.0;
620  y[21] = 0.0;
621  y[22] = 0.0;
622  y[23] = 0.0;
623  y[24] = 0.0;
624  y[25] = 0.0;
625  y[26] = 0.0;
626  y[27] = 0.0;
627  y[28] = 0.0;
628  y[29] = 0.0;
629  y[30] = 0.0;
630 
631 
632  runge_kutta4< state_type > stepper;
633 
634  ofstream file;
635  file.open("resultsKinetics.txt");
636  file.setf(ios::scientific | ios::showpoint);
637  cout.precision(20);
638  cout.setf(ios::fixed | ios::showpoint);
639 
640  file << setw(12) << "t" << setw(12) << "Catalyst_1"
641  << setw(12) << "CE_A0"
642  << setw(12) << "CE_A1"
643  << setw(12) << "CE_B"
644  << setw(12) << "CE_B2"
645  << setw(12) << "CE_I0"
646  << setw(12) << "CE_I1"
647  << setw(12) << "CE_I2"
648  << setw(12) << "CE_PBA"
649  << setw(12) << "CE_Breac"
650  << setw(12) << "CE_Areac0"
651  << setw(12) << "CE_Areac1"
652  << setw(12) << "CE_Ireac0"
653  << setw(12) << "CE_Ireac1"
654  << setw(12) << "CE_Ireac2"
655  << endl;
656 
657  for( double t=0.0 ; t<tend ; t+= dt )
658  {
659  cout << "Time = " << t << endl;
661  integrate_adaptive( make_controlled( abs_err , rel_err , error_stepper_type() ), QmomKinetics , y , t, t+dt , 1e-9 );
662  file << setw(12) << t << " " << setw(12) << y[11] << " " << setw(12) << y[12] << " " << setw(12) << y[13] << " "
663  << setw(12) << y[14] << " " << setw(12) << y[15] << " " << setw(12) << y[16] << " "
664  << setw(12) << y[17] << " " << setw(12) << y[18] << " " << setw(12) << y[19] << " "
665  << setw(12) << y[20] << " " << setw(12) << y[21] << " " << setw(12) << y[22] << " "
666  << setw(12) << y[23] << " " << setw(12) << y[24] << " " << setw(12) << y[25] << " "
667  << endl;
668  write_kinetics(y, t);
669 
670  pBA = partialPressureBA(y);
671  pCO2 = partialPressureCO2(y);
672  bubble_radius = bubbleRadius(y[7], y[8]);
673  ddtpartialPressure(y, t, dt, dpdt, pOld, pBA, pCO2, bubble_radius);
674  }
675  ofstream file2;
676  rho_foam=rhoPoly*(1.0 - (y[8]/(1.0+y[8])));
677  file2.open("after_foaming.txt");
678  file2.setf(ios::scientific | ios::showpoint);
679  file2 << setw(24) << "#foam_density"
680  << setw(24) << "mean_cell_diameter"
681  << setw(24) << "pressure1"
682  << setw(24) << "pressure2"
683  << endl;
684  file2 << setw(24) << rho_foam
685  << setw(24) << 2*bubble_radius
686  << setw(24) << pBA
687  << setw(24) << pCO2
688  << endl;
689 }
690 
691 /*
692 
693 Different methods of integrations:
694 
695 [ define_const_stepper
696  runge_kutta4< state_type > stepper;
697  integrate_const( stepper , harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
698 ]
699 
700 [ integrate_const_loop
701  const double dt = 0.01;
702  for( double t=0.0 ; t<10.0 ; t+= dt )
703  stepper.do_step( harmonic_oscillator , x , t , dt );
704  ]
705 
706  [ define_adapt_stepper
707  typedef runge_kutta_cash_karp54< state_type > error_stepper_type;
708  ]
709 
710  [integrate_adapt_make_controlled
711  integrate_adaptive( make_controlled< error_stepper_type >( 1.0e-10 , 1.0e-6 ) ,
712  harmonic_oscillator , x , 0.0 , 10.0 , 0.01 );
713  ]
714 [ integrate_const with abs and rel error
715 integrate_const( make_dense_output( 1.0e-6 , 1.0e-6 , runge_kutta_dopri5< state_type >() ) , sys , inout , t_start , t_end , dt );
716 irst two parameters are the absolute and the relative error tolerances
717 ]
718 [ using adaptive integrate
719 double abs_err = 1.0e-12;
720  double rel_err = 1.0e-10;
721  integrate_adaptive( make_controlled< error_stepper_type >(abs_err , rel_err), kinetics, y, 0.0, 300.0, 0.01, write_kinetics );
722 
723 ]
724 
725 */
size_t source_CE_Ireac2_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:160
Source terms due to the bubble growth.
size_t source_CE_PBA_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:142
size_t source_CE_A1_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:124
size_t m0_rheopos
memory allocation for the moment of order zero as the input of rheology surrogate model ...
Definition: modenaData.H:220
double E_W
activation energy for the blowing reaction, J/mol
Calculate difference table for the moments realizability check.
double C_BL
Specific heat of the physical blowing agent in liquid phase, J/kg K.
checks for moments realizability
real(dp) polyol1_ini
initial concentration of polyol 1
Definition: globals.f90:49
size_t CE_I2_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:79
size_t source_CE_Breac_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:145
double NN
correlated to number of initial bubbles in m^3
size_t source_Bulk_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:163
size_t modena_model_inputs_argPos(const modena_model_t *self, const char *name)
Function determining position of an argument in the input vector.
Definition: model.c:366
double rel_err
relative error
double dt
time step, s
initializes the moments
double abs_err
absolute error
double LMax(double)
Definition: liquidBA.h:11
size_t source_CE_Areac0_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:148
int HankelHadamard(const double *m, int nNodes)
Hankel-Hadamard function.
double OH_0
Initial concentration of polyol OH groups in the mixutre, mol/m3.
real(dp), dimension(:), allocatable y
state variables
Definition: globals.f90:106
real(dp) amine_ini
initial concentration of amine
Definition: globals.f90:49
Source term due to the bubble coalescence.
size_t CE_Ireac0_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:94
size_t CE_PBA_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:82
size_t CE_Breac_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:85
void printMoms(const double *m, int nNodes)
prints moments
void growthSource(double *, double *, double *, double *, int &, int *, double &, double &, double &, double *, double *)
Definition: growth.h:22
double sig
correlated to variance of initial distribution
size_t R_1_mass_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:112
void WrightCorrection(double *m, int nNodes)
size_t source_CE_I1_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:136
real(dp) isocyanate2_ini
initial concentration of isocyanate 2
Definition: globals.f90:49
bool momentsPositivite(const double *moments, int &nNodes)
size_t Catalyst_1_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:58
void coalescenceSource(double *, double *, double *, int &, int *, double &)
Definition: coalescence.h:17
size_t source_CE_B_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:127
int denMod
density mode, 1 = modena, 2 = constant
double C_BG
Specific heat of the physical blowing agent in gas phase, J/kg K.
real(dp) isocyanate3_ini
initial concentration of isocyanate 3
Definition: globals.f90:49
size_t CE_Areac1_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:91
double partialPressureCO2(const state_type &y)
partial pressure of CO2
size_t CE_I1_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:76
controlled_stepper_type controlled_stepper
double surfaceTension
required for the computation of partial pressure
size_t temp_rheopos
memory allocation for the temperature as the input of rheology surrogate model
Definition: modenaData.H:211
write the results into text files
size_t source_CE_Ireac1_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:157
void QmomKinetics(const state_type &y, state_type &dydt, double t)
This is to calculate the RHD of all the ODEs.
double M_CO2
Molecular mass of carbon dioxide, kg/kmol.
Product Difference Algorithm.
size_t CE_Ireac2_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:100
double W_0
Initial concentration of water in the mixture, mol/m3.
size_t source_CE_A0_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:121
size_t CE_Areac0_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:88
size_t source_CE_I2_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:139
double init_size
initial mean bubble diameter, m
int modena_model_call(modena_model_t *self, modena_inputs_t *inputs, modena_outputs_t *outputs)
Function calling the surrogate model and checking for errors.
Definition: model.c:553
double RR
ideal gas constant, J/mol K
size_t m1_rheopos
memory allocation for the moment of order one as the input of rheology surrogate model ...
Definition: modenaData.H:223
double DH_OH
Reaction heat for the gelling reaction, J/mol.
size_t source_CE_I0_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:133
double Pr
initial/final pressure of the mixture, Pa
size_t CE_I0_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:73
size_t source_R_1_mass_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:169
size_t source_CE_Ireac0_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:154
double dpdt[2]
This is used to compute the partial pressures.
double C_TOT
Total specifc heat of the mixture.
double bubbleRadius(const double m0, const double m1)
radius of bubbles based on the moments
Definition: bubbleRadius.h:28
int main(int argc, char *argv[])
Reads parameters. Creates struts and walls. Saves foam morphology to a file.
Definition: foams.cc:23
double air_g
air weight fraction
size_t conv_rheopos
memory allocation for the conversion as the input of rheology surrogate model
Definition: modenaData.H:217
double rhoPoly
density of the liquid polymer, kg/m3
size_t source_Catalyst_1_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:118
void modena_model_destroy(modena_model_t *self)
Function deallocating the memory allocated for the surrogate model.
Definition: model.c:665
allocate memory for the surrogate models
void write_kinetics(const state_type &y, const double t)
double nodeRadius(const double &v)
radius of bubbles at each node
Definition: bubbleRadius.h:38
bool dilution
use dilution effect
double rhoBL
density of the blowing agent, kg/m3
double C_CO2
CO2 specific heat, J/kg K.
size_t R_1_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:106
real(dp) polyol2_ini
initial concentration of polyol 2
Definition: globals.f90:49
double NCO_0
Initial concentration of isocianate NCO groups in the mixutre, mol/m3.
void mom_init(double *, double &, int &, double &, double &)
double M_B
Molecular mass of blowing agent, kg/kmol.
reads the inputs from input files.
Converts moments based on the unit volume of foam.
double partialPressureBA(const state_type &y)
partial pressure of the physical blowing agent
Maximum soluble physical blowing agent in the liquid mixture.
Wright corrections for moments realizability.
size_t Bulk_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:103
All the experimental data required for the source terms calculations.
double Temp0
initial temperature, K
size_t source_CE_Areac1_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:151
double M_air
Molecular weight of air, kg/kmol.
size_t kineticTime_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:55
double L0
Initial weight fraction of blowing agent in the liquid, -.
size_t CE_A1_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:64
size_t CE_B_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:67
functions related to the calculations of partial pressures
int kinMod
kinetics model, 1 = Baser, 2 = Baser with R(x)
void PDA(double *, double *, double *, int &)
Definition: pda.h:13
real(dp) isocyanate1_ini
initial concentration of isocyanate 1
Definition: globals.f90:49
size_t shear_rheopos
memory allocation for the shear rate as the input of rheology surrogate model
Definition: modenaData.H:214
size_t source_CE_B2_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:130
size_t CE_B2_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:70
double A_OH
pre-exponential factor for the gelling reaction, 1/s
Calcualte the determinant of an n by n matrix.
void McGrawCorrection(double *moments, int nNodes)
McGraw correction algorithm.
instantiate the surrogate models:
double tend
end time, s
double pOld[2]
This is to hold the old pressure values during the partial pressure calculations. ...
void modena_model_argPos_check(const modena_model_t *self)
Function checking that the user has queried all input positions.
Definition: model.c:408
double DH_W
Reaction heat for the blowing reaction, J/mol.
size_t CE_Ireac1_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:97
double A_W
pre-exponential factor for the blowing reaction, 1/s
real(dp) catalyst
concentration of catalyst
Definition: globals.f90:49
size_t CE_A0_Pos
memory allocation for inputs of kinetics surrogate model
Definition: modenaData.H:61
double E_OH
activation energy for the gelling reaction, J/mol
void ddtpartialPressure(const state_type &y, const double t, const double dt, double *dpdt, double *pOld, const double p_1, const double p_2, const double R)
time derivative of partial pressure
McGraw correction algorithm.
size_t source_R_1_Pos
memory allocation for outputs of kinetics surrogate model
Definition: modenaData.H:166