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
49namespace Nektar
50{
51
54 "IterativeElasticSystem", IterativeElasticSystem::create);
55
59 : LinearElasticSystem(pSession, pGraph)
60{
61}
62
64{
66
67 const int nVel = m_fields[0]->GetCoordim(0);
68
69 // Read in number of steps to take.
70 m_session->LoadParameter("NumSteps", m_numSteps, 0);
71 ASSERTL0(m_numSteps > 0, "You must specify at least one step");
72
73 // Read in whether to repeatedly apply boundary conditions (for e.g.
74 // rotation purposes).
75 std::string bcType;
76 m_session->LoadSolverInfo("BCType", bcType, "Normal");
77 m_repeatBCs = bcType != "Normal";
78
79 if (!m_repeatBCs)
80 {
81 // Loop over BCs, identify which ones we need to deform.
83 &bndCond = m_fields[0]->GetBndConditions();
84
85 for (int i = 0; i < bndCond.size(); ++i)
86 {
87 if (boost::iequals(bndCond[i]->GetUserDefined(), "Wall"))
88 {
89 m_toDeform.push_back(i);
90 }
91 }
92
93 int numDeform = m_toDeform.size();
94 m_comm->AllReduce(numDeform, LibUtilities::ReduceMax);
95 ASSERTL0(numDeform > 0, "You must specify at least one WALL tag on"
96 "a boundary condition");
97
100
101 for (int i = 0; i < m_toDeform.size(); ++i)
102 {
104 for (int j = 0; j < nVel; ++j)
105 {
106 const int id = m_toDeform[i];
108 m_fields[j]->GetBndCondExpansions()[id];
109 int nCoeffs = bndCondExp->GetNcoeffs();
110
111 m_savedBCs[i][j] = Array<OneD, NekDouble>(nCoeffs);
112 Vmath::Smul(nCoeffs, 1.0 / m_numSteps, bndCondExp->GetCoeffs(),
113 1, bndCondExp->UpdateCoeffs(), 1);
114 Vmath::Vcopy(nCoeffs, bndCondExp->GetCoeffs(), 1,
115 m_savedBCs[i][j], 1);
116 }
117 }
118 }
119}
120
122{
124}
125
127{
128 int i, j, k;
129
130 // Write initial geometry for consistency/script purposes
131 WriteGeometry(0);
132
134
135 for (int i = 0; i < m_fields.size(); ++i)
136 {
137 physvals[i] = m_fields[i]->UpdatePhys();
138 }
139
140 // Now loop over desired number of steps
141 for (i = 1; i <= m_numSteps; ++i)
142 {
143 int invalidElmtId = -1;
144
145 // Perform solve for this iteration and update geometry accordingly.
148 WriteGeometry(i);
149
150 // Check for invalid elements.
151 for (j = 0; j < m_fields[0]->GetExpSize(); ++j)
152 {
154 m_fields[0]->GetExp(j)->GetGeom()->GetGeomFactors();
155
156 if (!geomFac->IsValid())
157 {
158 invalidElmtId =
159 m_fields[0]->GetExp(j)->GetGeom()->GetGlobalID();
160 break;
161 }
162 }
163
164 m_session->GetComm()->AllReduce(invalidElmtId, LibUtilities::ReduceMax);
165
166 // If we found an invalid element, exit loop without writing output.
167 if (invalidElmtId >= 0)
168 {
169 if (m_session->GetComm()->GetRank() == 0)
170 {
171 std::cout << "- Detected negative Jacobian in element "
172 << invalidElmtId
173 << "; terminating at"
174 " step: "
175 << i << std::endl;
176 }
177
178 break;
179 }
180
181 if (m_session->GetComm()->GetRank() == 0)
182 {
183 std::cout << "Step: " << i << std::endl;
184 }
185
186 // Update boundary conditions
187 if (m_repeatBCs)
188 {
189 for (j = 0; j < m_fields.size(); ++j)
190 {
191 std::string varName = m_session->GetVariable(j);
192 m_fields[j]->EvaluateBoundaryConditions(m_time, varName);
193 }
194 }
195 else
196 {
197 for (j = 0; j < m_fields.size(); ++j)
198 {
200 &bndCondExp = m_fields[j]->GetBndCondExpansions();
201
202 for (k = 0; k < m_toDeform.size(); ++k)
203 {
204 const int id = m_toDeform[k];
205 const int nCoeffs = bndCondExp[id]->GetNcoeffs();
206 Vmath::Vcopy(nCoeffs, m_savedBCs[k][j], 1,
207 bndCondExp[id]->UpdateCoeffs(), 1);
208 }
209 }
210 }
211 }
212}
213
214/**
215 * @brief Write out a file in serial or directory in parallel containing new
216 * mesh geometry.
217 */
219{
220 fs::path filename;
221 std::stringstream s;
222 s << m_session->GetSessionName() << "-" << i;
223
224 if (m_session->GetComm()->GetSize() > 1)
225 {
226 s << "_xml";
227
228 std::string ss = s.str();
229 if (!fs::is_directory(ss))
230 {
231 fs::create_directory(ss);
232 }
233
234 boost::format pad("P%1$07d.xml");
235 pad % m_session->GetComm()->GetRank();
236 filename = fs::path(ss) / fs::path(pad.str());
237 }
238 else
239 {
240 s << ".xml";
241 filename = fs::path(s.str());
242 }
243
244 std::string fname = LibUtilities::PortablePath(filename);
245 std::string geomType = m_session->GetGeometryType();
246 auto graphIO =
248 graphIO->SetMeshGraph(m_graph);
249 graphIO->WriteGeometry(fname);
250}
251
252} // 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.
bool m_repeatBCs
Flag determining whether to repeat boundary conditions.
void v_GenerateSummary(SolverUtils::SummaryList &s) override
Virtual function for generating summary information.
static std::string className
Name of class.
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:53
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:66
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:45
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