ExpListHomogeneous1D.cpp 50.3 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 ExpListHomogeneous1D.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: An ExpList which is homogeneous in 1-direction
33 34 35
//
///////////////////////////////////////////////////////////////////////////////

36
#include <MultiRegions/ExpListHomogeneous1D.h>
37
#include <LibUtilities/Foundations/ManagerAccess.h>  // for PointsManager, etc
38
#include <StdRegions/StdSegExp.h>
39
#include <StdRegions/StdPointExp.h>
40
#include <LocalRegions/Expansion.h>
41
#include <LocalRegions/Expansion2D.h>
42

43 44
using namespace std;

45 46 47 48 49 50 51 52 53 54 55 56 57
namespace Nektar
{
    namespace MultiRegions
    {
        // Forward declaration for typedefs
        ExpListHomogeneous1D::ExpListHomogeneous1D():
            ExpList(),
            m_homogeneousBasis(LibUtilities::NullBasisSharedPtr),
            m_lhom(1),
            m_homogeneous1DBlockMat(MemoryManager<Homo1DBlockMatrixMap>::AllocateSharedPtr())
        {
        }

Chris Cantwell's avatar
Chris Cantwell committed
58
        ExpListHomogeneous1D::ExpListHomogeneous1D(const LibUtilities::SessionReaderSharedPtr
59
                &pSession,const LibUtilities::BasisKey &HomoBasis, const NekDouble lhom, const bool useFFT, const bool dealiasing):
60
            ExpList(pSession),
61
            m_useFFT(useFFT),
62 63 64
            m_lhom(lhom),
            m_homogeneous1DBlockMat(MemoryManager<Homo1DBlockMatrixMap>::AllocateSharedPtr()),
            m_dealiasing(dealiasing)
65
        {
66
            ASSERTL2(HomoBasis != LibUtilities::NullBasisKey,"Homogeneous Basis is a null basis");
67
            
Yan Bao's avatar
Yan Bao committed
68
            m_homogeneousBasis = LibUtilities::BasisManager()[HomoBasis];
69

70 71 72 73
            m_StripZcomm = m_session->DefinesSolverInfo("HomoStrip") ?
                           m_comm->GetColumnComm()->GetColumnComm()  :
                           m_comm->GetColumnComm();

74 75 76 77 78 79 80 81 82
            ASSERTL0( m_homogeneousBasis->GetNumPoints() %
                      m_StripZcomm->GetSize() == 0,
                      "HomModesZ should be a multiple of npz.");

            if (  (m_homogeneousBasis->GetBasisType() !=
                    LibUtilities::eFourierHalfModeRe)
               && (m_homogeneousBasis->GetBasisType() !=
                    LibUtilities::eFourierHalfModeIm) )
            {
83 84 85 86
                ASSERTL0(
                    (m_homogeneousBasis->GetNumPoints() /
                     m_StripZcomm->GetSize()) % 2 == 0,
                    "HomModesZ/npz should be an even integer.");
87 88
            }

89 90 91 92 93 94 95
            m_transposition = MemoryManager<LibUtilities::Transposition>
                                ::AllocateSharedPtr(HomoBasis, m_comm, m_StripZcomm);

            m_planes = Array<OneD,ExpListSharedPtr>(
                                m_homogeneousBasis->GetNumPoints() /
                                m_StripZcomm->GetSize());

96 97
            if(m_useFFT)
            {
98 99
                m_FFT = LibUtilities::GetNektarFFTFactory().CreateInstance(
                                "NekFFTW", m_homogeneousBasis->GetNumPoints());
100
            }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

            if(m_dealiasing)
            {
                if(m_useFFT)
                {
                    NekDouble size = 1.5*m_homogeneousBasis->GetNumPoints();
                    m_padsize = int(size);
                    m_FFT_deal = LibUtilities::GetNektarFFTFactory()
                                    .CreateInstance("NekFFTW", m_padsize);
                }
                else
                {
                    ASSERTL0(false, "Dealiasing available just in combination "
                                    "with FFTW");
                }
            }
        }
118 119 120 121 122 123 124


        /**
         * @param   In          ExpListHomogeneous1D object to copy.
         */
        ExpListHomogeneous1D::ExpListHomogeneous1D(const ExpListHomogeneous1D &In):
            ExpList(In,false),
125
            m_transposition(In.m_transposition),
126
            m_StripZcomm(In.m_StripZcomm),
127
            m_useFFT(In.m_useFFT),
128
            m_FFT(In.m_FFT),
129
            m_tmpIN(In.m_tmpIN),
130
            m_tmpOUT(In.m_tmpOUT),
131 132 133 134
            m_homogeneousBasis(In.m_homogeneousBasis),
            m_lhom(In.m_lhom), 
            m_homogeneous1DBlockMat(In.m_homogeneous1DBlockMat),
            m_dealiasing(In.m_dealiasing),
135
            m_padsize(In.m_padsize)
136 137 138
        {
            m_planes = Array<OneD, ExpListSharedPtr>(In.m_planes.num_elements());
        }
139 140 141 142 143 144 145
        
