AdjointAdvection.cpp 29.5 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
32
33
34
35
///////////////////////////////////////////////////////////////////////////////
//
// File AdjointAdvection.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.
//
// Description: Evaluation of the adjoint advective term
//
///////////////////////////////////////////////////////////////////////////////

36
#include <IncNavierStokesSolver/AdvectionTerms/AdjointAdvection.h>
37
#include <StdRegions/StdSegExp.h>
38
39
40
41
42
43
44
45
46
47
48

#include <MultiRegions/ContField1D.h>
#include <MultiRegions/ContField2D.h>
#include <MultiRegions/ContField3D.h>
#include <MultiRegions/DisContField1D.h>
#include <MultiRegions/DisContField2D.h>
#include <MultiRegions/ContField3DHomogeneous1D.h>
#include <MultiRegions/DisContField3DHomogeneous1D.h>
#include <MultiRegions/ContField3DHomogeneous2D.h>
#include <MultiRegions/DisContField3DHomogeneous2D.h>

49
50
51

namespace Nektar
{
52
    string AdjointAdvection::className = SolverUtils::GetAdvectionFactory().RegisterCreatorFunction("Adjoint", AdjointAdvection::create);
53

54
55
56
57
58
59
    /**
     * Constructor. Creates ...
     *
     * \param 
     * \param
     */
60

61
62
   AdjointAdvection::AdjointAdvection():
        Advection()
63
64
65
66
	{
	}
	

67
68
69
70
71
72
73
74
75
76
   void AdjointAdvection::v_InitObject(
           LibUtilities::SessionReaderSharedPtr        pSession,
           Array<OneD, MultiRegions::ExpListSharedPtr> pFields)
   {
       Advection::v_InitObject(pSession, pFields);

       m_session = pSession;

       m_boundaryConditions = MemoryManager<SpatialDomains::BoundaryConditions>
		::AllocateSharedPtr(m_session, pFields[0]->GetGraph());
77
78
79
80
81
		
		//Setting parameters for homogeneous problems
		m_HomoDirec       = 0;
        m_useFFT          = false;
        m_HomogeneousType = eNotHomogeneous;
82
83
84
		m_SingleMode	   =false;
		m_HalfMode		   =false;
		m_MultipleModes    =false;
85
86
87
88
        m_spacedim = pFields[0]->GetGraph()->GetSpaceDimension();
        m_expdim   = pFields[0]->GetGraph()->GetMeshDimension();
        m_CoeffState = MultiRegions::eLocal;

89
90
91
92
93
94
95
96
97
98
99
100
        if(m_session->DefinesSolverInfo("HOMOGENEOUS"))
        {
            std::string HomoStr = m_session->GetSolverInfo("HOMOGENEOUS");
            m_spacedim          = 3;
			
            if((HomoStr == "HOMOGENEOUS1D")||(HomoStr == "Homogeneous1D")||
               (HomoStr == "1D")||(HomoStr == "Homo1D"))
            {
                m_HomogeneousType = eHomogeneous1D;
                m_LhomZ           = m_session->GetParameter("LZ");
                m_HomoDirec       = 1;
				
101
102
                m_homogen_dealiasing = pSession->DefinesSolverInfo("DEALIASING");

103
104
105
106
107
108
				if(m_session->DefinesSolverInfo("ModeType"))
				{
					m_session->MatchSolverInfo("ModeType","SingleMode",m_SingleMode,false);
					m_session->MatchSolverInfo("ModeType","HalfMode",m_HalfMode,false);
					m_session->MatchSolverInfo("ModeType","MultipleModes",m_MultipleModes,false);
				}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
				if(m_session->DefinesSolverInfo("ModeType"))
				{
					if(m_SingleMode)
					{
						m_npointsZ=2;
						
					}
					else if(m_HalfMode)
					{
						m_npointsZ=1;
						
					}
					else if(m_MultipleModes)
					{
						m_npointsZ        = m_session->GetParameter("HomModesZ");
					}
					else
					{
						ASSERTL0(false, "SolverInfo ModeType not valid");	
						
						
					}
				}
				else 
				{
					m_session->LoadParameter("HomModesZ",m_npointsZ);
					
				}
				
            }
			
            if((HomoStr == "HOMOGENEOUS2D")||(HomoStr == "Homogeneous2D")||
               (HomoStr == "2D")||(HomoStr == "Homo2D"))
            {
                m_HomogeneousType = eHomogeneous2D;
                m_session->LoadParameter("HomModesY", m_npointsY);
                m_session->LoadParameter("LY",        m_LhomY);
                m_session->LoadParameter("HomModesZ", m_npointsZ);
                m_session->LoadParameter("LZ",        m_LhomZ);
                m_HomoDirec       = 2;
            }
			
            if((HomoStr == "HOMOGENEOUS3D")||(HomoStr == "Homogeneous3D")||
               (HomoStr == "3D")||(HomoStr == "Homo3D"))
            {
                m_HomogeneousType = eHomogeneous3D;
                m_session->LoadParameter("HomModesX",m_npointsX);
                m_session->LoadParameter("LX",       m_LhomX   );
                m_session->LoadParameter("HomModesY",m_npointsY);
                m_session->LoadParameter("LY",       m_LhomY   );
                m_session->LoadParameter("HomModesZ",m_npointsZ);
                m_session->LoadParameter("LZ",       m_LhomZ   );
                m_HomoDirec       = 3;
            }
			
            if(m_session->DefinesSolverInfo("USEFFT"))
            {
                m_useFFT = true;
            }
        }
        else
        {
            m_npointsZ = 1; // set to default value so can use to identify 2d or 3D (homogeneous) expansions
        }
		
		if(m_session->DefinesSolverInfo("PROJECTION"))
        {
            std::string ProjectStr
			= m_session->GetSolverInfo("PROJECTION");
			
            if((ProjectStr == "Continuous")||(ProjectStr == "Galerkin")||
               (ProjectStr == "CONTINUOUS")||(ProjectStr == "GALERKIN"))
            {
                m_projectionType = MultiRegions::eGalerkin;
            }
            else if(ProjectStr == "DisContinuous")
            {
186
                m_projectionType = MultiRegions::eDiscontinuous;
187
188
189
190
191
192
193
194
195
196
197
198
199
            }
            else
            {
                ASSERTL0(false,"PROJECTION value not recognised");
            }
        }
        else
        {
            cerr << "Projection type not specified in SOLVERINFO,"
			"defaulting to continuous Galerkin" << endl;
            m_projectionType = MultiRegions::eGalerkin;
        }
		
200
201
202
203
204
205
206
        int nvar = m_session->GetVariables().size();
        m_baseflow = Array<OneD, Array<OneD, NekDouble> >(nvar);
        for (int i = 0; i < nvar; ++i)
        {
            m_baseflow[i] = Array<OneD, NekDouble>(pFields[i]->GetTotPoints(), 0.0);
        }
		//SetUpBaseFields(pFields[0]->GetGraph());
207
208
		ASSERTL0(m_session->DefinesFunction("BaseFlow"),
				 "Base flow must be defined for linearised forms.");
209
		string file = m_session->GetFunctionFilename("BaseFlow", 0);
210
		
211
		
212
213
214
		//Periodic base flows
		if(m_session->DefinesParameter("N_slices"))
		{
215
216
217
            m_session->LoadParameter("N_slices",m_slices);
            if(m_slices>1)
            {
218
				DFT(file,pFields,m_slices);
219
			}
220
221
222
			else
			{
				ASSERTL0(false,"Number of slices must be a positive number greater than 1");
223
224
225
226
227
228
			}
		}
		//Steady base-flow
		else
		{
			m_slices=1;
229
230
231
232
233
			
			//BaseFlow from file
			if (m_session->GetFunctionType("BaseFlow", m_session->GetVariable(0))
				== LibUtilities::eFunctionTypeFile)
			{
234
				ImportFldBase(file,pFields,1);
235
236
237
238
239
				
			}
			//analytic base flow
			else
			{
240
				int nq = pFields[0]->GetNpoints();
241
242
243
244
245
246
				Array<OneD,NekDouble> x0(nq);
				Array<OneD,NekDouble> x1(nq);
				Array<OneD,NekDouble> x2(nq);
				
				// get the coordinates (assuming all fields have the same
				// discretisation)
247
248
				pFields[0]->GetCoords(x0,x1,x2);
				for(unsigned int i = 0 ; i < m_baseflow.num_elements(); i++)
249
250
251
252
				{
					LibUtilities::EquationSharedPtr ifunc
					= m_session->GetFunction("BaseFlow", i);
					
253
					ifunc->Evaluate(x0,x1,x2,m_baseflow[i]);
254
255
256
257
				}
				
			}
			
258
259
		}
}
260
	
261
262

