Nektar++
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Attributes | Friends | List of all members
Nektar::SolverUtils::FilterIntegral Class Reference

#include <FilterIntegral.h>

Inheritance diagram for Nektar::SolverUtils::FilterIntegral:
[legend]

Public Member Functions

SOLVER_UTILS_EXPORT FilterIntegral (const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
 Constructs the integral filter and parses filter options, opens file. More...
 
SOLVER_UTILS_EXPORT ~FilterIntegral () override=default
 Default destructor. More...
 
- Public Member Functions inherited from Nektar::SolverUtils::Filter
SOLVER_UTILS_EXPORT Filter (const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
 
virtual SOLVER_UTILS_EXPORT ~Filter ()
 
SOLVER_UTILS_EXPORT void Initialise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
 
SOLVER_UTILS_EXPORT void Update (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
 
SOLVER_UTILS_EXPORT void Finalise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)
 
SOLVER_UTILS_EXPORT bool IsTimeDependent ()
 

Static Public Member Functions

static FilterSharedPtr create (const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
 Creates an instance of this class. More...
 

Static Public Attributes

static std::string className
 Name of the class. More...
 

Protected Member Functions

void v_Initialise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
 Parse composite list and geometric entities to integrate over. More...
 
void v_Update (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
 Calculate integral over requested composites. More...
 
void v_Finalise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
 Finalise the filter. More...
 
bool v_IsTimeDependent () final
 Returns true as filter depends on time. More...
 
virtual void v_Initialise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
 
virtual void v_Update (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
 
virtual void v_Finalise (const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time)=0
 
virtual bool v_IsTimeDependent ()=0
 

Private Attributes

size_t m_index = 0
 
size_t m_outputFrequency
 Frequency to write to output data file in timesteps. More...
 
size_t m_numVariables
 Number of fields to perform integral on. More...
 
std::ofstream m_outFile
 Out file. More...
 
LibUtilities::CommSharedPtr m_comm
 Global communicator. More...
 
std::vector< std::string > m_splitCompString
 Vector of composite IDs as a single string. More...
 
std::vector< std::vector< unsigned int > > m_compVector
 Vector of vector of composites IDs as integers. More...
 
std::map< size_t, size_t > m_geomElmtIdToExpId
 Mapping from geometry ID to expansion ID. More...
 
std::map< int, std::vector< std::pair< LocalRegions::ExpansionSharedPtr, int > > > m_compExpMap
 Map of composite ID to vector of expansions an face/edge local ID. More...
 

Friends

class MemoryManager< FilterIntegral >
 

Additional Inherited Members

- Public Types inherited from Nektar::SolverUtils::Filter
typedef std::map< std::string, std::string > ParamMap
 
- Protected Attributes inherited from Nektar::SolverUtils::Filter
LibUtilities::SessionReaderSharedPtr m_session
 
const std::weak_ptr< EquationSystemm_equ
 

Detailed Description

Definition at line 42 of file FilterIntegral.h.

Constructor & Destructor Documentation

◆ FilterIntegral()

Nektar::SolverUtils::FilterIntegral::FilterIntegral ( const LibUtilities::SessionReaderSharedPtr pSession,
const std::weak_ptr< EquationSystem > &  pEquation,
const ParamMap pParams 
)

Constructs the integral filter and parses filter options, opens file.

Initialise the filter and parse the session file parameters.

Definition at line 52 of file FilterIntegral.cpp.

55 : Filter(pSession, pEquation)
56{
57 std::string outName;
58
59 // OutputFile
60 auto it = pParams.find("OutputFile");
61 if (it == pParams.end())
62 {
63 outName = m_session->GetSessionName();
64 }
65 else
66 {
67 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFile'.");
68 outName = it->second;
69 }
70 outName += ".int";
71
72 // Composites (to calculate integrals on)
73 it = pParams.find("Composites");
74 ASSERTL0(it != pParams.end(), "Missing parameter 'Composites'.");
75 ASSERTL0(it->second.length() > 0, "Empty parameter 'Composites'.");
76
77 std::vector<std::string> splitComposite;
78 boost::split(m_splitCompString, it->second, boost::is_any_of(","),
79 boost::token_compress_on);
80
81 for (auto &comp : m_splitCompString)
82 {
83 boost::trim(comp);
84 size_t first = comp.find_first_of('[') + 1;
85 size_t last = comp.find_last_of(']') - 1;
86 auto tmpString = comp.substr(first, last - first + 1);
87
88 std::vector<unsigned int> tmpVec;
89 bool parseGood = ParseUtils::GenerateSeqVector(tmpString, tmpVec);
90
91 ASSERTL0(parseGood && !tmpVec.empty(),
92 "Unable to read composite regions index range for "
93 "FilterIntegral: " +
94 comp);
95
96 m_compVector.emplace_back(tmpVec);
97 }
98
99 // OutputPrecision
100 size_t precision;
101 it = pParams.find("OutputPrecision");
102 if (it == pParams.end())
103 {
104 precision = 7;
105 }
106 else
107 {
108 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputPrecision'.");
109 precision = std::stoi(it->second);
110 }
111
112 // Lock equation system pointer
113 auto equationSys = m_equ.lock();
114 ASSERTL0(equationSys, "Weak pointer expired");
115
116 m_numVariables = equationSys->GetNvariables();
117
118 m_comm = pSession->GetComm();
119 if (m_comm->GetRank() == 0)
120 {
121 m_outFile.open(outName);
122 ASSERTL0(m_outFile.good(), "Unable to open: '" + outName + "'");
123 m_outFile.setf(std::ios::scientific, std::ios::floatfield);
124 m_outFile.precision(precision);
125 m_outFile << "#Time";
126
127 for (auto &compName : m_splitCompString)
128 {
129 for (size_t j = 0; j < m_numVariables; ++j)
130 {
131 std::string varName = equationSys->GetVariable(j);
132 m_outFile << " " + compName + "_" + varName + "_integral";
133 }
134 }
135 m_outFile << std::endl;
136 }
137
138 // OutputFrequency
139 it = pParams.find("OutputFrequency");
140 if (it == pParams.end())
141 {
143 }
144 else
145 {
146 ASSERTL0(it->second.length() > 0, "Empty parameter 'OutputFrequency'.");
147 LibUtilities::Equation equ(m_session->GetInterpreter(), it->second);
148 m_outputFrequency = round(equ.Evaluate());
149 }
150}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
static bool GenerateSeqVector(const std::string &str, std::vector< unsigned int > &out)
Takes a comma-separated compressed string and converts it to entries in a vector.
Definition: ParseUtils.cpp:104
LibUtilities::SessionReaderSharedPtr m_session
Definition: Filter.h:83
const std::weak_ptr< EquationSystem > m_equ
Definition: Filter.h:84
SOLVER_UTILS_EXPORT Filter(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation)
Definition: Filter.cpp:45
size_t m_outputFrequency
Frequency to write to output data file in timesteps.
size_t m_numVariables
Number of fields to perform integral on.
std::ofstream m_outFile
Out file.
LibUtilities::CommSharedPtr m_comm
Global communicator.
std::vector< std::vector< unsigned int > > m_compVector
Vector of vector of composites IDs as integers.
std::vector< std::string > m_splitCompString
Vector of composite IDs as a single string.

References ASSERTL0, Nektar::LibUtilities::Equation::Evaluate(), Nektar::ParseUtils::GenerateSeqVector(), m_comm, m_compVector, Nektar::SolverUtils::Filter::m_equ, m_numVariables, m_outFile, m_outputFrequency, Nektar::SolverUtils::Filter::m_session, and m_splitCompString.

◆ ~FilterIntegral()

SOLVER_UTILS_EXPORT Nektar::SolverUtils::FilterIntegral::~FilterIntegral ( )
overridedefault

Default destructor.

Member Function Documentation

◆ create()

static FilterSharedPtr Nektar::SolverUtils::FilterIntegral::create ( const LibUtilities::SessionReaderSharedPtr pSession,
const std::weak_ptr< EquationSystem > &  pEquation,
const std::map< std::string, std::string > &  pParams 
)
inlinestatic

Creates an instance of this class.

Definition at line 48 of file FilterIntegral.h.

52 {
54 pSession, pEquation, pParams);
55 return p;
56 }
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< Filter > FilterSharedPtr
A shared pointer to a Driver object.
Definition: Filter.h:51

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), and CellMLToNektar.cellml_metadata::p.

◆ v_Finalise()

void Nektar::SolverUtils::FilterIntegral::v_Finalise ( const Array< OneD, const MultiRegions::ExpListSharedPtr > &  pFields,
const NekDouble time 
)
finalprotectedvirtual

Finalise the filter.

Closes the output data file

Parameters
pFieldsField data
timeCurrent time

Implements Nektar::SolverUtils::Filter.

Definition at line 355 of file FilterIntegral.cpp.

359{
360 if (m_comm->GetRank() == 0)
361 {
362 m_outFile.close();
363 }
364}

References m_comm, and m_outFile.

◆ v_Initialise()

void Nektar::SolverUtils::FilterIntegral::v_Initialise ( const Array< OneD, const MultiRegions::ExpListSharedPtr > &  pFields,
const NekDouble time 
)
finalprotectedvirtual

Parse composite list and geometric entities to integrate over.

Initialises the integral filter and stores the composite expansions in m_compExpMap for all composites specified in the XML file

Parameters
pFieldsField data
timeCurrent time

Implements Nektar::SolverUtils::Filter.

Definition at line 155 of file FilterIntegral.cpp.

158{
159
160 // Create map from element ID -> expansion list ID
161 auto expList = pFields[0]->GetExp();
162 auto meshGraph = pFields[0]->GetGraph();
163
164 for (size_t i = 0; i < expList->size(); ++i)
165 {
166 auto exp = (*expList)[i];
167 m_geomElmtIdToExpId[exp->GetGeom()->GetGlobalID()] = i;
168 }
169
170 // Create a map from geom ID -> trace expansion list ID
171 std::map<size_t, size_t> geomIdToTraceId;
172 auto trace = pFields[0]->GetTrace()->GetExp();
173 for (size_t i = 0; i < trace->size(); ++i)
174 {
175 auto exp = (*trace)[i];
176 geomIdToTraceId[exp->GetGeom()->GetGlobalID()] = i;
177 }
178
179 // Get comp list dimension from first composite & element
180 auto composites = pFields[0]->GetGraph()->GetComposites();
181 size_t meshDim = pFields[0]->GetGraph()->GetMeshDimension();
182
183 for (size_t i = 0; i < m_compVector.size(); ++i)
184 {
185 // Check composite is present in the rank
186 if (composites.find(m_compVector[i][0]) == composites.end())
187 {
188 continue;
189 }
190
191 std::vector<std::shared_ptr<SpatialDomains::Geometry>> geomVec =
192 composites[m_compVector[i][0]]->m_geomVec;
193 size_t dim =
194 composites[m_compVector[i][0]]->m_geomVec[0]->GetShapeDim();
195
196 // Vector of all geometry IDs contained within the composite list
197 std::vector<size_t> compGeomIds;
198 for (auto compNum : m_compVector[i])
199 {
200 ASSERTL0(composites.find(compNum) != composites.end(),
201 "In FilterIntegral defined composite C[" +
202 std::to_string(compNum) +
203 "] does not exist in the mesh.")
204
205 auto compGeom = composites[compNum]->m_geomVec;
206
207 for (auto &geom : compGeom)
208 {
209 compGeomIds.emplace_back(geom->GetGlobalID());
210 }
211
212 // Only check first element in each comp for dimension
213 ASSERTL0(
214 dim == compGeom[0]->GetShapeDim(),
215 "Differing geometry dimensions specified in FilterIntegral '" +
216 m_splitCompString[i] + "'.");
217 }
218
219 std::vector<std::pair<LocalRegions::ExpansionSharedPtr, int>>
220 tmpCompExp(compGeomIds.size());
221
222 // If dimension of composite == dimension of mesh then we only need the
223 // expansion of the element
224 if (dim == pFields[0]->GetShapeDimension())
225 {
226 for (size_t j = 0; j < compGeomIds.size(); ++j)
227 {
228 tmpCompExp[j] = std::make_pair(
229 (*expList)[m_geomElmtIdToExpId[compGeomIds[j]]], -1);
230 }
231 }
232 // however if the dimension is less we need the expansion of the element
233 // containing the global composite geometry and the face/edge local ID
234 // within that. 3D mesh -> 2D, 2D -> 1D.
235 else if (meshDim > 1 && dim == meshDim - 1)
236 {
237 for (size_t j = 0; j < compGeomIds.size(); ++j)
238 {
240 (*trace)[geomIdToTraceId[compGeomIds[j]]];
241
242 LocalRegions::ExpansionSharedPtr leftAdjElmtExp =
243 exp->GetLeftAdjacentElementExp();
244 int leftAdjElmtFace = exp->GetLeftAdjacentElementTrace();
245
246 tmpCompExp[j] = std::make_pair(leftAdjElmtExp, leftAdjElmtFace);
247 }
248 }
249 else
250 {
252 "FilterIntegral: Only composite dimensions equal to or "
253 "one lower than the mesh dimension are supported.")
254 }
255
256 m_compExpMap[i] = tmpCompExp;
257 }
258
259 v_Update(pFields, time);
260}
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:202
std::map< int, std::vector< std::pair< LocalRegions::ExpansionSharedPtr, int > > > m_compExpMap
Map of composite ID to vector of expansions an face/edge local ID.
void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) final
Calculate integral over requested composites.
std::map< size_t, size_t > m_geomElmtIdToExpId
Mapping from geometry ID to expansion ID.
std::shared_ptr< Expansion > ExpansionSharedPtr
Definition: Expansion.h:66

References ASSERTL0, Nektar::ErrorUtil::efatal, m_compExpMap, m_compVector, m_geomElmtIdToExpId, m_splitCompString, NEKERROR, and v_Update().

◆ v_IsTimeDependent()

bool Nektar::SolverUtils::FilterIntegral::v_IsTimeDependent ( )
finalprotectedvirtual

Returns true as filter depends on time.

This is a time-dependent filter.

Implements Nektar::SolverUtils::Filter.

Definition at line 369 of file FilterIntegral.cpp.

370{
371 return true;
372}

◆ v_Update()

void Nektar::SolverUtils::FilterIntegral::v_Update ( const Array< OneD, const MultiRegions::ExpListSharedPtr > &  pFields,
const NekDouble time 
)
finalprotectedvirtual

Calculate integral over requested composites.

Performs the integration on the stored composite expansions and outputs in to the output data file

Parameters
pFieldsField data
timeCurrent time

Implements Nektar::SolverUtils::Filter.

Definition at line 265 of file FilterIntegral.cpp.

268{
269 if (m_index++ % m_outputFrequency > 0)
270 {
271 return;
272 }
273
274 if (m_comm->GetRank() == 0)
275 {
276 m_outFile << time;
277 }
278
279 for (size_t j = 0; j < m_compVector.size(); ++j)
280 {
281 for (size_t i = 0; i < m_numVariables; ++i)
282 {
283 Array<OneD, const NekDouble> phys;
284 phys = pFields[i]->GetPhys();
285
286 NekDouble sum = 0.0;
287 NekDouble c = 0.0;
288
289 // Check if composite is on the rank
290 if (m_compExpMap.find(j) != m_compExpMap.end())
291 {
292 auto compExp = m_compExpMap[j];
293 size_t dim = compExp[0].first->GetGeom()->GetShapeDim();
294 size_t meshDim = pFields[i]->GetGraph()->GetMeshDimension();
295
296 // Evaluate integral using improved Kahan–Babuška summation
297 // algorithm to reduce numerical error from adding floating
298 // points
299 for (auto &expPair : compExp)
300 {
301 NekDouble input = 0;
302 auto exp = expPair.first;
303
304 if (meshDim == dim)
305 {
306 size_t offset = pFields[i]->GetPhys_Offset(
307 m_geomElmtIdToExpId[exp->GetGeom()->GetGlobalID()]);
308 input = exp->Integral(phys + offset);
309 }
310 else if (meshDim > 1 && dim == meshDim - 1)
311 {
312 Array<OneD, NekDouble> tracePhys;
313 exp->GetTracePhysVals(expPair.second, exp, phys,
314 tracePhys);
315 input =
316 pFields[i]
317 ->GetTrace()
318 ->GetExp(exp->GetGeom()->GetTid(expPair.second))
319 ->Integral(tracePhys);
320 }
321 else
322 {
324 "FilterIntegral: Only composite dimensions "
325 "equal to or one lower than the mesh "
326 "dimension are supported.")
327 }
328
329 NekDouble t = sum + input;
330 c += fabs(sum) >= fabs(input) ? (sum - t) + input
331 : (input - t) + sum;
332 sum = t;
333 }
334 }
335
336 // Sum integral values from all ranks
337 Array<OneD, NekDouble> sumArray(1, sum + c);
338 m_comm->AllReduce(sumArray, LibUtilities::ReduceSum);
339 if (m_comm->GetRank() == 0)
340 {
341 m_outFile << " " << sumArray[0];
342 }
343 }
344 }
345
346 if (m_comm->GetRank() == 0)
347 {
348 m_outFile << std::endl;
349 }
350}
double NekDouble

