LocProject_Diff2D.cpp 17.5 KB
Newer Older
Mike Kirby's avatar
Mike Kirby committed
1
#include <cstdlib>
2
#include <math.h>
Mike Kirby's avatar
Mike Kirby committed
3

4
5
6
7
#include <StdRegions/StdExpansion2D.h>
#include <LocalRegions/QuadExp.h>
#include <LocalRegions/TriExp.h>
#include <LocalRegions/NodalTriExp.h>
Mike Kirby's avatar
Mike Kirby committed
8

9
#include <LibUtilities/Foundations/Foundations.hpp>
10

Mike Kirby's avatar
Mike Kirby committed
11
12
using namespace Nektar;

13
NekDouble Tri_sol(NekDouble x, NekDouble y, int order1, int order2);
14
NekDouble Tri_Dsol(NekDouble x, NekDouble y, int order1, int order2);
15
NekDouble Quad_sol(NekDouble x, NekDouble y, int order1, int order2,
16
                   LibUtilities::BasisType btype1, LibUtilities::BasisType btype2);
17
NekDouble Quad_Dsol(NekDouble x, NekDouble y, int order1, int order2,
18
                    LibUtilities::BasisType btype1, LibUtilities::BasisType btype2);
Mike Kirby's avatar
Mike Kirby committed
19

20
// This routine projects a polynomial or trigonmetric functions which
Mike Kirby's avatar
Mike Kirby committed
21
22
// has energy in all mdoes of the expansions and reports and error

23
24
static double  pow_loc(const double val, const int i)
{
25
    return (i < 0)? 1.0: pow(val,i);
26
27
28
}


