Nektar++
IterativeElasticSystem.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: IterativeElasticSystem.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: IterativeElasticSystem solve routines
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <boost/format.hpp>
36
46
48
49using namespace std;
50
51namespace Nektar
52{
53
56 "IterativeElasticSystem", IterativeElasticSystem::create);
57
61 : LinearElasticSystem(pSession, pGraph)
62{
63}
64
66{
68
69 const int nVel = m_fields[0]->GetCoordim(0);
70
71 // Read in number of steps to take.
72 m_session->LoadParameter("NumSteps", m_numSteps, 0);
73 ASSERTL0(m_numSteps > 0, "You must specify at least one step");
74
75 // Read in whether to repeatedly apply boundary conditions (for e.g.
76 // rotation purposes).
77 string bcType;
78 m_session->LoadSolverInfo("BCType", bcType, "Normal");
79 m_repeatBCs = bcType != "Normal";
80
81 if (!m_repeatBCs)
82 {
83 // Loop over BCs, identify which ones we need to deform.
85 &bndCond = m_fields[0]->GetBndConditions();
86
87 for (int i = 0; i < bndCond.size(); ++i)
88 {
89 if (boost::iequals(bndCond[i]->GetUserDefined(), "Wall"))
90 {
91 m_toDeform.push_back(i);
92 }
93 }
94
95 int numDeform = m_toDeform.size();
96 m_comm->AllReduce(numDeform, LibUtilities::ReduceMax);
97 ASSERTL0(numDeform > 0, "You must specify at least one WALL tag on"
98 "a boundary condition");
99
100 m_savedBCs =
102
103 for (int i = 0; i < m_toDeform.size(); ++i)
104 {
106 for (int j = 0; j < nVel; ++j)
107 {
108 const int id = m_toDeform[i];
110 m_fields[j]->GetBndCondExpansions()[id];
111 int nCoeffs = bndCondExp->GetNcoeffs();
112
113 m_savedBCs[i][j] = Array<OneD, NekDouble>(nCoeffs);
114 Vmath::Smul(nCoeffs, 1.0 / m_numSteps, bndCondExp->GetCoeffs(),
115 1, bndCondExp->UpdateCoeffs(), 1);
116 Vmath::Vcopy(nCoeffs, bndCondExp->GetCoeffs(), 1,
117 m_savedBCs[i][j], 1);
118 }
119 }
120 }
121}
122
124{
126}
127
129{
130 int i, j, k;
131
132 // Write initial geometry for consistency/script purposes
133 WriteGeometry(0);
134
136
137 for (int i = 0; i < m_fields.size(); ++i)
138 {
139 physvals[i] = m_fields[i]->UpdatePhys();
140 }
141
142 // Now loop over desired number of steps
143 for (i = 1; i <= m_numSteps; ++i)
144 {
145 int invalidElmtId = -1;
146
147 // Perform solve for this iteration and update geometry accordingly.
150 WriteGeometry(i);
151
152 // Check for invalid elements.
153 for (j = 0; j < m_fields[0]->GetExpSize(); ++j)
154 {
156 m_fields[0]->GetExp(j)->GetGeom()->GetGeomFactors();
157
158 if (!geomFac->IsValid())
159 {
160 invalidElmtId =
161 m_fields[0]->GetExp(j)->GetGeom()->GetGlobalID();
162 break;
163 }
164 }
165
166 m_session->GetComm()->AllReduce(invalidElmtId, LibUtilities::ReduceMax);
167
168 // If we found an invalid element, exit loop without writing output.
169 if (invalidElmtId >= 0)
170 {
171 if (m_session->GetComm()->GetRank() == 0)
172 {
173 cout << "- Detected negative Jacobian in element "
174 << invalidElmtId
175 << "; terminating at"
176 " step: "
177 << i << endl;
178 }
179
180 break;
181 }
182
183 if (m_session->GetComm()->GetRank() == 0)
184 {
185 cout << "Step: " << i << endl;
186 }
187
188 // Update boundary conditions
189 if (m_repeatBCs)
190 {
191 for (j = 0; j < m_fields.size(); ++j)
192 {
193 string varName = m_session->GetVariable(j);
194 m_fields[j]->EvaluateBoundaryConditions(m_time, varName);
195 }
196 }
197 else
198 {
199 for (j = 0; j < m_fields.size(); ++j)
200 {
202 &bndCondExp = m_fields[j]->GetBndCondExpansions();
203
204 for (k = 0; k < m_toDeform.size(); ++k)
205 {
206 const int id = m_toDeform[k];
207 const int nCoeffs = bndCondExp[id]->GetNcoeffs();
208 Vmath::Vcopy(nCoeffs, m_savedBCs[k][j], 1,
209 bndCondExp[id]->UpdateCoeffs(), 1);
210 }
211 }
212 }
213 }
214}
215
216/**
217 * @brief Write out a file in serial or directory in parallel containing new
218 * mesh geometry.
219 */
221{
222 fs::path filename;
223 stringstream s;
224 s << m_session->GetSessionName() << "-" << i;
225
226 if (m_session->GetComm()->GetSize() > 1)
227 {
228 s << "_xml";
229
230 string ss = s.str();
231 if (!fs::is_directory(ss))
232 {
233 fs::create_directory(ss);
234 }
235
236 boost::format pad("P%1$07d.xml");
237 pad % m_session->GetComm()->GetRank();
238 filename = fs::path(ss) / fs::path(pad.str());
239 }
240 else
241 {
242 s << ".xml";
243 filename = fs::path(s.str());
244 }
245
246 string fname = LibUtilities::PortablePath(filename);
247 string geomType = m_session->GetGeometryType();
248 auto graphIO =
250 graphIO->SetMeshGraph(m_graph);
251 graphIO->WriteGeometry(fname);
252}
253
254} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
Array< OneD, Array< OneD, Array< OneD, NekDouble > > > m_savedBCs
Storage for boundary conditions.
static EquationSystemSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const SpatialDomains::MeshGraphSharedPtr &pGraph)
Creates an instance of this class.
void WriteGeometry(const int i)
Write out a file in serial or directory in parallel containing new mesh geometry.
void v_InitObject(bool DeclareFields=true) override
Initialisation object for EquationSystem.
static std::string className
Name of class.
bool m_repeatBCs
Flag determining whether to repeat boundary conditions.
void v_GenerateSummary(SolverUtils::SummaryList &s) override
Virtual function for generating summary information.
std::vector< int > m_toDeform
Vector of boundary regions to deform.
void v_DoSolve() override
Virtual function for solve implementation.
int m_numSteps
Number of steps to split deformation across.
IterativeElasticSystem(const LibUtilities::SessionReaderSharedPtr &pSession, const SpatialDomains::MeshGraphSharedPtr &pGraph)
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Base class for linear elastic system.
void v_InitObject(bool DeclareFields=true) override
Set up the linear elasticity system.
void v_DoSolve() override
Solve elliptic linear elastic system.
void v_GenerateSummary(SolverUtils::SummaryList &s) override
Generate summary at runtime.
SpatialDomains::MeshGraphSharedPtr m_graph
Pointer to graph defining mesh.
LibUtilities::CommSharedPtr m_comm
Communicator.
NekDouble m_time
Current time of simulation.
Array< OneD, MultiRegions::ExpListSharedPtr > m_fields
Array holding all dependent variables.
LibUtilities::SessionReaderSharedPtr m_session
The session reader.
void UpdateGeometry(SpatialDomains::MeshGraphSharedPtr graph, Array< OneD, MultiRegions::ExpListSharedPtr > &fields, Array< OneD, Array< OneD, NekDouble > > &PhysVals, bool modal)
Update geometry according to displacement that is in current fields.
Definition: Deform.cpp:59
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:56
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::vector< std::pair< std::string, std::string > > SummaryList
Definition: Misc.h:46
EquationSystemFactory & GetEquationSystemFactory()
std::shared_ptr< GeomFactors > GeomFactorsSharedPtr
Pointer to a GeomFactors object.
Definition: GeomFactors.h:60
MeshGraphIOFactory & GetMeshGraphIOFactory()
Definition: MeshGraphIO.cpp:47
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
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 Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825
STL namespace.