Nektar++
ProcessFieldFromString.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: ProcessFieldFromString.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 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Modify an existing or add a new field from a string based on existing variable
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 #include <iostream>
35 #include <string>
36 using namespace std;
37 
38 #include <boost/core/ignore_unused.hpp>
39 
42 
43 #include "ProcessFieldFromString.h"
44 
45 namespace Nektar
46 {
47 namespace FieldUtils
48 {
49 
50 ModuleKey ProcessFieldFromString::className =
52  ModuleKey(eProcessModule, "fieldfromstring"),
53  ProcessFieldFromString::create,
54  "Modify an existing or create a new field from the existing fields as "
55  "specified by a string using a required argument of the form "
56  "fieldstr=\"x + y + u\" ");
57 
58 ProcessFieldFromString::ProcessFieldFromString(FieldSharedPtr f)
59  : ProcessModule(f)
60 {
61  m_config["fieldstr"] = ConfigOption(
62  false, "NotSet", "Analytic expression");
63  m_config["fieldname"] =
64  ConfigOption(false,
65  "newfield",
66  "name for modified new field, default is \"newfield\" (optional)");
67 }
68 
70 {
71 }
72 
73 void ProcessFieldFromString::Process(po::variables_map &vm)
74 {
75  m_f->SetUpExp(vm);
76 
77  // Check if required parameter fieldstr was provided
78  ASSERTL0(m_config["fieldstr"].m_beenSet, "fieldstr must be specified");
79 
80  // Get number of fields (before adding new entry)
81  int nfields = m_f->m_variables.size();
82 
83  // Set up new field name
84  string fieldName = m_config["fieldname"].as<string>();
85 
86  int fieldID;
87  bool addField;
88  // check if field exists
89  auto it =
90  std::find(m_f->m_variables.begin(), m_f->m_variables.end(), fieldName);
91  if (it != m_f->m_variables.end())
92  {
93  addField = false;
94  fieldID = std::distance(m_f->m_variables.begin(), it);
95  }
96  else
97  {
98  // Create new expansion
99  addField = true;
100  fieldID = nfields;
101  m_f->m_variables.push_back(fieldName);
102  }
103 
104  // Skip in case of empty partition
105  if (m_f->m_exp[0]->GetNumElmts() == 0)
106  {
107  return;
108  }
109 
110  // Check if using strips
111  int nstrips;
112  m_f->m_session->LoadParameter("Strip_Z", nstrips, 1);
113  ASSERTL0(nstrips == 1,
114  "Routine is currently only setup for non-strip files");
115 
116  if (addField)
117  {
118  m_f->m_exp.resize(nfields + 1);
119  m_f->m_exp[nfields] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
120  }
121 
122  // Variables for storing names and values for evaluating the function
123  string varstr;
124  vector<Array<OneD, const NekDouble> > interpfields;
125 
126  // Add the coordinate values
127  varstr += "x y z";
128  int npoints = m_f->m_exp[0]->GetTotPoints();
129  Array<OneD, NekDouble> x(npoints, 0.0);
130  Array<OneD, NekDouble> y(npoints, 0.0);
131  Array<OneD, NekDouble> z(npoints, 0.0);
132  m_f->m_exp[0]->GetCoords(x, y, z);
133  interpfields.push_back(x);
134  interpfields.push_back(y);
135  interpfields.push_back(z);
136 
137  // Add the field values
138  for (int i = 0; i < nfields; ++i)
139  {
140  varstr += " " + m_f->m_variables[i];
141  interpfields.push_back(m_f->m_exp[i]->GetPhys());
142  }
143 
144  // Create new function
146  int exprId = -1;
147  string fieldstr = m_config["fieldstr"].as<string>();
148  exprId = strEval.DefineFunction(varstr.c_str(), fieldstr);
149 
150  // Evaluate function
151  strEval.Evaluate(exprId, interpfields, m_f->m_exp[fieldID]->UpdatePhys());
152 
153  // Update coeffs
154  m_f->m_exp[fieldID]->FwdTrans_IterPerExp(
155  m_f->m_exp[fieldID]->GetPhys(), m_f->m_exp[fieldID]->UpdateCoeffs());
156 }
157 }
158 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
FieldSharedPtr m_f
Field object.
Definition: Module.h:230
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:233
virtual void Process(po::variables_map &vm)
Abstract base class for processing modules.
Definition: Module.h:265
Interpreter class for the evaluation of mathematical expressions.
Definition: Interpreter.h:78
int DefineFunction(const std::string &vlist, const std::string &expr)
Defines a function for the purposes of evaluation.
NekDouble Evaluate(const int id)
Evaluate a function which depends only on constants and/or parameters.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:200
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:989
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:290
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:362
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
Represents a command-line configuration option.
Definition: Module.h:134