29
30
int main(int argc, char *argv[])
{
31

32
33
    int           i;

Dave Moxey's avatar
Dave Moxey committed
34
    int           order1,order2, nq1,nq2;
35
    LibUtilities::PointsType    Qtype1,Qtype2;
36
37
38
    LibUtilities::BasisType     btype1 =   LibUtilities::eOrtho_A;
    LibUtilities::BasisType     btype2 =   LibUtilities::eOrtho_B;
    LibUtilities::PointsType    NodalType = LibUtilities::eNodalTriElec;
39
    LibUtilities::ShapeType     regionshape;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    StdRegions::StdExpansion2D *E;
    Array<OneD, NekDouble> sol,x,y,dx,dy;
    Array<OneD, NekDouble> coords(8);
    StdRegions::Orientation edgeDir = StdRegions::eForwards;


    if((argc != 16)&&(argc != 14))
    {
        //       arg[0]    arg[1]   arg[2]  arg[3] arg[4] arg[5] arg[6] arg[7] arg[8] arg[9] arg[10] arg[11] arg[12] arg[13]
        fprintf(stderr,"Usage: Project2D RegionShape Type1 Type2 order1 order2  nq1    nq2     x1,    y1,      x2,     y2,     x3,    y3 \n");

        fprintf(stderr,"Example : ./LocProject2D-g 2 4 5 3 3 4 4 .1 -.5 .6 .1 .3 .2 \n" );

        fprintf(stderr,"Where RegionShape is an integer value which "
                "dictates the region shape:\n");
55
56
        fprintf(stderr,"\t Triangle      = 3\n");
        fprintf(stderr,"\t Quadrilateral = 4\n");
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

        fprintf(stderr,"Where type is an integer value which "
                "dictates the basis as:\n");

        fprintf(stderr,"\t Ortho_A    = 1\n");
        fprintf(stderr,"\t Ortho_B    = 2\n");
        fprintf(stderr,"\t Modified_A = 4\n");
        fprintf(stderr,"\t Modified_B = 5\n");
        fprintf(stderr,"\t Fourier    = 7\n");
        fprintf(stderr,"\t Lagrange   = 8\n");
        fprintf(stderr,"\t Legendre   = 9\n");
        fprintf(stderr,"\t Chebyshev  = 10\n");
        fprintf(stderr,"\t Nodal tri (Electro) = 11\n");
        fprintf(stderr,"\t Nodal tri (Fekete)  = 12\n");

        fprintf(stderr,"Note type = 3,6 are for three-dimensional basis\n");

        fprintf(stderr,"The last series of values are the coordinates\n");
        exit(1);
    }

78
    regionshape = (LibUtilities::ShapeType) atoi(argv[1]);
79
80

    // Check to see if 2D region
81
82
    if((regionshape != LibUtilities::eTriangle)&&
       (regionshape != LibUtilities::eQuadrilateral))
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    {
        NEKERROR(ErrorUtil::efatal,"This shape is not a 2D region");
    }

    int btype1_val = atoi(argv[2]);
    int btype2_val = atoi(argv[3]);

    if(( btype1_val <= 10)&&( btype2_val <= 10))
    {
        btype1 =   (LibUtilities::BasisType) btype1_val;
        btype2 =   (LibUtilities::BasisType) btype2_val;
    }
    else if(( btype1_val >=11)&&(btype2_val <= 12))
    {
        btype1 =   LibUtilities::eOrtho_A;
        btype2 =   LibUtilities::eOrtho_B;

        if(btype1_val == 11)
        {
            NodalType = LibUtilities::eNodalTriElec;
        }
        else
        {
            NodalType = LibUtilities::eNodalTriFekete;
        }

    }


    // Check to see that correct Expansions are used
    switch(regionshape)
    {
115
116
117
118
119
120
121
    case LibUtilities::eTriangle:
        if((btype1 == LibUtilities::eOrtho_B)||(btype1 == LibUtilities::eModified_B))
        {
            NEKERROR(ErrorUtil::efatal,
                     "Basis 1 cannot be of type Ortho_B or Modified_B");
        }
        
122
            break;
123
124
125
126
127
128
129
130
131
132
133
134
135
    case LibUtilities::eQuadrilateral:
        if((btype1 == LibUtilities::eOrtho_B)||(btype1 == LibUtilities::eOrtho_C)||
           (btype1 == LibUtilities::eModified_B)||(btype1 == LibUtilities::eModified_C))
        {
            NEKERROR(ErrorUtil::efatal,
                     "Basis 1 is for 2 or 3D expansions");
        }
        
        if((btype2 == LibUtilities::eOrtho_B)||(btype2 == LibUtilities::eOrtho_C)||
           (btype2 == LibUtilities::eModified_B)||(btype2 == LibUtilities::eModified_C))
        {
            NEKERROR(ErrorUtil::efatal,
                     "Basis 2 is for 2 or 3D expansions");
136
            }
137
138
139
140
        break;
    default:
        ASSERTL0(false, "Not a 2D expansion.");
        break;
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
    order1 =   atoi(argv[4]);
    order2 =   atoi(argv[5]);
    nq1    =   atoi(argv[6]);
    nq2    =   atoi(argv[7]);

    dx  = Array<OneD, NekDouble>(nq1*nq2);
    dy  = Array<OneD, NekDouble>(nq1*nq2);
    sol = Array<OneD, NekDouble>(nq1*nq2);
    x   = Array<OneD, NekDouble>(nq1*nq2);
    y   = Array<OneD, NekDouble>(nq1*nq2);

    if(btype1 != LibUtilities::eFourier)
    {
        Qtype1 = LibUtilities::eGaussLobattoLegendre;
    }
    else
    {
        Qtype1 = LibUtilities::eFourierEvenlySpaced;
    }

    if(btype2 != LibUtilities::eFourier)
    {
166
167
        if (regionshape == LibUtilities::eTriangle) 
        {
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
            Qtype2 = LibUtilities::eGaussRadauMAlpha1Beta0;
        }
        else
        {
            Qtype2 = LibUtilities::eGaussLobattoLegendre;
        }
    }
    else
    {
        Qtype2 = LibUtilities::eFourierEvenlySpaced;
    }

    //-----------------------------------------------
    // Define a 2D expansion based on basis definition

    switch(regionshape)
    {
185
    case LibUtilities::eTriangle:
186
        {
187
            
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
            coords[0]    =   atof(argv[8]);
            coords[1]    =   atof(argv[9]);
            coords[2]    =   atof(argv[10]);
            coords[3]    =   atof(argv[11]);
            coords[4]    =   atof(argv[12]);
            coords[5]    =   atof(argv[13]);

            // Set up coordinates
            SpatialDomains::VertexComponentSharedPtr verts[3];
            const int zero = 0;
            const int one=1;
            const int two=2;
            const double dZero = 0.0;
            verts[0] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,zero,coords[0],coords[1],dZero);
            verts[1] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,one,coords[2],coords[3],dZero);
            verts[2] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,two,coords[4],coords[5],dZero);

            // Set up Edges
            SpatialDomains::SegGeomSharedPtr edges[3];
            edges[0] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(zero,verts[0],verts[1]);
            edges[1] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(one,verts[1],verts[2]);
            edges[2] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(two,verts[2],verts[0]);

            StdRegions::Orientation eorient[3];
            eorient[0] = edgeDir;
            eorient[1] = edgeDir;
            eorient[2] = edgeDir;

            SpatialDomains::TriGeomSharedPtr geom = MemoryManager<SpatialDomains::TriGeom>::AllocateSharedPtr(zero,verts,edges,eorient);
            geom->SetOwnData();

            const LibUtilities::PointsKey Pkey1(nq1,Qtype1);
            const LibUtilities::PointsKey Pkey2(nq2,Qtype2);
            const LibUtilities::BasisKey  Bkey1(btype1,order1,Pkey1);
            const LibUtilities::BasisKey  Bkey2(btype2,order2,Pkey2);

            if(btype1_val >= 10)
            {
                E = new LocalRegions::NodalTriExp(Bkey1,Bkey2,NodalType,geom);
            }
            else
            {
                E = new LocalRegions::TriExp(Bkey1,Bkey2,geom);
            }

            E->GetCoords(x,y);

            //----------------------------------------------
            // Define solution to be projected
            for(i = 0; i < nq1*nq2; ++i)
            {
                sol[i]  = Tri_sol(x[i],y[i],order1,order2);
            }
            //----------------------------------------------

        }
        break;
