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 
37 #include <boost/core/ignore_unused.hpp>
38 
43 
44 #include <MultiRegions/ContField.h>
46 
47 using namespace std;
48 using namespace Nektar;
49 
50 class Diffusion
51 {
52 public:
53  Diffusion(int argc, char *argv[]);
55 
56  void TimeIntegrate();
57 
58  void DoImplicitSolve(
59  const Array<OneD, const Array<OneD, NekDouble>> &inarray,
60  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble time,
61  const NekDouble lambda);
62 
63 private:
66  string sessionName;
69 
73 
75  unsigned int nSteps;
79 
80  void WriteSolution();
81  void ExactSolution();
82 };
83 
84 Diffusion::Diffusion(int argc, char *argv[])
85 {
86  // Create session reader.
87  session = LibUtilities::SessionReader::CreateInstance(argc, argv);
88 
89  // Read the geometry and the expansion information
90  graph = SpatialDomains::MeshGraph::Read(session);
91 
92  // Create Field I/O object.
93  fld = LibUtilities::FieldIO::CreateDefault(session);
94 
95  // Get some information from the session
96  sessionName = session->GetSessionName();
97 
98  // Create time integration scheme.
99  if (session->DefinesTimeIntScheme())
100  {
101  timeInt = session->GetTimeIntScheme();
102  }
103  else
104  {
105  timeInt.method = session->GetSolverInfo("TimeIntegrationMethod");
106  }
107 
108  nSteps = session->GetParameter("NumSteps");
109  delta_t = session->GetParameter("TimeStep");
110  epsilon = session->GetParameter("epsilon");
111  lambda = 1.0 / delta_t / epsilon;
112 
113  // Set up the field
115  session, graph, session->GetVariable(0));
116 
118  fields[0] = field->UpdatePhys();
119 
120  // Get coordinates of physical points
121  unsigned int nq = field->GetNpoints();
122  Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
123  field->GetCoords(x0, x1, x2);
124 
125  // Evaluate initial condition
127  session->GetFunction("InitialConditions", "u");
128  icond->Evaluate(x0, x1, x2, 0.0, field->UpdatePhys());
129 }
130 
132 {
133  session->Finalise();
134 }
135 
137 {
139  timeInt.method, timeInt.variant, timeInt.order, timeInt.freeParams);
140 
141  ode.DefineImplicitSolve(&Diffusion::DoImplicitSolve, this);
142 
143  // Initialise the scheme for actual time integration scheme
144  intScheme->InitializeScheme(delta_t, fields, 0.0, ode);
145 
146  // Zero field coefficients for initial guess for linear solver.
147  Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
148 
149  for (int n = 0; n < nSteps; ++n)
150  {
151  fields = intScheme->TimeIntegrate(n, delta_t, ode);
152  }
153 
154  Vmath::Vcopy(field->GetNpoints(), fields[0], 1, field->UpdatePhys(), 1);
155 
156  WriteSolution();
157  ExactSolution();
158 }
159 
161  const Array<OneD, const Array<OneD, NekDouble>> &inarray,
162  Array<OneD, Array<OneD, NekDouble>> &outarray, const NekDouble time,
163  const NekDouble lambda)
164 {
165  boost::ignore_unused(time);
166 
168  factors[StdRegions::eFactorLambda] = 1.0 / lambda / epsilon;
169 
170  for (int i = 0; i < inarray.size(); ++i)
171  {
172  // Multiply RHS by 1.0/timestep/lambda
173  Vmath::Smul(field->GetNpoints(), -factors[StdRegions::eFactorLambda],
174  inarray[i], 1, outarray[i], 1);
175 
176  // Solve a system of equations with Helmholtz solver
177  field->HelmSolve(outarray[i], field->UpdateCoeffs(), factors);
178 
179  // Transform to physical space and store in solution vector
180  field->BwdTrans(field->GetCoeffs(), outarray[i]);
181  }
182 }
183 
185 {
186  // Write solution to file
187  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
188  field->GetFieldDefinitions();
189  std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
190  for (int i = 0; i < FieldDef.size(); ++i)
191  {
192  FieldDef[i]->m_fields.push_back("u");
193  field->AppendFieldData(FieldDef[i], FieldData[i]);
194  }
195  fld->Write(session->GetSessionName() + ".fld", FieldDef, FieldData);
196 }
197 
199 {
200  unsigned int nq = field->GetNpoints();
201  Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
202  field->GetCoords(x0, x1, x2);
203 
205  session->GetFunction("ExactSolution", 0);
206 
207  if (ex_sol)
208  {
209  // evaluate exact solution
210  Array<OneD, NekDouble> exact(nq);
211  ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
212 
213  // Calculate errors
214  cout << "L inf error: " << field->Linf(field->GetPhys(), exact)
215  << endl;
216  cout << "L 2 error: " << field->L2(field->GetPhys(), exact)
217  << endl;
218  cout << "H 1 error: " << field->H1(field->GetPhys(), exact)
219  << endl;
220  }
221 }
222 
223 int main(int argc, char *argv[])
224 {
225  try
226  {
227  Diffusion ops(argc, argv);
228  ops.TimeIntegrate();
229  }
230  catch (const std::runtime_error &e)
231  {
232  exit(-1);
233  }
234  catch (const std::string &eStr)
235  {
236  cout << "Error: " << eStr << endl;
237  exit(-1);
238  }
239 }
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
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
Binds a set of functions for use by time integration schemes.
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
TimeIntegrationSchemeFactory & GetTimeIntegrationSchemeFactory()
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:327
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:129
std::shared_ptr< TimeIntegrationScheme > TimeIntegrationSchemeSharedPtr
std::shared_ptr< ContField > ContFieldSharedPtr
Definition: ContField.h:289
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:172
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:399
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
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.cpp:248
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:492
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255