        ExpListHomogeneous1D::ExpListHomogeneous1D(const ExpListHomogeneous1D &In,
                                            const std::vector<unsigned int> &eIDs):
            ExpList(In,eIDs,false),
            m_transposition(In.m_transposition),
            m_useFFT(In.m_useFFT),
            m_FFT(In.m_FFT),
146
            m_tmpIN(In.m_tmpIN),
147
            m_tmpOUT(In.m_tmpOUT),
148 149
            m_homogeneousBasis(In.m_homogeneousBasis),
            m_lhom(In.m_lhom), 
150
            m_homogeneous1DBlockMat(MemoryManager<Homo1DBlockMatrixMap>::AllocateSharedPtr()),
151
            m_dealiasing(In.m_dealiasing),
152
            m_padsize(In.m_padsize)
153 154 155 156 157 158 159 160 161 162
        {
            m_planes = Array<OneD, ExpListSharedPtr>(In.m_planes.num_elements());
        }

        /**
         * Destructor
         */
        ExpListHomogeneous1D::~ExpListHomogeneous1D()
        {
        }
Yan Bao's avatar
Yan Bao committed
163
    
164
        void ExpListHomogeneous1D::v_HomogeneousFwdTrans(const Array<OneD, const NekDouble> &inarray, 
165 166 167 168
                                                         Array<OneD, NekDouble> &outarray, 
                                                         CoeffState coeffstate,
                                                         bool Shuff,
                                                         bool UnShuff)
169
        {
170
            // Forwards trans
171
            Homogeneous1DTrans(inarray,outarray,true,coeffstate,Shuff,UnShuff);
172
        }
Yan Bao's avatar
Yan Bao committed
173
    
174
        void ExpListHomogeneous1D::v_HomogeneousBwdTrans(const Array<OneD, const NekDouble> &inarray, 
175 176 177 178
                                                         Array<OneD, NekDouble> &outarray, 
                                                         CoeffState coeffstate,
                                                         bool Shuff,
                                                         bool UnShuff)
179
        {
180
            // Backwards trans
181
            Homogeneous1DTrans(inarray,outarray,false,coeffstate,Shuff,UnShuff);
182
        }
Yan Bao's avatar
Yan Bao committed
183
        
184 185 186 187 188 189 190 191
        /**
         * Dealiasing routine
         */
        void ExpListHomogeneous1D::v_DealiasedProd(const Array<OneD, NekDouble> &inarray1,
                                                   const Array<OneD, NekDouble> &inarray2,
                                                   Array<OneD, NekDouble> &outarray, 
                                                   CoeffState coeffstate)
        {
192 193
            // inarray1 = first term of the product in full physical space
            // inarray2 = second term of the product in full physical space
194
            // dealiased product stored in outarray
195 196 197 198 199

            int num_dofs = inarray1.num_elements();

            int N = m_homogeneousBasis->GetNumPoints();

200 201
            Array<OneD, NekDouble> V1(num_dofs);
            Array<OneD, NekDouble> V2(num_dofs);
202 203 204 205 206 207
            Array<OneD, NekDouble> V1V2(num_dofs);

            HomogeneousFwdTrans(inarray1,V1,coeffstate);
            HomogeneousFwdTrans(inarray2,V2,coeffstate);

            int num_points_per_plane = num_dofs/m_planes.num_elements();
Yan Bao's avatar
Yan Bao committed
208 209 210 211 212 213 214 215 216
            int num_proc;
            if(!m_session->DefinesSolverInfo("HomoStrip"))
            {
                num_proc             = m_comm->GetColumnComm()->GetSize();
            }
            else
            {
                num_proc             = m_StripZcomm->GetSize();
            }
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            int num_dfts_per_proc    = num_points_per_plane / num_proc
                                        + (num_points_per_plane % num_proc > 0);

            Array<OneD, NekDouble> ShufV1(num_dfts_per_proc*N,0.0);
            Array<OneD, NekDouble> ShufV2(num_dfts_per_proc*N,0.0);
            Array<OneD, NekDouble> ShufV1V2(num_dfts_per_proc*N,0.0);

            Array<OneD, NekDouble> ShufV1_PAD_coef(m_padsize,0.0);
            Array<OneD, NekDouble> ShufV2_PAD_coef(m_padsize,0.0);
            Array<OneD, NekDouble> ShufV1_PAD_phys(m_padsize,0.0);
            Array<OneD, NekDouble> ShufV2_PAD_phys(m_padsize,0.0);

            Array<OneD, NekDouble> ShufV1V2_PAD_coef(m_padsize,0.0);
            Array<OneD, NekDouble> ShufV1V2_PAD_phys(m_padsize,0.0);

            m_transposition->Transpose(V1, ShufV1, false, LibUtilities::eXYtoZ);
            m_transposition->Transpose(V2, ShufV2, false, LibUtilities::eXYtoZ);

            // Looping on the pencils
236
            for(int i = 0 ; i < num_dfts_per_proc ; i++)
237
            {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
                // Copying the i-th pencil pf lenght N into a bigger
                // pencil of lenght 2N We are in Fourier space
                Vmath::Vcopy(N, &(ShufV1[i*N]), 1, &(ShufV1_PAD_coef[0]), 1);
                Vmath::Vcopy(N, &(ShufV2[i*N]), 1, &(ShufV2_PAD_coef[0]), 1);

                // Moving to physical space using the padded system
                m_FFT_deal->FFTBwdTrans(ShufV1_PAD_coef, ShufV1_PAD_phys);
                m_FFT_deal->FFTBwdTrans(ShufV2_PAD_coef, ShufV2_PAD_phys);

                // Perfroming the vectors multiplication in physical space on
                // the padded system
                Vmath::Vmul(m_padsize, ShufV1_PAD_phys,   1,
                                       ShufV2_PAD_phys,   1,
                                       ShufV1V2_PAD_phys, 1);

                // Moving back the result (V1*V2)_phys in Fourier space, padded
                // system
                m_FFT_deal->FFTFwdTrans(ShufV1V2_PAD_phys, ShufV1V2_PAD_coef);

                // Copying the first half of the padded pencil in the full
                // vector (Fourier space)
                Vmath::Vcopy(N, &(ShufV1V2_PAD_coef[0]), 1,
                                &(ShufV1V2[i*N]),        1);
261
            }
262 263 264 265 266 267

            m_transposition->Transpose(ShufV1V2, V1V2, false,
                                       LibUtilities::eZtoXY);

            // Moving the results in physical space for the output
            HomogeneousBwdTrans(V1V2, outarray, coeffstate);
268
        }
269

270 271 272 273
        /**
         * Forward transform
         */
        void ExpListHomogeneous1D::v_FwdTrans(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray, CoeffState coeffstate )
274 275 276
        {
            int cnt = 0, cnt1 = 0;
            Array<OneD, NekDouble> tmparray;
277
            
278
            for(int n = 0; n < m_planes.num_elements(); ++n)
279
            {
280
                m_planes[n]->FwdTrans(inarray+cnt, tmparray = outarray + cnt1,
281
                                      coeffstate);
282
                cnt   += m_planes[n]->GetTotPoints();
283 284
                
                cnt1  += m_planes[n]->GetNcoeffs(); // need to skip ncoeffs
285
            }
286
            if(!m_WaveSpace)
287 288 289
            {
                HomogeneousFwdTrans(outarray,outarray,coeffstate);
            }
290 291
        }

292 293 294
        /**
         * Forward transform element by element
         */
295 296 297 298
        void ExpListHomogeneous1D::v_FwdTrans_IterPerExp(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray)
        {
            int cnt = 0, cnt1 = 0;
            Array<OneD, NekDouble> tmparray;
299
            
300
            //spectral element FwdTrans plane by plane
301
            for(int n = 0; n < m_planes.num_elements(); ++n)
302 303 304 305 306
            {
                m_planes[n]->FwdTrans_IterPerExp(inarray+cnt, tmparray = outarray + cnt1);
                cnt   += m_planes[n]->GetTotPoints();
                cnt1  += m_planes[n]->GetNcoeffs();
            }
307 308 309 310
            if(!m_WaveSpace)
            {
                HomogeneousFwdTrans(outarray,outarray);
            }
311
        }
312 313 314 315 316
        