245
        case LibUtilities::eQuadrilateral:
246
            {
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
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
                // Gather coordinates
                coords[0]    =   atof(argv[8]);
                coords[1]    =   atof(argv[9]);
                coords[2]    =   atof(argv[10]);
                coords[3]    =   atof(argv[11]);
                coords[4]    =   atof(argv[12]);
                coords[5]    =   atof(argv[13]);
                coords[6]    =   atof(argv[14]);
                coords[7]    =   atof(argv[15]);
                
                // Set up coordinates
                const int zero=0;
                const int one=1;
                const int two=2;
                const int three=3;
                const double dZero=0.0;
                SpatialDomains::VertexComponentSharedPtr verts[4];
                verts[0] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,zero,coords[0],coords[1],dZero);
                verts[1] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,one,coords[2],coords[3],dZero);
                verts[2] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,two,coords[4],coords[5],dZero);
                verts[3] = MemoryManager<SpatialDomains::VertexComponent>::AllocateSharedPtr(two,three,coords[6],coords[7],dZero);
                
                // Set up Edges
                SpatialDomains::SegGeomSharedPtr edges[4];
                edges[0] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(zero,verts[0],verts[1]);
                edges[1] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(one,verts[1],verts[2]);
                edges[2] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(two,verts[2],verts[3]);
                edges[3] = MemoryManager<SpatialDomains::SegGeom>::AllocateSharedPtr(three,verts[3],verts[0]);
                
                StdRegions::Orientation eorient[4];
                eorient[0] = edgeDir;
                eorient[1] = edgeDir;
                eorient[2] = edgeDir;
                eorient[3] = edgeDir;
                
                SpatialDomains::QuadGeomSharedPtr geom = MemoryManager<SpatialDomains::QuadGeom>::AllocateSharedPtr(zero,verts,edges,eorient);
                geom->SetOwnData();
                
                const LibUtilities::PointsKey Pkey1(nq1,Qtype1);
                const LibUtilities::PointsKey Pkey2(nq2,Qtype2);
                const LibUtilities::BasisKey  Bkey1(btype1,order1,Pkey1);
                const LibUtilities::BasisKey  Bkey2(btype2,order2,Pkey2);
                
                E = new LocalRegions::QuadExp(Bkey1,Bkey2,geom);
                
                //----------------------------------------------
                // Define solution to be projected
                E->GetCoords(x,y);
                
                for(i = 0; i < nq1*nq2; ++i)
                {
                    sol[i]  = Quad_sol(x[i],y[i],order1,order2,btype1,btype2);
                }
                //---------------------------------------------
