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