コード例 #1
0
/// Main program of uncertainty propagation of the ODE model parameters via intrusive spectral projection (ISP)
int main()
{
  // Model parameters
  Array1D<double> modelparams;
  // Model parameter names
  Array1D<string> modelparamnames;
  // Auxiliary parameters: final time and time step of integration
  Array1D<double> modelauxparams;
  
  // Read the xml tree
  RefPtr<XMLElement> xmlTree=readXMLTree("lorenz.in.xml");
  // Read the model-specific input
  readXMLModelInput(xmlTree,modelparams, modelparamnames, modelauxparams);
  // Total nuber of input parameters
  int fulldim=modelparams.XSize();
  // Read the output preferences
  dumpInfo* outPrint=new dumpInfo;
  readXMLDumpInfo( xmlTree, &(outPrint->dumpInt), &(outPrint->fdumpInt), &(outPrint->dumpfile) );

  // Output PC order
 int order;
 // PC type
 string pcType;

 // A 2d array (each row is an array of coefficients for the corresponding uncertain input parameter)
 Array2D<double> allPCcoefs;
 // The indices of the uncertain model parameters in the list of model parameters
 Array1D<int> uncParamInd;
 // Read the UQ-specific information from the xml tree
 readXMLUncInput(xmlTree,allPCcoefs,uncParamInd , &order, &pcType);

 // Stochastic dimensionality
 int dim=uncParamInd.XSize();
 
 // Instantiate a PC object for ISP computations
 PCSet myPCSet("ISP",order,dim,pcType,0.0,1.0); 
 
 // The number of PC terms
 const int nPCTerms = myPCSet.GetNumberPCTerms();
 cout << "The number of PC terms in an expansion is " << nPCTerms << endl;

  // Print the multiindices on screen
  myPCSet.PrintMultiIndex();

 // Initial time
 double t0 = 0.0;
 // Final time
 double tf = modelauxparams(0);
 // Time step
 double dTym = modelauxparams(1);
 // Number of steps
 int nStep=(int) tf / dTym;
 
  // Initial conditions of zero coverage (based on Makeev:2002)
  Array1D<double> u(nPCTerms,0.e0);
  Array1D<double> v(nPCTerms,0.e0);
  Array1D<double> w(nPCTerms,0.e0);
  Array1D<double> z(nPCTerms,0.e0);

  // Array to hold the PC representation of the number 1
  Array1D<double> one(nPCTerms,0.e0);
  one(0)=1.0;

  // The z-species is described as z=1-u-v-w
  z=one;
  myPCSet.SubtractInPlace(z,u);
  myPCSet.SubtractInPlace(z,v);
  myPCSet.SubtractInPlace(z,w);

  // Right-hand sides
  Array1D<double> dudt(nPCTerms,0.e0);
  Array1D<double> dvdt(nPCTerms,0.e0);
  Array1D<double> dwdt(nPCTerms,0.e0);

  // Array of arrays to hold the input parameter PC representations in the output PC
  // Each element is an array of coefficients for the corresponding input parameter, whether deterministic or uncertain
  // The size of the array is the total number input parameters
  
  Array1D< Array1D<double> > modelparamPCs(fulldim);
  

  printf("\nInput parameter PC coefficients are given below\n");
  for (int i=0; i<fulldim; i++){
    printf("%s: ",modelparamnames(i).c_str());
    modelparamPCs(i).Resize(nPCTerms,0.e0);
    for (int j=0; j<nPCTerms; j++){
      modelparamPCs(i)(j)=allPCcoefs(j,i);
      printf(" %lg ",modelparamPCs(i)(j));
    }
    printf("\n");
  }
  printf("\n");


  // Initial time and time step counter
  int step=0;
  double tym=t0;

  // Work arrays for integration
  Array1D<double> u_o(nPCTerms,0.e0);
  Array1D<double> v_o(nPCTerms,0.e0);
  Array1D<double> w_o(nPCTerms,0.e0);

  Array1D<double> tmp_u(nPCTerms,0.e0);
  Array1D<double> tmp_v(nPCTerms,0.e0);
  Array1D<double> tmp_w(nPCTerms,0.e0);

  // File to write the mean and stdev, name read from xml
  FILE *f_dump,*modes_dump;
  if(!(f_dump = fopen(outPrint->dumpfile.c_str(),"w"))){ 
    printf("Could not open file '%s'\n",outPrint->dumpfile.c_str()); 
    exit(1); 
  }
  
  // File to dump the PC modes, name hardwired
  string modes_dumpfile = "solution_ISP_modes.dat";
  if(!(modes_dump = fopen(modes_dumpfile.c_str(),"w"))){ 
    printf("Could not open file '%s'\n",modes_dumpfile.c_str()); 
    exit(1); 
  }

  // write time, u, v, w (all modes) to file
  WriteModesToFilePtr(tym, u.GetArrayPointer(), v.GetArrayPointer(), w.GetArrayPointer(), nPCTerms, modes_dump);
    
  // Write out initial step
  // Get standard deviations
  double uStDv = myPCSet.StDv(u);
  double vStDv = myPCSet.StDv(v);
  double wStDv = myPCSet.StDv(w);

  // write u, v, w (mean and standard deviation) to file
  WriteMeanStdDevToFilePtr(tym, u(0), v(0), w(0), uStDv, vStDv, wStDv, f_dump);
    
  // write u, v, w (mean and standard deviation) to screen
  WriteMeanStdDevToStdOut(step, tym, u(0), v(0), w(0), uStDv, vStDv, wStDv);
    
  // Forward run
  while(tym < tf) {
    // Integrate with 2nd order Runge Kutta

    // Save solution at current time step
    myPCSet.Copy(u_o,u);
    myPCSet.Copy(v_o,v);
    myPCSet.Copy(w_o,w);
    
    // Compute right hand sides
    GetRHS(myPCSet,modelparamPCs(0).GetArrayPointer(),modelparamPCs(1).GetArrayPointer(),modelparamPCs(2).GetArrayPointer(),u.GetArrayPointer(),v.GetArrayPointer(),w.GetArrayPointer(),dudt.GetArrayPointer(),dvdt.GetArrayPointer(),dwdt.GetArrayPointer());

    // Advance u, v, w to mid-point
    myPCSet.Multiply(dudt,0.5*dTym,tmp_u); // 0.5*dTym*dudt
    myPCSet.Multiply(dvdt,0.5*dTym,tmp_v); // 0.5*dTym*dvdt
    myPCSet.Multiply(dwdt,0.5*dTym,tmp_w); // 0.5*dTym*dwdt

    myPCSet.Add(u_o,tmp_u,u); // u = u_o + 0.5*dTym*dudt
    myPCSet.Add(v_o,tmp_v,v); // v = v_o + 0.5*dTym*dvdt
    myPCSet.Add(w_o,tmp_w,w); // w = w_o + 0.5*dTym*dwdt
    
    // Compute z = 1 - u - v - w
    z=one;
    myPCSet.SubtractInPlace(z,u);
    myPCSet.SubtractInPlace(z,v);
    myPCSet.SubtractInPlace(z,w);
    
    // Compute right hand sides
    GetRHS(myPCSet,modelparamPCs(0).GetArrayPointer(),modelparamPCs(1).GetArrayPointer(),modelparamPCs(2).GetArrayPointer(),u.GetArrayPointer(),v.GetArrayPointer(),w.GetArrayPointer(),dudt.GetArrayPointer(),dvdt.GetArrayPointer(),dwdt.GetArrayPointer());
    
    // Advance u, v, w to next time step
    myPCSet.Multiply(dudt,dTym,tmp_u); // dTym*dudt
    myPCSet.Multiply(dvdt,dTym,tmp_v); // dTym*dvdt
    myPCSet.Multiply(dwdt,dTym,tmp_w); // dTym*dwdt
    
    
    myPCSet.Add(u_o,tmp_u,u); // u = u_o + dTym*dudt
    myPCSet.Add(v_o,tmp_v,v); // v = v_o + dTym*dvdt
    myPCSet.Add(w_o,tmp_w,w); // w = w_o + dTym*dwdt
    
    // Compute z = 1 - u - v - w
    z=one;
    myPCSet.SubtractInPlace(z,u);
    myPCSet.SubtractInPlace(z,v);
    myPCSet.SubtractInPlace(z,w);


    // Advance time and step counter
    tym += dTym;
    step+=1;
   

    // write time, u, v, w (all modes) to file
    if(step % outPrint->fdumpInt == 0){
      WriteModesToFilePtr(tym, u.GetArrayPointer(), v.GetArrayPointer(), w.GetArrayPointer(), nPCTerms, modes_dump);
    }

    // Get standard deviations
    uStDv = myPCSet.StDv(u);
    vStDv = myPCSet.StDv(v);
    wStDv = myPCSet.StDv(w);
    
    // write u, v, w (mean and standard deviation) to file
    if(step % outPrint->fdumpInt == 0){
      WriteMeanStdDevToFilePtr(tym, u(0), v(0), w(0), uStDv, vStDv, wStDv, f_dump);
    }
    // write u, v, w (mean and standard deviation) to screen
    if(step % outPrint->dumpInt == 0){
      WriteMeanStdDevToStdOut(step, tym, u(0), v(0), w(0), uStDv, vStDv, wStDv);
    }
    
  }
  
  // Close output file
  if(fclose(f_dump)){ 
    printf("Could not close file '%s'\n",outPrint->dumpfile.c_str()); 
    exit(1); 
  }


  // Close output file
  if(fclose(modes_dump)){ 
    printf("Could not close file '%s'\n",modes_dumpfile.c_str()); 
    exit(1); 
  }
  
  return 0;
}
コード例 #2
0
int main(int ac, char** av)
{
  if (ac != 2) error_macro("expecting 1 argument: CMAKE_BINARY_DIR")

  std::string
    dir = string(av[1]) + "/paper_GMD_2015/fig_a/",
    h5  = dir + "out_lgrngn",
    svg = dir + "out_lgrngn_spec.svg";

  Gnuplot gp;

  int off = 2; // TODO!!!
  float ymin = .4 * .01, ymax = .9 * 10000;
  const int at = 9000;

  gp << "set term svg dynamic enhanced fsize 15 size 900, 1500 \n";
  gp << "set output '" << svg << "'\n";
  gp << "set logscale xy\n";
  gp << "set xrange [.002:100]\n";
  gp << "set yrange [" << ymin << ":" << ymax << "]\n";
  gp << "set ylabel '[mg^{-1} μm^{-1}]'\n"; // TODO: add textual description (PDF?)
  gp << "set grid\n";
  gp << "set nokey\n";

  // FSSP range
  gp << "set arrow from .5," << ymin << " to .5," << ymax << " nohead\n";
  gp << "set arrow from 25," << ymin << " to 25," << ymax << " nohead\n";

  gp << "set xlabel offset 0,1.5 'particle radius [μm]'\n";
  gp << "set key samplen 1.2\n";
  gp << "set xtics rotate by 65 right (.01, .1, 1, 10, 100) \n";

// TODO: use dashed lines to allow printing in black and white... same in image plots

  assert(focus.first.size() == focus.second.size());
  gp << "set multiplot layout " << focus.first.size() << ",2 columnsfirst upwards\n";

  // focus to the gridbox from where the size distribution is plotted
  char lbl = 'i';
  for (auto &fcs : std::set<std::set<std::pair<int,int>>>({focus.first, focus.second}))
  {
    for (auto it = fcs.begin(); it != fcs.end(); ++it)
    {
      const int &x = it->first, &y = it->second;

      gp << "set label 1 '(" << lbl << ")' at graph -.15, 1.02 font ',20'\n";
      //gp << "set title 'x=" << x << " y=" << y << "'\n";

      std::map<float, float> focus_d;
      std::map<float, float> focus_w;

      //info on the number and location of histogram edges
      vector<quantity<si::length>> left_edges_rd = bins_dry();
      int nsd = left_edges_rd.size() - 1;
      vector<quantity<si::length>> left_edges_rw = bins_wet();
      int nsw = left_edges_rw.size() - 1;

      for (int i = 0; i < nsd; ++i)
      {
	const string name = "rd_rng" + zeropad(i) + "_mom0";
	blitz::Array<float, 2> tmp_d(1e-6 * h5load(h5, name, at));

	focus_d[left_edges_rd[i] / 1e-6 / si::metres] = sum(tmp_d(
	  blitz::Range(x-1, x+1),
	  blitz::Range(y-1, y+1)
	)) 
	/ 9  // mean over 9 gridpoints
	/ ((left_edges_rd[i+1] - left_edges_rd[i]) / 1e-6 / si::metres); // per micrometre
      }

      for (int i = 0; i < nsw; ++i)
      {
	const string name = "rw_rng" + zeropad(i + off) + "_mom0";
	blitz::Array<float, 2> tmp_w(1e-6 * h5load(h5, name, at));

	focus_w[left_edges_rw[i] / 1e-6 / si::metres] = sum(tmp_w(
	  blitz::Range(x-1, x+1),
	  blitz::Range(y-1, y+1)
	)) 
	/ 9 
	/ ((left_edges_rw[i+1] - left_edges_rw[i]) / 1e-6 / si::metres); // per micrometre
      }

      notice_macro("setting-up plot parameters");
      gp << "plot"
	 << "'-' with histeps title 'wet radius' lw 3 lc rgb 'blue'," 
	 << "'-' with histeps title 'dry radius' lw 1 lc rgb 'red' " << endl;
      gp.send(focus_w);
      gp.send(focus_d);

      lbl -= 2;
    }
    lbl = 'j';
  }
}