301
302
            }
            break;
303
304
305
    default:
        ASSERTL0(false, "Not a 2D expansion.");
        break;
306
    }
307
308
    
    
309
310
311
312
313
    //--------------------------------------------
    // Take the numerical derivative of the solution and add together in sol
    E->PhysDeriv(sol,dx,dy);
    Vmath::Vadd(nq1*nq2,dx,1,dy,1,sol,1);
    //---------------------------------------------
314
    
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    //---------------------------------------------
    // Project onto Expansion
    E->FwdTrans(sol,E->UpdateCoeffs());
    //---------------------------------------------

    //-------------------------------------------
    // Backward Transform Solution to get projected values
    E->BwdTrans(E->GetCoeffs(),E->UpdatePhys());
    //-------------------------------------------

    //----------------------------------------------
    // Define exact solution of differential
    switch(regionshape)
    {
329
    case LibUtilities::eTriangle:
330
331
332
333
334
335
336
337
338
339
        {
            //----------------------------------------------
            // Define solution to be differentiated
            for(i = 0; i < nq1*nq2; ++i)
            {
                sol[i] = Tri_Dsol(x[i],y[i],order1,order2);
            }
            //----------------------------------------------
        }
        break;
340
    case LibUtilities::eQuadrilateral:
341
342
343
344
345
346
347
348
        {
            for(i = 0; i < nq1*nq2; ++i)
            {
                sol[i] = Quad_Dsol(x[i],y[i],order1,order2,btype1,btype2);
            }
        }
        //---------------------------------------------
        break;
349
350
351
    default:
        ASSERTL0(false, "Not a 2D expansion.");
        break;
352
    }
353
    
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    //--------------------------------------------
    // Write solution
    ofstream outfile("ProjectFile2D.dat");
    E->WriteToFile(outfile,eTecplot);
    outfile.close();
    //-------------------------------------------

    //--------------------------------------------
    // Calculate L_inf error
    cout << "L infinity error: " << E->Linf(sol) << endl;
    cout << "L 2 error:        " << E->L2  (sol) << endl;
    //--------------------------------------------

    return 0;
368
369
370
}


371
372
NekDouble Tri_sol(NekDouble x, NekDouble y, int order1, int order2)
{
373
374
    int    l,k;
    NekDouble sol = 0.0;
375

376
    for(k = 0; k < order1; ++k)
Mike Kirby's avatar
Mike Kirby committed
377
    {
378
379
380
381
        for(l = 0; l < order2-k; ++l)
        {
            sol += pow(x,k)*pow(y,l);
        }
Mike Kirby's avatar
Mike Kirby committed
382
    }
383

384
385
386
    return sol;
}

387
388
389
390
NekDouble Tri_Dsol(NekDouble x, NekDouble y, int order1, int order2)
{
    int    l,k;
    NekDouble sol = 0;
391

392
393
394
395
    for(k = 0; k < order1; ++k)
    {
        for(l = 0; l < order2-k; ++l)
        {
396
            sol +=  k*pow_loc(x,k-1)*pow_loc(y,l) +
397
398
399
                l*pow_loc(x,k)*pow_loc(y,l-1);
        }
    }
400

401
402
403
    return sol;
}

404
NekDouble Quad_sol(NekDouble x, NekDouble y, int order1, int order2,
405
406
                   LibUtilities::BasisType btype1,
                   LibUtilities::BasisType btype2)
