msbGrid  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gridcreator.hh
Go to the documentation of this file.
1 /*****************************************************************************
2 * This program is part of the msbGrid software *
3 * *
4 * msbGrid stands for multi-structured block Grid generator *
5 * *
6 * msbGrid is a free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * msbGrid is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * See the file COPYING for full copying permissions. *
17 *****************************************************************************/
23 #include <inputdata.hh>
24 #include <gridfactory.hh>
25 #include <commandlinereader.hh>
26 
27 #ifndef _GRID_CREATOR_HH_
28 #define _GRID_CREATOR_HH_
29 
30 namespace msbGrid
31 {
32 template< typename CellT, typename InitialDistributionT >
33 int makeGrid( int argc,
34  char **argv,
35  void (*usage)(const char *, const std :: string &) );
36 
37 std :: string usageTextBlock()
38 {
39  return "Options usually are parameters given to the simulation, \n"
40  "and have to be specified with this syntax: \n"
41  "\t--ParameterName=VALUE, for example --xMin=\"0\"\n"
42  "Parameters specified on the command line have priority over those in the parameter file.\n"
43  "If no parameter file name is given, './<programname>.input' is chosen as default.\n"
44  "\n"
45  "Important options include:\n"
46  "\t-h, --help Print this usage message and exit\n"
47  "\t-PrintParameters [true|false] Print the run-time modifiable parameters _after_ \n"
48  "\t the simulation [default: true]\n"
49  "\t-PrintProperties [true|false] Print the compile-time parameters _before_ \n"
50  "\t the simulation [default: false]\n"
51  "\t-ParameterFile FILENAME File with parameter definitions\n"
52  "\n";
53 }
54 }
55 
56 template< typename CellT, typename InitialDistributionT >
57 int msbGrid :: makeGrid( int argc,
58  char **argv,
59  void (*usage)(const char *, const std :: string &) )
60 {
61  typedef CellT Cell;
62  enum { dim = Cell :: dim };
63 
64  typedef InitialDistributionT InitialDistribution;
65 
67  typedef msbGrid :: Scalar Scalar;
69  typedef msbGrid :: Point Point;
70  typedef msbGrid :: Edge Edge;
71 
72  try
73  {
75  // parse the command line arguments
77 
78  // check whether the user did not specify any parameter.
79  // in this case print the usage message
80  if ( argc == 1 ) {
81  std :: cout << "No parameter file given. Defaulting to '"
82  << argv [ 0 ]
83  << ".input' for input file.\n";
84  std :: ifstream parameterFile;
85  // check whether the parameter file exists.
86  std :: string defaultName = argv [ 0 ];
87  defaultName += ".input";
88  parameterFile.open( defaultName.c_str() );
89  if ( !parameterFile.is_open() ) {
90  std :: cout << " -> Could not open file '"
91  << defaultName
92  << "'. <- \n\n";
93  usage( argv [ 0 ], msbGrid :: usageTextBlock() );
94  return 1;
95  }
96 
97  parameterFile.close();
98  }
99 
100  // check whether the user wanted to see the help message
101  for ( int i = 1; i < argc; ++i ) {
102  if ( std :: string("--help") == argv [ i ] || std :: string("-h") == argv [ i ] ) {
103  usage( argv [ 0 ], msbGrid :: usageTextBlock() );
104  return 0;
105  }
106  }
107 
108  // check whether the user wanted to see the package version
109  for ( int i = 1; i < argc; ++i ) {
110  if ( std :: string("--version") == argv [ i ] || std :: string("-v") == argv [ i ] ) {
111  std :: cout << PACKAGE_STRING << "\n";
112  return 0;
113  }
114  }
115 
116  // fill the parameters tree with the options from the command line
117  std :: string s = msbGrid :: readCommandLineOptions( argc, argv, msbGrid :: ParameterTree :: tree() );
118  #if VERBOSE_LEVEL >= 2
119  std :: cout << "command line options are:\n";
121  #endif
122 
123  if ( !s.empty() ) {
124  std :: cerr << s;
125  std :: string usageMessage = msbGrid :: usageTextBlock();
126  usage(argv [ 0 ], usageMessage);
127  return 1;
128  }
129 
130  // obtain the name of the parameter file
131  std :: string defaultName = argv [ 0 ];
132  defaultName += ".input";
133  std :: string parameterFileName = msbGrid :: ParameterTree :: tree().get("ParameterFile", defaultName);
134 
135  // open and check whether the parameter file exists.
136  std :: ifstream parameterFile( parameterFileName.c_str() );
137  if ( !parameterFile.is_open() ) {
138  // if the name of the file has been specified,
139  // this must be an error. Otherwise proceed.
140  if ( ParameterTree :: tree().hasKey("ParameterFile") ) {
141  std :: cout << "\n\t -> Could not open file '"
142  << parameterFileName
143  << "'. <- \n\n";
144  usage( argv [ 0 ], usageTextBlock() );
145  return 1;
146  }
147  } else {
148  // read parameters from the file without overwriting
151  /*overwrite=*/ false);
152  }
153 
154  parameterFile.close();
155 
156  // read primary input data
157  std :: string outputPath = msbGrid :: ParameterTree :: tree().get("OutputPath", "../../output/");
158  std :: string outputDirName = msbGrid :: ParameterTree :: tree().get("OutputDirName", "");
159  const Scalar xMin = msbGrid :: ParameterTree :: tree().get("XMin", 0.0);
160  const Scalar xMax = msbGrid :: ParameterTree :: tree().get("XMax", 1.0);
161  std :: vector< Scalar > blockNumberCoeff;
162  blockNumberCoeff.clear();
163  blockNumberCoeff.push_back( msbGrid :: ParameterTree :: tree().get("BnCx", 1.0) ); // x axis
164  blockNumberCoeff.push_back( msbGrid :: ParameterTree :: tree().get("BnCy", 1.0) ); // y axis
165  const Scalar dr = msbGrid :: ParameterTree :: tree().get("Dr", 0.1);
166  const Scalar rafCoeff = msbGrid :: ParameterTree :: tree().get("RafC", 1.0);
167  // read secondary input data
168  const std :: string blockCenterDistribution = msbGrid :: ParameterTree :: tree().get("BCDistType", "random");
169  const std :: string blockAngleDistribution = msbGrid :: ParameterTree :: tree().get("BADistType", "random");
170  const int blockCenterRandSeqIdDefault = 0 /*(unsigned)time(0)*/;
171  const int blockAngleRandSeqIdDefault = 0 /*(unsigned)time(0)*/;
172  const int blockCenterRandSeqId = msbGrid :: ParameterTree :: tree().get("BCId", blockCenterRandSeqIdDefault);
173  const int blockAngleRandSeqId = msbGrid :: ParameterTree :: tree().get("BAId", blockAngleRandSeqIdDefault);
174  const Scalar blockCenterPertCoeff = msbGrid :: ParameterTree :: tree().get("BCPC", 0.0);
175  const Scalar dist2BoundCoeff = msbGrid :: ParameterTree :: tree().get("D2bC", 1.0);
176  const Scalar interBlockDistCoeff = msbGrid :: ParameterTree :: tree().get("IntbC", 1.0);
177 
178  #if VERBOSE_LEVEL >= 2
179  std :: cout << "\n//*** primary input data ***//\n";
180  std :: cout << "outputPath = " << outputPath << "\n";
181  std :: cout << "xMin = " << xMin << ( xMin == 0 ? " (default value)" : "" ) << "\n";
182  std :: cout << "xMax = " << xMax << ( xMax == 1 ? " (default value)" : "" ) << "\n";
183  std :: cout << "blockNumberCoeffx = " << blockNumberCoeff [ 0 ] << ( blockNumberCoeff [ 0 ] == 1 ? " (default value)" : "" ) << "\n";
184  std :: cout << "blockNumberCoeffy = " << blockNumberCoeff [ 1 ] << ( blockNumberCoeff [ 1 ] == 1 ? " (default value)" : "" ) << "\n";
185  std :: cout << "dr = " << dr << ( dr == 0.1 ? " (default value)" : "" ) << "\n";
186  std :: cout << "rafCoeff = " << rafCoeff << ( rafCoeff == 1 ? " (default value)" : "" ) << "\n";
187 
188  std :: cout << "\n//*** secondary input data ***//\n";
189  std :: cout << "blockCenterDistribution = " << blockCenterDistribution << ( blockCenterDistribution == "periodic" ? " (default value)" : "" ) << "\n";
190  std :: cout << "blockAngleDistribution = " << blockAngleDistribution << ( blockAngleDistribution == "random" ? " (default value)" : "" ) << "\n";
191  std :: cout << "blockCenterRandSeqId = " << blockCenterRandSeqId << ( blockCenterRandSeqId == blockCenterRandSeqIdDefault ? " ( = (unsigned)time(0))" : "" ) << "\n";
192  std :: cout << "blockAngleRandSeqId = " << blockAngleRandSeqId << ( blockAngleRandSeqId == blockAngleRandSeqIdDefault ? " ( = (unsigned)time(0))" : "" ) << "\n";
193  std :: cout << "blockCenterPertCoeff = " << blockCenterPertCoeff << ( blockCenterPertCoeff == 0 ? " (default value)" : "" ) << "\n";
194  std :: cout << "dist2BoundCoeff = " << dist2BoundCoeff << ( dist2BoundCoeff == 1 ? " (default value)" : "" ) << "\n";
195  std :: cout << "interBlockDistCoeff = " << interBlockDistCoeff << ( interBlockDistCoeff == 1 ? " (default value)" : "" ) << "\n";
196  #endif
197 
198  InputData inputDataTmp(xMin, xMax, blockNumberCoeff, dr, rafCoeff, dist2BoundCoeff, interBlockDistCoeff, outputPath, outputDirName);
199  inputDataTmp.setSecondaryInputData(blockCenterDistribution, blockAngleDistribution, blockCenterRandSeqId, blockAngleRandSeqId, blockCenterPertCoeff);
200 
201  InitialDistribution initialDist( &inputDataTmp);
202 
203  if ( blockCenterDistribution == "periodic" ) {
204  initialDist.periodicDist();
205  }
206 
207  if ( blockCenterDistribution == "random" ) {
208  initialDist.randomDist();
209  }
210 
211  if ( blockCenterDistribution == "predefined" ) {
212  initialDist.predefinedDist();
213  }
214 
215  if ( blockAngleDistribution == "random" ) {
216  initialDist.randomAng();
217  }
218 
219  if ( blockAngleDistribution == "predefined" ) {
220  initialDist.predefinedAng();
221  }
222 
223  /* input data */
224  InputData inputData( xMin, xMax, blockNumberCoeff, dr, rafCoeff, dist2BoundCoeff, interBlockDistCoeff
225  , outputPath, outputDirName, initialDist.blockCenter(), initialDist.blockAngle() );
226 
227  if ( blockCenterDistribution == "predefined" ) {
228  inputData.xMin() = initialDist.inData().xMin();
229  inputData.xMax() = initialDist.inData().xMax();
230  }
231 
232  inputData.setSecondaryInputData(blockCenterDistribution
233  , blockAngleDistribution
234  , blockCenterRandSeqId
235  , blockAngleRandSeqId
236  , blockCenterPertCoeff);
237 
238  /* make the grid */
239  GridFactory gridFactory1( &inputData);
240 
241  return 0;
242  }
243 
244  catch(int x)
245  {
246  return 1;
247  }
248 }
249 
250 #endif /* _GRID_CREATOR_HH_ */