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