407
{
408
409
    int k,l;
    NekDouble sol = 0.0;
410

411
    if(btype1 != LibUtilities::eFourier)
Mike Kirby's avatar
Mike Kirby committed
412
    {
413
414
        if(btype2 != LibUtilities::eFourier)
        {
415
416
417
418
419
420
421
            for(k = 0; k < order1; ++k)
            {
                for(l = 0; l < order2; ++l)
                {
                    sol += pow(x,k)*pow(y,l);
                }
            }
422
423
424
425
426
427
428
429
430
431
432
        }
        else
        {
            for(k = 0; k < order1; ++k)
            {
                for(l = 0; l < order2/2; ++l)
                {
                    sol += pow(x,k)*sin(M_PI*l*y) + pow(x,k)*cos(M_PI*l*y);
                }
            }
        }
Mike Kirby's avatar
Mike Kirby committed
433
434
435
    }
    else
    {
436
        if(btype2 != LibUtilities::eFourier)
437
        {
438
439
440
441
442
443
444
            for(k = 0; k < order1/2; ++k)
            {
                for(l = 0; l < order2; ++l)
                {
                    sol += sin(M_PI*k*x)*pow(y,l) + cos(M_PI*k*x)*pow(y,l);
                }
            }
445
        }
446
447
448
449
450
451
452
453
454
455
456
457
        else
        {
            for(k = 0; k < order1/2; ++k)
            {
                for(l = 0; l < order2/2; ++l)
                {
                    sol += sin(M_PI*k*x)*sin(M_PI*l*y)
                        + sin(M_PI*k*x)*cos(M_PI*l*y)
                        + cos(M_PI*k*x)*sin(M_PI*l*y)
                        + cos(M_PI*k*x)*cos(M_PI*l*y);
                }
            }
458
459
        }
    }
460

461
462
463
    return sol;
}

464
NekDouble Quad_Dsol(NekDouble x, NekDouble y, int order1, int order2,
465
466
467
                    LibUtilities::BasisType btype1,
                    LibUtilities::BasisType btype2)
{
468

469
470
    int k,l;
    NekDouble sol = 0;
471

472
    if(btype1 != LibUtilities::eFourier)
473
    {
474
        if(btype2 !=LibUtilities:: eFourier)
475
        {
476
477
478
479
            for(k = 0; k < order1; ++k)
            {
                for(l = 0; l < order2; ++l)
                {
480
                    sol +=  k*pow_loc(x,k-1)*pow_loc(y,l)
481
482
483
                        + l*pow_loc(x,k)*pow_loc(y,l-1);
                }
            }
484
        }
485
486
487
488
489
490
        else
        {
            for(k = 0; k < order1; ++k)
            {
                for(l = 0; l < order2/2; ++l)
                {
491
492
493
                    sol += k*pow_loc(x,k-1)*sin(M_PI*l*y)
                        + M_PI*l*pow_loc(x,k)*cos(M_PI*l*y) +
                        + k*pow_loc(x,k-1)*cos(M_PI*l*y)
494
495
496
                        - M_PI*l*pow_loc(x,k)*sin(M_PI*l*y);
                }
            }
497
498
499
500
        }
    }
    else
    {
501
        if(btype2 != LibUtilities::eFourier)
502
        {
503
504
505
506
            for(k = 0; k < order1/2; ++k)
            {
                for(l = 0; l < order2; ++l)
                {
507
508
509
                    sol += M_PI*k*cos(M_PI*k*x)*pow_loc(y,l)
                        + l*sin(M_PI*k*x)*pow_loc(y,l-1) +
                        - M_PI*k*sin(M_PI*k*x)*pow_loc(y,l)
510
511
512
                        + l*sin(M_PI*k*x)*pow_loc(y,l-1);
                }
            }
513
        }
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
        else
        {
            for(k = 0; k < order1/2; ++k)
            {
                for(l = 0; l < order2/2; ++l)
                {
                    sol += M_PI*k*cos(M_PI*k*x)*sin(M_PI*l*y)
                        + M_PI*l*sin(M_PI*k*x)*cos(M_PI*l*y)
                        + M_PI*k*cos(M_PI*k*x)*cos(M_PI*l*y)
                        + M_PI*l*sin(M_PI*k*x)*sin(M_PI*l*y)
                        - M_PI*k*sin(M_PI*k*x)*sin(M_PI*l*y)
                        + M_PI*l*cos(M_PI*k*x)*cos(M_PI*l*y)
                        - M_PI*k*sin(M_PI*k*x)*cos(M_PI*l*y)
                        - M_PI*l*cos(M_PI*k*x)*sin(M_PI*l*y);
                }
            }
530
531
        }
    }
532

533
    return sol;
Mike Kirby's avatar
Mike Kirby committed
534
}