Nektar++
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 Utilities
46 {
47 
48 ModuleKey OutputFld::m_className[2] = {
50  ModuleKey(eOutputModule, "fld"), OutputFld::create,
51  "Writes a Fld file."),
53  ModuleKey(eOutputModule, "chk"), OutputFld::create,
54  "Writes a Fld file."),
55 };
56 
57 OutputFld::OutputFld(FieldSharedPtr f) : OutputModule(f)
58 {
59 }
60 
62 {
63 }
64 
65 void OutputFld::Process(po::variables_map &vm)
66 {
67  // Extract the output filename and extension
68  string filename = m_config["outfile"].as<string>();
69 
70  if (m_f->m_writeBndFld)
71  {
72  ModuleKey module;
73 
74  // Extract data to boundaryconditions
75  if (m_f->m_fldToBnd) {
76  for (int i = 0; i < m_f->m_exp.size(); ++i)
77  {
78  m_f->m_exp[i]->FillBndCondFromField();
79  }
80  }
81 
82  if (m_f->m_verbose)
83  {
84  cout << "OutputFld: Writing boundary file(s): ";
85  for(int i = 0; i < m_f->m_bndRegionsToWrite.size(); ++i)
86  {
87  if(i < m_f->m_bndRegionsToWrite.size()-1)
88  {
89  cout << ",";
90  }
91  }
92  cout << endl;
93  }
94 
95  int nfields = m_f->m_exp.size();
97  BndExp(nfields);
98  for (int i = 0; i < nfields; ++i)
99  {
100  BndExp[i] = m_f->m_exp[i]->GetBndCondExpansions();
101  }
102 
103  // get hold of partition boundary regions so we can match it to desired
104  // region extraction
106  m_f->m_exp[0]->GetGraph());
108  bcs.GetBoundaryRegions();
109  SpatialDomains::BoundaryRegionCollection::const_iterator breg_it;
110  map<int,int> BndRegionMap;
111  int cnt =0;
112  for(breg_it = bregions.begin(); breg_it != bregions.end();
113  ++breg_it, ++cnt)
114  {
115  BndRegionMap[breg_it->first] = cnt;
116  }
117 
118  // find ending of output file and insert _b1, _b2
119  int dot = filename.find_last_of('.') + 1;
120  string ext = filename.substr(dot, filename.length() - dot);
121  string name = filename.substr(0, dot-1);
122 
124  m_f->m_session->GetBndRegionOrdering();
125 
126  for(int i = 0; i < m_f->m_bndRegionsToWrite.size(); ++i)
127  {
128  string outname = name + "_b"
129  + boost::lexical_cast<string>(m_f->m_bndRegionsToWrite[i])
130  + "." + ext;
131 
132  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
133  std::vector<std::vector<NekDouble> > FieldData;
134 
135  if(BndRegionMap.count(m_f->m_bndRegionsToWrite[i]) == 1)
136  {
137  int Border = BndRegionMap[m_f->m_bndRegionsToWrite[i]];
138 
139  FieldDef = BndExp[0][Border]->GetFieldDefinitions();
140  FieldData.resize(FieldDef.size());
141 
142  for (int j = 0; j < nfields; ++j)
143  {
144  for (int k = 0; k < FieldDef.size(); ++k)
145  {
146  BndExp[j][Border]->AppendFieldData(FieldDef[k],
147  FieldData[k]);
148 
149  if (m_f->m_fielddef.size() > 0)
150  {
151  FieldDef[k]->m_fields.push_back(
152  m_f->m_fielddef[0]->m_fields[j]);
153  }
154  else
155  {
156  FieldDef[k]->m_fields.push_back(
157  m_f->m_session->GetVariable(j));
158  }
159  }
160  }
161 
162  if(m_f->m_addNormals)
163  {
164  ASSERTL0(m_f->m_exp[0]->GetCoordim(0) == 3,
165  "Add normals to extracted boundaries only set up in 3 dimensions");
166  int normdim = 3; // currently assuming 3D normals;
167  string normstr[3] = {"Norm_x","Norm_y","Norm_z"};
168 
169  // Add normal information
172  Array<OneD, int> BoundarytoElmtID, BoundarytoTraceID;
173 
174  m_f->m_exp[0]->GetBoundaryToElmtMap(BoundarytoElmtID,
175  BoundarytoTraceID);
176 
177  // determine offset of this Bnd Expansion Border
178  int cnt = 0;
179  for(int n = 0; n < Border; ++n)
180  {
181  cnt += BndExp[0][n]->GetExpSize();
182  }
183 
184  Array<OneD, NekDouble> tmp_array;
185  Array<OneD, Array<OneD, NekDouble> > NormCoeff(normdim);
186 
187  for(int j = 0; j < normdim; ++j)
188  {
189  NormCoeff[j] = Array<OneD, NekDouble>(BndExp[0][Border]->GetNcoeffs(),0.0);
190  }
191 
192  // setup coeff arrays of normals.
193  for(int j = 0; j < BndExp[0][Border]->GetExpSize();
194  ++j, cnt++)
195  {
196  // find element and face of this expansion.
197  int elmtid = BoundarytoElmtID[cnt];
198  elmt = m_f->m_exp[0]->GetExp(elmtid);
199 
200  // Get face 2D expansion from element expansion
201  bc = boost::dynamic_pointer_cast<StdRegions::StdExpansion2D> (BndExp[0][Border]->GetExp(j));
202 
203  //identify boundary of element looking at.
204  int boundary = BoundarytoTraceID[cnt];
205 
206  //Get face normals
207  const Array<OneD, const Array<OneD, NekDouble> > normals = elmt->GetFaceNormal(boundary);
208 
209  for(int k = 0; k < normdim; ++k)
210  {
211  bc->FwdTrans(normals[k],tmp_array = NormCoeff[k]+BndExp[0][Border]->GetCoeff_Offset(j));
212  }
213  }
214 
215  // add normal coefficients to list to be dumped
216  for (int j = 0; j < normdim; ++j)
217  {
218  Vmath::Vcopy(BndExp[0][Border]->GetNcoeffs(),
219  NormCoeff[j],1,
220  BndExp[0][Border]->UpdateCoeffs(),1);
221 
222  for (int k = 0; k < FieldDef.size(); ++k)
223  {
224  BndExp[0][Border]->AppendFieldData(FieldDef[k],
225  FieldData[k]);
226  FieldDef[k]->m_fields.push_back(normstr[j]);
227  }
228  }
229  }
230 
231  // output error for regression checking.
232  if (vm.count("error"))
233  {
234  int rank = m_f->m_session->GetComm()->GetRank();
235 
236  for (int j = 0; j < nfields; ++j)
237  {
238  BndExp[j][Border]->BwdTrans(BndExp[j][Border]->GetCoeffs(),
239  BndExp[j][Border]->UpdatePhys());
240 
241  //Note currently these calls will
242  //hange since not all partitions will
243  //call error.
244  NekDouble l2err = BndExp[j][Border]
245  ->L2(BndExp[j][Border]->GetPhys());
246 
247  NekDouble linferr = BndExp[j][Border]
248  ->Linf(BndExp[j][Border]->GetPhys());
249 
250  if (rank == 0)
251  {
252  cout << "L 2 error (variable "
253  << FieldDef[0]->m_fields[j]
254  << ") : " << l2err << endl;
255 
256  cout << "L inf error (variable "
257  << FieldDef[0]->m_fields[j]
258  << ") : " << linferr << endl;
259  }
260  }
261  }
262  }
263 
264  m_f->m_fld->Write(outname, FieldDef, FieldData,
265  m_f->m_fieldMetaDataMap);
266 
267  }
268  }
269  else
270  {
271  if (m_f->m_verbose)
272  {
273  cout << "OutputFld: Writing file..." << endl;
274  }
275 
276  fs::path writefile(filename);
277  int writefld = 1;
278  if(fs::exists(writefile)&&(vm.count("forceoutput") == 0))
279  {
280  LibUtilities::CommSharedPtr comm = m_f->m_session->GetComm();
281  int rank = comm->GetRank();
282  writefld = 0; // set to zero for reduce all to be correct.
283 
284  if(rank == 0)
285  {
286  string answer;
287  cout << "Did you wish to overwrite " << filename << " (y/n)? ";
288  getline(cin,answer);
289  if(answer.compare("y") == 0)
290  {
291  writefld = 1;
292  }
293  else
294  {
295  cout << "Not writing file " << filename << " because it already exists" << endl;
296  }
297  }
298 
299  comm->AllReduce(writefld,LibUtilities::ReduceSum);
300 
301  }
302 
303  if(writefld)
304  {
305  m_f->m_fld->Write(filename, m_f->m_fielddef, m_f->m_data);
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(
318  m_f->m_exp[j]->GetCoeffs(),
319  m_f->m_exp[j]->UpdatePhys());
320  }
321 
322  NekDouble l2err = m_f->m_exp[j]->L2(
323  m_f->m_exp[j]->GetPhys());
324 
325  NekDouble linferr = m_f->m_exp[j]->Linf(
326  m_f->m_exp[j]->GetPhys());
327  if (rank == 0)
328  {
329  cout << "L 2 error (variable "
330  << m_f->m_fielddef[0]->m_fields[j]
331  << ") : " << l2err << endl;
332 
333  cout << "L inf error (variable "
334  << m_f->m_fielddef[0]->m_fields[j]
335  << ") : " << linferr << endl;
336  }
337  }
338  }
339  }
340 }
341 
342 }
343 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
pair< ModuleType, string > ModuleKey
Abstract base class for output modules.
virtual void Process()=0
map< string, ConfigOption > m_config
List of configuration values.
STL namespace.
std::map< int, std::vector< unsigned int > > BndRegionOrdering
Definition: MeshPartition.h:53
FieldSharedPtr m_f
Field object.
boost::shared_ptr< StdExpansion2D > StdExpansion2DSharedPtr
boost::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:53
std::map< int, BoundaryRegionShPtr > BoundaryRegionCollection
Definition: Conditions.h:204
double NekDouble
boost::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:677
const BoundaryRegionCollection & GetBoundaryRegions(void) const
Definition: Conditions.h:225
boost::shared_ptr< StdExpansion > StdExpansionSharedPtr
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1038
ModuleFactory & GetModuleFactory()
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215