Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DiffusionSolver.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File ADRSolver.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: Advection Diffusion Reaction framework solver
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
40 
41 using namespace Nektar;
42 
43 int main(int argc, char *argv[])
44 {
49  LibUtilities::EquationSharedPtr icond, ex_sol;
51 
52  try
53  {
54  // Create session reader.
55  session = LibUtilities::SessionReader::CreateInstance(argc, argv);
56 
57  // Create Field I/O object.
59  AllocateSharedPtr(session->GetComm());
60 
61  // Get some information about the session
62  string fileName = session->GetFilename();
63  string sessionName = session->GetSessionName();
64  string outFile = sessionName + ".fld";
65  unsigned int nSteps = session->GetParameter("NumSteps");
66  NekDouble delta_t = session->GetParameter("TimeStep");
67  NekDouble epsilon = session->GetParameter("epsilon" );
68 
69  // Read the geometry and the expansion information
70  graph = SpatialDomains::MeshGraph::Read(session);
71 
72  // Create field
74  ::AllocateSharedPtr(session, graph, session->GetVariable(0));
75 
76  // Get coordinates of physical points
77  unsigned int nq = field->GetNpoints();
78  Array<OneD,NekDouble> x0(nq), x1(nq), x2(nq);
79  field->GetCoords(x0,x1,x2);
80 
81  // Evaluate initial condition at these points
82  icond = session->GetFunction("InitialConditions", "u");
83  icond->Evaluate(x0, x1, x2, 0.0, field->UpdatePhys());
84 
85  // Compute lambda in the Helmholtz problem
86  factors[StdRegions::eFactorLambda] = 1.0/delta_t/epsilon;
87 
88  // Zero field coefficients for initial guess for linear solver.
89  Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
90 
91  // Time integrate using backward Euler
92  for (unsigned int n = 0; n < nSteps; ++n)
93  {
94  Vmath::Smul(nq, -1.0/delta_t/epsilon, field->GetPhys(), 1,
95  field->UpdatePhys(), 1);
96 
97  field->HelmSolve(field->GetPhys(), field->UpdateCoeffs(),
98  NullFlagList, factors);
99 
100  field->BwdTrans(field->GetCoeffs(), field->UpdatePhys());
101  }
102 
103  // Write solution to file
104  std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef
105  = field->GetFieldDefinitions();
106  std::vector<std::vector<NekDouble> > FieldData(FieldDef.size());
107  for(int i = 0; i < FieldDef.size(); ++i)
108  {
109  FieldDef[i]->m_fields.push_back("u");
110  field->AppendFieldData(FieldDef[i], FieldData[i]);
111  }
112  fld->Write(outFile, FieldDef, FieldData);
113 
114  // Check for exact solution
115  ex_sol = session->GetFunction("ExactSolution",0);
116  if(ex_sol)
117  {
118  // Allocate storage
119  Array<OneD, NekDouble> exact(nq);
120 
121  //----------------------------------------------
122  // evaluate exact solution
123  ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
124 
125  //--------------------------------------------
126  // Calculate errors
127  cout << "L inf error: "
128  << field->Linf(field->GetPhys(), exact) << endl;
129  cout << "L 2 error: "
130  << field->L2(field->GetPhys(), exact) << endl;
131  cout << "H 1 error: "
132  << field->H1(field->GetPhys(), exact) << endl;
133  //--------------------------------------------
134  }
135 
136  // Finalise session
137  session->Finalise();
138  }
139  catch (const std::runtime_error& e)
140  {
141  return 1;
142  }
143  catch (const std::string& eStr)
144  {
145  cout << "Error: " << eStr << endl;
146  }
147 
148  return 0;
149 }