Nektar++
ProcessPowerSpectrum.cpp
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////////////
2//
3// File: ProcessPowerSpectrum.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: Find the spanwise energy distribution of a 3DH1D field.
32//
33////////////////////////////////////////////////////////////////////////////////
34
38#include <iostream>
39#include <string>
40
41using namespace std;
42
43namespace Nektar::FieldUtils
44{
45
48 ModuleKey(eProcessModule, "powerspectrum"),
50 "Output power spectrum at given regions from a 3DH1D expansion.");
51
53{
54 m_config["box"] =
55 ConfigOption(false, "NotSet",
56 "Specify a rectangular box by box=xmin,xmax,ymin,ymax");
57 m_config["vars"] = ConfigOption(false, "NotSet", "signal variable");
58}
59
61{
62}
63
64void ProcessPowerSpectrum::v_Process(po::variables_map &vm)
65{
66 m_f->SetUpExp(vm);
67
68 ASSERTL0(m_f->m_numHomogeneousDir == 1,
69 "ProcessPowerSpectrum only works for Homogeneous1D fields.");
70 // Skip in case of empty partition
71 if (m_f->m_exp[0]->GetNumElmts() == 0)
72 {
73 return;
74 }
75
76 std::set<int> variables;
77 if (m_config["vars"].as<string>().compare("NotSet") != 0)
78 {
79 ParseUtils::GenerateVariableSet(m_config["vars"].as<string>(),
80 m_f->m_variables, variables);
81 }
82 ASSERTL0(variables.size() >= 1,
83 "At least one variable should be selected.");
84 if (variables.empty())
85 {
86 return;
87 }
88 std::vector<NekDouble> box;
89 if (m_config["box"].as<string>().compare("NotSet") != 0)
90 {
91 ASSERTL0(ParseUtils::GenerateVector(m_config["box"].as<string>(), box),
92 "Failed to interpret box string");
94 box.size() >= 4,
95 "box string should contain at least 4 values xmin,xmax,ymin,ymax");
96 }
97 box.resize(4, 0.);
98 if (m_f->m_comm->GetSpaceComm()->GetRank() == 0)
99 {
100
101 cout << "Processing frequency spectra of variables (";
102 for (auto nsignal : variables)
103 {
104 cout << m_f->m_variables[nsignal] << ", ";
105 }
106 cout << ") in box [" << box[0] << ", " << box[1] << "]X[" << box[2]
107 << ", " << box[3] << "].\n";
108 }
109
110 for (auto nsignal : variables)
111 {
112 m_f->m_exp[nsignal]->SetWaveSpace(true);
113 m_f->m_exp[nsignal]->BwdTrans(m_f->m_exp[nsignal]->GetCoeffs(),
114 m_f->m_exp[nsignal]->UpdatePhys());
115 }
116
117 int nplanes = m_f->m_exp[0]->GetZIDs().size();
118 NekDouble lz = m_f->m_exp[0]->GetHomoLen();
119 MultiRegions::ExpListSharedPtr plane0 = m_f->m_exp[0]->GetPlane(0);
120 int ntot = plane0->GetNpoints();
121 Array<OneD, NekDouble> masks(ntot, 0.);
122 for (size_t i = 0; i < plane0->GetExpSize(); ++i)
123 {
124 LocalRegions::ExpansionSharedPtr exp = plane0->GetExp(i);
125 SpatialDomains::GeometrySharedPtr geom = exp->GetGeom();
126 int nv = geom->GetNumVerts();
127 NekDouble gc[3] = {0., 0., 0.};
128 NekDouble gct[3] = {0., 0., 0.};
129 for (size_t j = 0; j < nv; ++j)
130 {
131 SpatialDomains::PointGeomSharedPtr vertex = geom->GetVertex(j);
132 vertex->GetCoords(gct[0], gct[1], gct[2]);
133 gc[0] += gct[0] / NekDouble(nv);
134 gc[1] += gct[1] / NekDouble(nv);
135 }
136 if (box[0] <= gc[0] && gc[0] <= box[1] && box[2] <= gc[1] &&
137 gc[1] <= box[3])
138 {
139 Vmath::Fill(exp->GetTotPoints(), 1.,
140 &masks[plane0->GetPhys_Offset(i)], 1);
141 }
142 }
143 NekDouble area = m_f->m_exp[0]->GetPlane(0)->Integral(masks);
144 m_f->m_comm->GetSpaceComm()->AllReduce(area, LibUtilities::ReduceMax);
145 if (area <= NekConstants::kNekZeroTol)
146 {
147 area = 1.;
148 }
149
150 Array<OneD, NekDouble> value(nplanes / 2, 0.);
151 for (size_t m = 0; m < value.size(); ++m)
152 {
153 for (auto nsignal : variables)
154 {
155 Array<OneD, NekDouble> wavecoef(ntot, 0.);
156 Vmath::Vvtvp(ntot, m_f->m_exp[nsignal]->GetPlane(2 * m)->GetPhys(),
157 1, m_f->m_exp[nsignal]->GetPlane(2 * m)->GetPhys(), 1,
158 wavecoef, 1, wavecoef, 1);
159 Vmath::Vvtvp(ntot,
160 m_f->m_exp[nsignal]->GetPlane(2 * m + 1)->GetPhys(), 1,
161 m_f->m_exp[nsignal]->GetPlane(2 * m + 1)->GetPhys(), 1,
162 wavecoef, 1, wavecoef, 1);
163 Vmath::Vmul(ntot, wavecoef, 1, masks, 1, wavecoef, 1);
164 if (m > 0)
165 {
166 Vmath::Smul(ntot, 0.25, wavecoef, 1, wavecoef, 1);
167 }
168 value[m] += m_f->m_exp[0]->GetPlane(2 * m)->Integral(wavecoef);
169 }
170 }
171 m_f->m_comm->GetSpaceComm()->AllReduce(value, LibUtilities::ReduceMax);
172 if (m_f->m_comm->GetSpaceComm()->GetRank() == 0)
173 {
174 NekDouble energy = 0.;
175 ofstream powersp("spectra.dat");
176 powersp << "variables = lambda beta energy" << endl;
177 for (size_t m = 1; m < value.size(); ++m)
178 {
179 powersp << (lz / m) << " " << (2. * M_PI * m / lz) << " "
180 << value[m] << endl;
181 energy += value[m] * value[m];
182 }
183 powersp.close();
184 if (vm.count("error"))
185 {
186 cout << "L 2 error (variable E) : " << energy << endl;
187 }
188 }
189
190 int nfields = m_f->m_variables.size();
191 m_f->m_exp.resize(nfields + 1);
192 m_f->m_variables.push_back("regions");
193 m_f->m_exp[nfields] = m_f->AppendExpList(m_f->m_numHomogeneousDir);
194 for (size_t m = 0; m < nplanes; ++m)
195 {
196 Vmath::Vcopy(ntot, masks, 1,
197 m_f->m_exp[nfields]->GetPlane(m)->UpdatePhys(), 1);
198 }
199 m_f->m_exp[nfields]->FwdTrans(m_f->m_exp[nfields]->GetPhys(),
200 m_f->m_exp[nfields]->UpdateCoeffs());
201}
202} // 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
Abstract base class for processing modules.
Definition: Module.h:301
static std::shared_ptr< Module > create(FieldSharedPtr f)
Creates an instance of this class.
void v_Process(po::variables_map &vm) override
Write mesh to output file.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
static bool GenerateVariableSet(const std::string &str, const std::vector< std::string > &variables, std::set< int > &out)
Generate a set of variable locations.
Definition: ParseUtils.cpp:168
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:130
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
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:66
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
static const NekDouble kNekZeroTol
std::shared_ptr< PointGeom > PointGeomSharedPtr
Definition: Geometry.h:57
std::shared_ptr< Geometry > GeometrySharedPtr
Definition: Geometry.h:51
double NekDouble
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.hpp:72
void Vvtvp(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvp (vector times vector plus vector): z = w*x + y
Definition: Vmath.hpp:366
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 Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
Definition: Vmath.hpp:54
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