        /**
         * Backward transform
         */
        void ExpListHomogeneous1D::v_BwdTrans(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray, CoeffState coeffstate)
317 318 319
        {
            int cnt = 0, cnt1 = 0;
            Array<OneD, NekDouble> tmparray;
320
            
321
            for(int n = 0; n < m_planes.num_elements(); ++n)
322 323
            {
                m_planes[n]->BwdTrans(inarray+cnt, tmparray = outarray + cnt1,
324 325 326 327 328 329 330
                                      coeffstate);
                cnt  += m_planes[n]->GetNcoeffs();
                cnt1 += m_planes[n]->GetTotPoints();
            }
            if(!m_WaveSpace)
            {
                HomogeneousBwdTrans(outarray,outarray);
331 332
            }
        }
Yan Bao's avatar
Yan Bao committed
333
    
334 335 336 337
        /**
         * Backward transform element by element
         */
        void ExpListHomogeneous1D::v_BwdTrans_IterPerExp(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray)
338 339 340
        {
            int cnt = 0, cnt1 = 0;
            Array<OneD, NekDouble> tmparray;
341
            
342
            for(int n = 0; n < m_planes.num_elements(); ++n)
343 344
            {
                m_planes[n]->BwdTrans_IterPerExp(inarray+cnt, tmparray = outarray + cnt1);
345 346
                
                cnt    += m_planes[n]->GetNcoeffs();
347 348
                cnt1   += m_planes[n]->GetTotPoints();
            }
349 350 351 352
            if(!m_WaveSpace)
            {
                HomogeneousBwdTrans(outarray,outarray);
            }
353
        }
354 355 356 357 358
        
