Nektar++
ProcessAddFld.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessAddFld.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: Add a field to the intput field
32//
33////////////////////////////////////////////////////////////////////////////////
34
35#include <iostream>
36#include <string>
37using namespace std;
38
39#include <boost/core/ignore_unused.hpp>
40
42
43#include "ProcessAddFld.h"
44
45namespace Nektar
46{
47namespace FieldUtils
48{
49
52 "add two fields together with optional scaling. Must specify fromfld and "
53 "scaling is optionally specified with input option scale.");
54
56{
57 m_config["scale"] = ConfigOption(false, "1.0", "scale factor");
58
59 m_config["fromfld"] =
60 ConfigOption(false, "NotSet", "Fld file form which to add field");
61
62 if (f->m_inputfiles.count("xml"))
63 {
65 }
66 else
67 {
69 }
70}
71
73{
74}
75
76void ProcessAddFld::v_Process(po::variables_map &vm)
77{
78 boost::ignore_unused(vm);
79
80 string scalestr = m_config["scale"].as<string>();
81 NekDouble scale = boost::lexical_cast<NekDouble>(scalestr);
82
83 ASSERTL0(m_config["fromfld"].as<string>().compare("NotSet") != 0,
84 "Need to specify fromfld=file.fld ");
85 string fromfld = m_config["fromfld"].as<string>();
86
87 vector<LibUtilities::FieldDefinitionsSharedPtr> fromFieldDef;
88 vector<vector<double>> fromFieldData;
89
90 if (m_f->m_graph)
91 {
92 const SpatialDomains::ExpansionInfoMap &expansions =
93 m_f->m_graph->GetExpansionInfo();
94
95 // if Range has been speficied it is possible to have a
96 // partition which is empty so check this and return if
97 // no elements present.
98
99 if (!expansions.size())
100 {
101 return;
102 }
103
104 Array<OneD, int> ElementGIDs(expansions.size());
105
106 int i = 0;
107 for (auto &expIt : expansions)
108 {
109 ElementGIDs[i++] = expIt.second->m_geomShPtr->GetGlobalID();
110 }
111 m_f->FieldIOForFile(fromfld)->Import(
112 fromfld, fromFieldDef, fromFieldData,
114 }
115 else
116 {
117 m_f->FieldIOForFile(fromfld)->Import(
118 fromfld, fromFieldDef, fromFieldData,
120 }
121
122 bool samelength = true;
123 if (fromFieldData.size() != m_f->m_data.size())
124 {
125 samelength = false;
126 }
127
128 // scale input field
129 for (int i = 0; i < fromFieldData.size(); ++i)
130 {
131 int datalen = fromFieldData[i].size();
132
133 Vmath::Smul(datalen, scale, &(fromFieldData[i][0]), 1,
134 &(fromFieldData[i][0]), 1);
135
136 if (samelength)
137 {
138 if (datalen != m_f->m_data[i].size())
139 {
140 samelength = false;
141 }
142 }
143 }
144
146 {
147 ASSERTL0(samelength == true,
148 "Input fields have partitions of different length and so xml "
149 "file needs to be specified");
150 for (int i = 0; i < m_f->m_data.size(); ++i)
151 {
152 int datalen = m_f->m_data[i].size();
153
154 Vmath::Vadd(datalen, &(m_f->m_data[i][0]), 1,
155 &(fromFieldData[i][0]), 1, &(m_f->m_data[i][0]), 1);
156 }
157 }
158 else
159 {
160 // Skip in case of empty partition
161 if (m_f->m_exp[0]->GetNumElmts() == 0)
162 {
163 return;
164 }
165
166 int nfields = m_f->m_variables.size();
167 int ncoeffs = m_f->m_exp[0]->GetNcoeffs();
168 Array<OneD, NekDouble> SaveFld(ncoeffs);
169
170 for (int j = 0; j < nfields; ++j)
171 {
172 Vmath::Vcopy(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1, SaveFld, 1);
173
174 // Check if new field has this variable
175 auto it =
176 find(fromFieldDef[0]->m_fields.begin(),
177 fromFieldDef[0]->m_fields.end(), m_f->m_variables[j]);
178
179 ASSERTL0(it != fromFieldDef[0]->m_fields.end(),
180 "Could not find field " + m_f->m_variables[j] +
181 " in from field");
182
183 // load new field
184 for (int i = 0; i < fromFieldData.size(); ++i)
185 {
186 m_f->m_exp[j]->ExtractDataToCoeffs(
187 fromFieldDef[i], fromFieldData[i], m_f->m_variables[j],
188 m_f->m_exp[j]->UpdateCoeffs());
189 }
190
191 Vmath::Vadd(ncoeffs, m_f->m_exp[j]->GetCoeffs(), 1, SaveFld, 1,
192 m_f->m_exp[j]->UpdateCoeffs(), 1);
193 m_f->m_exp[j]->BwdTrans(m_f->m_exp[j]->GetCoeffs(),
194 m_f->m_exp[j]->UpdatePhys());
195 }
196 }
197}
198} // namespace FieldUtils
199} // 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
Write mesh to output file.
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
Definition: ProcessAddFld.h:53
Abstract base class for processing modules.
Definition: Module.h:292
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
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53
std::map< int, ExpansionInfoShPtr > ExpansionInfoMap
Definition: MeshGraph.h:143
InputIterator find(InputIterator first, InputIterator last, InputIterator startingpoint, const EqualityComparable &value)
Definition: StdRegions.hpp:453
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.cpp:354
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition: Vmath.cpp:245
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1191
Represents a command-line configuration option.
Definition: Module.h:131