Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Diffusion solver
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <cstdlib>
37 
43 
44 using namespace Nektar;
45 
46 class Diffusion
47 {
48  public:
49  Diffusion(int argc, char* argv[]);
50  ~Diffusion();
51 
52  void TimeIntegrate();
53 
54  void DoImplicitSolve(
55  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
56  Array<OneD, Array<OneD, NekDouble> >&outarray,
57  const NekDouble time,
58  const NekDouble lambda);
59 
60  private:
63  string sessionName;
64  string fileName;
67 
71  Array<OneD, Array<OneD, NekDouble> > fields;
72 
73  string scheme;
74  unsigned int nSteps;
78 
79  void WriteSolution();
80  void ExactSolution();
81 };
82 
83 
84 Diffusion::Diffusion(int argc, char* argv[])
85 {
86  // Create session reader.
87  session = LibUtilities::SessionReader::CreateInstance(argc, argv);
88 
89  // Create Field I/O object.
91  AllocateSharedPtr(session->GetComm());
92 
93  // Get some information from the session
94  fileName = session->GetFilename();
95  sessionName = session->GetSessionName();
96  scheme = session->GetSolverInfo("TimeIntegrationMethod");
97  nSteps = session->GetParameter("NumSteps");
98  delta_t = session->GetParameter("TimeStep");
99  epsilon = session->GetParameter("epsilon");
100  lambda = 1.0/delta_t/epsilon;
101 
102  // Read the geometry and the expansion information
103  graph = SpatialDomains::MeshGraph::Read(session);
104 
105  // Set up the field
107  AllocateSharedPtr(session, graph, session->GetVariable(0));
108 
109  fields = Array<OneD, Array<OneD, NekDouble> >(1);
110  fields[0] = field->UpdatePhys();
111 
112  // Get coordinates of physical points
113  unsigned int nq = field->GetNpoints();
114  Array<OneD,NekDouble> x0(nq), x1(nq), x2(nq);
115  field->GetCoords(x0,x1,x2);
116 
117  // Evaluate initial condition
119  = session->GetFunction("InitialConditions", "u");
120  icond->Evaluate(x0,x1,x2,0.0,field->UpdatePhys());
121 }
122 
124 {
125  session->Finalise();
126 }
127 
129 {
131  CreateInstance(scheme);
132 
133  ode.DefineImplicitSolve(&Diffusion::DoImplicitSolve, this);
134 
135  // Initialise the scheme for actual time integration scheme
136  u = IntScheme->InitializeScheme(delta_t, fields, 0.0, ode);
137 
138  // Zero field coefficients for initial guess for linear solver.
139  Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
140 
141  for (int n = 0; n < nSteps; ++n)
142  {
143  fields = IntScheme->TimeIntegrate(n, delta_t, u, ode);
144  }
145  Vmath::Vcopy(field->GetNpoints(), fields[0], 1, field->UpdatePhys(), 1);
146 
147  WriteSolution();
148  ExactSolution();
149 
150 }
151 
152 
154  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
155  Array<OneD, Array<OneD, NekDouble> >&outarray,
156  const NekDouble time,
157  const NekDouble lambda)
158 {
160  factors[StdRegions::eFactorLambda] = 1.0/lambda/epsilon;
161 
162  for (int i = 0; i < inarray.num_elements(); ++i)
163  {
164  // Multiply RHS by 1.0/timestep/lambda
165  Vmath::Smul(field->GetNpoints(), -factors[StdRegions::eFactorLambda],
166  inarray [i], 1,
167  outarray[i], 1);
168 
169  // Solve a system of equations with Helmholtz solver
170  field->HelmSolve(outarray[i],
171  field->UpdateCoeffs(),
172  NullFlagList, factors);
173 
174  // Transform to physical space and store in solution vector
175  field->BwdTrans (field->GetCoeffs(), outarray[i]);
176  }
177 }
178 
180 {
181  // Write solution to file
182  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
183  = field->GetFieldDefinitions();
184  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
185  for(int i = 0; i < FieldDef.size(); ++i)
186  {
187  FieldDef[i]->m_fields.push_back("u");
188  field->AppendFieldData(FieldDef[i], FieldData[i]);
189  }
190  fld->Write(session->GetSessionName() + ".fld", FieldDef, FieldData);
191 
192 }
193 
194 
196 {
197  unsigned int nq = field->GetNpoints();
198  Array<OneD,NekDouble> x0(nq), x1(nq), x2(nq);
199  field->GetCoords(x0,x1,x2);
200 
202  session->GetFunction("ExactSolution",0);
203 
204  if(ex_sol)
205  {
206  // evaluate exact solution
207  Array<OneD, NekDouble> exact(nq);
208  ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
209 
210  // Calculate errors
211  cout << "L inf error: "
212  << field->Linf(field->GetPhys(), exact) << endl;
213  cout << "L 2 error: "
214  << field->L2(field->GetPhys(), exact) << endl;
215  cout << "H 1 error: "
216  << field->H1(field->GetPhys(), exact) << endl;
217  }
218 
219 }
220 
221 int main(int argc, char *argv[])
222 {
223  try
224  {
225  Diffusion ops(argc, argv);
226  ops.TimeIntegrate();
227  }
228  catch (const std::runtime_error& e)
229  {
230  exit(-1);
231  }
232  catch (const std::string& eStr)
233  {
234  cout << "Error: " << eStr << endl;
235  exit(-1);
236  }
237 }
238