Nektar++
DiffusionSolver.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: DiffusionSolver.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 framework solver
32//
33///////////////////////////////////////////////////////////////////////////////
34
39
40using namespace std;
41using namespace Nektar;
42
43int main(int argc, char *argv[])
44{
51
52 try
53 {
54 // Create session reader.
55 session = LibUtilities::SessionReader::CreateInstance(argc, argv);
56
57 // Read the geometry and the expansion information
58 graph = SpatialDomains::MeshGraphIO::Read(session);
59
60 // Create Field I/O object.
61 fld = LibUtilities::FieldIO::CreateDefault(session);
62
63 // Get some information about the session
64 string sessionName = session->GetSessionName();
65 string outFile = sessionName + ".fld";
66 unsigned int nSteps = session->GetParameter("NumSteps");
67 NekDouble delta_t = session->GetParameter("TimeStep");
68 NekDouble epsilon = session->GetParameter("epsilon");
69
70 // Create field
72 session, graph, session->GetVariable(0));
73
74 // Get coordinates of physical points
75 unsigned int nq = field->GetNpoints();
76 Array<OneD, NekDouble> x0(nq), x1(nq), x2(nq);
77 field->GetCoords(x0, x1, x2);
78
79 // Evaluate initial condition at these points
80 icond = session->GetFunction("InitialConditions", "u");
81 icond->Evaluate(x0, x1, x2, 0.0, field->UpdatePhys());
82
83 // Compute lambda in the Helmholtz problem
84 factors[StdRegions::eFactorLambda] = 1.0 / delta_t / epsilon;
85
86 // Zero field coefficients for initial guess for linear solver.
87 Vmath::Zero(field->GetNcoeffs(), field->UpdateCoeffs(), 1);
88
89 // Time integrate using backward Euler
90 for (unsigned int n = 0; n < nSteps; ++n)
91 {
92 Vmath::Smul(nq, -1.0 / delta_t / epsilon, field->GetPhys(), 1,
93 field->UpdatePhys(), 1);
94
95 field->HelmSolve(field->GetPhys(), field->UpdateCoeffs(), factors);
96
97 field->BwdTrans(field->GetCoeffs(), field->UpdatePhys());
98 }
99
100 // Write solution to file
101 std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
102 field->GetFieldDefinitions();
103 std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
104 for (int i = 0; i < FieldDef.size(); ++i)
105 {
106 FieldDef[i]->m_fields.push_back("u");
107 field->AppendFieldData(FieldDef[i], FieldData[i]);
108 }
109 fld->Write(outFile, FieldDef, FieldData);
110
111 // Check for exact solution
112 ex_sol = session->GetFunction("ExactSolution", 0);
113 if (ex_sol)
114 {
115 // Allocate storage
116 Array<OneD, NekDouble> exact(nq);
117
118 //----------------------------------------------
119 // Evaluate exact solution
120 ex_sol->Evaluate(x0, x1, x2, (nSteps)*delta_t, exact);
121
122 //--------------------------------------------
123 // Calculate errors
124 cout << "L inf error: " << field->Linf(field->GetPhys(), exact)
125 << endl;
126 cout << "L 2 error: " << field->L2(field->GetPhys(), exact)
127 << endl;
128 cout << "H 1 error: " << field->H1(field->GetPhys(), exact)
129 << endl;
130 //--------------------------------------------
131 }
132
133 // Finalise session
134 session->Finalise();
135 }
136 catch (const std::runtime_error &e)
137 {
138 return 1;
139 }
140 catch (const std::string &eStr)
141 {
142 cout << "Error: " << eStr << endl;
143 }
144
145 return 0;
146}
int main(int argc, char *argv[])
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
std::shared_ptr< FieldIO > FieldIOSharedPtr
Definition: FieldIO.h:322
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< Equation > EquationSharedPtr
Definition: Equation.h:125
std::shared_ptr< ContField > ContFieldSharedPtr
Definition: ContField.h:268
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
Definition: MeshGraph.h:174
std::map< ConstFactorType, NekDouble > ConstFactorMap
Definition: StdRegions.hpp:430
StdRegions::ConstFactorMap factors
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.hpp:100
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.hpp:273
STL namespace.