References Nektar::ErrorUtil::efatal, m_comm, m_compExpMap, m_compVector, m_geomElmtIdToExpId, m_index, m_numVariables, m_outFile, m_outputFrequency, NEKERROR, and Nektar::LibUtilities::ReduceSum.

Referenced by v_Initialise().

Friends And Related Function Documentation

◆ MemoryManager< FilterIntegral >

friend class MemoryManager< FilterIntegral >
friend

Definition at line 1 of file FilterIntegral.h.

Member Data Documentation

◆ className

std::string Nektar::SolverUtils::FilterIntegral::className
static
Initial value:
=
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:197
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::weak_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
FilterFactory & GetFilterFactory()
Definition: Filter.cpp:39

Name of the class.

Definition at line 59 of file FilterIntegral.h.

◆ m_comm

LibUtilities::CommSharedPtr Nektar::SolverUtils::FilterIntegral::m_comm
private

Global communicator.

Definition at line 115 of file FilterIntegral.h.

Referenced by FilterIntegral(), v_Finalise(), and v_Update().

◆ m_compExpMap

std::map<int, std::vector<std::pair<LocalRegions::ExpansionSharedPtr, int> > > Nektar::SolverUtils::FilterIntegral::m_compExpMap
private

Map of composite ID to vector of expansions an face/edge local ID.

