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