Nektar++
DiffusionSolverTimeInt.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File DiffusionTestTI.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 
45 #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,
61  const NekDouble time,
62  const NekDouble lambda);
63 
64  private:
67  string sessionName;
70 
74 
76  unsigned int nSteps;
80 
81  void WriteSolution();
82  void ExactSolution();
83 };
84 
85 
86 Diffusion::Diffusion( int argc, char* argv[] )
87 {
88  // Create session reader.
89  session = LibUtilities::SessionReader::CreateInstance(argc, argv);
90 
91  // Read the geometry and the expansion information
92  graph = SpatialDomains::MeshGraph::Read(session);
93 
94  // Create Field I/O object.
95  fld = LibUtilities::FieldIO::CreateDefault(session);
96 
97  // Get some information from the session
98  sessionName = session->GetSessionName();
99 
100  // Create time integration scheme.
101  if (session->DefinesTimeIntScheme())
102  {
103  timeInt = session->GetTimeIntScheme();
104  }
105  else
106  {
107  timeInt.method = session->GetSolverInfo("TimeIntegrationMethod");
108  }
109 
110  nSteps = session->GetParameter("NumSteps");
111  delta_t = session->GetParameter("TimeStep");
112  epsilon = session->GetParameter("epsilon");
113  lambda = 1.0 / delta_t / epsilon;
114 
115  // Set up the field
117  AllocateSharedPtr(session, graph, session->GetVariable(0));
118 
120  fields[0] = field->UpdatePhys();
121 
122  // Get coordinates of physical points
123  unsigned int nq = field->GetNpoints();
124  Array<OneD,NekDouble> x0(nq), x1(nq), x2(nq);
125  field->GetCoords(x0,x1,x2);
126 
127  // Evaluate initial condition
129  = session->GetFunction("InitialConditions", "u");
130  icond->Evaluate(x0,x1,x2,0.0,field->UpdatePhys());
131 }
132 
134 {
135  session->Finalise();
136 }
137 
139 {
141  CreateInstance(timeInt.method, timeInt.variant, timeInt.order,
142  timeInt.freeParams);
143 
144  ode.DefineImplicitSolve(&Diffusion::DoImplicitSolve, this);
145 
146  // Initialise the scheme for actual time integration scheme
147  intScheme->InitializeScheme(delta_t, fields, 0.0, ode);
148 
149  // Zero field coefficients for initial guess for linear solver.
150  Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
151 
152  for (int n = 0; n < nSteps; ++n)
153  {
154  fields = intScheme->TimeIntegrate( n, delta_t, ode );
155  }
156 
157  Vmath::Vcopy(field->GetNpoints(), fields[0], 1, field->UpdatePhys(), 1);
158 
159  WriteSolution();
160  ExactSolution();
161 }
162 
163 
165  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
166  Array<OneD, Array<OneD, NekDouble> >&outarray,
167  const NekDouble time,
168  const NekDouble lambda)
169 {
170  boost::ignore_unused(time);
171 
173  factors[StdRegions::eFactorLambda] = 1.0/lambda/epsilon;
174 
175  for (int i = 0; i < inarray.size(); ++i)
176  {
177  // Multiply RHS by 1.0/timestep/lambda
178  Vmath::Smul(field->GetNpoints(), -factors[StdRegions::eFactorLambda],
179  inarray [i], 1,
180  outarray[i], 1);
181 
182  // Solve a system of equations with Helmholtz solver
183  field->HelmSolve(outarray[i],
184  field->UpdateCoeffs(), factors);
185 
186  // Transform to physical space and store in solution vector
187  field->BwdTrans (field->GetCoeffs(), outarray[i]);
188  }
189 }
190 
192 {
193  // Write solution to file
194  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
195  = field->GetFieldDefinitions();
196  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
197  for(int i = 0; i < FieldDef.size(); ++i)
198  {
199  FieldDef[i]->m_fields.push_back("u");
200  field->AppendFieldData(FieldDef[i], FieldData[i]);
201  }
202  fld->Write(session->GetSessionName() + ".fld", FieldDef, FieldData);
203 
204 }
205 
206 
208 {
209  unsigned int nq = field->GetNpoints();
210  Array<OneD,NekDouble> x0(nq), x1(nq), x2(nq);
211  field->GetCoords(x0,x1,x2);
212 
214  session->GetFunction("ExactSolution",0);
215 
216  if(ex_sol)
217  {
218  // evaluate exact solution
219  Array<OneD, NekDouble> exact(nq);
220  ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
221 
222  // Calculate errors
223  cout << "L inf error: "
224  << field->Linf(field->GetPhys(), exact) << endl;
225  cout << "L 2 error: "
226  << field->L2(field->GetPhys(), exact) << endl;
227  cout << "H 1 error: "
228  << field->H1(field->GetPhys(), exact) << endl;
229  }
230 
231 }
232 
233 int main(int argc, char *argv[])
234 {
235  try
236  {
237  Diffusion ops(argc, argv);
238  ops.TimeIntegrate();
239  }
240  catch (const std::runtime_error& e)
241  {
242  exit(-1);
243  }
244  catch (const std::string& eStr)
245  {
246  cout << "Error: " << eStr << endl;
247  exit(-1);
248  }
249 }
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
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:306
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:292
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:314
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
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:225
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:436
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199