CompressibleFlowSystem.cpp 41.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
///////////////////////////////////////////////////////////////////////////////
//
// File CompressibleFlowSystem.cpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
32
// Description: Auxiliary functions for the compressible flow system
33
34
35
//
///////////////////////////////////////////////////////////////////////////////

36
#include <CompressibleFlowSolver/EquationSystems/CompressibleFlowSystem.h>
37
38
#include <LocalRegions/TriExp.h>
#include <LocalRegions/QuadExp.h>
Dirk Ekelschot's avatar
Dirk Ekelschot committed
39
#include <LocalRegions/HexExp.h>
40
#include <MultiRegions/ExpList.h>
41
#include <MultiRegions/AssemblyMap/AssemblyMapDG.h>
42
43
#include <solvers/ADRSolver/EquationSystems/UnsteadyDiffusion.cpp>
#include <solvers/ADRSolver/EquationSystems/UnsteadyDiffusion.h>
44
45
46

namespace Nektar
{
47
48
49
50
51
    string CompressibleFlowSystem::className = 
        SolverUtils::GetEquationSystemFactory().RegisterCreatorFunction(
            "CompressibleFlowSystem", 
            CompressibleFlowSystem::create, 
            "Auxiliary functions for the compressible flow system.");
52
    
53
54
55
56
57
    CompressibleFlowSystem::CompressibleFlowSystem(
        const LibUtilities::SessionReaderSharedPtr& pSession)
        : UnsteadySystem(pSession)
    {
    }
58
    
59
    /**
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
60
     * @brief Initialization object for CompressibleFlowSystem class.
61
     */
62
63
64
65
    void CompressibleFlowSystem::v_InitObject()
    {
        UnsteadySystem::v_InitObject();
        
66
67
68
        ASSERTL0(m_session->DefinesSolverInfo("UPWINDTYPE"),
                 "No UPWINDTYPE defined in session.");
        
69
70
71
72
73
74
        // Set up locations of velocity vector.
        m_velLoc = Array<OneD, NekDouble>(m_expdim);
        for (int i = 0; i < m_expdim; ++i)
        {
            m_velLoc[i] = i+1;
        }
75
76
77
78
79
        
        // Get gamma parameter from session file.
        ASSERTL0(m_session->DefinesParameter("Gamma"),
                 "Compressible flow sessions must define a Gamma parameter.");
        m_session->LoadParameter("Gamma", m_gamma, 1.4);
80
        
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
81
82
83
84
85
86
87
88
89
        // Get E0 parameter from session file.
        ASSERTL0(m_session->DefinesParameter("pInf"),
                 "Compressible flow sessions must define a pInf parameter.");
        m_session->LoadParameter("pInf", m_pInf, 101325);
        
        // Get rhoInf parameter from session file.
        ASSERTL0(m_session->DefinesParameter("rhoInf"),
                 "Compressible flow sessions must define a rhoInf parameter.");
        m_session->LoadParameter("rhoInf", m_rhoInf, 1.225);
90
        
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
91
92
93
94
        // Get uInf parameter from session file.
        ASSERTL0(m_session->DefinesParameter("uInf"),
                 "Compressible flow sessions must define a uInf parameter.");
        m_session->LoadParameter("uInf", m_uInf, 0.1);
95
        
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
96
        // Get vInf parameter from session file.
97
98
        if (m_expdim == 2 || m_expdim == 3)
        {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
99
100
101
102
            ASSERTL0(m_session->DefinesParameter("vInf"),
                     "Compressible flow sessions must define a vInf parameter"
                     "for 2D/3D problems.");
            m_session->LoadParameter("vInf", m_vInf, 0.0);
103
104
        }
        
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
105
        // Get wInf parameter from session file.
106
107
        if (m_expdim == 3)
        {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
108
109
110
111
            ASSERTL0(m_session->DefinesParameter("wInf"),
                     "Compressible flow sessions must define a wInf parameter"
                     "for 3D problems.");
            m_session->LoadParameter("wInf", m_wInf, 0.0);
112
        }
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
113
                
114
        m_session->LoadParameter("GasConstant", m_gasConstant, 287.058);
115
        
116
117
118
119
120
121
122
123
124
125
126
127
128
        // Type of advection class to be used
        switch(m_projectionType)
        {
            // Continuous field 
            case MultiRegions::eGalerkin:
            {
                ASSERTL0(false, "Continuous field not supported.");
                break;
            }
            // Discontinuous field 
            case MultiRegions::eDiscontinuous:
            {
                string advName;
129
                string diffName;
130
131
132
                string riemName;
                
                m_session->LoadSolverInfo("AdvectionType", advName, "WeakDG");
133
                m_session->LoadSolverInfo("DiffusionType", diffName, "LDGNS");
134

135
136
137
                m_advection = SolverUtils::GetAdvectionFactory()
                                            .CreateInstance(advName, advName);
                
138
139
140
                m_diffusion = SolverUtils::GetDiffusionFactory()
                                            .CreateInstance(diffName, diffName);

141
                m_advection->SetFluxVector(&CompressibleFlowSystem::
142
                                            GetFluxVector, this);
143
                
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
144
145
146
                m_diffusion->SetFluxVectorNS(&CompressibleFlowSystem::
                                             GetViscousFluxVector, this);

147
148
149
                m_session->LoadSolverInfo("UpwindType", riemName, "Exact");
                
                m_riemannSolver = SolverUtils::GetRiemannSolverFactory()
150
                                            .CreateInstance(riemName);
151
152
153
154
155
156
157
158
159
160
161
162
163

                m_riemannSolver->AddParam (
                                    "gamma",  
                                    &CompressibleFlowSystem::GetGamma,   this);
                m_riemannSolver->AddScalar(
                                    "velLoc", 
                                    &CompressibleFlowSystem::GetVelLoc,  this);
                m_riemannSolver->AddVector(
                                    "N",
                                    &CompressibleFlowSystem::GetNormals, this);
                
                m_advection->SetRiemannSolver(m_riemannSolver);
                m_advection->InitObject      (m_session, m_fields);
164
                m_diffusion->InitObject      (m_session);
165
166
167
168
169
170
171
172
                break;
            }
            default:
            {
                ASSERTL0(false, "Unsupported projection type.");
                break;
            }
        }
173
    }
174
    
175
    /**
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
176
     * @brief Destructor for CompressibleFlowSystem class.
177
     */
178
179
180
181
    CompressibleFlowSystem::~CompressibleFlowSystem()
    {
        
    }
182
    
183
    /**
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
184
     * @brief Print out a summary with some relevant information.
185
     */
186
187
188
189
    void CompressibleFlowSystem::v_PrintSummary(std::ostream &out)
    {
        UnsteadySystem::v_PrintSummary(out);
    }
190
    
191
    /**
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
192
     * @brief Wall boundary conditions for compressible flow problems.
193
     */
194
    void CompressibleFlowSystem::WallBoundary(
195
        int                                   bcRegion,
196
197
198
199
200
201
202
203
204
205
206
207
        int                                   cnt, 
        Array<OneD, Array<OneD, NekDouble> > &physarray)
    { 
        int i;
        int nTraceNumPoints = GetTraceTotPoints();
        int nvariables      = physarray.num_elements();
        
        // get physical values of the forward trace
        Array<OneD, Array<OneD, NekDouble> > Fwd(nvariables);
        for (i = 0; i < nvariables; ++i)
        {
            Fwd[i] = Array<OneD, NekDouble>(nTraceNumPoints);
208
            m_fields[i]->ExtractTracePhys(physarray[i], Fwd[i]);
209
210
211
212
213
214
        }
        
        // Adjust the physical values of the trace to take 
        // user defined boundaries into account
        int e, id1, id2, npts;
        
215
        for(e = 0; e < m_fields[0]->
216
            GetBndCondExpansions()[bcRegion]->GetExpSize(); ++e)
217
        {
218
219
220
221
            npts = m_fields[0]->GetBndCondExpansions()[bcRegion]->
                GetExp(e)->GetNumPoints(0);
            id1  = m_fields[0]->GetBndCondExpansions()[bcRegion]->
                GetPhys_Offset(e);
222
            id2  = m_fields[0]->GetTrace()->GetPhys_Offset(
223
224
                        m_fields[0]->GetTraceMap()->
                                    GetBndCondCoeffsToGlobalCoeffsMap(cnt+e));
225
            
Dirk Ekelschot's avatar
Dirk Ekelschot committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
            // For 2D/3D, define: v* = v - 2(v.n)n
            
            Array<OneD,NekDouble> tmp(npts,0.0);
            
            // Calculate (v.n)
            for (i = 0; i < m_expdim; ++i)
            {
                Vmath::Vvtvp(npts,
                             &Fwd[1+i][id2], 1,
                             &m_traceNormals[i][id2], 1,
                             &tmp[0], 1,
                             &tmp[0], 1);
                
            }
            // Calculate 2.0(v.n)
            Vmath::Smul(npts,
                        -2.0,
                        &tmp[0],1,
                        &tmp[0],1);
            
            // Calculate v* = v - 2.0(v.n)n
            
            for (i = 0; i < m_expdim; ++i)
249
            {
Dirk Ekelschot's avatar
Dirk Ekelschot committed
250
251
252
253
254
255
                Vmath::Vvtvp(npts,
                             &tmp[0], 1,
                             &m_traceNormals[i][id2], 1,
                             &Fwd[1+i][id2], 1,
                             &Fwd[1+i][id2], 1);
                
256
257
258
259
260
            }
            
            // copy boundary adjusted values into the boundary expansion
            for (i = 0; i < nvariables; ++i)
            {
Dirk Ekelschot's avatar
Dirk Ekelschot committed
261
                Vmath::Vcopy(npts, &Fwd[i][id2], 1,
262
263
                             &(m_fields[i]->GetBndCondExpansions()[bcRegion]->
                             UpdatePhys())[id1], 1);
264
265
            }
        }
266
    }
267
    
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    /**
     * @brief Wall boundary conditions for viscous compressible flow problems.
     */
    void CompressibleFlowSystem::WallBoundaryViscous(
        int                                   bcRegion,
        int                                   cnt, 
        Array<OneD, Array<OneD, NekDouble> > &physarray)
    { 
        int i;
        int nTraceNumPoints = GetTraceTotPoints();
        int nvariables      = physarray.num_elements();
        
        // get physical values of the forward trace
        Array<OneD, Array<OneD, NekDouble> > Fwd(nvariables);
        for (i = 0; i < nvariables; ++i)
        {
            Fwd[i] = Array<OneD, NekDouble>(nTraceNumPoints);
            m_fields[i]->ExtractTracePhys(physarray[i], Fwd[i]);
        }
        
        // Adjust the physical values of the trace to take 
        // user defined boundaries into account
        int e, id1, id2, npts;
        
        for(e = 0; e < m_fields[0]->
            GetBndCondExpansions()[bcRegion]->GetExpSize(); ++e)
        {
            npts = m_fields[0]->GetBndCondExpansions()[bcRegion]->
                GetExp(e)->GetNumPoints(0);
            id1  = m_fields[0]->GetBndCondExpansions()[bcRegion]->
                GetPhys_Offset(e);
            id2  = m_fields[0]->GetTrace()->GetPhys_Offset(
                m_fields[0]->GetTraceMap()->
                    GetBndCondCoeffsToGlobalCoeffsMap(cnt+e));
302
            /*
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
            switch(m_expdim)
            {
                // Special case for 2D
                case 2:
                {
                    Array<OneD, NekDouble> tmp_n(npts);
                    Array<OneD, NekDouble> tmp_t(npts);
                    
                    Vmath::Vmul(npts, 
                                &Fwd[1][id2], 1, 
                                &m_traceNormals[0][id2], 1, 
                                &tmp_n[0], 1);
                    
                    Vmath::Vvtvp(npts, 
                                 &Fwd[2][id2], 1, 
                                 &m_traceNormals[1][id2], 1, 
                                 &tmp_n[0], 1, 
                                 &tmp_n[0], 1);
                    
                    Vmath::Vmul(npts, 
                                &Fwd[1][id2], 1, 
                                &m_traceNormals[1][id2], 1, 
                                &tmp_t[0], 1);
                    
                    Vmath::Vvtvm(npts, 
                                 &Fwd[2][id2], 1, 
                                 &m_traceNormals[0][id2], 1, 
                                 &tmp_t[0], 1, 
                                 &tmp_t[0], 1);
                    
                    // negate fluxes
                    Vmath::Neg(npts, tmp_n, 1);
                    Vmath::Neg(npts, tmp_t, 1);
                    
                    // rotate back to Cartesian
                    Vmath::Vmul(npts, 
                                &tmp_t[0], 1, 
                                &m_traceNormals[1][id2], 1, 
                                &Fwd[1][id2], 1);
                    
                    Vmath::Vvtvm(npts,
                                 &tmp_n[0], 1, 
                                 &m_traceNormals[0][id2], 1, 
                                 &Fwd[1][id2], 1, 
                                 &Fwd[1][id2], 1);
                    
                    Vmath::Vmul(npts, 
                                &tmp_t[0], 1, 
                                &m_traceNormals[0][id2], 1, 
                                &Fwd[2][id2], 1);
                    
                    Vmath::Vvtvp(npts, 
                                 &tmp_n[0], 1, 
                                 &m_traceNormals[1][id2], 1, 
                                 &Fwd[2][id2], 1, 
                                 &Fwd[2][id2], 1);
                    break;
                }
                    
                // For 1D/3D, define: v* = v - (v.n)n so that v*.n = 0
                case 1:
                case 3:
                {
                    ASSERTL0(false, "1D/3D WallViscous boundary conditions "
                                    "not implemented yet")
                    break;
                }
                default:
                    ASSERTL0(false, "Illegal expansion dimension");
                    break;
            }
374
375
376
377
378
379
            */
            
            for (i = 0; i < m_expdim ; i++)
            {
                Vmath::Neg(npts,&Fwd[i+1][id2],1);
            }
380
381
382
383
384
385
386
387
388
389
390
            
            // copy boundary adjusted values into the boundary expansion
            for (i = 0; i < nvariables; ++i)
            {
                Vmath::Vcopy(npts, &Fwd[i][id2], 1, 
                             &(m_fields[i]->GetBndCondExpansions()[bcRegion]->
                               UpdatePhys())[id1], 1);
            }
        }
    }
    
391
    /**
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
392
     * @brief Simmetry boundary conditions for compressible flow problems.
393
     */
394
395
396
397
    void CompressibleFlowSystem::SymmetryBoundary(
        int                                      bcRegion, 
        int                                      cnt, 
        Array<OneD, Array<OneD, NekDouble> >    &physarray)
398
399
400
401
402
403
404
405
406
407
    {  
        int i;
        int nTraceNumPoints = GetTraceTotPoints();
        int nvariables      = physarray.num_elements();
        
        // get physical values of the forward trace (from exp to phys)
        Array<OneD, Array<OneD, NekDouble> > Fwd(nvariables);
        for (i = 0; i < nvariables; ++i)
        {
            Fwd[i] = Array<OneD, NekDouble>(nTraceNumPoints);
408
            m_fields[i]->ExtractTracePhys(physarray[i], Fwd[i]);
409
410
411
412
        }
        
        int e, id1, id2, npts;
        
413
414
        for(e = 0; e < m_fields[0]->
            GetBndCondExpansions()[bcRegion]->GetExpSize(); ++e)
415
        {
416
417
418
419
420
421
            npts = m_fields[0]->GetBndCondExpansions()[bcRegion]->
            GetExp(e)->GetNumPoints(0);
            id1  = m_fields[0]->GetBndCondExpansions()[bcRegion]->
            GetPhys_Offset(e);
            id2  = m_fields[0]->GetTrace()->GetPhys_Offset(m_fields[0]->
                    GetTraceMap()->GetBndCondCoeffsToGlobalCoeffsMap(cnt+e));
422
423
424
425
426
            
            switch(m_expdim)
            {
                case 1:
                {
427
                    ASSERTL0(false,
428
429
                             "1D not yet implemented for Compressible " 
                             "Flow Equations");
430
                    break;
431
                }
432
433
434
435
                case 2:
                {
                    Array<OneD, NekDouble> tmp_t(npts);
                    
436
437
438
439
440
441
442
443
444
445
                    Vmath::Vmul(npts, 
                                &Fwd[1][id2], 1, 
                                &m_traceNormals[1][id2], 1, 
                                &tmp_t[0], 1);
                    
                    Vmath::Vvtvm(npts, 
                                 &Fwd[2][id2], 1, 
                                 &m_traceNormals[0][id2], 1, 
                                 &tmp_t[0], 1, 
                                 &tmp_t[0], 1);
446
                    
447
                    Array<OneD, NekDouble> tmp_n(npts, 0.0);
448
449
                    
                    // rotate back to Cartesian
450
451
452
453
454
455
456
457
458
459
                    Vmath::Vmul(npts, 
                                &tmp_t[0], 1, 
                                &m_traceNormals[1][id2], 1, 
                                &Fwd[1][id2], 1);
                    
                    Vmath::Vvtvm(npts, 
                                 &tmp_n[0], 1,
                                 &m_traceNormals[0][id2], 1, 
                                 &Fwd[1][id2], 1, 
                                 &Fwd[1][id2], 1);
460
                    
461
462
463
464
465
466
467
468
469
470
                    Vmath::Vmul(npts, 
                                &tmp_t[0], 1, 
                                &m_traceNormals[0][id2], 1,
                                &Fwd[2][id2], 1);
                    Vmath::Vvtvp(npts, 
                                 &tmp_n[0], 1, 
                                 &m_traceNormals[1][id2], 1, 
                                 &Fwd[2][id2], 1, 
                                 &Fwd[2][id2], 1);
                    break;
471
                }
472
                case 3:
473
                {
474
                    ASSERTL0(false,
475
476
                             "3D not yet implemented for Compressible "
                             "Flow Equations");
477
                    break;
478
                }
479
                default:
480
                {
481
                    ASSERTL0(false, "Illegal expansion dimension");
482
                }
483
484
485
486
487
            }
            
            // copy boundary adjusted values into the boundary expansion
            for (i = 0; i < nvariables; ++i)
            {
488
489
490
491
                Vmath::Vcopy(npts, 
                             &Fwd[i][id2], 1, 
                             &(m_fields[i]->GetBndCondExpansions()[bcRegion]->
                               UpdatePhys())[id1], 1);	
492
493
494
            }
        }
    }
495
    
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
    /**
     * @brief Return the flux vector for the compressible Euler equations.
     * 
     * @param i           Component of the flux vector to calculate.
     * @param physfield   Fields.
     * @param flux        Resulting flux.
     */
    void CompressibleFlowSystem::GetFluxVector(
        const int                                   i,
        const Array<OneD, Array<OneD, NekDouble> > &physfield,
              Array<OneD, Array<OneD, NekDouble> > &flux)
    {
        int j, nq = m_fields[0]->GetTotPoints();
        
        if (i == 0)
        {
            // Flux vector for the rho equation.
            for (j = 0; j < m_expdim; ++j)
            {
                Vmath::Vcopy(nq, physfield[j+1], 1, flux[j], 1);
            }
        } 
        else if (i >= 1 && i <= m_expdim)
        {
            // Flux vector for the velocity fields.
            Array<OneD, NekDouble> pressure(nq);
522
            GetPressure(physfield, pressure);
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
            
            for (j = 0; j < m_expdim; ++j)
            {
                Vmath::Vmul(nq, physfield[j+1], 1, physfield[i], 1, flux[j], 1);
                Vmath::Vdiv(nq, flux[j], 1, physfield[0], 1, flux[j], 1);
            }
            
            // Add pressure to appropriate field
            Vmath::Vadd(nq, flux[i-1], 1, pressure, 1, flux[i-1], 1);
        }
        else if (i == m_expdim+1) 
        {
            // Flux vector for the total energy field.
            Array<OneD, NekDouble> pressure(nq);
            GetPressure(physfield, pressure);
            Vmath::Vadd(nq, physfield[m_expdim+1], 1, pressure, 1, pressure, 1);
            
            for (j = 0; j < m_expdim; ++j)
            {
                Vmath::Vdiv(nq, physfield[j+1], 1, physfield[0], 1, flux[j], 1);
                Vmath::Vmul(nq, flux[j], 1, pressure, 1, flux[j], 1);
            }
        }
        else
        {
            ASSERTL0(false, "Invalid vector index.");
        }
    }
551
552
    
