Nektar++
Loading...
Searching...
No Matches
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
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 timeInt = session->GetTimeIntScheme();
98
99 nSteps = session->GetParameter("NumSteps");
100 delta_t = session->GetParameter("TimeStep");
101 epsilon = session->GetParameter("epsilon");
102 lambda = 1.0 / delta_t / epsilon;
103
104 // Set up the field
106 session, graph, session->GetVariable(0));
107
109 fields[0] = field->UpdatePhys();
110
111 // Get coordinates of physical points
112 unsigned int nq = field->GetNpoints();
113 Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
114 field->GetCoords(x0, x1, x2);
115
116 // Evaluate initial condition
118 session->GetFunction("InitialConditions", "u");
119 icond->Evaluate(x0, x1, x2, 0.0, field->UpdatePhys());
120}
121
123{
124 session->Finalise();
125}
126
128{
130 timeInt.method, timeInt.variant, timeInt.order, timeInt.freeParams);
131
132 ode.DefineImplicitSolve(&Diffusion::DoImplicitSolve, this);
133
134 // Initialise the scheme for actual time integration scheme
135 intScheme->InitializeScheme(delta_t, fields, 0.0, ode);
136
137 // Zero field coefficients for initial guess for linear solver.
138 Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
139
140 for (int n = 0; n < nSteps; ++n)
141 {
142 fields = intScheme->TimeIntegrate(n, delta_t);
143 }
144
145 Vmath::Vcopy(field->GetNpoints(), fields[0], 1, field->UpdatePhys(), 1);
146
147 WriteSolution();
148 ExactSolution();
149}
150
152 const Array<OneD, const Array<OneD, NekDouble>> &inarray,
154 [[maybe_unused]] const NekDouble time, const NekDouble lambda)
155{
157 factors[StdRegions::eFactorLambda] = 1.0 / lambda / epsilon;
158
159 for (int i = 0; i < inarray.size(); ++i)
160 {
161 // Multiply RHS by 1.0/timestep/lambda
162 Vmath::Smul(field->GetNpoints(), -factors[StdRegions::eFactorLambda],
163 inarray[i], 1, outarray[i], 1);
164
165 // Solve a system of equations with Helmholtz solver
166 field->HelmSolve(outarray[i], field->UpdateCoeffs(), factors);
167
168 // Transform to physical space and store in solution vector
169 field->BwdTrans(field->GetCoeffs(), outarray[i]);
170 }
171}
172
174{
175 // Write solution to file
176 std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
177 field->GetFieldDefinitions();
178 std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
179 for (int i = 0; i < FieldDef.size(); ++i)
180 {
181 FieldDef[i]->m_fields.push_back("u");
182 field->AppendFieldData(FieldDef[i], FieldData[i]);
183 }
184 fld->Write(session->GetSessionName() + ".fld", FieldDef, FieldData);
185}
186
188{
189 unsigned int nq = field->GetNpoints();
190 Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
191 field->GetCoords(x0, x1, x2);
192
194 session->GetFunction("ExactSolution", 0);
195
196 if (ex_sol)
197 {
198 // evaluate exact solution
199 Array<OneD, NekDouble> exact(nq);
200 ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
201
202 // Calculate errors
203 cout << "L inf error: " << field->Linf(field->GetPhys(), exact)
204 << endl;
205 cout << "L 2 error: " << field->L2(field->GetPhys(), exact)
206 << endl;
207 cout << "H 1 error: " << field->H1(field->GetPhys(), exact)
208 << endl;
209 }
210}
211
212int main(int argc, char *argv[])
213{
214 try
215 {
216 Diffusion ops(argc, argv);
217 ops.TimeIntegrate();
218 }
219 catch (const std::runtime_error &e)
220 {
221 exit(-1);
222 }
223 catch (const std::string &eStr)
224 {
225 cout << "Error: " << eStr << endl;
226 exit(-1);
227 }
228}
LibUtilities::TimeIntScheme timeInt
MultiRegions::ContFieldSharedPtr field
SpatialDomains::MeshGraphSharedPtr graph
LibUtilities::TimeIntegrationSchemeOperators ode
LibUtilities::FieldIOSharedPtr fld
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:194
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()=default
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
TimeIntegrationSchemeFactory & GetTimeIntegrationSchemeFactory()
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition FieldIO.h:322
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition Equation.h:131
std::shared_ptr< TimeIntegrationScheme > TimeIntegrationSchemeSharedPtr
std::shared_ptr< ContField > ContFieldSharedPtr
Definition ContField.h:278
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition MeshGraph.h:217
std::map< ConstFactorType, NekDouble > ConstFactorMap
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
Definition main.py:1
STL namespace.