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 std;
42 using namespace Nektar;
43 
44 int main(int argc, char *argv[])
45 {
50  LibUtilities::EquationSharedPtr icond, ex_sol;
52 
53  try
54  {
55  // Create session reader.
56  session = LibUtilities::SessionReader::CreateInstance(argc, argv);
57 
58  // Create Field I/O object.
60  AllocateSharedPtr(session->GetComm());
61 
62  // Get some information about the session
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 }
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
STL namespace.
boost::shared_ptr< ContField2D > ContField2DSharedPtr
Definition: ContField2D.h:293
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:251
boost::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition: MeshPartition.h:51
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:199
boost::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:225
double NekDouble
int main(int argc, char *argv[])
boost::shared_ptr< Equation > EquationSharedPtr
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:359
boost::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:442
static FlagList NullFlagList
An empty flag list.