msbGrid  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
geofileswriter.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 #ifndef _GEO_FILES_WRITER_HH_
24 #define _GEO_FILES_WRITER_HH_
25 
26 #include <fstream>
27 #include <iomanip>
28 #include <cstdlib>
29 
30 namespace msbGrid
31 {
32 class GeoFilesWriter;
33 }
34 
35 class msbGrid :: GeoFilesWriter
36 {
37 public:
40 
44 
45 private:
47  std :: ofstream outputFile_;
48 
49 public:
50 
51  GeoFilesWriter(const InputData * inputData)
52  {
53  inputData_ = inputData;
54  }
55 
57 
58  void openFile(const std :: string &name)
59  {
60  std :: string fileName = inputData_->outputPath() + "geo/" + name + ".geo";
61 #if VERBOSE_LEVEL >= 2
62  std :: cout << "fileName = " << fileName << "\n";
63 #endif
64  outputFile_.open(fileName.c_str(), std :: ios :: out);
65  if ( !outputFile_ ) {
66  std :: cerr << "Can't open output file" << fileName << std :: endl;
67  std :: exit(1);
68  }
69 
70  outputFile_ << "/////////////////////////////////////////////////\n";
71  outputFile_ << "// This file was generated by msbGrid software //\n";
72  outputFile_ << "/////////////////////////////////////////////////\n\n";
73 
75 
76  outputFile_ << "// primary input data : //\n";
77 
78  outputFile_ << "// xMin = " << inputData_->xMin() << "\n";
79  outputFile_ << "// xMax = " << inputData_->xMax() << "\n";
80 
81  outputFile_ << "// blockNumberCoeffx = " << inputData_->blockNumberController() [ 0 ]
82  << " , blockNumberCoeffy = " << inputData_->blockNumberController() [ 1 ] << "\n\n";
83 
84  outputFile_ << "// dr = " << inputData_->dr() << "\n";
85  outputFile_ << "// rafCoeff = " << inputData_->rafCoeff() << " , note : this value is used only for the fully unstructured geometry file (it is not used in msbGrid geometry files).\n\n";
86 
87  outputFile_ << "// secondary input data : //\n";
88  outputFile_ << "// blockCenterDistribution = " << inputData_->blockCenterDistribution()
89  << " , blockAngleDistribution = " << inputData_->blockAngleDistribution()
90  << "\n";
91 
92  outputFile_ << "// blockCenterRandSeqId = " << inputData_->blockCenterRandSeqId()
93  << " , blockAngleRandSeqId = " << inputData_->blockAngleRandSeqId()
94  << "\n";
95  outputFile_ << "// blockCenterPertCoeff = " << inputData_->blockCenterPertCoeff() << " , note : random distribution with pertCoeff null (blockCenterPertCoeff = 0) is equivalent to a periodic distribution\n\n";
96 
97  outputFile_ << "// dist2BoundCoeff = " << inputData_->dist2BoundCoeff()
98  << " , interBlockDistCoeff = " << inputData_->interBlockDistCoeff()
99  << "\n\n";
100 
101  outputFile_ << "// some statistics : //\n";
102  for ( int ib = 0; ib < inputData_->blockNumber(); ++ib ) {
103  outputFile_ << "// block " << ib
104  << " , origine : x = " << inputData_->blockCenter() [ ib ].first << " , y = " << inputData_->blockCenter() [ ib ].second
105  << " , inclinaison : beta = " << inputData_->blockAngle() [ ib ] << " [deg]\n";
106  }
107 
108  outputFile_ << "\n";
109  }
110 
112  {
113  outputFile_ << "// command line options : ";
114  outputFile_ << " --outputPath=\"" << inputData_->outputPath() << "\""
115  << " --outputDirName=\"" << inputData_->outputDirName() << "\""
116  << " --xMin=\"" << inputData_->xMin() << "\""
117  << " --xMax=\"" << inputData_->xMax() << "\""
118  << " --bnCx=\"" << inputData_->blockNumberController() [ 0 ] << "\""
119  << " --bnCy=\"" << inputData_->blockNumberController() [ 1 ] << "\""
120  << " --dr=\"" << inputData_->dr() << "\""
121  << " --rafC=\"" << inputData_->rafCoeff() << "\""
122  << " --bCId=\"" << inputData_->blockCenterRandSeqId() << "\""
123  << " --bAId=\"" << inputData_->blockAngleRandSeqId() << "\""
124  << " --bCPC=\"" << inputData_->blockCenterPertCoeff() << "\""
125  << " --d2bC=\"" << inputData_->dist2BoundCoeff() << "\""
126  << " --intbC=\"" << inputData_->interBlockDistCoeff() << "\""
127  << "\n\n";
128  }
129 
130  const std :: string geoFile(const std :: string &name = "newFile", const bool &fromBuildDirectory = false)
131  {
132  if ( fromBuildDirectory ) {
133  return ( inputData_->outputPath() + "/gmsh/" + name + ".geo" );
134  }
135 
136  return ( name + ".geo" );
137  }
138 
139  void closeFile()
140  {
141  // Note : always add 1 line at the end of .geo file
142  outputFile_ << "\n";
143  outputFile_.close();
144  }
145 
146  void write(const std :: string &texte)
147  { outputFile_ << texte; }
148 
149  /* Point(ptId) = {x, y, z, cl}; */
150  void write(const Point &pt0, const std :: string &clName0 = "")
151  {
152  if ( pt0.mark() ) {
153  return;
154  }
155 
156  outputFile_ << std :: setw(1) << std :: fixed
157  << "Point(" << pt0.Id() << ") = {"
158  << pt0.x() [ 0 ] << ", "
159  << pt0.x() [ 1 ] << ", "
160  << ( ( Point :: dim == 3 ) ? pt0.x() [ 2 ] : 0.0 ) << ", ";
161  // outputFile_ << ( (clName0 != "") ? clName0 : pt0.cl() ) << "};\n";
162  if ( clName0 != "" ) {
163  outputFile_ << clName0;
164  } else {
165  outputFile_ << pt0.cl();
166  }
167 
168  outputFile_ << "};\n";
169  }
170 
171  /* Line(edId) = {pt1,pt2}; */
172  void write(const Edge &ed0)
173  {
174  if ( ed0.mark() ) {
175  return;
176  }
177 
178  outputFile_ << std :: setw(1) << std :: fixed
179  << "Line(" << ed0.Id() << ") = {"
180  << ed0.vPt() [ 0 ]->Id() << ", "
181  << ed0.vPt() [ 1 ]->Id() << "};\n";
182  }
183 
184  void write(const msbGrid :: Block< Point > &bl0, const std :: string &clName)
185  {
186  if ( bl0.size() == 0 ) {
187  return;
188  }
189 
190  for ( int iEnt = 0; iEnt < bl0.size(); ++iEnt ) {
191  write(bl0.vEnt() [ iEnt ], clName);
192  }
193  }
194 
195  /* Line loop(llId) = {line1,...,lineI,...,lineN}; */
196  void writeLineLoop(const std :: vector< Edge * > &vEd0, const int &lineLoopNumber)
197  {
198  std :: vector< Edge * > vEd1;
199  vEd1.clear();
200  vEd1.push_back(vEd0 [ 0 ]);
201  int headPointIndex = 0; // first point of the first edge
202 
203  outputFile_ << std :: setw(1) << std :: fixed << "Line Loop(" << lineLoopNumber << ") = {";
204  outputFile_ << ( headPointIndex == 0 ? "+" : "-" ) << vEd1.front()->Id() << ", ";
205 
206  for ( int ie0 = 0; ie0 < vEd0.size() - 2; ++ie0 ) {
207  addLeftSideNeighbor(vEd0, vEd1, headPointIndex);
208  const std :: string sign = ( headPointIndex == 0 ? "+" : "-" );
209  outputFile_ << ( headPointIndex == 0 ? "+" : "-" ) << vEd1.front()->Id() << ", ";
210  }
211 
212  addLeftSideNeighbor(vEd0, vEd1, headPointIndex);
213  outputFile_ << ( headPointIndex == 0 ? "+" : "-" ) << vEd1.front()->Id() << "};\n";
214  }
215 
216  /*
217  * Line Loop(pgId) = {ed1, ed2,..., edN};
218  * Plane Surface(pgId) = {pgId};
219  * Transfinite Line{ed1, ed2,..., edN} = 2 Using Progression 1;
220  * Transfinite Surface{pgId} = {pt1, pt2,..., ptN};
221  * Physical Surface(pgId) = pgId;
222  */
223  template< int locEdNumber >
225  {
226  if ( pl0.mark() ) {
227  return;
228  }
229 
230  const int locPtNumber = locEdNumber;
231  const int lastEdIndex = locEdNumber - 1;
232  const int lastPtIndex = locPtNumber - 1;
233 
234  outputFile_ << std :: setw(1) << std :: fixed;
235 
236  /* Line Loop */
237  outputFile_ << "Line Loop(" << pl0.Id() << ") = {";
238  for ( int iEd1 = 0; iEd1 < lastEdIndex; ++iEd1 ) {
239  outputFile_ << ( ( pl0.vEd() [ iEd1 ]->vPt() [ 0 ]->Id() == pl0.vPt() [ iEd1 ]->Id() ) ? "+" : "-" )
240  << pl0.vEd() [ iEd1 ]->Id() << ", ";
241  }
242 
243  outputFile_ << ( ( pl0.vEd() [ lastEdIndex ]->vPt() [ 0 ]->Id() == pl0.vPt() [ lastEdIndex ]->Id() ) ? "+" : "-" )
244  << pl0.vEd() [ lastEdIndex ]->Id() << "};\n";
245 
246  /* Plane Surface */
247  outputFile_ << "Plane Surface(" << pl0.Id() << ") = {" << pl0.Id() << "};\n";
248 
249  /* Transfinite Line */
250  outputFile_ << "Transfinite Line{";
251  for ( int iEd1 = 0; iEd1 < lastEdIndex; ++iEd1 ) {
252  outputFile_ << pl0.vEd() [ iEd1 ]->Id() << ", ";
253  }
254 
255  outputFile_ << pl0.vEd() [ lastEdIndex ]->Id() << "} = 2 Using Progression 1;\n";
256 
257  /* Transfinite Surface */
258  outputFile_ << "Transfinite Surface{" << pl0.Id() << "} = {";
259  for ( int iPt1 = 0; iPt1 < lastPtIndex; ++iPt1 ) {
260  outputFile_ << pl0.vPt() [ iPt1 ]->Id() << ", ";
261  }
262 
263  outputFile_ << pl0.vPt() [ lastPtIndex ]->Id() << "};\n";
264  outputFile_ << "\n";
265  }
266 
267  template< typename EntityT >
268  void write(const msbGrid :: Block< EntityT > &bl0, const bool &physical = false)
269  {
270  if ( bl0.size() == 0 ) {
271  return;
272  }
273 
274  outputFile_ << "\n/// Block " << bl0.Id() << "\n";
275  for ( int iEnt = 0; iEnt < bl0.size(); ++iEnt ) {
276  write(bl0.vEnt() [ iEnt ]);
277  }
278 
279  if ( physical ) {
280  outputFile_ << "Physical Surface(" << bl0.Id() << ") = {" << bl0.vEnt().front().Id() << ":" << bl0.vEnt().back().Id() << "};\n";
281  }
282  }
283 };
284 
285 #endif /* #ifndef _GEO_FILES_WRITER_HH_ */