    /** 
553
554
     * @brief Return the flux vector for the LDG diffusion problem.
     * \todo Complete the viscous flux vector
555
     */
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
556
    void CompressibleFlowSystem::GetViscousFluxVector(
557
        const int i, 
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
558
559
560
        const Array<OneD, Array<OneD, NekDouble> >               &physfield,
              Array<OneD, Array<OneD, Array<OneD, NekDouble> > > &derivatives,
              Array<OneD, Array<OneD, Array<OneD, NekDouble> > > &viscousTensor)
561
    {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
562
563
564
565
566
567
568
        int k, j, d;
        
        int nq                       = m_fields[0]->GetTotPoints();
        NekDouble lambda             = -0.66666;
        NekDouble thermalDiffusivity = 0.000019;;

        
569
570
        Array<OneD, NekDouble> mu(nq);
        Array<OneD, NekDouble> tmp_mu(nq);
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
        Array<OneD, NekDouble> tmp(nq);
        
        Array<OneD, Array<OneD, NekDouble> > velocities(m_expdim);
        Array<OneD, Array<OneD, NekDouble> > du(m_expdim);
        Array<OneD, Array<OneD, NekDouble> > dv(m_expdim);
        Array<OneD, Array<OneD, NekDouble> > dw(m_expdim);
        Array<OneD, Array<OneD, NekDouble> > dT(m_expdim);
        
        Array<OneD, NekDouble > pressure   (nq, 0.0);
        Array<OneD, NekDouble > temperature(nq, 0.0);
        
        GetPressure(physfield, pressure);
        GetTemperature(physfield, pressure, temperature);
        GetDynamicViscosity(physfield, mu);
        
        for (k = 0; k < m_expdim; ++k)
587
        {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
            velocities[k] = Array<OneD, NekDouble >(nq, 0.0);
            du[k] = Array<OneD, NekDouble >(nq, 0.0);
            dv[k] = Array<OneD, NekDouble >(nq, 0.0);
            dw[k] = Array<OneD, NekDouble >(nq, 0.0);
            dT[k] = Array<OneD, NekDouble >(nq, 0.0);
        }
        
        // Building the velocities
        for (k = 0; k < m_expdim; ++k)
        {
            Vmath::Vdiv(nq, physfield[k+1], 1, 
                        physfield[0], 1, 
                        velocities[k], 1);
        }
         
        // Building the proper derivatives
        if (m_expdim == 1)
        {
            for (d = 0; d < m_expdim; ++d)
607
            {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
608
609
610
611
612
613
614
                Vmath::Zero(nq, tmp, 1);
                Vmath::Vmul(nq, velocities[0], 1, derivatives[d][0], 1, tmp, 1);
                Vmath::Vsub(nq, derivatives[d][1], 1, tmp, 1, tmp, 1);
                for (j = 0; j < nq; ++j)
                {
                    du[d][j] = (1.0 / physfield[0][j]) * tmp[j];
                }
615
            }
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
616
617
        }
        else if (m_expdim == 2)
618
        {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
619
            for (d = 0; d < m_expdim; ++d)
620
            {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
                Vmath::Zero(nq, tmp, 1);
                Vmath::Vmul(nq, velocities[0], 1, derivatives[d][0], 1, tmp, 1);
                Vmath::Vsub(nq, derivatives[d][1], 1, tmp, 1, tmp, 1);
                for (j = 0; j < nq; ++j)
                {
                    du[d][j] = (1.0 / physfield[0][j]) * tmp[j];
                }
            }
            for (d = 0; d < m_expdim; ++d)
            {
                Vmath::Zero(nq, tmp, 1);
                Vmath::Vmul(nq, velocities[1], 1, derivatives[d][0], 1, tmp, 1);
                Vmath::Vsub(nq, derivatives[d][2], 1, tmp, 1, tmp, 1);
                for (j = 0; j < nq; ++j)
                {
                    dv[d][j] = (1.0 / physfield[0][j]) * tmp[j];
                }
638
639
            }
            
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
            // At the moment for 2D only
            for (d = 0; d < m_expdim; ++d)
            {
                for (j = 0; j < nq; ++j)
                {
                    dT[d][j] = 1.0 / physfield[0][j] * derivatives[d][3][j] -
                    (temperature[j]/physfield[0][j]) * derivatives[d][0][j] - 
                    (0.5 * m_gasConstant / (m_gamma - 1)) * 
                    (2.0 * velocities[0][j] * derivatives[d][1][j] + 
                     ((velocities[0][j] * velocities[0][j] +
                       velocities[1][j] * velocities[1][j]) / 
                      physfield[0][j]) * derivatives[d][0][j]);
                }
            }
654
        }
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
655
656
         
        else if (m_expdim == 3)
657
        {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
            for (d = 0; d < m_expdim; ++d)
            {
                Vmath::Zero(nq, tmp, 1);
                Vmath::Vmul(nq, velocities[0], 1, derivatives[d][0], 1, tmp, 1);
                Vmath::Vsub(nq, derivatives[d][1], 1, tmp, 1, tmp, 1);
                for (j = 0; j < nq; ++j)
                {
                    du[d][j] = (1.0 / physfield[0][j]) * tmp[j];
                }
            }
            for (d = 0; d < m_expdim; ++d)
            {
                Vmath::Zero(nq, tmp, 1);
                Vmath::Vmul(nq, velocities[1], 1, derivatives[d][0], 1, tmp, 1);
                Vmath::Vsub(nq, derivatives[d][2], 1, tmp, 1, tmp, 1);
                for (j = 0; j < nq; ++j)
                {
                    dv[d][j] = (1.0 / physfield[0][j]) * tmp[j];
                }
            }
            for (d = 0; d < m_expdim; ++d)
            {
                Vmath::Zero(nq, tmp, 1);
                Vmath::Vmul(nq, velocities[2], 1, derivatives[d][0], 1, tmp, 1);
                Vmath::Vsub(nq, derivatives[d][3], 1, tmp, 1, tmp, 1);
                for (j = 0; j < nq; ++j)
                {
                    dw[d][j] = (1.0 / physfield[0][j]) * tmp[j];
                }
            }
        }
        
        // Building the viscous flux vector
        if (i == 0)
        {
            // Viscous flux vector for the rho equation
694
695
            for (k = 0; k < m_expdim; ++k)
            {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
                Vmath::Zero(nq, viscousTensor[k][i], 1);
            }
        }
        
        if (m_expdim == 1)
        {
            // to be completed
        }
        else if (m_expdim == 2)
        {
            if (i == 1)
            {
                for (j = 0; j < nq; ++j)
                {
                    viscousTensor[0][i][j] = 2.0 * mu[j] * du[0][j] 
                                            + lambda * (du[0][j] + 
                                                        dv[0][j]);
                    
                    viscousTensor[1][i][j] = dv[0][j] + du[1][j];
                }
            }
            else if (i == 2)
            {
                for (j = 0; j < nq; ++j)
                {
                    viscousTensor[0][i][j] = dv[0][j] + du[1][j];
                
                    viscousTensor[1][i][j] = 2.0 * mu[j] * dv[1][j] 
                                            + lambda * (du[0][j] + 
                                                        dv[0][j]);
                }
            }
            else if (i == 3)
            {
                for (j = 0; j < nq; ++j)
                {
                    viscousTensor[0][i][j] = 
                        velocities[0][j] * viscousTensor[0][0][j] + 
                        velocities[1][j] * viscousTensor[1][1][j] + 
                        (thermalDiffusivity / mu[j]) * (dT[0][j]);
                
                    viscousTensor[1][i][j] = 
                        velocities[1][j] * viscousTensor[1][2][j] + 
                        velocities[0][j] * viscousTensor[1][1][j] + 
                        (thermalDiffusivity / mu[j]) * (dT[1][j]);
                }
            }
        }
        else if (m_expdim == 3)
        {
            if (i == 1)
            {
                // to be completed
            }
            else if (i == 2)
            {
                // to be completed
            }
            else if (i == 3)
            {
                // to be completed
757
758
759
760
761
762
            }
        }
        else
        {
            ASSERTL0(false, "Invalid vector index.");
        }
763
    }
764

765
766
    /**
     * @brief Calculate the pressure field \f$ p =
767
768
     * (\gamma-1)(E-\frac{1}{2}\rho\| \mathbf{v} \|^2) \f$ assuming an ideal 
     * gas law.
769
770
771
772
773
774
775
776
     * 
     * @param physfield  Input momentum.
     * @param pressure   Computed pressure field.
     */
    void CompressibleFlowSystem::GetPressure(
        const Array<OneD, const Array<OneD, NekDouble> > &physfield,
              Array<OneD,                   NekDouble>   &pressure)
    {
777
778
        int       npts  = m_fields[0]->GetTotPoints();
        double    alpha = -0.5;
779
780
781
782
783
784
785
786
        
        // Calculate ||rho v||^2.
        Vmath::Zero(npts, pressure, 1);
        for (int i = 0; i < m_expdim; ++i)
        {
            Vmath::Vvtvp(npts, physfield[1+i], 1, physfield[1+i], 1, 
                               pressure,       1, pressure,       1);
        }
787
        // Divide by rho to get rho*||v||^2.
788
789
        Vmath::Vdiv (npts, pressure, 1, physfield[0], 1, pressure, 1);
        // pressure <- E - 0.5*pressure
790
791
        Vmath::Svtvp(npts,     alpha, 
                     pressure, 1, physfield[m_expdim+1], 1, pressure, 1);
792
793
794
        // Multiply by (gamma-1).
        Vmath::Smul (npts, m_gamma-1, pressure, 1, pressure, 1);
    }
795
    
796
    /**
797
798
     * @brief Compute the velocity field \f$ \mathbf{v} \f$ given the momentum
     * \f$ \rho\mathbf{v} \f$.
799
800
801
802
803
804
805
806
807
808
809
810
     * 
     * @param physfield  Momentum field.
     * @param velocity   Velocity field.
     */
    void CompressibleFlowSystem::GetVelocityVector(
        const Array<OneD, Array<OneD, NekDouble> > &physfield,
              Array<OneD, Array<OneD, NekDouble> > &velocity)
    {
        const int npts = m_fields[0]->GetTotPoints();
        
        for (int i = 0; i < m_expdim; ++i)
        {
811
812
            Vmath::Vdiv(npts, physfield[1+i], 1, physfield[0], 1, 
                              velocity[i],    1);
813
814
        }
    }
815
  
816
817
818
819
820
821
822
823
    /**
     * @brief Compute the temperature \f$ T = p/\rho R \f$.
     * 
     * @param physfield    Input physical field.
     * @param pressure     The pressure field corresponding to physfield.
     * @param temperature  The resulting temperature \f$ T \f$.
     */
    void CompressibleFlowSystem::GetTemperature(
824
825
826
        const Array<OneD, const Array<OneD, NekDouble> > &physfield,
        Array<OneD,                         NekDouble  > &pressure,
        Array<OneD,                         NekDouble  > &temperature)
827
828
829
    {
        for (int i = 0; i < m_fields[0]->GetTotPoints(); ++i)
        {
830
            temperature[i] = pressure[i] / (physfield[0][i] * m_gasConstant);
831
832
        }
    }
833
    
834
835
836
837
838
839
840
841
842
843
844
845
846
847
    /**
     * @brief Compute the sound speed \f$ c = sqrt(\gamma p/\rho) \f$.
     * 
     * @param physfield    Input physical field.
     * @param pressure     The pressure field corresponding to physfield.
     * @param soundspeed   The resulting sound speed \f$ c \f$.
     */
    void CompressibleFlowSystem::GetSoundSpeed(
        const Array<OneD, Array<OneD, NekDouble> > &physfield,
              Array<OneD,             NekDouble  > &pressure,
              Array<OneD,             NekDouble  > &soundspeed)
    {
        for (int i = 0; i < m_fields[0]->GetTotPoints(); ++i)
        {
848
            soundspeed[i] = sqrt(m_gamma * pressure[i] / physfield[0][i]);
849
850
        }
    }
851
    
852
    /**
853
     * @brief Compute the mach number \f$ M = \| \mathbf{v} \|^2 / c \f$.
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
     * 
     * @param physfield    Input physical field.
     * @param soundfield   The speed of sound corresponding to physfield.
     * @param mach         The resulting mach number \f$ M \f$.
     */
    void CompressibleFlowSystem::GetMach(
        Array<OneD, Array<OneD, NekDouble> > &physfield,
        Array<OneD,             NekDouble  > &soundspeed,
        Array<OneD,             NekDouble  > &mach)
    {
        const int npts = m_fields[0]->GetTotPoints();

        Vmath::Zero(npts, mach, 1);
        
        for (int i = 0; i < m_expdim; ++i)
        {
870
871
            Vmath::Vvtvp(npts, physfield[1+i], 1, physfield[1+i], 1, 
                               mach,           1, mach,           1);
872
873
874
875
876
        }
        Vmath::Vdiv(npts, mach, 1, physfield[0], 1, mach, 1);
        Vmath::Vdiv(npts, mach, 1, physfield[0], 1, mach, 1);
        Vmath::Vdiv(npts, mach, 1, soundspeed,   1, mach, 1);
    }
877
    
878
879
880
881
882
883
884
885
886
887
888
889
890
    /**
     * @brief Compute the dynamic viscosity using the Sutherland's law 
     * \f$ \mu = \mu_star * (T / T_star)^3/2 * (T_star + 110) / (T + 110) \f$,
     * where:   \mu_star = 1.7894 * 10^-5 Kg / (m * s)
     *          T_star   = 288.15 K
     * 
     * @param physfield    Input physical field.
     * @param mu           The resulting dynamic viscosity.
     */
    void CompressibleFlowSystem::GetDynamicViscosity(
        const Array<OneD, const Array<OneD, NekDouble> > &physfield,
             Array<OneD,                    NekDouble  > &mu)
    {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
891
892
893
        const int npts       = m_fields[0]->GetTotPoints();
        const double mu_star = 0.00001794;
        const double T_star  = 288.15;
894
895
        Vmath::Zero(npts, mu, 1);
        
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
896
897
898
        Array<OneD, NekDouble > pressure         (npts, 0.0);
        Array<OneD, NekDouble > temperature      (npts, 0.0);
        Array<OneD, NekDouble > temperature_ratio(npts, 0.0);
899
900
901
902
903
904
        
        GetPressure(physfield, pressure);
        GetTemperature(physfield, pressure, temperature);
        
        for (int i = 0; i < npts; ++i)
        {
Gianmarco Mengaldo's avatar
Gianmarco Mengaldo committed
905
906
907
            temperature_ratio[i] = temperature[i] / T_star;
            mu[i] = mu_star * pow(temperature_ratio[i], 1.50) * 
                    (T_star + 110.0) / (temperature[i] + 110.0);
908
909
910
        }
    }
    
911
912
913
    /**
     * @brief Calculate the maximum timestep subject to CFL restrictions.
     */
914
915
    NekDouble CompressibleFlowSystem::v_GetTimeStep(
        const Array<OneD, const Array<OneD, NekDouble> > &inarray)
916
917
918
919
920
921
922
923
924
925
926
    { 
        int i, n;
        int nvariables     = m_fields.num_elements();
        int nTotQuadPoints = GetTotPoints();
        int nElements      = m_fields[0]->GetExpSize(); 
        const Array<OneD, int> ExpOrder = GetNumExpModesPerExp();
        
        Array<OneD, NekDouble> tstep      (nElements, 0.0);
        Array<OneD, NekDouble> stdVelocity(nElements);
        
        // Get standard velocity to compute the time-step limit
927
        GetStdVelocity(inarray, stdVelocity);
928
929
930
931
932
933
934
935
936
937
938
939
940
941
        
        // Factors to compute the time-step limit
        NekDouble minLength;        
        NekDouble alpha   = MaxTimeStepEstimator();
        NekDouble cLambda = 0.2; // Spencer book-317
        
        // Loop over elements to compute the time-step limit for each element
        for(n = 0; n < nElements; ++n)
        {
            int npoints = m_fields[0]->GetExp(n)->GetTotPoints();
            Array<OneD, NekDouble> one2D(npoints, 1.0);
            NekDouble Area = m_fields[0]->GetExp(n)->Integral(one2D);
            
            if (boost::dynamic_pointer_cast<LocalRegions::TriExp>(
942
                    m_fields[0]->GetExp(n)))
943
944
945
946
947
            {
                minLength = 2.0 * sqrt(Area);
            }
            
            else if (boost::dynamic_pointer_cast<LocalRegions::QuadExp>(
948
                         m_fields[0]->GetExp(n)))
949
950
951
            {
                minLength = sqrt(Area);
            }
Dirk Ekelschot's avatar
Dirk Ekelschot committed
952
953
954
955
956
957
            else if (boost::dynamic_pointer_cast<LocalRegions::HexExp>(
                         m_fields[0]->GetExp(n)))
            {
                minLength = sqrt(Area);
            }

958
            
959
960
961
            tstep[n] = m_cflSafetyFactor * alpha * minLength 
                     / (stdVelocity[n] * cLambda 
                        * (ExpOrder[n] - 1) * (ExpOrder[n] - 1));
962
963
964
965
966
967
968
        }
        
        // Get the minimum time-step limit and return the time-step
        NekDouble TimeStep = Vmath::Vmin(nElements, tstep, 1);
        m_comm->AllReduce(TimeStep, LibUtilities::ReduceMin);
        return TimeStep;
    }
969
970
971
972
973
974
975
976
    
