Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OutputFld.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: OutputFld.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: FLD file format output.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <set>
37 #include <string>
38 using namespace std;
39 
40 #include "OutputFld.h"
42 
43 namespace Nektar
44 {
45 namespace FieldUtils
46 {
47 
48 ModuleKey OutputFld::m_className[2] = {
50  OutputFld::create,
51  "Writes a Fld file."),
53  OutputFld::create,
54  "Writes a Fld file."),
55 };
56 
57 OutputFld::OutputFld(FieldSharedPtr f) : OutputModule(f)
58 {
59  m_config["format"] = ConfigOption(
60  false, "Xml", "Output format of field file");
61 }
62 
64 {
65 }
66 
67 void OutputFld::Process(po::variables_map &vm)
68 {
69  // Extract the output filename and extension
70  string filename = m_config["outfile"].as<string>();
71 
72  // Get IO format
73  std::string iofmt("Xml");
74  if(m_f->m_session)
75  {
76  if (m_f->m_session->DefinesSolverInfo("IOFormat"))
77  {
78  iofmt = m_f->m_session->GetSolverInfo("IOFormat");
79  }
80  if (m_f->m_session->DefinesCmdLineArgument("io-format"))
81  {
82  iofmt =
83  m_f->m_session->GetCmdLineArgument<std::string>("io-format");
84  }
85  }
86  if(m_config["format"].m_beenSet)
87  {
88  iofmt = m_config["format"].as<string>();
89  }
90 
91  // Set up communicator and FieldIO object.
92  LibUtilities::CommSharedPtr c = m_f->m_session ? m_f->m_session->GetComm() :
96 
97  if (m_f->m_writeBndFld)
98  {
99  ModuleKey module;
100 
101  if (m_f->m_verbose)
102  {
103  if (m_f->m_comm->TreatAsRankZero())
104  {
105  cout << "OutputFld: Writing boundary file(s): ";
106  for (int i = 0; i < m_f->m_bndRegionsToWrite.size(); ++i)
107  {
108  cout << m_f->m_bndRegionsToWrite[i];
109  if (i < m_f->m_bndRegionsToWrite.size() - 1)
110  {
111  cout << ",";
112  }
113  }
114  cout << endl;
115  }
116  }
117 
118  // Extract data to boundaryconditions
119  if (m_f->m_fldToBnd)
120  {
121  for (int i = 0; i < m_f->m_exp.size(); ++i)
122  {
123  m_f->m_exp[i]->FillBndCondFromField();
124  }
125  }
126 
127  int nfields = m_f->m_exp.size();
129  nfields);
130  for (int i = 0; i < nfields; ++i)
131  {
132  BndExp[i] = m_f->m_exp[i]->GetBndCondExpansions();
133  }
134 
135  // get hold of partition boundary regions so we can match it to desired
136  // region extraction
138  m_f->m_exp[0]->GetGraph());
140  bcs.GetBoundaryRegions();
141  SpatialDomains::BoundaryRegionCollection::const_iterator breg_it;
142  map<int, int> BndRegionMap;
143  int cnt = 0;
144  for (breg_it = bregions.begin(); breg_it != bregions.end();
145  ++breg_it, ++cnt)
146  {
147  BndRegionMap[breg_it->first] = cnt;
148  }
149 
150  // find ending of output file and insert _b1, _b2
151  int dot = filename.find_last_of('.') + 1;
152  string ext = filename.substr(dot, filename.length() - dot);
153  string name = filename.substr(0, dot - 1);
154 
155  for (int i = 0; i < m_f->m_bndRegionsToWrite.size(); ++i)
156  {
157  string outname =
158  name + "_b" +
159  boost::lexical_cast<string>(m_f->m_bndRegionsToWrite[i]) + "." +
160  ext;
161 
162  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
163  std::vector<std::vector<NekDouble> > FieldData;
164 
165  if (BndRegionMap.count(m_f->m_bndRegionsToWrite[i]) == 1)
166  {
167  int Border = BndRegionMap[m_f->m_bndRegionsToWrite[i]];
168 
169  FieldDef = BndExp[0][Border]->GetFieldDefinitions();
170  FieldData.resize(FieldDef.size());
171 
172  for (int j = 0; j < nfields; ++j)
173  {
174  for (int k = 0; k < FieldDef.size(); ++k)
175  {
176  BndExp[j][Border]->AppendFieldData(FieldDef[k],
177  FieldData[k]);
178 
179  if (m_f->m_fielddef.size() > 0)
180  {
181  FieldDef[k]->m_fields.push_back(
182  m_f->m_fielddef[0]->m_fields[j]);
183  }
184  else
185  {
186  FieldDef[k]->m_fields.push_back(
187  m_f->m_session->GetVariable(j));
188  }
189  }
190  }
191 
192  if (m_f->m_addNormals)
193  {
194  int normdim = m_f->m_graph->GetMeshDimension();
195  string normstr[3] = {"Norm_x", "Norm_y", "Norm_z"};
196 
197  // Get normals
199  m_f->m_exp[0]->GetBoundaryNormals(Border, NormPhys);
200 
201  // add normal coefficients to list to be dumped
202  for (int j = 0; j < normdim; ++j)
203  {
204  BndExp[0][Border]->FwdTrans(
205  NormPhys[j], BndExp[0][Border]->UpdateCoeffs());
206 
207  for (int k = 0; k < FieldDef.size(); ++k)
208  {
209  BndExp[0][Border]->AppendFieldData(FieldDef[k],
210  FieldData[k]);
211  FieldDef[k]->m_fields.push_back(normstr[j]);
212  }
213  }
214  }
215 
216  // output error for regression checking.
217  if (vm.count("error"))
218  {
219  int rank = m_f->m_session->GetComm()->GetRank();
220 
221  for (int j = 0; j < nfields; ++j)
222  {
223  BndExp[j][Border]->BwdTrans(
224  BndExp[j][Border]->GetCoeffs(),
225  BndExp[j][Border]->UpdatePhys());
226 
227  // Note currently these calls will
228  // hange since not all partitions will
229  // call error.
230  NekDouble l2err =
231  BndExp[j][Border]->L2(BndExp[j][Border]->GetPhys());
232 
233  NekDouble linferr = BndExp[j][Border]->Linf(
234  BndExp[j][Border]->GetPhys());
235 
236  if (rank == 0)
237  {
238  cout << "L 2 error (variable "
239  << FieldDef[0]->m_fields[j] << ") : " << l2err
240  << endl;
241 
242  cout << "L inf error (variable "
243  << FieldDef[0]->m_fields[j]
244  << ") : " << linferr << endl;
245  }
246  }
247  }
248  }
249 
250  fld->Write(outname, FieldDef, FieldData, m_f->m_fieldMetaDataMap);
251  }
252  }
253  else
254  {
255  if (m_f->m_verbose)
256  {
257  if (m_f->m_comm->TreatAsRankZero())
258  {
259  cout << "OutputFld: Writing file..." << endl;
260  }
261  }
262 
263  fs::path writefile(filename);
264  int writefld = 1;
265  if (fs::exists(writefile) && (vm.count("forceoutput") == 0))
266  {
267  int rank = 0;
269 
270  if (m_f->m_session)
271  {
272  comm = m_f->m_session->GetComm();
273  rank = comm->GetRank();
274  }
275  else
276  {
278  "Serial", 0, 0);
279  }
280 
281  writefld = 0; // set to zero for reduce all to be correct.
282 
283  if (rank == 0)
284  {
285  string answer;
286  cout << "Did you wish to overwrite " << filename << " (y/n)? ";
287  getline(cin, answer);
288  if (answer.compare("y") == 0)
289  {
290  writefld = 1;
291  }
292  else
293  {
294  cout << "Not writing file " << filename
295  << " because it already exists" << endl;
296  }
297  }
298 
299  comm->AllReduce(writefld, LibUtilities::ReduceSum);
300  }
301 
302  if (writefld)
303  {
304  fld->Write(filename, m_f->m_fielddef, m_f->m_data,
305  m_f->m_fieldMetaDataMap);
306  }
307 
308  // output error for regression checking.
309  if (vm.count("error"))
310  {
311  int rank = m_f->m_session->GetComm()->GetRank();
312 
313  for (int j = 0; j < m_f->m_exp.size(); ++j)
314  {
315  if (m_f->m_exp[j]->GetPhysState() == false)
316  {
317  m_f->m_exp[j]->BwdTrans(m_f->m_exp[j]->GetCoeffs(),
318  m_f->m_exp[j]->UpdatePhys());
319  }
320 
321  NekDouble l2err = m_f->m_exp[j]->L2(m_f->m_exp[j]->GetPhys());
322 
323  NekDouble linferr =
324  m_f->m_exp[j]->Linf(m_f->m_exp[j]->GetPhys());
325  if (rank == 0)
326  {
327  cout << "L 2 error (variable "
328  << m_f->m_fielddef[0]->m_fields[j] << ") : " << l2err
329  << endl;
330 
331  cout << "L inf error (variable "
332  << m_f->m_fielddef[0]->m_fields[j] << ") : " << linferr
333  << endl;
334  }
335  }
336  }
337  }
338 }
339 }
340 }
map< string, ConfigOption > m_config
List of configuration values.
tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:162
Represents a command-line configuration option.
STL namespace.
pair< ModuleType, string > ModuleKey
virtual void Process(po::variables_map &vm)
Write fld to output file.
Definition: OutputFld.cpp:67
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
Definition: FieldIO.cpp:74
CommFactory & GetCommFactory()
Definition: Comm.cpp:61
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:55
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:767
std::map< int, BoundaryRegionShPtr > BoundaryRegionCollection
Definition: Conditions.h:217
boost::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:309
double NekDouble
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition: Conditions.h:238
Abstract base class for output modules.
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215