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
32 // existing variable
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 
43 
44 #include "ProcessFieldFromString.h"
45 
46 namespace Nektar
47 {
48 namespace FieldUtils
49 {
50 
51 ModuleKey ProcessFieldFromString::className =
53  ModuleKey(eProcessModule, "fieldfromstring"),
54  ProcessFieldFromString::create,
55  "Modify an existing or create a new field from the existing fields as "
56  "specified by a string using a required argument of the form "
57  "fieldstr=\"x + y + u\" ");
58 
59 ProcessFieldFromString::ProcessFieldFromString(FieldSharedPtr f)
60  : ProcessModule(f)
61 {
62  m_config["fieldstr"] = ConfigOption(false, "NotSet", "Analytic expression");
63  m_config["fieldname"] = ConfigOption(
64  false, "newfield",
65  "name for modified new field, default is \"newfield\" (optional)");
66 }
67 
69 {
70 }
71 
72 void ProcessFieldFromString::v_Process(po::variables_map &vm)
73 {
74  m_f->SetUpExp(vm);
75 
76  // Check if required parameter fieldstr was provided
77  ASSERTL0(m_config["fieldstr"].m_beenSet, "fieldstr must be specified");
78 
79  // Get number of fields (before adding new entry)
80  int nfields = m_f->m_variables.size();
81 
82  // Set up new field name
83  string fieldName = m_config["fieldname"].as<string>();
84 
85  int fieldID;
86  bool addField;
87  // check if field exists
88  auto it =
89  std::find(m_f->m_variables.begin(), m_f->m_variables.end(), fieldName);
90  if (it != m_f->m_variables.end())
91  {
92  addField = false;
93  fieldID = std::distance(m_f->m_variables.begin(), it);
94  }
95  else
96  {
97  // Create new expansion
98  addField = true;
99  fieldID = nfields;
100  m_f->m_variables.push_back(fieldName);
101  }
102 
103  // Skip in case of empty partition
104  if (m_f->m_exp[0]->GetNumElmts() == 0)
105  {
106  return;
107  }
108 
109  // Check if using strips
110  int nstrips;
111  m_f->m_session->LoadParameter("Strip_Z", nstrips, 1);
112  ASSERTL0(nstrips == 1,
113  "Routine is currently only setup for non-strip files");
114 
115  if (addField)
116  {
117  m_f->m_exp.resize(nfields + 1);
118  m_f->m_exp[nfields] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
119  }
120 
121  // Variables for storing names and values for evaluating the function
122  string varstr;
123  vector<Array<OneD, const NekDouble>> interpfields;
124 
125  // Add the coordinate values
126  varstr += "x y z";
127  int npoints = m_f->m_exp[0]->GetTotPoints();
128  Array<OneD, NekDouble> x(npoints, 0.0);
129  Array<OneD, NekDouble> y(npoints, 0.0);
130  Array<OneD, NekDouble> z(npoints, 0.0);
131  m_f->m_exp[0]->GetCoords(x, y, z);
132  interpfields.push_back(x);
133  interpfields.push_back(y);
134  interpfields.push_back(z);
135 
136  // Add the field values
137  for (int i = 0; i < nfields; ++i)
138  {
139  varstr += " " + m_f->m_variables[i];
140  interpfields.push_back(m_f->m_exp[i]->GetPhys());
141  }
142 
143  // Create new function
145  int exprId = -1;
146  string fieldstr = m_config["fieldstr"].as<string>();
147  exprId = strEval.DefineFunction(varstr.c_str(), fieldstr);
148 
149  // Evaluate function
150  strEval.Evaluate(exprId, interpfields, m_f->m_exp[fieldID]->UpdatePhys());
151  // Update coeffs
152  m_f->m_exp[fieldID]->FwdTransLocalElmt(m_f->m_exp[fieldID]->GetPhys(),
153  m_f->m_exp[fieldID]->UpdateCoeffs());
154 }
155 } // namespace FieldUtils
156 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
std::map< std::string, ConfigOption > m_config
List of configuration values.
Definition: Module.h:263
virtual void v_Process(po::variables_map &vm) override
Abstract base class for processing modules.
Definition: Module.h:292
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:198
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:991
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:317
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:444
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
Represents a command-line configuration option.
Definition: Module.h:131