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 
44 
45 using namespace std;
46 using namespace Nektar;
47 
48 class Diffusion
49 {
50  public:
51  Diffusion(int argc, char* argv[]);
52  ~Diffusion();
53 
54  void TimeIntegrate();
55 
56  void DoImplicitSolve(
57  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
58  Array<OneD, Array<OneD, NekDouble> >&outarray,
59  const NekDouble time,
60  const NekDouble lambda);
61 
62  private:
65  string sessionName;
68 
73 
74  string scheme;
75  unsigned int nSteps;
79 
80  void WriteSolution();
81  void ExactSolution();
82 };
83 
84 
85 Diffusion::Diffusion(int argc, char* argv[])
86 {
87  // Create session reader.
88  session = LibUtilities::SessionReader::CreateInstance(argc, argv);
89 
90  // Read the geometry and the expansion information
91  graph = SpatialDomains::MeshGraph::Read(session);
92 
93  // Create Field I/O object.
94  fld = LibUtilities::FieldIO::CreateDefault(session);
95 
96  // Get some information from the session
97  sessionName = session->GetSessionName();
98  scheme = session->GetSolverInfo("TimeIntegrationMethod");
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  AllocateSharedPtr(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  CreateInstance(scheme);
131 
132  ode.DefineImplicitSolve(&Diffusion::DoImplicitSolve, this);
133 
134  // Initialise the scheme for actual time integration scheme
135  u = 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, u, ode);
143  }
144  Vmath::Vcopy(field->GetNpoints(), fields[0], 1, field->UpdatePhys(), 1);
145 
146  WriteSolution();
147  ExactSolution();
148 
149 }
150 
151 
153  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
154  Array<OneD, Array<OneD, NekDouble> >&outarray,
155  const NekDouble time,
156  const NekDouble lambda)
157 {
158  boost::ignore_unused(time);
159 
161  factors[StdRegions::eFactorLambda] = 1.0/lambda/epsilon;
162 
163  for (int i = 0; i < inarray.num_elements(); ++i)
164  {
165  // Multiply RHS by 1.0/timestep/lambda
166  Vmath::Smul(field->GetNpoints(), -factors[StdRegions::eFactorLambda],
167  inarray [i], 1,
168  outarray[i], 1);
169 
170  // Solve a system of equations with Helmholtz solver
171  field->HelmSolve(outarray[i],
172  field->UpdateCoeffs(),
173  NullFlagList, 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 }
194 
195 
197 {
198  unsigned int nq = field->GetNpoints();
199  Array<OneD,NekDouble> x0(nq), x1(nq), x2(nq);
200  field->GetCoords(x0,x1,x2);
201 
203  session->GetFunction("ExactSolution",0);
204 
205  if(ex_sol)
206  {
207  // evaluate exact solution
208  Array<OneD, NekDouble> exact(nq);
209  ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
210 
211  // Calculate errors
212  cout << "L inf error: "
213  << field->Linf(field->GetPhys(), exact) << endl;
214  cout << "L 2 error: "
215  << field->L2(field->GetPhys(), exact) << endl;
216  cout << "H 1 error: "
217  << field->H1(field->GetPhys(), exact) << endl;
218  }
219 
220 }
221 
222 int main(int argc, char *argv[])
223 {
224  try
225  {
226  Diffusion ops(argc, argv);
227  ops.TimeIntegrate();
228  }
229  catch (const std::runtime_error& e)
230  {
231  exit(-1);
232  }
233  catch (const std::string& eStr)
234  {
235  cout << "Error: " << eStr << endl;
236  exit(-1);
237  }
238 }
239 
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:163
LibUtilities::TimeIntegrationWrapperSharedPtr IntScheme
MultiRegions::ContField2DSharedPtr field
Diffusion(int argc, char *argv[])
int main(int argc, char *argv[])
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
std::shared_ptr< ContField2D > ContField2DSharedPtr
Definition: ContField2D.h:289
LibUtilities::TimeIntegrationSolutionSharedPtr u
STL namespace.
LibUtilities::FieldIOSharedPtr fld
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:294
std::shared_ptr< TimeIntegrationWrapper > TimeIntegrationWrapperSharedPtr
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*y.
Definition: Vmath.cpp:216
Array< OneD, Array< OneD, NekDouble > > fields
double NekDouble
TimeIntegrationWrapperFactory & GetTimeIntegrationWrapperFactory()
LibUtilities::SessionReaderSharedPtr session
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:131
void DoImplicitSolve(const Array< OneD, const Array< OneD, NekDouble > > &inarray, Array< OneD, Array< OneD, NekDouble > > &outarray, const NekDouble time, const NekDouble lambda)
SpatialDomains::MeshGraphSharedPtr graph
LibUtilities::TimeIntegrationSchemeOperators ode
unsigned int nSteps
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:376
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:306
std::shared_ptr< TimeIntegrationSolution > TimeIntegrationSolutionSharedPtr
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1064
std::shared_ptr< SessionReader > SessionReaderSharedPtr
static FlagList NullFlagList
An empty flag list.