    AdjointAdvection::~AdjointAdvection()
263
264
    {
    }
265

266
    
267
268
269
270
271
272
273
274
275
276
277
    void AdjointAdvection::v_Advect(
        const int nConvectiveFields,
        const Array<OneD, MultiRegions::ExpListSharedPtr> &fields,
        const Array<OneD, Array<OneD, NekDouble> >        &advVel,
        const Array<OneD, Array<OneD, NekDouble> >        &inarray,
        Array<OneD, Array<OneD, NekDouble> >              &outarray,
        const NekDouble                                   &time)
    {
        int nqtot            = fields[0]->GetTotPoints();
        ASSERTL1(nConvectiveFields == inarray.num_elements(),"Number of convective fields and Inarray are not compatible");

Chris Cantwell's avatar
Chris Cantwell committed
278
        Array<OneD, NekDouble > Deriv = Array<OneD, NekDouble> (nqtot*nConvectiveFields);
279

Chris Cantwell's avatar
Chris Cantwell committed
280
        for(int n = 0; n < nConvectiveFields; ++n)
281
        {
Chris Cantwell's avatar
Chris Cantwell committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
            //v_ComputeAdvectionTerm(fields,advVel,inarray[i],outarray[i],i,time,Deriv);
            int ndim       = advVel.num_elements();
            int nPointsTot = fields[0]->GetNpoints();
            Array<OneD, NekDouble> grad0,grad1,grad2;

            //Evaluation of the gradiend of each component of the base flow
            //\nabla U
            Array<OneD, NekDouble> grad_base_u0,grad_base_u1,grad_base_u2;
            // \nabla V
            Array<OneD, NekDouble> grad_base_v0,grad_base_v1,grad_base_v2;
            // \nabla W
            Array<OneD, NekDouble> grad_base_w0,grad_base_w1,grad_base_w2;


            grad0 = Array<OneD, NekDouble> (nPointsTot);
            grad_base_u0 = Array<OneD, NekDouble> (nPointsTot);
            grad_base_v0 = Array<OneD, NekDouble> (nPointsTot);
            grad_base_w0 = Array<OneD, NekDouble> (nPointsTot);


            //Evaluation of the base flow for periodic cases
            //(it requires fld files)

            if(m_slices>1)
            {
                if (m_session->GetFunctionType("BaseFlow", 0)
                    == LibUtilities::eFunctionTypeFile)
                {
                    for(int i=0; i<ndim;++i)
                    {
312
                        UpdateBase(m_slices,m_interp[i],m_baseflow[i],time,m_period);
Chris Cantwell's avatar
Chris Cantwell committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
                    }
                }
                else
                {
                    ASSERTL0(false, "Periodic Base flow requires .fld files");
                }
            }

            //Evaluate the Adjoint advection term
            switch(ndim)
            {
                        // 1D
                    case 1:
                        fields[0]->PhysDeriv(inarray[n],grad0);
327
                        fields[0]->PhysDeriv(m_baseflow[0],grad_base_u0);
Chris Cantwell's avatar
Chris Cantwell committed
328
                        //Evaluate  U du'/dx
329
                        Vmath::Vmul(nPointsTot,grad0,1,m_baseflow[0],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
                        //Evaluate U du'/dx+ u' dU/dx
                        Vmath::Vvtvp(nPointsTot,grad_base_u0,1,advVel[0],1,outarray[n],1,outarray[n],1);
                        break;

                        //2D
                    case 2:

                        grad1 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_u1 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_v1 = Array<OneD, NekDouble> (nPointsTot);

                        fields[0]->PhysDeriv(inarray[n],grad0,grad1);

                        //Derivates of the base flow
344
345
                        fields[0]-> PhysDeriv(m_baseflow[0], grad_base_u0, grad_base_u1);
                        fields[0]-> PhysDeriv(m_baseflow[1], grad_base_v0, grad_base_v1);
Chris Cantwell's avatar
Chris Cantwell committed
346
347
348
349
350
351
352
353

                        //Since the components of the velocity are passed one by one, it is necessary to distinguish which
                        //term is consider
                        switch (n)
                        {
                            //x-equation
                        case 0:
                            // Evaluate U du'/dx
354
                            Vmath::Vmul (nPointsTot,grad0,1,m_baseflow[0],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
355
                            //Evaluate U du'/dx+ V du'/dy
356
                            Vmath::Vvtvp(nPointsTot,grad1,1,m_baseflow[1],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
357
358
359
360
361
362
363
364
365
366
367
                            //Evaluate - (U du'/dx+ V du'/dy)
                            Vmath::Neg(nPointsTot,outarray[n],1);
                            //Evaluate -(U du'/dx+ V du'/dy)+u' dU/dx
                            Vmath::Vvtvp(nPointsTot,grad_base_u0,1,advVel[0],1,outarray[n],1,outarray[n],1);
                            //Evaluate -(U du'/dx+ V du'/dy) +u' dU/dx +v' dV/dx
                            Vmath::Vvtvp(nPointsTot,grad_base_v0,1,advVel[1],1,outarray[n],1,outarray[n],1);
                            break;

                            //y-equation
                        case 1:
                            // Evaluate U dv'/dx
368
                            Vmath::Vmul (nPointsTot,grad0,1,m_baseflow[0],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
369
                            //Evaluate U dv'/dx+ V dv'/dy
370
                            Vmath::Vvtvp(nPointsTot,grad1,1,m_baseflow[1],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
                            //Evaluate -(U dv'/dx+ V dv'/dy)
                            Vmath::Neg(nPointsTot,outarray[n],1);
                            //Evaluate (U dv'/dx+ V dv'/dy)+u' dU/dy
                            Vmath::Vvtvp(nPointsTot,grad_base_u1,1,advVel[0],1,outarray[n],1,outarray[n],1);
                            //Evaluate (U dv'/dx+ V dv'/dy +u' dv/dx)+v' dV/dy
                            Vmath::Vvtvp(nPointsTot,grad_base_v1,1,advVel[1],1,outarray[n],1,outarray[n],1);
                            break;
                    }
                        break;


                        //3D
                    case 3:

                        grad1 = Array<OneD, NekDouble> (nPointsTot);
                        grad2 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_u1 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_v1 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_w1 = Array<OneD, NekDouble> (nPointsTot);

                        grad_base_u2 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_v2 = Array<OneD, NekDouble> (nPointsTot);
                        grad_base_w2 = Array<OneD, NekDouble> (nPointsTot);

395
396
397
                        fields[0]->PhysDeriv(m_baseflow[0], grad_base_u0, grad_base_u1,grad_base_u2);
                        fields[0]->PhysDeriv(m_baseflow[1], grad_base_v0, grad_base_v1,grad_base_v2);
                        fields[0]->PhysDeriv(m_baseflow[2], grad_base_w0, grad_base_w1, grad_base_w2);
Chris Cantwell's avatar
Chris Cantwell committed
398
399

                        //HalfMode has W(x,y,t)=0
400
                        if(m_HalfMode || m_SingleMode)
Chris Cantwell's avatar
Chris Cantwell committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
                        {
                            for(int i=0; i<grad_base_u2.num_elements();++i)
                            {
                                grad_base_u2[i]=0;
                                grad_base_v2[i]=0;
                                grad_base_w2[i]=0;

                            }
                        }

                    fields[0]->PhysDeriv(inarray[n], grad0, grad1, grad2);

                    switch (n)
                    {
                            //x-equation
                        case 0:
                            //Evaluate U du'/dx
418
                            Vmath::Vmul (nPointsTot,grad0,1,m_baseflow[0],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
419
                            //Evaluate U du'/dx+ V du'/dy
420
                            Vmath::Vvtvp(nPointsTot,grad1,1,m_baseflow[1],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
421
                            //Evaluate U du'/dx+ V du'/dy+W du'/dz
422
                            Vmath::Vvtvp(nPointsTot,grad2,1,m_baseflow[2],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
423
424
425
426
427
428
429
430
431
432
433
434
                            //Evaluate -(U du'/dx+ V du'/dy+W du'/dz)
                            Vmath::Neg(nPointsTot,outarray[n],1);
                            //Evaluate -(U du'/dx+ V du'/dy+W du'/dz)+u' dU/dx
                            Vmath::Vvtvp(nPointsTot,grad_base_u0,1,advVel[0],1,outarray[n],1,outarray[n],1);
                            //Evaluate -(U du'/dx+ V du'/dy+W du'/dz)+u'dU/dx+ v' dV/dx
                            Vmath::Vvtvp(nPointsTot,grad_base_v0,1,advVel[1],1,outarray[n],1,outarray[n],1);
                            //Evaluate -(U du'/dx+ V du'/dy+W du'/dz)+u'dU/dx+ v' dV/dx+ w' dW/dz
                            Vmath::Vvtvp(nPointsTot,grad_base_w0,1,advVel[2],1,outarray[n],1,outarray[n],1);
                            break;
                            //y-equation
                        case 1:
                            //Evaluate U dv'/dx
435
                            Vmath::Vmul (nPointsTot,grad0,1,m_baseflow[0],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
436
                            //Evaluate U dv'/dx+ V dv'/dy
437
                            Vmath::Vvtvp(nPointsTot,grad1,1,m_baseflow[1],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
438
                            //Evaluate U dv'/dx+ V dv'/dy+W dv'/dz
439
                            Vmath::Vvtvp(nPointsTot,grad2,1,m_baseflow[2],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
440
441
442
443
444
445
446
447
448
449
450
451
452
                            //Evaluate -(U dv'/dx+ V dv'/dy+W dv'/dz)
                            Vmath::Neg(nPointsTot,outarray[n],1);
                            //Evaluate  -(U dv'/dx+ V dv'/dy+W dv'/dz)+u' dU/dy
                            Vmath::Vvtvp(nPointsTot,grad_base_u1,1,advVel[0],1,outarray[n],1,outarray[n],1);
                            //Evaluate  -(U dv'/dx+ V dv'/dy+W dv'/dz)+u' dU/dy +v' dV/dy
                            Vmath::Vvtvp(nPointsTot,grad_base_v1,1,advVel[1],1,outarray[n],1,outarray[n],1);
                            //Evaluate  -(U dv'/dx+ V dv'/dy+W dv'/dz)+u' dU/dy +v' dV/dy+ w' dW/dy
                            Vmath::Vvtvp(nPointsTot,grad_base_w1,1,advVel[2],1,outarray[n],1,outarray[n],1);
                            break;

                            //z-equation
                        case 2:
                            //Evaluate U dw'/dx
453
                            Vmath::Vmul (nPointsTot,grad0,1,m_baseflow[0],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
454
                            //Evaluate U dw'/dx+ V dw'/dx
455
                            Vmath::Vvtvp(nPointsTot,grad1,1,m_baseflow[1],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
456
                            //Evaluate U dw'/dx+ V dw'/dx+ W dw'/dz
457
                            Vmath::Vvtvp(nPointsTot,grad2,1,m_baseflow[2],1,outarray[n],1,outarray[n],1);
Chris Cantwell's avatar
Chris Cantwell committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
                            //Evaluate -(U dw'/dx+ V dw'/dx+ W dw'/dz)
                            Vmath::Neg(nPointsTot,outarray[n],1);
                            //Evaluate -(U dw'/dx+ V dw'/dx+ W dw'/dz)+u' dU/dz
                            Vmath::Vvtvp(nPointsTot,grad_base_u2,1,advVel[0],1,outarray[n],1,outarray[n],1);
                            //Evaluate -(U dw'/dx+ V dw'/dx+ W dw'/dz)+u' dU/dz+v'dV/dz
                            Vmath::Vvtvp(nPointsTot,grad_base_v2,1,advVel[1],1,outarray[n],1,outarray[n],1);
                            //Evaluate -(U dw'/dx+ V dw'/dx+ W dw'/dz)+u' dU/dz+v'dV/dz + w' dW/dz
                            Vmath::Vvtvp(nPointsTot,grad_base_w2,1,advVel[2],1,outarray[n],1,outarray[n],1);
                            break;
                    }
                        break;


            default:
                ASSERTL0(false,"dimension unknown");
            }

            Vmath::Neg(nqtot,outarray[n],1);
476
477
478
479
480
        }

    }


481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    void AdjointAdvection::v_SetBaseFlow(
            const Array<OneD, Array<OneD, NekDouble> >    &inarray)
    {
        ASSERTL1(inarray.num_elements() == m_baseflow.num_elements(),
                 "Number of base flow variables does not match what is"
                 "expected.");

        int npts = inarray[0].num_elements();
        for (int i = 0; i < inarray.num_elements(); ++i)
        {
            ASSERTL1(npts == m_baseflow.num_elements(),
                    "Size of base flow array does not match expected.");
            Vmath::Vcopy(npts, inarray[i], 1, m_baseflow[i], 1);
        }
    }


498
499
500
501
502
503
504
    /**
     * Import field from infile and load into \a m_fields. This routine will
     * also perform a \a BwdTrans to ensure data is in both the physical and
     * coefficient storage.
     * @param   infile          Filename to read.
     */
    void AdjointAdvection::ImportFldBase(std::string pInfile,
505
            Array<OneD, MultiRegions::ExpListSharedPtr>& pFields, int slice)
506
    {
507
        std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef;
508
        std::vector<std::vector<NekDouble> > FieldData;
509
510
511
        int nqtot = pFields[0]->GetTotPoints();
        Array<OneD, NekDouble> tmp_coeff(pFields[0]->GetNcoeffs(), 0.0);

512
513
        //Get Homogeneous
        LibUtilities::Import(pInfile,FieldDef,FieldData);
514
		
515
        int nvar = m_session->GetVariables().size();
516
517
518
519
520
521
522
        int s;
	
        if(m_session->DefinesSolverInfo("HOMOGENEOUS"))
        {
            std::string HomoStr = m_session->GetSolverInfo("HOMOGENEOUS");
        }
	
523
524
525
526
527
        // copy FieldData into m_fields
        for(int j = 0; j < nvar; ++j)
        {
            for(int i = 0; i < FieldDef.size(); ++i)
            {
528
529
530
531
532
                if ((m_session->DefinesSolverInfo("HOMOGENEOUS") &&
                    (m_session->GetSolverInfo("HOMOGENEOUS")=="HOMOGENEOUS1D" ||
                     m_session->GetSolverInfo("HOMOGENEOUS")=="1D" ||
                     m_session->GetSolverInfo("HOMOGENEOUS")=="Homo1D")) &&
                    nvar==4)
533
534
                {
                    // w-component must be ignored and set to zero.
535
                    if (j != nvar - 2)
536
537
                    {
                        // p component (it is 4th variable of the 3D and corresponds 3nd variable of 2D)
538
                        s = (j == nvar - 1) ? 2 : j;
539
			
540
                        //extraction of the 2D
541
                        pFields[j]->ExtractDataToCoeffs(
542
543
544
                                            FieldDef[i],
                                            FieldData[i],
                                            FieldDef[i]->m_fields[s],
545
                                            tmp_coeff);
546
547
548
                    }

                    // Put zero on higher modes
549
                    int ncplane = (pFields[0]->GetNcoeffs()) / m_npointsZ;
550
551
552
553

                    if (m_npointsZ > 2)
                    {
                        Vmath::Zero(ncplane*(m_npointsZ-2),
554
                                    &tmp_coeff[2*ncplane],1);
555
556
                    }
                }
557
                //2D cases and Homogeneous1D Base Flows
558
559
                else
                {
560
561
562
                    bool flag = FieldDef[i]->m_fields[j] ==
                                                    m_session->GetVariable(j);

563
564
565
566
                    ASSERTL1(flag, (std::string("Order of ") + pInfile
                                    + std::string(" data and that defined in "
                                                  "m_boundaryconditions differs")).c_str());
                    
567
                    pFields[j]->ExtractDataToCoeffs(FieldDef[i], FieldData[i],
568
                                                   FieldDef[i]->m_fields[j],
569
                                                   tmp_coeff);
570
571
572
573
574
                }
            }
            
            if(m_SingleMode || m_HalfMode)
            {
575
                pFields[j]->GetPlane(0)->BwdTrans(tmp_coeff, m_baseflow[j]);
576
577
578
579
                
                if(m_SingleMode)
                {
                    //copy the bwd into the second plane for single Mode Analysis
580
581
                    int ncplane=(pFields[0]->GetNpoints())/m_npointsZ;
                    Vmath::Vcopy(ncplane,&m_baseflow[j][0],1,&m_baseflow[j][ncplane],1);
582
583
584
585
                }
            }
            else
            {
586
                pFields[j]->BwdTrans(tmp_coeff, m_baseflow[j]);
587
588
589
590
591
592
593
594
595
596
                
            }
            
        }
	
        //std::string outname ="BaseFlow.bse";
        //WriteFldBase(outname);
	
        if(m_session->DefinesParameter("N_slices"))
        {
597
            int n = pFields.num_elements()-1;
598
            
599
            for(int i=0; i<n;++i)
600
601
            {
                
602
                Vmath::Vcopy(nqtot, &m_baseflow[i][0], 1, &m_interp[i][slice*nqtot], 1);
603
604
605
            }
            
        }
606
607
608
    }
        
   
609
610
611
612
613
614
615
		void AdjointAdvection::UpdateBase( const NekDouble m_slices,
											 Array<OneD, const NekDouble> &inarray,
											 Array<OneD, NekDouble> &outarray,
											 const NekDouble m_time,
											 const NekDouble m_period)
		{
			
616
			int npoints=m_baseflow[0].num_elements();
617
618
619
620
621
622
623
624
625
626
627
			
			NekDouble BetaT=2*M_PI*fmod (m_time, m_period) / m_period;
			NekDouble phase;
			Array<OneD, NekDouble> auxiliary(npoints);
			
			Vmath::Vcopy(npoints,&inarray[0],1,&outarray[0],1);
			Vmath::Svtvp(npoints, cos(0.5*m_slices*BetaT),&inarray[npoints],1,&outarray[0],1,&outarray[0],1);
			
			for (int i = 2; i < m_slices; i += 2) 
			{
				phase = (i>>1) * BetaT;
628
				
629
				Vmath::Svtvp(npoints, cos(phase),&inarray[i*npoints],1,&outarray[0],1,&outarray[0],1);
630
				Vmath::Svtvp(npoints, sin(phase), &inarray[(i+1)*npoints], 1, &outarray[0], 1,&outarray[0],1);
631
632
633
634
			}
			
		}
		
635
636
637
638
639
640
		DNekBlkMatSharedPtr AdjointAdvection::GetFloquetBlockMatrix(FloquetMatType mattype, bool UseContCoeffs) const
		{
			DNekMatSharedPtr    loc_mat;
			DNekBlkMatSharedPtr BlkMatrix;
			int n_exp = 0;
			
641
			n_exp = m_baseflow[0].num_elements(); // will operatore on m_phys
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
			
			Array<OneD,unsigned int> nrows(n_exp);
			Array<OneD,unsigned int> ncols(n_exp);
			
			nrows = Array<OneD, unsigned int>(n_exp,m_slices);
			ncols = Array<OneD, unsigned int>(n_exp,m_slices);
			
			MatrixStorage blkmatStorage = eDIAGONAL;
			BlkMatrix = MemoryManager<DNekBlkMat>
			::AllocateSharedPtr(nrows,ncols,blkmatStorage);
			
			
			const LibUtilities::PointsKey Pkey(m_slices,LibUtilities::eFourierEvenlySpaced);
			const LibUtilities::BasisKey  BK(LibUtilities::eFourier,m_slices,Pkey);
			StdRegions::StdSegExp StdSeg(BK);
			
			StdRegions::StdMatrixKey matkey(StdRegions::eFwdTrans,
659
660
                                                        StdSeg.DetShapeType(),
                                                        StdSeg);
661
662
663
664
665
666
667
668
669
670
671
672
673
			
			loc_mat = StdSeg.GetStdMatrix(matkey);
			
			// set up array of block matrices.
			for(int i = 0; i < n_exp; ++i)
			{
				BlkMatrix->SetBlock(i,i,loc_mat);
			}
			
			return BlkMatrix;
		}
		
		//Discrete Fourier Transform for Floquet analysis
674
675
676
		void AdjointAdvection::DFT(const string file,
	            Array<OneD, MultiRegions::ExpListSharedPtr>& pFields,
	            const NekDouble m_slices)
677
		{
678
			int npoints=m_baseflow[0].num_elements();
679
680
			
			//Convected fields
681
			int ConvectedFields=m_baseflow.num_elements()-1;
682
683
684
685
686
687
688
689
690
691
692
693
694
			
			m_interp= Array<OneD, Array<OneD, NekDouble> > (ConvectedFields);
			for(int i=0; i<ConvectedFields;++i)
			{
				m_interp[i]=Array<OneD,NekDouble>(npoints*m_slices);
			}
			
			//Import the slides into auxiliary vector
			//The base flow should be stored in the form filename_i.bse
			for (int i=0; i< m_slices; ++i)
			{
				char chkout[16] = "";
				sprintf(chkout, "%d", i);
695
				ImportFldBase(file+"_"+chkout+".bse",pFields,i);
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
757
758
759
760
761
762
763
764
765
766
767
			} 
			
			
			// Discrete Fourier Transform of the fields
			for(int k=0; k< ConvectedFields;++k)
			{
#ifdef NEKTAR_USING_FFTW
				
				//Discrete Fourier Transform using FFTW
				
				
				Array<OneD, NekDouble> fft_in(npoints*m_slices);
				Array<OneD, NekDouble> fft_out(npoints*m_slices);
				
				Array<OneD, NekDouble> m_tmpIN(m_slices);
				Array<OneD, NekDouble> m_tmpOUT(m_slices);
				
				//Shuffle the data
				for(int j= 0; j < m_slices; ++j)
				{
					Vmath::Vcopy(npoints,&m_interp[k][j*npoints],1,&(fft_in[j]),m_slices);
				}
				
				m_FFT = LibUtilities::GetNektarFFTFactory().CreateInstance("NekFFTW", m_slices);
				
				//FFT Transform
				for(int i=0; i<npoints; i++)
				{
					m_FFT->FFTFwdTrans(m_tmpIN =fft_in + i*m_slices, m_tmpOUT =fft_out + i*m_slices);
					
				}
				
				//Reshuffle data
				for(int s = 0; s < m_slices; ++s)
				{						
					Vmath::Vcopy(npoints,&fft_out[s],m_slices,&m_interp[k][s*npoints],1);
					
				}
				
				Vmath::Zero(fft_in.num_elements(),&fft_in[0],1);
				Vmath::Zero(fft_out.num_elements(),&fft_out[0],1);				
#else
				//Discrete Fourier Transform using MVM
				
				
				DNekBlkMatSharedPtr blkmat;
				blkmat = GetFloquetBlockMatrix(eForwardsPhys);
				
				int nrows = blkmat->GetRows();
				int ncols = blkmat->GetColumns();
				
				Array<OneD, NekDouble> sortedinarray(ncols);
				Array<OneD, NekDouble> sortedoutarray(nrows);
				
				//Shuffle the data
				for(int j= 0; j < m_slices; ++j)
				{
					Vmath::Vcopy(npoints,&m_interp[k][j*npoints],1,&(sortedinarray[j]),m_slices);
				}
				
				// Create NekVectors from the given data arrays
				NekVector<NekDouble> in (ncols,sortedinarray,eWrapper);
				NekVector<NekDouble> out(nrows,sortedoutarray,eWrapper);
				
				// Perform matrix-vector multiply.
				out = (*blkmat)*in;
				
				//Reshuffle data
				for(int s = 0; s < m_slices; ++s)
				{	
					Vmath::Vcopy(npoints,&sortedoutarray[s],m_slices,&m_interp[k][s*npoints],1);
				}
768
                                
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
				Vmath::Zero(sortedinarray.num_elements(),&sortedinarray[0],1);
				Vmath::Zero(sortedoutarray.num_elements(),&sortedoutarray[0],1);	
								
#endif
				
				//scaling of the Fourier coefficients
				NekDouble j=-1;
				for (int i = 2; i < m_slices; i += 2) 
				{
					Vmath::Smul(2*npoints,j,&m_interp[k][i*npoints],1,&m_interp[k][i*npoints],1);
					j=-j;
					
				}
				
			}
			
			if(m_session->DefinesParameter("period"))
			{
				m_period=m_session->GetParameter("period");
			}
			else 
			{
				m_period=(m_session->GetParameter("TimeStep")*m_slices)/(m_slices-1.);
			}	
			
			
		}
		
797
798
799
	
} //end of namespace