Nektar++
DiffusionSolverTimeInt.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: DiffusionSolverTimeInt.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: Diffusion solver
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <cstdlib>
36
41
44
45using namespace std;
46using namespace Nektar;
47
49{
50public:
51 Diffusion(int argc, char *argv[]);
53
54 void TimeIntegrate();
55
56 void DoImplicitSolve(
57 const Array<OneD, const Array<OneD, NekDouble>> &inarray,
58 Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble time,
59 const NekDouble lambda);
60
61private:
67
71
73 unsigned int nSteps;
77
78 void WriteSolution();
79 void ExactSolution();
80};
81
82Diffusion::Diffusion(int argc, char *argv[])
83{
84 // Create session reader.
86
87 // Read the geometry and the expansion information
88 graph = SpatialDomains::MeshGraphIO::Read(session);
89
90 // Create Field I/O object.
92
93 // Get some information from the session
94 sessionName = session->GetSessionName();
95
96 // Create time integration scheme.
97 if (session->DefinesTimeIntScheme())
98 {
99 timeInt = session->GetTimeIntScheme();
100 }
101 else
102 {
103 timeInt.method = session->GetSolverInfo("TimeIntegrationMethod");
104 }
105
106 nSteps = session->GetParameter("NumSteps");
107 delta_t = session->GetParameter("TimeStep");
108 epsilon = session->GetParameter("epsilon");
109 lambda = 1.0 / delta_t / epsilon;
110
111 // Set up the field
113 session, graph, session->GetVariable(0));
114
116 fields[0] = field->UpdatePhys();
117
118 // Get coordinates of physical points
119 unsigned int nq = field->GetNpoints();
120 Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
121 field->GetCoords(x0, x1, x2);
122
123 // Evaluate initial condition
125 session->GetFunction("InitialConditions", "u");
126 icond->Evaluate(x0, x1, x2, 0.0, field->UpdatePhys());
127}
128
130{
131 session->Finalise();
132}
133
135{
137 timeInt.method, timeInt.variant, timeInt.order, timeInt.freeParams);
138
139 ode.DefineImplicitSolve(&Diffusion::DoImplicitSolve, this);
140
141 // Initialise the scheme for actual time integration scheme
142 intScheme->InitializeScheme(delta_t, fields, 0.0, ode);
143
144 // Zero field coefficients for initial guess for linear solver.
145 Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
146
147 for (int n = 0; n < nSteps; ++n)
148 {
149 fields = intScheme->TimeIntegrate(n, delta_t);
150 }
151
152 Vmath::Vcopy(field->GetNpoints(), fields[0], 1, field->UpdatePhys(), 1);
153
154 WriteSolution();
155 ExactSolution();
156}
157
159 const Array<OneD, const Array<OneD, NekDouble>> &inarray,
161 [[maybe_unused]] const NekDouble time, const NekDouble lambda)
162{
164 factors[StdRegions::eFactorLambda] = 1.0 / lambda / epsilon;
165
166 for (int i = 0; i < inarray.size(); ++i)
167 {
168 // Multiply RHS by 1.0/timestep/lambda
170 inarray[i], 1, outarray[i], 1);
171
172 // Solve a system of equations with Helmholtz solver
173 field->HelmSolve(outarray[i], field->UpdateCoeffs(), factors);
174
175 // Transform to physical space and store in solution vector
176 field->BwdTrans(field->GetCoeffs(), outarray[i]);
177 }
178}
179
181{
182 // Write solution to file
183 std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
184 field->GetFieldDefinitions();
185 std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
186 for (int i = 0; i < FieldDef.size(); ++i)
187 {
188 FieldDef[i]->m_fields.push_back("u");
189 field->AppendFieldData(FieldDef[i], FieldData[i]);
190 }
191 fld->Write(session->GetSessionName() + ".fld", FieldDef, FieldData);
192}
193
195{
196 unsigned int nq = field->GetNpoints();
197 Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
198 field->GetCoords(x0, x1, x2);
199
201 session->GetFunction("ExactSolution", 0);
202
203 if (ex_sol)
204 {
205 // evaluate exact solution
206 Array<OneD, NekDouble> exact(nq);
207 ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
208
209 // Calculate errors
210 cout << "L inf error: " << field->Linf(field->GetPhys(), exact)
211 << endl;
212 cout << "L 2 error: " << field->L2(field->GetPhys(), exact)
213 << endl;
214 cout << "H 1 error: " << field->H1(field->GetPhys(), exact)
215 << endl;
216 }
217}
218
219int main(int argc, char *argv[])
220{
221 try
222 {
223 Diffusion ops(argc, argv);
224 ops.TimeIntegrate();
225 }
226 catch (const std::runtime_error &e)
227 {
228 exit(-1);
229 }
230 catch (const std::string &eStr)
231 {
232 cout << "Error: " << eStr << endl;
233 exit(-1);
234 }
235}
int main(int argc, char *argv[])
LibUtilities::TimeIntScheme timeInt
MultiRegions::ContFieldSharedPtr field
SpatialDomains::MeshGraphSharedPtr graph
LibUtilities::TimeIntegrationSchemeOperators ode
LibUtilities::FieldIOSharedPtr fld
unsigned int nSteps
void DoImplicitSolve(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble time, const NekDouble lambda)
LibUtilities::TimeIntegrationSchemeSharedPtr intScheme
Diffusion(int argc, char *argv[])
LibUtilities::SessionReaderSharedPtr session
Array< OneD, Array< OneD, NekDouble > > fields
static std::shared_ptr< FieldIO > CreateDefault(const LibUtilities::SessionReaderSharedPtr session)
Returns an object for the default FieldIO method.
Definition: FieldIO.cpp:195
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
Binds a set of functions for use by time integration schemes.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
virtual SOLVER_UTILS_EXPORT ~Diffusion()
Definition: Diffusion.h:139
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Definition: MeshGraphIO.cpp:53
TimeIntegrationSchemeFactory & GetTimeIntegrationSchemeFactory()
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:322
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:125
std::shared_ptr< TimeIntegrationScheme > TimeIntegrationSchemeSharedPtr
std::shared_ptr< ContField > ContFieldSharedPtr
Definition: ContField.h:268
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:430
StdRegions::ConstFactorMap factors
double NekDouble
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 Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.hpp:273
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:825
STL namespace.