        /**
         * Inner product
         */
        void ExpListHomogeneous1D::v_IProductWRTBase(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray, CoeffState coeffstate)
359 360 361
        {
            int cnt = 0, cnt1 = 0;
            Array<OneD, NekDouble> tmparray;
362
            
363
            for(int n = 0; n < m_planes.num_elements(); ++n)
364
            {
365
                m_planes[n]->IProductWRTBase(inarray+cnt, tmparray = outarray + cnt1,coeffstate);
366

367
                cnt1    += m_planes[n]->GetNcoeffs();
368
                cnt   += m_planes[n]->GetTotPoints();
369 370
            }
        }
Yan Bao's avatar
Yan Bao committed
371
    
372 373 374 375
        /**
         * Inner product element by element
         */
        void ExpListHomogeneous1D::v_IProductWRTBase_IterPerExp(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray)
376
        { 
377 378
            int cnt = 0, cnt1 = 0;
            Array<OneD, NekDouble> tmparray;
379
            
380
            for(int n = 0; n < m_planes.num_elements(); ++n)
381 382
            {
                m_planes[n]->IProductWRTBase_IterPerExp(inarray+cnt, tmparray = outarray + cnt1);
Yan Bao's avatar
Yan Bao committed
383
        
384 385
                cnt1  += m_planes[n]->GetNcoeffs();
                cnt   += m_planes[n]->GetTotPoints();
386
            } 
387
        }
Yan Bao's avatar
Yan Bao committed
388
    
389 390 391 392 393 394 395 396
        /**
         * Homogeneous transform Bwd/Fwd (MVM and FFT)
         */
        void ExpListHomogeneous1D::Homogeneous1DTrans(const Array<OneD, const NekDouble> &inarray, Array<OneD, NekDouble> &outarray, 
                                                      bool IsForwards, 
                                                      CoeffState coeffstate,
                                                      bool Shuff,
                                                      bool UnShuff)
397
        {
398 399
            int num_dofs;
            
400
            if(IsForwards) 
401 402 403 404 405 406 407 408
            {
                num_dofs = inarray.num_elements();
            }
            else
            {
                num_dofs = outarray.num_elements();
            }
            
Alessandro Bolis's avatar
fftw  
Alessandro Bolis committed
409
            if(m_useFFT)
Yan Bao's avatar
Yan Bao committed
410
            {        
411
                int num_points_per_plane = num_dofs/m_planes.num_elements();
Yan Bao's avatar
Yan Bao committed
412
                int num_dfts_per_proc;
Yan Bao's avatar
Yan Bao committed
413 414 415 416 417 418 419 420 421 422 423 424
                if(!m_session->DefinesSolverInfo("HomoStrip"))
                {
                    int nP = m_comm->GetColumnComm()->GetSize();
                    num_dfts_per_proc = num_points_per_plane / nP
                                      + (num_points_per_plane % nP > 0);
                }
                else
                {
                    int nP = m_StripZcomm->GetSize();
                    num_dfts_per_proc = num_points_per_plane / nP
                                      + (num_points_per_plane % nP > 0);
                }
425

426
                Array<OneD, NekDouble> fft_in (num_dfts_per_proc*m_homogeneousBasis->GetNumPoints(),0.0);
427
                Array<OneD, NekDouble> fft_out(num_dfts_per_proc*m_homogeneousBasis->GetNumPoints(),0.0);
Yan Bao's avatar
Yan Bao committed
428
        
429 430 431 432 433 434
                if(Shuff)
                {
                    m_transposition->Transpose(inarray,fft_in,false,LibUtilities::eXYtoZ);
                }
                else 
                {
435 436
                    Vmath::Vcopy(num_dfts_per_proc*m_homogeneousBasis->GetNumPoints(),
                                 inarray,1,fft_in,1);
437 438
                }
                
439 440
                if(IsForwards)
                {
441
                    for(int i = 0 ; i < num_dfts_per_proc ; i++)
442
                    {
443
                        m_FFT->FFTFwdTrans(m_tmpIN = fft_in + i*m_homogeneousBasis->GetNumPoints(), m_tmpOUT = fft_out + i*m_homogeneousBasis->GetNumPoints());
444 445 446 447
                    }
                }
                else 
                {
448
                    for(int i = 0 ; i < num_dfts_per_proc ; i++)
449
                    {
450
                        m_FFT->FFTBwdTrans(m_tmpIN = fft_in + i*m_homogeneousBasis->GetNumPoints(), m_tmpOUT = fft_out + i*m_homogeneousBasis->GetNumPoints());
451 452
                    }
                }
Yan Bao's avatar
Yan Bao committed
453
        
454 455 456 457 458 459
                if(UnShuff)
                {
                    m_transposition->Transpose(fft_out,outarray,false,LibUtilities::eZtoXY);
                }
                else 
                {
460 461
                    Vmath::Vcopy(num_dfts_per_proc*m_homogeneousBasis->GetNumPoints(),
                                 fft_out,1,outarray,1);
462
                }
463 464 465 466
            }
            else 
            {
                DNekBlkMatSharedPtr blkmat;
Yan Bao's avatar
Yan Bao committed
467
        
468
                if(num_dofs == m_npoints) //transform phys space
469
                {
470 471 472 473 474 475 476 477
                    if(IsForwards)
                    {
                        blkmat = GetHomogeneous1DBlockMatrix(eForwardsPhysSpace1D);
                    }
                    else
                    {
                        blkmat = GetHomogeneous1DBlockMatrix(eBackwardsPhysSpace1D);
                    }
478
                }
479
                else
480
                {
481 482
                    if(IsForwards)
                    {
483
                        blkmat = GetHomogeneous1DBlockMatrix(eForwardsCoeffSpace1D,coeffstate);
484 485 486
                    }
                    else
                    {
487
                        blkmat = GetHomogeneous1DBlockMatrix(eBackwardsCoeffSpace1D,coeffstate);
488
                    }
489
                }
Yan Bao's avatar
Yan Bao committed
490
        
Alessandro Bolis's avatar
fftw  
Alessandro Bolis committed
491 492
                int nrows = blkmat->GetRows();
                int ncols = blkmat->GetColumns();
Yan Bao's avatar
Yan Bao committed
493
        
494 495
                Array<OneD, NekDouble> sortedinarray(ncols,0.0);
                Array<OneD, NekDouble> sortedoutarray(nrows,0.0);
Yan Bao's avatar
Yan Bao committed
496
        
497 498 499 500 501 502 503 504 505
                if(Shuff)
                {
                    m_transposition->Transpose(inarray,sortedinarray,!IsForwards,LibUtilities::eXYtoZ);
                }
                else 
                {
                    Vmath::Vcopy(ncols,inarray,1,sortedinarray,1);
                }
                
Alessandro Bolis's avatar
fftw  
Alessandro Bolis committed
506
                // Create NekVectors from the given data arrays
507
                NekVector<NekDouble> in (ncols,sortedinarray,eWrapper);
508
                NekVector<NekDouble> out(nrows,sortedoutarray,eWrapper);
Yan Bao's avatar
Yan Bao committed
509
        
Alessandro Bolis's avatar
fftw  
Alessandro Bolis committed
510 511
                // Perform matrix-vector multiply.
                out = (*blkmat)*in;
Yan Bao's avatar
Yan Bao committed
512
        
513 514 515 516 517 518
                if(UnShuff)
                {
                    m_transposition->Transpose(sortedoutarray,outarray,IsForwards,LibUtilities::eZtoXY);
                }
                else 
                {
Douglas Serson's avatar
Douglas Serson committed
519
                    Vmath::Vcopy(nrows,sortedoutarray,1,outarray,1);
520 521
                }
                
522
            }
523 524
        }

525
        DNekBlkMatSharedPtr ExpListHomogeneous1D::GetHomogeneous1DBlockMatrix(Homogeneous1DMatType mattype, CoeffState coeffstate) const
526 527
        {
            Homo1DBlockMatrixMap::iterator matrixIter = m_homogeneous1DBlockMat->find(mattype);
528
            
529 530
            if(matrixIter == m_homogeneous1DBlockMat->end())
            {
531
                return ((*m_homogeneous1DBlockMat)[mattype] =
532
                        GenHomogeneous1DBlockMatrix(mattype,coeffstate));
533 534 535 536 537 538 539
            }
            else
            {
                return matrixIter->second;
            }
        }

540

541
        DNekBlkMatSharedPtr ExpListHomogeneous1D::GenHomogeneous1DBlockMatrix(Homogeneous1DMatType mattype, CoeffState coeffstate) const
542
        {
543 544
            DNekMatSharedPtr    loc_mat;
            DNekBlkMatSharedPtr BlkMatrix;
545 546 547
            int n_exp = 0;
            int num_trans_per_proc = 0;
            
548 549
            if((mattype == eForwardsCoeffSpace1D)
               ||(mattype == eBackwardsCoeffSpace1D)) // will operate on m_coeffs
550
            {
551
                n_exp = m_planes[0]->GetNcoeffs();
552 553 554 555 556
            }
            else
            {
                n_exp = m_planes[0]->GetTotPoints(); // will operatore on m_phys
            }
557

558
            num_trans_per_proc = n_exp/m_comm->GetColumnComm()->GetSize() + (n_exp%m_comm->GetColumnComm()->GetSize() > 0);
559

560 561
            Array<OneD,unsigned int> nrows(num_trans_per_proc);
            Array<OneD,unsigned int> ncols(num_trans_per_proc);
562

563
            if((mattype == eForwardsCoeffSpace1D)||(mattype == eForwardsPhysSpace1D))
564
            {
565 566
                nrows = Array<OneD, unsigned int>(num_trans_per_proc,m_homogeneousBasis->GetNumModes());
                ncols = Array<OneD, unsigned int>(num_trans_per_proc,m_homogeneousBasis->GetNumPoints());
567 568 569
            }
            else
            {
570 571
                nrows = Array<OneD, unsigned int>(num_trans_per_proc,m_homogeneousBasis->GetNumPoints());
                ncols = Array<OneD, unsigned int>(num_trans_per_proc,m_homogeneousBasis->GetNumModes());
572 573 574 575 576
            }

            MatrixStorage blkmatStorage = eDIAGONAL;
            BlkMatrix = MemoryManager<DNekBlkMat>
                ::AllocateSharedPtr(nrows,ncols,blkmatStorage);
577

Yan Bao's avatar
Yan Bao committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
            //Half Mode
            if(m_homogeneousBasis->GetBasisType() == LibUtilities::eFourierHalfModeRe || m_homogeneousBasis->GetBasisType() == LibUtilities::eFourierHalfModeIm)
            {
                StdRegions::StdPointExp StdPoint(m_homogeneousBasis->GetBasisKey());
                
                if((mattype == eForwardsCoeffSpace1D)||(mattype == eForwardsPhysSpace1D))
                {
                    StdRegions::StdMatrixKey matkey(StdRegions::eFwdTrans,
                                                    StdPoint.DetShapeType(),
                                                    StdPoint);
                    
                    loc_mat = StdPoint.GetStdMatrix(matkey);
                }
                else
                {
                    StdRegions::StdMatrixKey matkey(StdRegions::eBwdTrans,
                                                    StdPoint.DetShapeType(),
                                                    StdPoint);
                    
                    loc_mat = StdPoint.GetStdMatrix(matkey);
                }
            }
            //other cases
            else 
            {
                StdRegions::StdSegExp StdSeg(m_homogeneousBasis->GetBasisKey());
                
                if((mattype == eForwardsCoeffSpace1D)||(mattype == eForwardsPhysSpace1D))
                {
                    StdRegions::StdMatrixKey matkey(StdRegions::eFwdTrans,
                                                    StdSeg.DetShapeType(),
                                                    StdSeg);
                    
                    loc_mat = StdSeg.GetStdMatrix(matkey);
                }
                else
                {
                    StdRegions::StdMatrixKey matkey(StdRegions::eBwdTrans,
                                                    StdSeg.DetShapeType(),
                                                    StdSeg);
                    
                    loc_mat = StdSeg.GetStdMatrix(matkey);
                }                
                
            }
623

624
            // set up array of block matrices.
625
            for(int i = 0; i < num_trans_per_proc; ++i)
626 627 628
            {
                BlkMatrix->SetBlock(i,i,loc_mat);
            }
629

630 631 632
            return BlkMatrix;
        }

633
        std::vector<LibUtilities::FieldDefinitionsSharedPtr> ExpListHomogeneous1D::v_GetFieldDefinitions()
634
        {
635
            std::vector<LibUtilities::FieldDefinitionsSharedPtr> returnval;
636
            
Yan Bao's avatar
Yan Bao committed
637
            // Set up Homogeneous length details.
638
            Array<OneD,LibUtilities::BasisSharedPtr> HomoBasis(1,m_homogeneousBasis);
639
            
640
            std::vector<NekDouble> HomoLen;
641
            HomoLen.push_back(m_lhom);
642
            
643 644
            std::vector<unsigned int> StripsIDs;

645 646 647 648 649 650
            bool strips;
            m_session->MatchSolverInfo("HomoStrip","True",strips,false);
            if (strips)
            {
                StripsIDs.push_back(m_transposition->GetStripID());
            }
651
            
652
            std::vector<unsigned int> PlanesIDs;
653 654 655
            int IDoffset = 0;

            // introduce a 2 plane offset for single mode case so can
656
            // be post-processed or used in MultiMode expansion.
657 658 659 660 661
            if(m_homogeneousBasis->GetBasisType() == LibUtilities::eFourierSingleMode)
            {
                IDoffset  = 2;
            }

662 663
            for(int i = 0; i < m_planes.num_elements(); i++)
            {
664
                PlanesIDs.push_back(m_transposition->GetPlaneID(i)+IDoffset);
665 666
            }
            
667 668
            m_planes[0]->GeneralGetFieldDefinitions(returnval, 1, HomoBasis, 
                    HomoLen, strips, StripsIDs, PlanesIDs);
669
            return returnval;
670 671
        }

672
        void  ExpListHomogeneous1D::v_GetFieldDefinitions(std::vector<LibUtilities::FieldDefinitionsSharedPtr> &fielddef)
673
        {
674
            // Set up Homogeneous length details.
675
            Array<OneD,LibUtilities::BasisSharedPtr> HomoBasis(1,m_homogeneousBasis);
676
            
677
            std::vector<NekDouble> HomoLen;
678
            HomoLen.push_back(m_lhom);
679
            
680 681
            std::vector<unsigned int> StripsIDs;

682 683 684 685 686 687
            bool strips;
            m_session->MatchSolverInfo("HomoStrip","True",strips,false);
            if (strips)
            {
                StripsIDs.push_back(m_transposition->GetStripID());
            }
688
            
689
            std::vector<unsigned int> PlanesIDs;
690 691 692 693 694 695 696
            int IDoffset = 0;

            if(m_homogeneousBasis->GetBasisType() == LibUtilities::eFourierSingleMode)
            {
                IDoffset = 2;
            }

697 698
            for(int i = 0; i < m_planes.num_elements(); i++)
            {
699
                PlanesIDs.push_back(m_transposition->GetPlaneID(i)+IDoffset);
700 701 702
            }
            
            // enforce NumHomoDir == 1 by direct call
703 704
            m_planes[0]->GeneralGetFieldDefinitions(fielddef, 1, HomoBasis,
                    HomoLen, strips, StripsIDs, PlanesIDs);
705 706 707
        }


708 709 710 711 712 713
        /** This routine appends the data from the expansion list into
            the output format where each element is given by looping
            over its Fourier modes where as data in the expandion is
            stored with all consecutive elements and then the Fourier
            modes
         */ 
714
        void ExpListHomogeneous1D::v_AppendFieldData(LibUtilities::FieldDefinitionsSharedPtr &fielddef, std::vector<NekDouble> &fielddata, Array<OneD, NekDouble> &coeffs)
715 716 717
        {
            int i,n;
            int ncoeffs_per_plane = m_planes[0]->GetNcoeffs();
718

719
            // Determine mapping from element ids to location in
720 721 722 723 724 725
            // expansion list
            map<int, int> ElmtID_to_ExpID;
            for(i = 0; i < m_planes[0]->GetExpSize(); ++i)
            {
                ElmtID_to_ExpID[(*m_exp)[i]->GetGeom()->GetGlobalID()] = i;
            }
726

727
            for(i = 0; i < fielddef->m_elementIDs.size(); ++i)
728
            {
729
                int eid     = ElmtID_to_ExpID[fielddef->m_elementIDs[i]];
730 731
                int datalen = (*m_exp)[eid]->GetNcoeffs();

732
                for(n = 0; n < m_planes.num_elements(); ++n)
733
                {
734
                    fielddata.insert(fielddata.end(),&coeffs[m_coeff_offset[eid]+n*ncoeffs_per_plane],&coeffs[m_coeff_offset[eid]+n*ncoeffs_per_plane]+datalen);
735 736 737
                }
            }
        }
Yan Bao's avatar
Yan Bao committed
738
        
739
        void ExpListHomogeneous1D::v_AppendFieldData(LibUtilities::FieldDefinitionsSharedPtr &fielddef, std::vector<NekDouble> &fielddata)
740 741 742
        {
           v_AppendFieldData(fielddef,fielddata,m_coeffs);
        }
743 744