    /**
     * @brief Compute the advection velocity in the standard space 
     * for each element of the expansion.
     * 
     * @param inarray    Momentum field.
     * @param stdV       Standard velocity field.
     */
977
978
979
    void CompressibleFlowSystem::GetStdVelocity(
        const Array<OneD, const Array<OneD, NekDouble> > &inarray,
              Array<OneD,                   NekDouble>   &stdV)
980
    {
981
982
983
        int nTotQuadPoints = GetTotPoints();
        int n_element      = m_fields[0]->GetExpSize();
        int npts           = 0;
984
985
986
987
988
989
990

        // Getting the velocity vector on the 2D normal space
        Array<OneD, Array<OneD, NekDouble> > velocity   (m_expdim);
        Array<OneD, Array<OneD, NekDouble> > stdVelocity(m_expdim);
        Array<OneD, NekDouble>               pressure   (nTotQuadPoints);
        Array<OneD, NekDouble>               soundspeed (nTotQuadPoints);
        
991
        // Zero output array
992
993
        Vmath::Zero(stdV.num_elements(), stdV, 1);
        
994
995
996
997
998
        for (int i = 0; i < m_expdim; ++i)
        {
            velocity   [i] = Array<OneD, NekDouble>(nTotQuadPoints);
            stdVelocity[i] = Array<OneD, NekDouble>(nTotQuadPoints, 0.0);
        }
999
1000
        GetVelocityVector(inarray, velocity);
        GetPressure      (inarray, pressure);