Definition at line 124 of file FilterIntegral.h.

Referenced by v_Initialise(), and v_Update().

◆ m_compVector

std::vector<std::vector<unsigned int> > Nektar::SolverUtils::FilterIntegral::m_compVector
private

Vector of vector of composites IDs as integers.

Definition at line 119 of file FilterIntegral.h.

Referenced by FilterIntegral(), v_Initialise(), and v_Update().

◆ m_geomElmtIdToExpId

std::map<size_t, size_t> Nektar::SolverUtils::FilterIntegral::m_geomElmtIdToExpId
private

Mapping from geometry ID to expansion ID.

Definition at line 121 of file FilterIntegral.h.

Referenced by v_Initialise(), and v_Update().

◆ m_index

size_t Nektar::SolverUtils::FilterIntegral::m_index = 0
private

Definition at line 107 of file FilterIntegral.h.

Referenced by v_Update().

◆ m_numVariables

size_t Nektar::SolverUtils::FilterIntegral::m_numVariables
private

Number of fields to perform integral on.

Definition at line 111 of file FilterIntegral.h.

Referenced by FilterIntegral(), and v_Update().

◆ m_outFile

std::ofstream Nektar::SolverUtils::FilterIntegral::m_outFile
private

Out file.

Definition at line 113 of file FilterIntegral.h.

Referenced by FilterIntegral(), v_Finalise(), and v_Update().

◆ m_outputFrequency

size_t Nektar::SolverUtils::FilterIntegral::m_outputFrequency
private

Frequency to write to output data file in timesteps.

Definition at line 109 of file FilterIntegral.h.

Referenced by FilterIntegral(), and v_Update().

◆ m_splitCompString

std::vector<std::string> Nektar::SolverUtils::FilterIntegral::m_splitCompString
private

Vector of composite IDs as a single string.

Definition at line 117 of file FilterIntegral.h.

Referenced by FilterIntegral(), and v_Initialise().