        //Extract the data in fielddata into the m_coeff list
745
        void ExpListHomogeneous1D::v_ExtractDataToCoeffs(
746 747 748 749
            LibUtilities::FieldDefinitionsSharedPtr    &fielddef,
            std::vector<NekDouble>       &fielddata,
            std::string                  &field,
            Array<OneD, NekDouble>       &coeffs)
750 751
        {
            int i,n;
752 753
            int offset  = 0;
            int nzmodes = 1;
754
            int datalen = fielddata.size()/fielddef->m_fields.size();
755
            std::vector<unsigned int> fieldDefHomoZids;
756 757
            
            
758
            // Find data location according to field definition
759
            for(i = 0; i < fielddef->m_fields.size(); ++i)
760
            {
761
                if(fielddef->m_fields[i] == field)
762 763 764 765 766
                {
                    break;
                }
                offset += datalen;
            }
767

768 769 770
            if(i == fielddef->m_fields.size())
            {
                cout << "Field "<< field<< "not found in data file. "  << endl;
771 772
            }
            else
773
            {
774
                
775 776 777
                int modes_offset = 0;
                int planes_offset = 0;
                Array<OneD, NekDouble> coeff_tmp;
778
                std::map<int,int>::iterator it;
779
                
780 781 782 783
                // Build map of plane IDs lying on this processor.
                std::map<int,int> homoZids;
                for (i = 0; i < m_planes.num_elements(); ++i)
                {
784
                    homoZids[m_transposition->GetPlaneID(i)] = i;
785
                }
786
                
787
                if(fielddef->m_numHomogeneousDir)
788
                {
789
                    nzmodes = fielddef->m_homogeneousZIDs.size();
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
                    fieldDefHomoZids = fielddef->m_homogeneousZIDs;
                }
                else // input file is 2D and so set nzmodes to 1
                {
                    nzmodes = 1;
                    fieldDefHomoZids.push_back(0);
                }
                
                // Determine mapping from element ids to location in expansion list.
                map<int, int> ElmtID_to_ExpID;
                for(i = 0; i < m_planes[0]->GetExpSize(); ++i)
                {
                    ElmtID_to_ExpID[(*m_exp)[i]->GetGeom()->GetGlobalID()] = i;
                }
                

                // calculate number of modes in the current partition
                int ncoeffs_per_plane = m_planes[0]->GetNcoeffs(); 
                
                for(i = 0; i < fielddef->m_elementIDs.size(); ++i)
                {
811
                    if(fielddef->m_uniOrder == true) // reset modes_offset to zero
812
                    {
813
                        modes_offset = 0;
814 815
                    }
                    
816 817 818 819 820 821 822 823 824 825 826
                    int datalen = LibUtilities::GetNumberOfCoefficients(fielddef->m_shapeType, 
                                                                        fielddef->m_numModes,
                                                                        modes_offset);

                    it = ElmtID_to_ExpID.find(fielddef->m_elementIDs[i]); 
                    
                    // ensure element is on this partition for parallel case. 
                    if(it == ElmtID_to_ExpID.end())
                    {
                        // increase offset for correct FieldData access
                        offset += datalen*nzmodes;
827 828
                        modes_offset += (*m_exp)[0]->GetNumBases() +
                                        fielddef->m_numHomogeneousDir;
829 830 831 832 833 834
                        continue;
                    }
                    
                    int eid = it->second;
                        
                    
835
                    for(n = 0; n < nzmodes; ++n, offset += datalen)
836
                    {
837 838 839
                        
                        it = homoZids.find(fieldDefHomoZids[n]);
                        
840 841 842 843
                        // Check to make sure this mode number lies in this field.
                        if (it == homoZids.end())
                        {
                            continue;
844 845
                        } 
                        
846 847 848 849
                        planes_offset = it->second;
                        if(datalen == (*m_exp)[eid]->GetNcoeffs())
                        {
                            Vmath::Vcopy(datalen,&fielddata[offset],1,&coeffs[m_coeff_offset[eid]+planes_offset*ncoeffs_per_plane],1);
850
                        }
851 852
                        else // unpack data to new order
                        {
853
                            (*m_exp)[eid]->ExtractDataToCoeffs(&fielddata[offset], fielddef->m_numModes,modes_offset,&coeffs[m_coeff_offset[eid] + planes_offset*ncoeffs_per_plane]);
854
                        }
855
                    }
856
                    modes_offset += (*m_exp)[0]->GetNumBases() + fielddef->m_numHomogeneousDir;
857 858 859
                }
            }
        }
Yan Bao's avatar
Yan Bao committed
860
        
861 862 863
        //Extract the data in fielddata into the m_coeff list
        void ExpListHomogeneous1D::v_ExtractCoeffsToCoeffs(
                                                           const boost::shared_ptr<ExpList> &fromExpList,const  Array<OneD, const NekDouble> &fromCoeffs, Array<OneD, NekDouble> &toCoeffs)
864
        {
865
            int i;
866
            int fromNcoeffs_per_plane = fromExpList->GetPlane(0)->GetNcoeffs();
867
            int toNcoeffs_per_plane = m_planes[0]->GetNcoeffs();
868 869 870
            Array<OneD, NekDouble> tocoeffs_tmp, fromcoeffs_tmp; 
            
            for(i = 0; i < m_planes.num_elements(); ++i)
871
            {
872
                m_planes[i]->ExtractCoeffsToCoeffs(fromExpList->GetPlane(i),fromcoeffs_tmp =  fromCoeffs + fromNcoeffs_per_plane*i, tocoeffs_tmp = toCoeffs + toNcoeffs_per_plane*i);
873 874
            }
        }
875

876
        void ExpListHomogeneous1D::v_WriteVtkPieceData(std::ostream &outfile, int expansion,
877 878
                                        std::string var)
        {
879