Nektar++
ALEHelper.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: ALEHelper.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: Helper class for ALE process
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include "ALEHelper.h"
38
39namespace Nektar::SolverUtils
40{
41
44{
45 boost::ignore_unused(spaceDim, fields);
46}
47
48void ALEHelper::InitObject(int spaceDim,
50{
51 boost::ignore_unused(spaceDim, fields);
52 // Create ALE objects for each interface zone
53 if (fields[0]->GetGraph() !=
54 nullptr) // homogeneous graphs are missing the graph data
55 {
56 for (auto &zone : fields[0]->GetGraph()->GetMovement()->GetZones())
57 {
58 switch (zone.second->GetMovementType())
59 {
61 m_ALEs.emplace_back(ALEFixedShPtr(
63 zone.second)));
64 break;
66 m_ALEs.emplace_back(ALETranslateShPtr(
68 zone.second)));
69 m_ALESolver = true;
70 break;
72 m_ALEs.emplace_back(ALERotateShPtr(
74 zone.second)));
75 m_ALESolver = true;
76 break;
78 WARNINGL0(false,
79 "Zone cannot have movement type of 'None'.");
80 default:
81 break;
82 }
83 }
84 }
85
86 // Update grid velocity
88}
89
91{
92
93 // Reset grid velocity to 0
94 for (int i = 0; i < m_spaceDim; ++i)
95 {
96 std::fill(m_gridVelocity[i].begin(), m_gridVelocity[i].end(), 0.0);
97 }
98 if (m_ALESolver)
99 {
100 // Now update for each movement zone, adding the grid velocities
101 for (auto &ALE : m_ALEs)
102 {
103 ALE->UpdateGridVel(time, m_fieldsALE, m_gridVelocity);
104 }
105 }
106}
107
110{
111 if (m_ALESolver)
112 {
113 const int nm = m_fieldsALE[0]->GetNcoeffs();
115
116 // Premultiply each field by the mass matrix
117 for (int i = 0; i < m_fieldsALE.size(); ++i)
118 {
119 fields[i] = Array<OneD, NekDouble>(nm);
120 m_fieldsALE[i]->GeneralMatrixOp(mkey, m_fieldsALE[i]->GetCoeffs(),
121 fields[i]);
122 }
123 }
124}
125
126/**
127* @brief Update m_fields with u^n by multiplying by inverse mass
128 matrix. That's then used in e.g. checkpoint output and L^2 error
129 calculation.
130*/
132 Array<OneD, Array<OneD, NekDouble>> &traceNormals,
134{
135 boost::ignore_unused(time, traceNormals);
136
137 // @TODO: Look at geometric factor and junk only what is needed
138 // @TODO: Look at collections and see if they offer a speed up
139 for (int i = 0; i < m_fieldsALE.size(); ++i)
140 {
141 m_fieldsALE[i]->MultiplyByElmtInvMass(
142 fields[i],
143 m_fieldsALE[i]->UpdateCoeffs()); // @TODO: Potentially matrix free?
144 m_fieldsALE[i]->BwdTrans(m_fieldsALE[i]->GetCoeffs(),
145 m_fieldsALE[i]->UpdatePhys());
146 }
147}
148
150 const Array<OneD, const Array<OneD, NekDouble>> &inarray,
152{
153 const int nc = m_fieldsALE[0]->GetNcoeffs();
154 int nVariables = inarray.size();
155
156 // General idea is that we are time-integrating the quantity (Mu), so we
157 // need to multiply input by inverse mass matrix to get coefficients u,
158 // and then backwards transform to physical space so we can apply the DG
159 // operator.
161 for (int i = 0; i < nVariables; ++i)
162 {
163 outarray[i] = Array<OneD, NekDouble>(m_fieldsALE[0]->GetNpoints());
164 m_fieldsALE[i]->MultiplyByElmtInvMass(inarray[i], tmp);
165 m_fieldsALE[i]->BwdTrans(tmp, outarray[i]);
166 }
167}
168
170 Array<OneD, Array<OneD, NekDouble>> &traceNormals)
171{
172 // Only move if timestepped
173 if (time == m_prevStageTime)
174 {
175 return;
176 }
177
178 auto curvedEdges = m_fieldsALE[0]->GetGraph()->GetCurvedEdges();
179 auto curvedFaces = m_fieldsALE[0]->GetGraph()->GetCurvedFaces();
180
182 timer.Start();
183 m_fieldsALE[0]->GetGraph()->GetMovement()->PerformMovement(
184 time); // @TODO: Moved out of loop!
185 timer.Stop();
186 timer.AccumulateRegion("Movement::PerformMovement");
187
188 // The order of the resets below is v important to avoid errors
189 for (auto &field : m_fieldsALE)
190 {
191 field->ResetMatrices();
192 }
193
194 // Loop over all elements and faces and edges and reset geometry
195 // information. Only need to do this on the first field as the geometry
196 // information is shared.
197 for (auto &zone : m_fieldsALE[0]->GetGraph()->GetMovement()->GetZones())
198 {
199 if (zone.second->GetMoved())
200 {
201 auto conEl = zone.second->GetConstituentElements();
202 for (const auto &i : conEl)
203 {
204 for (const auto &j : i)
205 {
206 j->ResetNonRecursive(curvedEdges, curvedFaces);
207 }
208 }
209
210 // We need to rebuild geometric factors on the trace elements
211 for (const auto &i : conEl[m_fieldsALE[0]->GetShapeDimension() -
212 1]) // This only takes the trace elements
213 {
214 m_fieldsALE[0]
215 ->GetTrace()
216 ->GetExpFromGeomId(i->GetGlobalID())
217 ->Reset();
218 }
219 }
220 }
221
222 for (auto &field : m_fieldsALE)
223 {
224 for (auto &zone : field->GetGraph()->GetMovement()->GetZones())
225 {
226 if (zone.second->GetMoved())
227 {
228 auto conEl = zone.second->GetConstituentElements();
229 // Loop over zone elements expansions and rebuild geometric
230 // factors
231 for (const auto &i :
232 conEl[0]) // This only takes highest dimensioned elements
233 {
234 field->GetExpFromGeomId(i->GetGlobalID())->Reset();
235 }
236 }
237 }
238 }
239
240 for (auto &zone : m_fieldsALE[0]->GetGraph()->GetMovement()->GetZones())
241 {
242 if (zone.second->GetMoved())
243 {
244 auto conEl = zone.second->GetConstituentElements();
245 // Loop over zone elements expansions and rebuild geometric factors
246 // and recalc trace normals
247 for (const auto &i :
248 conEl[0]) // This only takes highest dimensioned elements
249 {
250 int nfaces = m_fieldsALE[0]
251 ->GetExpFromGeomId(i->GetGlobalID())
252 ->GetNtraces();
253 for (int j = 0; j < nfaces; ++j)
254 {
255 m_fieldsALE[0]
256 ->GetExpFromGeomId(i->GetGlobalID())
257 ->ComputeTraceNormal(j);
258 }
259 }
260 }
261 }
262
263 for (auto &field : m_fieldsALE)
264 {
265 // Reset collections (despite the default being eNoCollection it does
266 // remember the last auto-tuned values), eNoImpType gives lots of output
267 field->CreateCollections(Collections::eNoCollection);
268 }
269
270 // Reload new trace normals in to the solver cache
271 m_fieldsALE[0]->GetTrace()->GetNormals(traceNormals);
272
273 // Recompute grid velocity.
275
276 // Updates trace grid velocity
277 for (int i = 0; i < m_gridVelocityTrace.size(); ++i)
278 {
279 m_fieldsALE[0]->ExtractTracePhys(m_gridVelocity[i],
281 }
282
283 // Set the flag to exchange coords in InterfaceMapDG to true
284 m_fieldsALE[0]->GetGraph()->GetMovement()->GetCoordExchangeFlag() = true;
285
286 m_prevStageTime = time;
287}
288
291{
292 return m_gridVelocityTrace;
293}
294
296 : m_zone(std::static_pointer_cast<SpatialDomains::ZoneFixed>(zone))
297{
298}
299
302 Array<OneD, Array<OneD, NekDouble>> &gridVelocity)
303{
304 boost::ignore_unused(time, fields, gridVelocity);
305}
306
308 : m_zone(std::static_pointer_cast<SpatialDomains::ZoneTranslate>(zone))
309{
310}
311
314 Array<OneD, Array<OneD, NekDouble>> &gridVelocity)
315{
316 boost::ignore_unused(time);
317
318 auto vel = m_zone->GetVel(time);
319 auto exp = fields[0]->GetExp();
320
321 auto elements = m_zone->GetElements();
322 for (auto &el : elements)
323 {
324 int indx = fields[0]->GetElmtToExpId(el->GetGlobalID());
325 int offset = fields[0]->GetPhys_Offset(indx);
326 auto expansion = (*exp)[indx];
327
328 int nq = expansion->GetTotPoints();
329 for (int i = 0; i < nq; ++i)
330 {
331 for (int j = 0; j < gridVelocity.size(); ++j)
332 {
333 gridVelocity[j][offset + i] += vel[j];
334 }
335 }
336 }
337}
338
340 : m_zone(std::static_pointer_cast<SpatialDomains::ZoneRotate>(zone))
341{
342}
343
346 Array<OneD, Array<OneD, NekDouble>> &gridVelocity)
347{
348 boost::ignore_unused(time, fields, gridVelocity);
349
350 auto angVel = m_zone->GetAngularVel(time);
351 auto axis = m_zone->GetAxis();
352 auto origin = m_zone->GetOrigin();
353
354 auto exp = fields[0]->GetExp();
355
356 auto elements = m_zone->GetElements();
357 for (auto &el : elements)
358 {
359 int indx = fields[0]->GetElmtToExpId(el->GetGlobalID());
360 int offset = fields[0]->GetPhys_Offset(indx);
361 auto expansion = (*exp)[indx];
362
363 int nq = expansion->GetTotPoints();
364
365 Array<OneD, NekDouble> xc(nq, 0.0), yc(nq, 0.0), zc(nq, 0.0);
366 Array<OneD, NekDouble> norm(3, 0.0);
367 expansion->GetCoords(xc, yc, zc);
368 for (int i = 0; i < nq; ++i)
369 {
370 // Vector from origin to point
371 NekDouble xpointMinOrigin = xc[i] - origin(0);
372 NekDouble ypointMinOrigin = yc[i] - origin(1);
373 NekDouble zpointMinOrigin = zc[i] - origin(2);
374
375 // Vector orthogonal to plane formed by axis and point
376 // We negate angVel here as by convention a positive angular
377 // velocity is counter-clockwise
378 norm[0] = (ypointMinOrigin * axis[2] - zpointMinOrigin * axis[1]) *
379 (-angVel);
380 norm[1] = (zpointMinOrigin * axis[0] - xpointMinOrigin * axis[2]) *
381 (-angVel);
382 norm[2] = (xpointMinOrigin * axis[1] - ypointMinOrigin * axis[0]) *
383 (-angVel);
384 for (int j = 0; j < gridVelocity.size(); ++j)
385 {
386 gridVelocity[j][offset + i] = norm[j];
387 }
388 }
389 }
390}
391
393 std::vector<Array<OneD, NekDouble>> &fieldcoeffs,
394 std::vector<std::string> &variables)
395
396{
397 int nCoeffs = m_fieldsALE[0]->GetNcoeffs();
398 // Adds extra output variables for grid velocity
399 std::string gridVarName[3] = {"gridVx", "gridVy", "gridVz"};
400 for (int i = 0; i < m_spaceDim; ++i)
401 {
402 Array<OneD, NekDouble> gridVel(nCoeffs, 0.0);
403 m_fieldsALE[0]->FwdTransLocalElmt(m_gridVelocity[i], gridVel);
404 fieldcoeffs.emplace_back(gridVel);
405 variables.emplace_back(gridVarName[i]);
406 }
407}
408
409} // namespace Nektar::SolverUtils
#define WARNINGL0(condition, msg)
Definition: ErrorUtil.hpp:215
void AccumulateRegion(std::string, int iolevel=0)
Accumulate elapsed time for a region.
Definition: Timer.cpp:70
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
Describes a matrix with ordering defined by a local to global map.
Array< OneD, MultiRegions::ExpListSharedPtr > m_fieldsALE
Definition: ALEHelper.h:89
virtual SOLVER_UTILS_EXPORT void v_ALEPreMultiplyMass(Array< OneD, Array< OneD, NekDouble > > &fields)
Definition: ALEHelper.cpp:108
virtual SOLVER_UTILS_EXPORT void v_ALEInitObject(int spaceDim, Array< OneD, MultiRegions::ExpListSharedPtr > &fields)
Definition: ALEHelper.cpp:42
SOLVER_UTILS_EXPORT void ALEDoElmtInvMass(Array< OneD, Array< OneD, NekDouble > > &traceNormals, Array< OneD, Array< OneD, NekDouble > > &fields, NekDouble time)
Update m_fields with u^n by multiplying by inverse mass matrix. That's then used in e....
Definition: ALEHelper.cpp:131
SOLVER_UTILS_EXPORT void InitObject(int spaceDim, Array< OneD, MultiRegions::ExpListSharedPtr > &fields)
Definition: ALEHelper.cpp:48
Array< OneD, Array< OneD, NekDouble > > m_gridVelocityTrace
Definition: ALEHelper.h:91
SOLVER_UTILS_EXPORT void ALEDoElmtInvMassBwdTrans(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray)
Definition: ALEHelper.cpp:149
SOLVER_UTILS_EXPORT void ExtraFldOutputGridVelocity(std::vector< Array< OneD, NekDouble > > &fieldcoeffs, std::vector< std::string > &variables)
Definition: ALEHelper.cpp:392
SOLVER_UTILS_EXPORT void MoveMesh(const NekDouble &time, Array< OneD, Array< OneD, NekDouble > > &traceNormals)
Definition: ALEHelper.cpp:169
virtual SOLVER_UTILS_EXPORT void v_UpdateGridVelocity(const NekDouble &time)
Definition: ALEHelper.cpp:90
SOLVER_UTILS_EXPORT const Array< OneD, const Array< OneD, NekDouble > > & GetGridVelocityTrace()
Definition: ALEHelper.cpp:290
Array< OneD, Array< OneD, NekDouble > > m_gridVelocity
Definition: ALEHelper.h:90
std::vector< ALEBaseShPtr > m_ALEs
Definition: ALEHelper.h:92
const NekPoint< NekDouble > origin
std::shared_ptr< ALETranslate > ALETranslateShPtr
Definition: ALEHelper.h:158
std::shared_ptr< ALERotate > ALERotateShPtr
Definition: ALEHelper.h:159
std::shared_ptr< ALEFixed > ALEFixedShPtr
Definition: ALEHelper.h:157
std::shared_ptr< ZoneBase > ZoneBaseShPtr
Definition: Zones.h:170
double NekDouble
STL namespace.
void v_UpdateGridVel(const NekDouble time, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &gridVelocity) final
Definition: ALEHelper.cpp:300
ALEFixed(SpatialDomains::ZoneBaseShPtr zone)
Definition: ALEHelper.cpp:295
SpatialDomains::ZoneRotateShPtr m_zone
Definition: ALEHelper.h:154
ALERotate(SpatialDomains::ZoneBaseShPtr zone)
Definition: ALEHelper.cpp:339
void v_UpdateGridVel(const NekDouble time, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &gridVelocity) final
Definition: ALEHelper.cpp:344
SpatialDomains::ZoneTranslateShPtr m_zone
Definition: ALEHelper.h:141
ALETranslate(SpatialDomains::ZoneBaseShPtr zone)
Definition: ALEHelper.cpp:307
void v_UpdateGridVel(const NekDouble time, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &gridVelocity) final
Definition: ALEHelper.cpp:312