Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UnsteadyAdvectionDiffusion.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File UnsteadyAdvectionDiffusion.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: Unsteady advection-diffusion solve routines
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <iostream>
37 
39 
40 namespace Nektar
41 {
43  RegisterCreatorFunction("UnsteadyAdvectionDiffusion",
45 
48  : UnsteadySystem(pSession)
49  {
50  m_planeNumber = 0;
51  }
52 
53  /**
54  * @brief Initialisation object for the unsteady linear advection
55  * diffusion equation.
56  */
58  {
60 
61  m_session->LoadParameter("wavefreq", m_waveFreq, 0.0);
62  m_session->LoadParameter("epsilon", m_epsilon, 0.0);
63 
64  // Define Velocity fields
65  m_velocity = Array<OneD, Array<OneD, NekDouble> >(m_spacedim);
66  std::vector<std::string> vel;
67  vel.push_back("Vx");
68  vel.push_back("Vy");
69  vel.push_back("Vz");
70  vel.resize(m_spacedim);
71 
72  EvaluateFunction(vel, m_velocity, "AdvectionVelocity");
73 
74  // Type of advection and diffusion classes to be used
75  switch(m_projectionType)
76  {
77  // Discontinuous field
79  {
80  // Do not forwards transform initial condition
81  m_homoInitialFwd = false;
82 
83  // Advection term
84  string advName;
85  string riemName;
86  m_session->LoadSolverInfo("AdvectionType", advName, "WeakDG");
88  CreateInstance(advName, advName);
89  m_advection->SetFluxVector(&UnsteadyAdvectionDiffusion::
90  GetFluxVectorAdv, this);
91  m_session->LoadSolverInfo("UpwindType", riemName, "Upwind");
93  CreateInstance(riemName);
95  GetNormalVelocity, this);
96  m_advection->SetRiemannSolver(m_riemannSolver);
97  m_advection->InitObject (m_session, m_fields);
98 
99  // Diffusion term
100  std::string diffName;
101  m_session->LoadSolverInfo("DiffusionType", diffName, "LDG");
103  CreateInstance(diffName, diffName);
104  m_diffusion->SetFluxVector(&UnsteadyAdvectionDiffusion::
105  GetFluxVectorDiff, this);
106  m_diffusion->InitObject(m_session, m_fields);
107  break;
108  }
109  // Continuous field
112  {
113  // Advection term
114  std::string advName;
115  m_session->LoadSolverInfo("AdvectionType", advName,
116  "NonConservative");
118  CreateInstance(advName, advName);
119  m_advection->SetFluxVector(&UnsteadyAdvectionDiffusion::
120  GetFluxVectorAdv, this);
121 
122  // In case of Galerkin explicit diffusion gives an error
124  {
125  ASSERTL0(false, "Explicit Galerkin diffusion not set up.");
126  }
127  // In case of Galerkin implicit diffusion: do nothing
128  break;
129  }
130  default:
131  {
132  ASSERTL0(false, "Unsupported projection type.");
133  break;
134  }
135  }
136 
139 
141  m_explicitDiffusion == 1)
142  {
144  }
145  }
146 
147  /**
148  * @brief Unsteady linear advection diffusion equation destructor.
149  */
151  {
152  }
153 
154  /**
155  * @brief Get the normal velocity for the unsteady linear advection
156  * diffusion equation.
157  */
159  {
160  // Number of trace (interface) points
161  int i;
162  int nTracePts = GetTraceNpoints();
163 
164  // Auxiliary variable to compute the normal velocity
165  Array<OneD, NekDouble> tmp(nTracePts);
166  m_traceVn = Array<OneD, NekDouble>(nTracePts, 0.0);
167 
168  // Reset the normal velocity
169  Vmath::Zero(nTracePts, m_traceVn, 1);
170 
171  for (i = 0; i < m_velocity.num_elements(); ++i)
172  {
173  m_fields[0]->ExtractTracePhys(m_velocity[i], tmp);
174 
175  Vmath::Vvtvp(nTracePts,
176  m_traceNormals[i], 1,
177  tmp, 1,
178  m_traceVn, 1,
179  m_traceVn, 1);
180  }
181 
182  return m_traceVn;
183  }
184 
185  /**
186  * @brief Compute the right-hand side for the unsteady linear advection
187  * diffusion problem.
188  *
189  * @param inarray Given fields.
190  * @param outarray Calculated solution.
191  * @param time Time.
192  */
194  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
195  Array<OneD, Array<OneD, NekDouble> >&outarray,
196  const NekDouble time)
197  {
198  // Number of fields (variables of the problem)
199  int nVariables = inarray.num_elements();
200 
201  // Number of solution points
202  int nSolutionPts = GetNpoints();
203 
204  Array<OneD, Array<OneD, NekDouble> > outarrayDiff(nVariables);
205 
206  for (int i = 0; i < nVariables; ++i)
207  {
208  outarrayDiff[i] = Array<OneD, NekDouble>(nSolutionPts, 0.0);
209  }
210 
211  // RHS computation using the new advection base class
212  m_advection->Advect(nVariables, m_fields, m_velocity,
213  inarray, outarray);
214 
215  // Negate the RHS
216  for (int i = 0; i < nVariables; ++i)
217  {
218  Vmath::Neg(nSolutionPts, outarray[i], 1);
219  }
220 
221  // No explicit diffusion for CG
223  {
224  m_diffusion->Diffuse(nVariables, m_fields, inarray, outarrayDiff);
225 
226  for (int i = 0; i < nVariables; ++i)
227  {
228  Vmath::Vadd(nSolutionPts, &outarray[i][0], 1,
229  &outarrayDiff[i][0], 1, &outarray[i][0], 1);
230  }
231  }
232 
233  }
234 
235  /**
236  * @brief Compute the projection for the unsteady advection
237  * diffusion problem.
238  *
239  * @param inarray Given fields.
240  * @param outarray Calculated solution.
241  * @param time Time.
242  */
244  const Array<OneD, const Array<OneD, NekDouble> > &inarray,
245  Array<OneD, Array<OneD, NekDouble> > &outarray,
246  const NekDouble time)
247  {
248  int i;
249  int nvariables = inarray.num_elements();
250  SetBoundaryConditions(time);
251  switch(m_projectionType)
252  {
254  {
255  // Just copy over array
256  int npoints = GetNpoints();
257 
258  for(i = 0; i < nvariables; ++i)
259  {
260  Vmath::Vcopy(npoints, inarray[i], 1, outarray[i], 1);
261  }
262  break;
263  }
266  {
267  Array<OneD, NekDouble> coeffs(m_fields[0]->GetNcoeffs());
268 
269  for(i = 0; i < nvariables; ++i)
270  {
271  m_fields[i]->FwdTrans(inarray[i], coeffs);
272  m_fields[i]->BwdTrans_IterPerExp(coeffs, outarray[i]);
273  }
274  break;
275  }
276  default:
277  {
278  ASSERTL0(false, "Unknown projection scheme");
279  break;
280  }
281  }
282  }
283 
284 
285  /* @brief Compute the diffusion term implicitly.
286  *
287  * @param inarray Given fields.
288  * @param outarray Calculated solution.
289  * @param time Time.
290  * @param lambda Diffusion coefficient.
291  */
293  const Array<OneD, const Array<OneD, NekDouble> >&inarray,
294  Array<OneD, Array<OneD, NekDouble> >&outarray,
295  const NekDouble time,
296  const NekDouble lambda)
297  {
298  int nvariables = inarray.num_elements();
299  int nq = m_fields[0]->GetNpoints();
300 
302  factors[StdRegions::eFactorLambda] = 1.0/lambda/m_epsilon;
303 
304  Array<OneD, Array< OneD, NekDouble> > F(nvariables);
305  F[0] = Array<OneD, NekDouble> (nq*nvariables);
306 
307  for (int n = 1; n < nvariables; ++n)
308  {
309  F[n] = F[n-1] + nq;
310  }
311 
312  // We solve ( \nabla^2 - HHlambda ) Y[i] = rhs [i]
313  // inarray = input: \hat{rhs} -> output: \hat{Y}
314  // outarray = output: nabla^2 \hat{Y}
315  // where \hat = modal coeffs
316  for (int i = 0; i < nvariables; ++i)
317  {
318  // Multiply 1.0/timestep/lambda
319  Vmath::Smul(nq, -factors[StdRegions::eFactorLambda],
320  inarray[i], 1, F[i], 1);
321  }
322 
323  //Setting boundary conditions
324  SetBoundaryConditions(time);
325 
326  for (int i = 0; i < nvariables; ++i)
327  {
328  // Solve a system of equations with Helmholtz solver
329  m_fields[i]->HelmSolve(F[i], m_fields[i]->UpdateCoeffs(),
330  NullFlagList, factors);
331 
332  m_fields[i]->BwdTrans(m_fields[i]->GetCoeffs(), outarray[i]);
333  }
334  }
335 
336  /**
337  * @brief Return the flux vector for the advection part.
338  *
339  * @param physfield Fields.
340  * @param flux Resulting flux.
341  */
343  const Array<OneD, Array<OneD, NekDouble> > &physfield,
344  Array<OneD, Array<OneD, Array<OneD, NekDouble> > > &flux)
345  {
346  ASSERTL1(flux[0].num_elements() == m_velocity.num_elements(),
347  "Dimension of flux array and velocity array do not match");
348 
349  const int nq = m_fields[0]->GetNpoints();
350 
351  for (int i = 0; i < flux.num_elements(); ++i)
352  {
353  for (int j = 0; j < flux[0].num_elements(); ++j)
354  {
355  Vmath::Vmul(nq, physfield[i], 1, m_velocity[j], 1,
356  flux[i][j], 1);
357  }
358  }
359  }
360 
361  /**
362  * @brief Return the flux vector for the diffusion part.
363  *
364  * @param i Equation number.
365  * @param j Spatial direction.
366  * @param physfield Fields.
367  * @param derivatives First order derivatives.
368  * @param flux Resulting flux.
369  */
371  const int i,
372  const int j,
373  const Array<OneD, Array<OneD, NekDouble> > &physfield,
374  Array<OneD, Array<OneD, NekDouble> > &derivatives,
375  Array<OneD, Array<OneD, NekDouble> > &flux)
376  {
377  for (int k = 0; k < flux.num_elements(); ++k)
378  {
379  Vmath::Zero(GetNpoints(), flux[k], 1);
380  }
381  Vmath::Vcopy(GetNpoints(), physfield[i], 1, flux[j], 1);
382  }
383 
385  {
387  }
388 }