Nektar++
Loading...
Searching...
No Matches
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
43 [[maybe_unused]] int spaceDim,
44 [[maybe_unused]] Array<OneD, MultiRegions::ExpListSharedPtr> &fields)
45{
46}
47
49 [[maybe_unused]] int spaceDim,
50 [[maybe_unused]] Array<OneD, MultiRegions::ExpListSharedPtr> &fields)
51{
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 // Updates trace grid velocity
90 for (int i = 0; i < m_gridVelocityTrace.size(); ++i)
91 {
92 m_fieldsALE[0]->ExtractTracePhys(m_gridVelocity[i],
93 m_gridVelocityTrace[i], true);
94 }
95}
96
98{
99
100 // Reset grid velocity to 0
101 for (int i = 0; i < m_spaceDim; ++i)
102 {
103 std::fill(m_gridVelocity[i].begin(), m_gridVelocity[i].end(), 0.0);
104 }
105 if (m_ALESolver)
106 {
107 // Now update for each movement zone, adding the grid velocities
108 for (auto &ALE : m_ALEs)
109 {
110 ALE->UpdateGridVel(time, m_fieldsALE, m_gridVelocity);
111 }
112 }
113}
114
117{
118 const int nm = m_fieldsALE[0]->GetNcoeffs();
120
121 // Premultiply each field by the mass matrix
122 for (int i = 0; i < m_fieldsALE.size(); ++i)
123 {
124 fields[i] = Array<OneD, NekDouble>(nm);
125 m_fieldsALE[i]->GeneralMatrixOp(mkey, m_fieldsALE[i]->GetCoeffs(),
126 fields[i]);
127 }
128}
129
130/**
131* @brief Update m_fields with u^n by multiplying by inverse mass
132 matrix. That's then used in e.g. checkpoint output and L^2 error
133 calculation.
134*/
136 [[maybe_unused]] Array<OneD, Array<OneD, NekDouble>> &traceNormals,
138 [[maybe_unused]] NekDouble time)
139{
140 // @TODO: Look at geometric factor and junk only what is needed
141 // @TODO: Look at collections and see if they offer a speed up
142 for (int i = 0; i < m_fieldsALE.size(); ++i)
143 {
144 m_fieldsALE[i]->MultiplyByElmtInvMass(
145 fields[i],
146 m_fieldsALE[i]->UpdateCoeffs()); // @TODO: Potentially matrix free?
147 m_fieldsALE[i]->BwdTrans(m_fieldsALE[i]->GetCoeffs(),
148 m_fieldsALE[i]->UpdatePhys());
149 }
150}
151
153 const Array<OneD, const Array<OneD, NekDouble>> &inarray,
155{
156 const int nc = m_fieldsALE[0]->GetNcoeffs();
157 int nVariables = inarray.size();
158
159 // General idea is that we are time-integrating the quantity (Mu), so we
160 // need to multiply input by inverse mass matrix to get coefficients u,
161 // and then backwards transform to physical space so we can apply the DG
162 // operator.
164 for (int i = 0; i < nVariables; ++i)
165 {
166 outarray[i] = Array<OneD, NekDouble>(m_fieldsALE[0]->GetNpoints());
167 m_fieldsALE[i]->MultiplyByElmtInvMass(inarray[i], tmp);
168 m_fieldsALE[i]->BwdTrans(tmp, outarray[i]);
169 }
170}
171
173 Array<OneD, Array<OneD, NekDouble>> &traceNormals)
174{
175 // Only move if timestepped
176 if (time == m_prevStageTime)
177 {
178 return;
179 }
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 ResetMatricesNormal(traceNormals);
190 // Recompute grid velocity.
192
193 // Updates trace grid velocity
194 for (int i = 0; i < m_gridVelocityTrace.size(); ++i)
195 {
196 m_fieldsALE[0]->ExtractTracePhys(m_gridVelocity[i],
197 m_gridVelocityTrace[i], true);
198 }
199
200 // Set the flag to exchange coords in InterfaceMapDG to true
201 m_fieldsALE[0]->GetGraph()->GetMovement()->GetCoordExchangeFlag() = true;
202
203 m_prevStageTime = time;
204}
205
207 Array<OneD, Array<OneD, NekDouble>> &traceNormals)
208{
209 // Now update matrices and normal for each movement zone
210 for (auto &ALE : m_ALEs)
211 {
212 ALE->ResetMatricesNormal(traceNormals, m_fieldsALE);
213 }
214}
215
217{
218 // check all update nromal definitions and return true if any are true
219 for (auto &ALE : m_ALEs)
220 {
221 if (ALE->UpdateNormalsFlag())
222 {
223 m_updateNormals = ALE->UpdateNormalsFlag();
224 break;
225 }
226 }
227}
228
234
236 : m_zone(std::static_pointer_cast<SpatialDomains::ZoneFixed>(zone))
237{
238}
239
241 [[maybe_unused]] NekDouble time,
242 [[maybe_unused]] Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
243 [[maybe_unused]] Array<OneD, Array<OneD, NekDouble>> &gridVelocity)
244{
245}
246
248 [[maybe_unused]] Array<OneD, Array<OneD, NekDouble>> &traceNormals,
249 [[maybe_unused]] Array<OneD, MultiRegions::ExpListSharedPtr> &fields)
250{
251}
252
254 : m_zone(std::static_pointer_cast<SpatialDomains::ZoneTranslate>(zone))
255{
256}
257
259 [[maybe_unused]] NekDouble time,
261 Array<OneD, Array<OneD, NekDouble>> &gridVelocity)
262{
263 auto vel = m_zone->GetVel(time);
264 auto exp = fields[0]->GetExp();
265
266 auto elements = m_zone->GetElements();
267 for (auto &el : elements)
268 {
269 int indx = fields[0]->GetElmtToExpId(el->GetGlobalID());
270 int offset = fields[0]->GetPhys_Offset(indx);
271 auto expansion = (*exp)[indx];
272
273 int nq = expansion->GetTotPoints();
274 for (int i = 0; i < nq; ++i)
275 {
276 for (int j = 0; j < gridVelocity.size(); ++j)
277 {
278 gridVelocity[j][offset + i] += vel[j];
279 }
280 }
281 }
282}
283
285 [[maybe_unused]] Array<OneD, Array<OneD, NekDouble>> &traceNormals,
287{
288 auto &curvedEdges = fields[0]->GetGraph()->GetCurvedEdges();
289 auto &curvedFaces = fields[0]->GetGraph()->GetCurvedFaces();
290
291 // The order of the resets below is v important to avoid errors
292 if (m_meshDistorted)
293 {
294 for (auto &field : fields)
295 {
296 field->ResetMatrices();
297 }
298 }
299
300 // Loop over all elements and faces and edges and reset geometry
301 // information. Only need to do this on the first field as the geometry
302 // information is shared.
303
304 if (m_zone->GetMoved())
305 {
306 auto conEl = m_zone->GetConstituentElements();
307 for (const auto &i : conEl)
308 {
309 for (const auto &j : i)
310 {
311 j->ResetNonRecursive(curvedEdges, curvedFaces);
312 }
313 }
314
315 // We need to rebuild geometric factors on the trace elements
316 for (const auto &i : conEl[fields[0]->GetShapeDimension() -
317 1]) // This only takes the trace elements
318 {
319 fields[0]->GetTrace()->GetExpFromGeomId(i->GetGlobalID())->Reset();
320 }
321 }
322
323 if (m_zone->GetMoved())
324 {
325 auto conEl = m_zone->GetConstituentElements();
326 // Loop over zone elements expansions and rebuild geometric
327 // factors
328 for (const auto &i :
329 conEl[0]) // This only takes highest dimensioned elements
330 {
331 fields[0]->GetExpFromGeomId(i->GetGlobalID())->Reset();
332 }
333 }
334
335 for (auto &field : fields)
336 {
337 // Reset collections (despite the default being eNoCollection it does
338 // remember the last auto-tuned values), eNoImpType gives lots of output
339 field->CreateCollections(Collections::eNoCollection);
340 }
341}
342
344 : m_zone(std::static_pointer_cast<SpatialDomains::ZoneRotate>(zone))
345{
346}
347
349 [[maybe_unused]] NekDouble time,
350 [[maybe_unused]] Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
351 [[maybe_unused]] Array<OneD, Array<OneD, NekDouble>> &gridVelocity)
352{
353
354 auto angVel = m_zone->GetAngularVel(time);
355 auto axis = m_zone->GetAxis();
356 auto origin = m_zone->GetOrigin();
357
358 auto exp = fields[0]->GetExp();
359
360 auto elements = m_zone->GetElements();
361 for (auto &el : elements)
362 {
363 int indx = fields[0]->GetElmtToExpId(el->GetGlobalID());
364 int offset = fields[0]->GetPhys_Offset(indx);
365 auto expansion = (*exp)[indx];
366
367 int nq = expansion->GetTotPoints();
368
369 Array<OneD, NekDouble> xc(nq, 0.0), yc(nq, 0.0), zc(nq, 0.0);
370 Array<OneD, NekDouble> norm(3, 0.0);
371 expansion->GetCoords(xc, yc, zc);
372 for (int i = 0; i < nq; ++i)
373 {
374 // Vector from origin to point
375 NekDouble xpointMinOrigin = xc[i] - origin(0);
376 NekDouble ypointMinOrigin = yc[i] - origin(1);
377 NekDouble zpointMinOrigin = zc[i] - origin(2);
378
379 // Vector orthogonal to plane formed by axis and point
380 // We negate angVel here as by convention a positive angular
381 // velocity is counter-clockwise
382 norm[0] = (ypointMinOrigin * axis[2] - zpointMinOrigin * axis[1]) *
383 (-angVel);
384 norm[1] = (zpointMinOrigin * axis[0] - xpointMinOrigin * axis[2]) *
385 (-angVel);
386 norm[2] = (xpointMinOrigin * axis[1] - ypointMinOrigin * axis[0]) *
387 (-angVel);
388 for (int j = 0; j < gridVelocity.size(); ++j)
389 {
390 gridVelocity[j][offset + i] = norm[j];
391 }
392 }
393 }
394}
395
397 Array<OneD, Array<OneD, NekDouble>> &traceNormals,
399{
400
401 auto &curvedEdges = fields[0]->GetGraph()->GetCurvedEdges();
402 auto &curvedFaces = fields[0]->GetGraph()->GetCurvedFaces();
403
404 // The order of the resets below is v important to avoid errors
405 if (m_meshDistorted)
406 {
407 for (auto &field : fields)
408 {
409 field->ResetMatrices();
410 }
411 }
412
413 // Loop over all elements and faces and edges and reset geometry
414 // information. Only need to do this on the first field as the geometry
415 // information is shared.
416 if (m_zone->GetMoved() && m_zone->v_GetAngle() != 0)
417 {
418 auto conEl = m_zone->GetConstituentElements();
419 for (const auto &i : conEl)
420 {
421 for (const auto &j : i)
422 {
423 j->ResetNonRecursive(curvedEdges, curvedFaces);
424 }
425 }
426
427 // We need to rebuild geometric factors on the trace elements
428 for (const auto &i : conEl[fields[0]->GetShapeDimension() -
429 1]) // This only takes the trace elements
430 {
431 fields[0]->GetTrace()->GetExpFromGeomId(i->GetGlobalID())->Reset();
432 }
433 }
434
435 if (m_zone->GetMoved() && m_zone->v_GetAngle() != 0)
436 {
437 auto conEl = m_zone->GetConstituentElements();
438 // Loop over zone elements expansions and rebuild geometric
439 // factors
440 for (const auto &i :
441 conEl[0]) // This only takes highest dimensioned elements
442 {
443 fields[0]->GetExpFromGeomId(i->GetGlobalID())->Reset();
444 }
445 }
446
447 if (m_zone->GetMoved() && m_zone->v_GetAngle() != 0)
448 {
449 auto conEl = m_zone->GetConstituentElements();
450 // Loop over zone elements expansions and rebuild geometric factors
451 // and recalc trace normals
452 for (const auto &i :
453 conEl[0]) // This only takes highest dimensioned elements
454 {
455 int nfaces =
456 fields[0]->GetExpFromGeomId(i->GetGlobalID())->GetNtraces();
457 for (int j = 0; j < nfaces; ++j)
458 {
459 fields[0]
460 ->GetExpFromGeomId(i->GetGlobalID())
461 ->ComputeTraceNormal(j);
462 }
463 }
464 }
465
466 for (auto &field : fields)
467 {
468 // Reset collections (despite the default being eNoCollection it does
469 // remember the last auto-tuned values), eNoImpType gives lots of output
470 field->CreateCollections(Collections::eNoCollection);
471 }
472
473 // Reload new trace normals in to the solver cache
474 fields[0]->GetTrace()->GetNormals(traceNormals);
475}
476
478 std::vector<Array<OneD, NekDouble>> &fieldcoeffs,
479 std::vector<std::string> &variables)
480
481{
482 int nCoeffs = m_fieldsALE[0]->GetNcoeffs();
483 // Adds extra output variables for grid velocity
484 std::string gridVarName[3] = {"gridVx", "gridVy", "gridVz"};
485 for (int i = 0; i < m_spaceDim; ++i)
486 {
487 Array<OneD, NekDouble> gridVel(nCoeffs, 0.0);
488 m_fieldsALE[0]->FwdTransLocalElmt(m_gridVelocity[i], gridVel);
489 fieldcoeffs.emplace_back(gridVel);
490 variables.emplace_back(gridVarName[i]);
491 }
492}
493
495 std::vector<Array<OneD, NekDouble>> &fieldcoeffs,
496 std::vector<std::string> &variables)
497
498{
499 int nCoeffs = m_fieldsALE[0]->GetNcoeffs();
500 int nPhys = m_fieldsALE[0]->GetTotPoints();
501 // Adds extra output variables for grid velocity
502 std::string gridXName[3] = {"gridX", "gridY", "gridZ"};
504 for (int i = 0; i < m_spaceDim; ++i)
505 {
506 Xc[i] = Array<OneD, NekDouble>(nPhys, 0.0);
507 }
508
509 switch (m_spaceDim)
510 {
511 case 1:
512 {
513 m_fieldsALE[0]->GetCoords(Xc[0]);
514 break;
515 }
516 case 2:
517 {
518 m_fieldsALE[0]->GetCoords(Xc[0], Xc[1]);
519 break;
520 }
521 case 3:
522 {
523 m_fieldsALE[0]->GetCoords(Xc[0], Xc[1], Xc[2]);
524 break;
525 }
526 }
527
528 for (int i = 0; i < m_spaceDim; ++i)
529 {
530 Array<OneD, NekDouble> coeffs(nCoeffs);
531 m_fieldsALE[0]->FwdTransLocalElmt(Xc[i], coeffs);
532 fieldcoeffs.emplace_back(coeffs);
533 variables.emplace_back(gridXName[i]);
534 }
535}
536
537} // namespace Nektar::SolverUtils
#define WARNINGL0(condition, msg)
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:140
SOLVER_UTILS_EXPORT void UpdateNormalsFlag()
virtual SOLVER_UTILS_EXPORT void v_ALEPreMultiplyMass(Array< OneD, Array< OneD, NekDouble > > &fields)
SOLVER_UTILS_EXPORT void ExtraFldOutputGrid(std::vector< Array< OneD, NekDouble > > &fieldcoeffs, std::vector< std::string > &variables)
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....
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:142
SOLVER_UTILS_EXPORT void ALEDoElmtInvMassBwdTrans(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray)
SOLVER_UTILS_EXPORT void ResetMatricesNormal(Array< OneD, Array< OneD, NekDouble > > &traceNormals)
SOLVER_UTILS_EXPORT void ExtraFldOutputGridVelocity(std::vector< Array< OneD, NekDouble > > &fieldcoeffs, std::vector< std::string > &variables)
SOLVER_UTILS_EXPORT void MoveMesh(const NekDouble &time, Array< OneD, Array< OneD, NekDouble > > &traceNormals)
virtual SOLVER_UTILS_EXPORT void v_UpdateGridVelocity(const NekDouble &time)
Definition ALEHelper.cpp:97
SOLVER_UTILS_EXPORT const Array< OneD, const Array< OneD, NekDouble > > & GetGridVelocityTrace()
Array< OneD, Array< OneD, NekDouble > > m_gridVelocity
Definition ALEHelper.h:141
std::vector< ALEBaseShPtr > m_ALEs
Definition ALEHelper.h:143
std::shared_ptr< ALETranslate > ALETranslateShPtr
Definition ALEHelper.h:225
std::shared_ptr< ALERotate > ALERotateShPtr
Definition ALEHelper.h:226
std::shared_ptr< ALEFixed > ALEFixedShPtr
Definition ALEHelper.h:224
std::shared_ptr< ZoneBase > ZoneBaseShPtr
Definition Zones.h:177
STL namespace.
void v_UpdateGridVel(const NekDouble time, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &gridVelocity) final
ALEFixed(SpatialDomains::ZoneBaseShPtr zone)
void v_ResetMatricesNormal(Array< OneD, Array< OneD, NekDouble > > &traceNormals, Array< OneD, MultiRegions::ExpListSharedPtr > &fields) final
SpatialDomains::ZoneRotateShPtr m_zone
Definition ALEHelper.h:221
void v_ResetMatricesNormal(Array< OneD, Array< OneD, NekDouble > > &traceNormals, Array< OneD, MultiRegions::ExpListSharedPtr > &fields) final
ALERotate(SpatialDomains::ZoneBaseShPtr zone)
void v_UpdateGridVel(const NekDouble time, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &gridVelocity) final
SpatialDomains::ZoneTranslateShPtr m_zone
Definition ALEHelper.h:199
ALETranslate(SpatialDomains::ZoneBaseShPtr zone)
void v_ResetMatricesNormal(Array< OneD, Array< OneD, NekDouble > > &traceNormals, Array< OneD, MultiRegions::ExpListSharedPtr > &fields) final
void v_UpdateGridVel(const NekDouble time, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &gridVelocity) final