interpol.cc 151 KB
Newer Older
Phillip Lino Rall's avatar
Phillip Lino Rall committed
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
/**********************************************************************************
 * Copyright 2010 Christoph Pflaum 
 * 		Department Informatik Lehrstuhl 10 - Systemsimulation
 *		Friedrich-Alexander Universität Erlangen-Nürnberg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **********************************************************************************/


#include "../mympi.h"
#include "../abbrevi.h"
#include "../parameter.h"
#include "../math_lib/math_lib.h"
#include "../basics/basic.h"
#include "../grid/elements.h"
#include "../grid/parti.h"
#include "../grid/ug.h"
Phillip Lino Rall's avatar
Phillip Lino Rall committed
28
#include "../grid/examples_ug.h"
Phillip Lino Rall's avatar
Phillip Lino Rall committed
29
30
31
32
33
#include "../grid/blockgrid.h"
#include "../grid/marker.h"
#include "../extemp/extemp.h"
#include "../extemp/parallel.h"
#include "../extemp/variable.h"
34
#include "../extemp/cellvar.h"
Phillip Lino Rall's avatar
Phillip Lino Rall committed
35
36
37
#include "../extemp/co_fu.h"
#include "../extemp/functor.h"
#include "interpol.h"
Christoph Pflaum's avatar
Christoph Pflaum committed
38
//#include "customtime.h"
39
#include <iomanip>
Phillip Lino Rall's avatar
Phillip Lino Rall committed
40
41
#include "assert.h"

42

Phillip Lino Rall's avatar
Phillip Lino Rall committed
43
44
45
46
47
48
49
50
51
52
53
54
55
/////////////////////////////////////////////////////////////
// 1. Interpolate from  blockgrid to rectangular blockgrid
/////////////////////////////////////////////////////////////


bool contained_in_tet(D3vector lam) {
  if(lam.x < -0.1)                 return false;
  if(lam.y < -0.1)                 return false;
  if(lam.z < -0.1)                 return false;
  if(lam.x + lam.y + lam.z > 1.1)  return false;
  return true;
}

56
bool contained_in_tet_strong(D3vector lam) {
57
58
59
60
61
    double limit = 0.2; // 0.2
  if(lam.x < -limit)                 return false;
  if(lam.y < -limit)                 return false;
  if(lam.z < -limit)                 return false;
  if(lam.x + lam.y + lam.z > 1+limit)  return false;
62
63
64
65
66
  return true;
}

bool new_lam_better(D3vector lam_old, D3vector lam_new) {

67
68
    if (MIN(lam_new ) < -0.2 || MAX(lam_new) > 1.2) return false;
    if (MIN(lam_old ) < -0.2 || MAX(lam_old) > 1.2) return true;
69

70
71
    if( (MIN(lam_new ) < 0 || MAX(lam_new) >1 )&&  (MIN(lam_old ) >= 0 || MAX(lam_old) <=1 )) return false;
    if (MIN(lam_new) > MIN(lam_old)) return true;
72
    if (MAX(lam_new) < MAX(lam_old)) return true;
73
    return false;
74
75
}

Phillip Lino Rall's avatar
Phillip Lino Rall committed
76
bool new_lam_worse(D3vector lam_old, D3vector lam_new) {
77
78
  if(MIN(lam_new) < MIN(lam_old) &&  MIN(lam_old) < -0.2) return true;
  if(MAX(lam_new) > MAX(lam_old) &&  MAX(lam_old) >  1.2) return true;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
79
80
81
  return false;
}

Phillip Lino Rall's avatar
Phillip Lino Rall committed
82
/*
Phillip Lino Rall's avatar
Phillip Lino Rall committed
83

Phillip Lino Rall's avatar
Phillip Lino Rall committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Intermadiate_grid_for_PointInterpolator::Intermadiate_grid_for_PointInterpolator(int nx_, int ny_, int nz_, Variable<double>* U_from)
{
    nx = nx_;
    ny = ny_;
    nz = nz_;
 
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
     
    Blockgrid* blockgrid_from = U_from->Give_blockgrid();
    
    //Variable<double> coordXYZ(*blockgrid);
    X_coordinate Xc(*blockgrid_from);
    Y_coordinate Yc(*blockgrid_from);
    Z_coordinate Zc(*blockgrid_from);
    pWSD.x = Minimum(Xc);    pWSD.y = Minimum(Yc);    pWSD.z = Minimum(Zc);
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);  
Phillip Lino Rall's avatar
Phillip Lino Rall committed
102

Phillip Lino Rall's avatar
Phillip Lino Rall committed
103
104
105
    
    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
    
Phillip Lino Rall's avatar
Phillip Lino Rall committed
106
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
107
*/
108
109
110
111
112
113
114
Interpolate_on_structured_grid::~Interpolate_on_structured_grid() {
  delete[] ids_hex;
  delete[] ids_i;
  delete[] ids_j;
  delete[] ids_k;

  delete[] typ_tet;
115
    delete[] lambda;
116
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
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
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
245
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
void Interpolate_on_structured_grid::update_Interpolate_on_structured_grid(Blockgrid &blockgrid_)
{
        int Nx, Ny, Nz;
        int typ;

        int ilmin, jlmin, klmin;
        int ilmax, jlmax, klmax;

        double factor = 0.1;
        //  double factor = 0.00001;

        D3vector lam;



          //Variable<double> coordXYZ(*blockgrid);
          X_coordinate Xc(blockgrid_);
          Y_coordinate Yc(blockgrid_);
          Z_coordinate Zc(blockgrid_);
          //D3vector pWSD, pENT;
          pWSD.x = Minimum(Xc);    pWSD.y = Minimum(Yc);    pWSD.z = Minimum(Zc);
          pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);

        blockgrid = &blockgrid_;
        ug = blockgrid->Give_unstructured_grid();



        if(nx>1)
          hx = (pENT.x - pWSD.x) / (nx-1);
        else
          hx = 1.0;
        if(ny>1)
          hy = (pENT.y - pWSD.y) / (ny-1);
        else
          hy = 1.0;
        if(nz>1)
          hz = (pENT.z - pWSD.z) / (nz-1);
        else
          hz = 1.0;

        int num_total = nx * ny * nz;

        D3vector cWSD, cESD;
        D3vector cWND, cEND;

        D3vector cWST, cEST;
        D3vector cWNT, cENT;

        D3vector boxWSD, boxENT;

        D3vector ploc;

//        ids_hex = new int[num_total];

//        ids_i = new int[num_total];
//        ids_j = new int[num_total];
//        ids_k = new int[num_total];

//        typ_tet = new int[num_total];

//        lambda = new D3vector[num_total];

        for(int i=0;i<num_total;++i) ids_hex[i] = -1;

        for(int id_hex=0;id_hex<ug->Give_number_hexahedra();++id_hex) {
            Nx = blockgrid->Give_Nx_hexahedron(id_hex);
            Ny = blockgrid->Give_Ny_hexahedron(id_hex);
            Nz = blockgrid->Give_Nz_hexahedron(id_hex);

            for(int k=0;k<Nz;++k)
          for(int j=0;j<Ny;++j)
            for(int i=0;i<Nx;++i) {
                  // corner points of general hex-cell
              cWSD = blockgrid->Give_coord_hexahedron(id_hex,i,  j,  k  );
              cESD = blockgrid->Give_coord_hexahedron(id_hex,i+1,j  ,k  );
              cWND = blockgrid->Give_coord_hexahedron(id_hex,i,  j+1,k  );
              cEND = blockgrid->Give_coord_hexahedron(id_hex,i+1,j+1,k  );

              cWST = blockgrid->Give_coord_hexahedron(id_hex,i,  j,  k+1);
              cEST = blockgrid->Give_coord_hexahedron(id_hex,i+1,j  ,k+1);
              cWNT = blockgrid->Give_coord_hexahedron(id_hex,i,  j+1,k+1);
              cENT = blockgrid->Give_coord_hexahedron(id_hex,i+1,j+1,k+1);

                  // bounding box calculation
              boxWSD.x = MIN(MIN(MIN(cWSD.x,cESD.x),MIN(cWND.x,cEND.x)),
                     MIN(MIN(cWST.x,cEST.x),MIN(cWNT.x,cENT.x))) - factor *hx;
              boxWSD.y = MIN(MIN(MIN(cWSD.y,cESD.y),MIN(cWND.y,cEND.y)),
                     MIN(MIN(cWST.y,cEST.y),MIN(cWNT.y,cENT.y))) - factor *hy;
              boxWSD.z = MIN(MIN(MIN(cWSD.z,cESD.z),MIN(cWND.z,cEND.z)),
                     MIN(MIN(cWST.z,cEST.z),MIN(cWNT.z,cENT.z))) - factor *hz;

              boxENT.x = MAX(MAX(MAX(cWSD.x,cESD.x),MAX(cWND.x,cEND.x)),
                     MAX(MAX(cWST.x,cEST.x),MAX(cWNT.x,cENT.x))) + factor *hx;
              boxENT.y = MAX(MAX(MAX(cWSD.y,cESD.y),MAX(cWND.y,cEND.y)),
                     MAX(MAX(cWST.y,cEST.y),MAX(cWNT.y,cENT.y))) + factor *hy;
              boxENT.z = MAX(MAX(MAX(cWSD.z,cESD.z),MAX(cWND.z,cEND.z)),
                     MAX(MAX(cWST.z,cEST.z),MAX(cWNT.z,cENT.z))) + factor *hz;

              // calculation of indices of a collection of cells of structured grid which contains bounding box
              ilmin = Ganzzahliger_Anteil((boxWSD.x - pWSD.x) / hx);
              jlmin = Ganzzahliger_Anteil((boxWSD.y - pWSD.y) / hy);
              klmin = Ganzzahliger_Anteil((boxWSD.z - pWSD.z) / hz);


              ilmax = Ganzzahliger_Anteil((boxENT.x - pWSD.x) / hx);
              jlmax = Ganzzahliger_Anteil((boxENT.y - pWSD.y) / hy);
              klmax = Ganzzahliger_Anteil((boxENT.z - pWSD.z) / hz);

              /*
              cout << " indices: "
               << " ilmin: " << ilmin
               << " jlmin: " << jlmin
               << " klmin: " << klmin
               << " ilmax: " << ilmax
               << " jlmax: " << jlmax
               << " klmax: " << klmax
               << " boxWSD.x: " << boxWSD.x
               << " cWSD.x: " << cWSD.x
               << " Nx: " <<  Nx
               << endl;
              */
              /*
      bool now;
      if(boxWSD.z < 0 && boxENT.z > 0.0 && boxWSD.y < 0.5 && boxENT.y > 0.5 && boxWSD.x < 1.0 && boxENT.x > 1.0 ) {
        cout << "\n \n WSD   : ";  boxWSD.Print();
        cout << "\n ENT  : ";  boxENT.Print();
        now = true;
       }
       else now = false;


       cout << " tt: " << boxWSD.x << " " << pWSD.x << " " << hx << endl;
       cout << (boxWSD.x - pWSD.x) << endl;

       cout << " z: " << 0.1 << " g: " << Ganzzahliger_Anteil(0.1) << endl;
       cout << " z: " << -0.1 << " g: " << Ganzzahliger_Anteil(-0.1) << endl;

       cout << " z: " << 5.1 << " g: " << Ganzzahliger_Anteil(5.1) << endl;
       cout << " z: " << -5.1 << " g: " << Ganzzahliger_Anteil(-5.1) << endl;
              */

              if(ilmin<0) ilmin=0;
              if(jlmin<0) jlmin=0;
              if(klmin<0) klmin=0;

              for(int il = ilmin; (il <= ilmax) && (il < nx);++il)
                for(int jl = jlmin; (jl <= jlmax) && (jl < ny);++jl)
              for(int kl = klmin; (kl <= klmax) && (kl < nz);++kl) {

                ploc = D3vector(il * hx, jl * hy, kl * hz) + pWSD;

                //		  cout << "HI" << endl;

                typ = -1;

                lam = lambda_of_p_in_tet(ploc,cWND,cWNT,cWST,cEST);
                if(contained_in_tet(lam)) typ=0;
                else {
                  lam = lambda_of_p_in_tet(ploc,cEST,cWND,cWST,cESD);
                  if(contained_in_tet(lam)) typ=1;
                  else {
                    lam = lambda_of_p_in_tet(ploc,cWND,cWSD,cWST,cESD);
                    if(contained_in_tet(lam)) typ=2;
                    else {
                  lam = lambda_of_p_in_tet(ploc,cEST,cWND,cESD,cEND);
                  if(contained_in_tet(lam)) typ=3;
                  else {
                    lam = lambda_of_p_in_tet(ploc,cENT,cWNT,cEST,cEND);
                    if(contained_in_tet(lam)) typ=4;
                    else {
                      lam = lambda_of_p_in_tet(ploc,cWNT,cWND,cEST,cEND);
                      if(contained_in_tet(lam)) typ=5;
                    }
                  }
                    }
                  }
                }

                /*
                cout << " typ " << typ << id_hex
                     << " il: " << il
                     << " jl: " << jl
                     << " kl: " << kl
                     << endl;
                */

                if(typ!=-1) {
                  int ind_global;
                  ind_global = il+nx*(jl+ny*kl);
                  bool stop;
                  stop=false;

                  if(ids_hex[ind_global]!=-1) {
                    stop=new_lam_worse(lambda[ind_global],lam);
                  }

                  if(stop==false) {
                    ids_hex[ind_global] = id_hex;
                    ids_i[ind_global] = i;
                    ids_j[ind_global] = j;
                    ids_k[ind_global] = k;

                    typ_tet[ind_global] = typ;

                    lambda[ind_global] = lam;
                  }
                  //go_on = false;
                }


              }
            }
        }


        for(int i=0;i<num_total;++i) {
          if(ids_hex[i]==-1) {
            // wir nehmen default value!!
            /*
            cout << i
             << " Error: Interpolate_on_structured_grid: I cannot interpolate all data!"
             << endl;
            ids_hex[i] = 0;
            */
          }
          else {
            //cout << i << " Interpolate_on_structured_grid: o.k.!" << endl;
          }
        }
      }

Phillip Lino Rall's avatar
Phillip Lino Rall committed
350
Interpolate_on_structured_grid::Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
351
352
                                   D3vector pWSD, D3vector pENT,
                                   Blockgrid& blockgrid_) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
353
  int Nx, Ny, Nz;
354
  // int typ;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
355
356
357
358
359

  assert(nx_ > 1);
  assert(ny_ > 1);
  assert(nz_ > 1);

360
361
  //int ilmin, jlmin, klmin;
  //int ilmax, jlmax, klmax;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
362
363
364
365

  double factor = 0.1;
  //  double factor = 0.00001;

366
  // D3vector lam;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
367
368
369
370
371
372
373
374
375
376

  blockgrid = &blockgrid_;
  ug = blockgrid->Give_unstructured_grid();

  nx = nx_;
  ny = ny_;
  nz = nz_;

  if(nx_>1)
    hx = (pENT.x - pWSD.x) / (nx_-1);
377
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
378
379
380
    hx = 1.0;
  if(ny_>1)
    hy = (pENT.y - pWSD.y) / (ny_-1);
381
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
382
383
384
    hy = 1.0;
  if(nz_>1)
    hz = (pENT.z - pWSD.z) / (nz_-1);
385
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
386
387
388
389
    hz = 1.0;

  int num_total = nx * ny * nz;

390
  /*
Phillip Lino Rall's avatar
Phillip Lino Rall committed
391
392
393
394
395
  D3vector cWSD, cESD;
  D3vector cWND, cEND;

  D3vector cWST, cEST;
  D3vector cWNT, cENT;
396
*/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
397

398
  // D3vector boxWSD, boxENT;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
399

400
  // D3vector ploc;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

  ids_hex = new int[num_total];

  ids_i = new int[num_total];
  ids_j = new int[num_total];
  ids_k = new int[num_total];

  typ_tet = new int[num_total];

  lambda = new D3vector[num_total];

  for(int i=0;i<num_total;++i) ids_hex[i] = -1;

  for(int id_hex=0;id_hex<ug->Give_number_hexahedra();++id_hex) {
      Nx = blockgrid->Give_Nx_hexahedron(id_hex);
      Ny = blockgrid->Give_Ny_hexahedron(id_hex);
      Nz = blockgrid->Give_Nz_hexahedron(id_hex);

419
#pragma omp parallel for num_threads(UGBlocks::numThreadsToTake) if(UGBlocks::useOpenMP)
Phillip Lino Rall's avatar
Phillip Lino Rall committed
420
      for(int k=0;k<Nz;++k)
421
422
    for(int j=0;j<Ny;++j)
      for(int i=0;i<Nx;++i) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
423
            // corner points of general hex-cell
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
        D3vector cWSD = blockgrid->Give_coord_hexahedron(id_hex,i,  j,  k  );
        D3vector cESD = blockgrid->Give_coord_hexahedron(id_hex,i+1,j  ,k  );
        D3vector cWND = blockgrid->Give_coord_hexahedron(id_hex,i,  j+1,k  );
        D3vector cEND = blockgrid->Give_coord_hexahedron(id_hex,i+1,j+1,k  );

        D3vector cWST = blockgrid->Give_coord_hexahedron(id_hex,i,  j,  k+1);
        D3vector cEST = blockgrid->Give_coord_hexahedron(id_hex,i+1,j  ,k+1);
        D3vector cWNT = blockgrid->Give_coord_hexahedron(id_hex,i,  j+1,k+1);
        D3vector cENT = blockgrid->Give_coord_hexahedron(id_hex,i+1,j+1,k+1);

            // bounding box calculation
        D3vector boxWSD, boxENT;
        boxWSD.x = MIN(MIN(MIN(cWSD.x,cESD.x),MIN(cWND.x,cEND.x)),
               MIN(MIN(cWST.x,cEST.x),MIN(cWNT.x,cENT.x))) - factor *hx;
        boxWSD.y = MIN(MIN(MIN(cWSD.y,cESD.y),MIN(cWND.y,cEND.y)),
               MIN(MIN(cWST.y,cEST.y),MIN(cWNT.y,cENT.y))) - factor *hy;
        boxWSD.z = MIN(MIN(MIN(cWSD.z,cESD.z),MIN(cWND.z,cEND.z)),
               MIN(MIN(cWST.z,cEST.z),MIN(cWNT.z,cENT.z))) - factor *hz;

        boxENT.x = MAX(MAX(MAX(cWSD.x,cESD.x),MAX(cWND.x,cEND.x)),
               MAX(MAX(cWST.x,cEST.x),MAX(cWNT.x,cENT.x))) + factor *hx;
        boxENT.y = MAX(MAX(MAX(cWSD.y,cESD.y),MAX(cWND.y,cEND.y)),
               MAX(MAX(cWST.y,cEST.y),MAX(cWNT.y,cENT.y))) + factor *hy;
        boxENT.z = MAX(MAX(MAX(cWSD.z,cESD.z),MAX(cWND.z,cEND.z)),
               MAX(MAX(cWST.z,cEST.z),MAX(cWNT.z,cENT.z))) + factor *hz;

        // calculation of indices of a collection of cells of structured grid which contains bounding box
        int ilmin = Ganzzahliger_Anteil((boxWSD.x - pWSD.x) / hx);
        int jlmin = Ganzzahliger_Anteil((boxWSD.y - pWSD.y) / hy);
        int klmin = Ganzzahliger_Anteil((boxWSD.z - pWSD.z) / hz);


        int ilmax = Ganzzahliger_Anteil((boxENT.x - pWSD.x) / hx);
        int jlmax = Ganzzahliger_Anteil((boxENT.y - pWSD.y) / hy);
        int klmax = Ganzzahliger_Anteil((boxENT.z - pWSD.z) / hz);

        /*
        cout << " indices: "
         << " ilmin: " << ilmin
         << " jlmin: " << jlmin
         << " klmin: " << klmin
         << " ilmax: " << ilmax
         << " jlmax: " << jlmax
         << " klmax: " << klmax
         << " boxWSD.x: " << boxWSD.x
         << " cWSD.x: " << cWSD.x
         << " Nx: " <<  Nx
         << endl;
        */
        /*
Phillip Lino Rall's avatar
Phillip Lino Rall committed
474
475
476
477
478
479
480
481
bool now;
if(boxWSD.z < 0 && boxENT.z > 0.0 && boxWSD.y < 0.5 && boxENT.y > 0.5 && boxWSD.x < 1.0 && boxENT.x > 1.0 ) {
  cout << "\n \n WSD   : ";  boxWSD.Print();
  cout << "\n ENT  : ";  boxENT.Print();
  now = true;
 }
 else now = false;

482

Phillip Lino Rall's avatar
Phillip Lino Rall committed
483
484
485
486
487
488
489
490
 cout << " tt: " << boxWSD.x << " " << pWSD.x << " " << hx << endl;
 cout << (boxWSD.x - pWSD.x) << endl;

 cout << " z: " << 0.1 << " g: " << Ganzzahliger_Anteil(0.1) << endl;
 cout << " z: " << -0.1 << " g: " << Ganzzahliger_Anteil(-0.1) << endl;

 cout << " z: " << 5.1 << " g: " << Ganzzahliger_Anteil(5.1) << endl;
 cout << " z: " << -5.1 << " g: " << Ganzzahliger_Anteil(-5.1) << endl;
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
        */

        if(ilmin<0) ilmin=0;
        if(jlmin<0) jlmin=0;
        if(klmin<0) klmin=0;

        for(int il = ilmin; (il <= ilmax) && (il < nx_);++il)
          for(int jl = jlmin; (jl <= jlmax) && (jl < ny_);++jl)
        for(int kl = klmin; (kl <= klmax) && (kl < nz_);++kl) {

          D3vector ploc = D3vector(il * hx, jl * hy, kl * hz) + pWSD;

          //		  cout << "HI" << endl;

          int typ = -1;

          D3vector lam = lambda_of_p_in_tet(ploc,cWND,cWNT,cWST,cEST);
          if(contained_in_tet(lam)) typ=0;
          else {
            lam = lambda_of_p_in_tet(ploc,cEST,cWND,cWST,cESD);
            if(contained_in_tet(lam)) typ=1;
            else {
              lam = lambda_of_p_in_tet(ploc,cWND,cWSD,cWST,cESD);
              if(contained_in_tet(lam)) typ=2;
              else {
            lam = lambda_of_p_in_tet(ploc,cEST,cWND,cESD,cEND);
            if(contained_in_tet(lam)) typ=3;
            else {
              lam = lambda_of_p_in_tet(ploc,cENT,cWNT,cEST,cEND);
              if(contained_in_tet(lam)) typ=4;
              else {
                lam = lambda_of_p_in_tet(ploc,cWNT,cWND,cEST,cEND);
                if(contained_in_tet(lam)) typ=5;
              }
            }
              }
            }
          }

          /*
          cout << " typ " << typ << id_hex
               << " il: " << il
               << " jl: " << jl
               << " kl: " << kl
               << endl;
          */

          if(typ!=-1) {
            int ind_global;
            ind_global = il+nx*(jl+ny*kl);
            bool stop;
            stop=false;

            if(ids_hex[ind_global]!=-1) {
              stop=new_lam_worse(lambda[ind_global],lam);
            }

            #pragma omp critical
            if(stop==false) {
              ids_hex[ind_global] = id_hex;
              ids_i[ind_global] = i;
              ids_j[ind_global] = j;
              ids_k[ind_global] = k;

              typ_tet[ind_global] = typ;

              lambda[ind_global] = lam;
            }
            //go_on = false;
          }

          /*
          cout << " out "
               << " ilmin: " << ilmin
               << " ilmax: " << ilmax
               << " jlmin: " << jlmin
               << " jlmax: " << jlmax
               << " klmin: " << klmin
               << " klmax: " << klmax;
          cout << "\n   "; cWSD.Print();
          cout << "\n   "; cESD.Print();
          cout << "\n   "; cWND.Print();
          cout << "\n   "; cEND.Print();
          cout << "\n   "; cWST.Print();
          cout << "\n   "; cEST.Print();
          cout << "\n   "; cWNT.Print();
          cout << "\n   "; cENT.Print();
          cout << "\n   p: "; ploc.Print();

          cout << "\n   : ";  boxWSD.Print();
          cout << "\n   : ";  boxENT.Print();

          cout << endl;
          */
        }
      }
Phillip Lino Rall's avatar
Phillip Lino Rall committed
587
588
589
590
591
592
593
  }


  for(int i=0;i<num_total;++i) {
    if(ids_hex[i]==-1) {
      // wir nehmen default value!!
      /*
594
595
596
      cout << i
       << " Error: Interpolate_on_structured_grid: I cannot interpolate all data!"
       << endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
597
598
599
600
601
602
603
604
605
      ids_hex[i] = 0;
      */
    }
    else {
      //cout << i << " Interpolate_on_structured_grid: o.k.!" << endl;
    }
  }
}

606
607
608
609
610
/////////////////////////////////////////////////////////////
// 2. Interpolate from  blockgrid  to  blockgrid
/////////////////////////////////////////////////////////////


Phillip Lino Rall's avatar
Phillip Lino Rall committed
611
612
613
614
615
616
Interpolate_on_structured_grid::Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
							       Blockgrid& blockgrid_) {
  int Nx, Ny, Nz;
  int typ;

  
Phillip Lino Rall's avatar
Phillip Lino Rall committed
617
  
Phillip Lino Rall's avatar
Phillip Lino Rall committed
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
  
  assert(nx_ > 1);
  assert(ny_ > 1);
  assert(nz_ > 1);

  int ilmin, jlmin, klmin;
  int ilmax, jlmax, klmax;

  double factor = 0.1;
  //  double factor = 0.00001;

  D3vector lam;

     
    
    //Variable<double> coordXYZ(*blockgrid);
    X_coordinate Xc(blockgrid_);
    Y_coordinate Yc(blockgrid_);
    Z_coordinate Zc(blockgrid_);
    //D3vector pWSD, pENT;
    pWSD.x = Minimum(Xc);    pWSD.y = Minimum(Yc);    pWSD.z = Minimum(Zc);
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);  
  
  blockgrid = &blockgrid_;
  ug = blockgrid->Give_unstructured_grid();

  nx = nx_;
  ny = ny_;
  nz = nz_;

  if(nx_>1)
    hx = (pENT.x - pWSD.x) / (nx_-1);
  else  
    hx = 1.0;
  if(ny_>1)
    hy = (pENT.y - pWSD.y) / (ny_-1);
  else  
    hy = 1.0;
  if(nz_>1)
    hz = (pENT.z - pWSD.z) / (nz_-1);
  else 
    hz = 1.0;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
660

Phillip Lino Rall's avatar
Phillip Lino Rall committed
661
662
663
664
665
666
667
668
669
670
671
  int num_total = nx * ny * nz;

  D3vector cWSD, cESD;
  D3vector cWND, cEND;

  D3vector cWST, cEST;
  D3vector cWNT, cENT;

  D3vector boxWSD, boxENT;

  D3vector ploc;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688

  ids_hex = new int[num_total];

  ids_i = new int[num_total];
  ids_j = new int[num_total];
  ids_k = new int[num_total];

  typ_tet = new int[num_total];

  lambda = new D3vector[num_total];

  for(int i=0;i<num_total;++i) ids_hex[i] = -1;

  for(int id_hex=0;id_hex<ug->Give_number_hexahedra();++id_hex) {
      Nx = blockgrid->Give_Nx_hexahedron(id_hex);
      Ny = blockgrid->Give_Ny_hexahedron(id_hex);
      Nz = blockgrid->Give_Nz_hexahedron(id_hex);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
689

Phillip Lino Rall's avatar
Phillip Lino Rall committed
690
691
692
693
      for(int k=0;k<Nz;++k)
	for(int j=0;j<Ny;++j)
	  for(int i=0;i<Nx;++i) {
            // corner points of general hex-cell
Phillip Lino Rall's avatar
Phillip Lino Rall committed
694
695
696
697
	    cWSD = blockgrid->Give_coord_hexahedron(id_hex,i,  j,  k  );
	    cESD = blockgrid->Give_coord_hexahedron(id_hex,i+1,j  ,k  );
	    cWND = blockgrid->Give_coord_hexahedron(id_hex,i,  j+1,k  );
	    cEND = blockgrid->Give_coord_hexahedron(id_hex,i+1,j+1,k  );
Phillip Lino Rall's avatar
Phillip Lino Rall committed
698

Phillip Lino Rall's avatar
Phillip Lino Rall committed
699
700
701
702
	    cWST = blockgrid->Give_coord_hexahedron(id_hex,i,  j,  k+1);
	    cEST = blockgrid->Give_coord_hexahedron(id_hex,i+1,j  ,k+1);
	    cWNT = blockgrid->Give_coord_hexahedron(id_hex,i,  j+1,k+1);
	    cENT = blockgrid->Give_coord_hexahedron(id_hex,i+1,j+1,k+1);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719

            // bounding box calculation 
	    boxWSD.x = MIN(MIN(MIN(cWSD.x,cESD.x),MIN(cWND.x,cEND.x)),
			   MIN(MIN(cWST.x,cEST.x),MIN(cWNT.x,cENT.x))) - factor *hx;
	    boxWSD.y = MIN(MIN(MIN(cWSD.y,cESD.y),MIN(cWND.y,cEND.y)),
			   MIN(MIN(cWST.y,cEST.y),MIN(cWNT.y,cENT.y))) - factor *hy;
	    boxWSD.z = MIN(MIN(MIN(cWSD.z,cESD.z),MIN(cWND.z,cEND.z)),
			   MIN(MIN(cWST.z,cEST.z),MIN(cWNT.z,cENT.z))) - factor *hz;

	    boxENT.x = MAX(MAX(MAX(cWSD.x,cESD.x),MAX(cWND.x,cEND.x)),
			   MAX(MAX(cWST.x,cEST.x),MAX(cWNT.x,cENT.x))) + factor *hx;
	    boxENT.y = MAX(MAX(MAX(cWSD.y,cESD.y),MAX(cWND.y,cEND.y)),
			   MAX(MAX(cWST.y,cEST.y),MAX(cWNT.y,cENT.y))) + factor *hy;
	    boxENT.z = MAX(MAX(MAX(cWSD.z,cESD.z),MAX(cWND.z,cEND.z)),
			   MAX(MAX(cWST.z,cEST.z),MAX(cWNT.z,cENT.z))) + factor *hz;

	    // calculation of indices of a collection of cells of structured grid which contains bounding box
Phillip Lino Rall's avatar
Phillip Lino Rall committed
720
721
722
	    ilmin = Ganzzahliger_Anteil((boxWSD.x - pWSD.x) / hx);
	    jlmin = Ganzzahliger_Anteil((boxWSD.y - pWSD.y) / hy);
	    klmin = Ganzzahliger_Anteil((boxWSD.z - pWSD.z) / hz);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
723
724
	    

Phillip Lino Rall's avatar
Phillip Lino Rall committed
725
726
727
	    ilmax = Ganzzahliger_Anteil((boxENT.x - pWSD.x) / hx);
	    jlmax = Ganzzahliger_Anteil((boxENT.y - pWSD.y) / hy);
	    klmax = Ganzzahliger_Anteil((boxENT.z - pWSD.z) / hz);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
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
768
769

	    /*
	    cout << " indices: "
		 << " ilmin: " << ilmin 
		 << " jlmin: " << jlmin 
		 << " klmin: " << klmin 
		 << " ilmax: " << ilmax 
		 << " jlmax: " << jlmax 
		 << " klmax: " << klmax 
		 << " boxWSD.x: " << boxWSD.x
		 << " cWSD.x: " << cWSD.x
		 << " Nx: " <<  Nx
		 << endl;
	    */
	    /*	    
bool now;
if(boxWSD.z < 0 && boxENT.z > 0.0 && boxWSD.y < 0.5 && boxENT.y > 0.5 && boxWSD.x < 1.0 && boxENT.x > 1.0 ) {
  cout << "\n \n WSD   : ";  boxWSD.Print();
  cout << "\n ENT  : ";  boxENT.Print();
  now = true;
 }
 else now = false;

 
 cout << " tt: " << boxWSD.x << " " << pWSD.x << " " << hx << endl;
 cout << (boxWSD.x - pWSD.x) << endl;

 cout << " z: " << 0.1 << " g: " << Ganzzahliger_Anteil(0.1) << endl;
 cout << " z: " << -0.1 << " g: " << Ganzzahliger_Anteil(-0.1) << endl;

 cout << " z: " << 5.1 << " g: " << Ganzzahliger_Anteil(5.1) << endl;
 cout << " z: " << -5.1 << " g: " << Ganzzahliger_Anteil(-5.1) << endl;
	    */

	    if(ilmin<0) ilmin=0;
	    if(jlmin<0) jlmin=0;
	    if(klmin<0) klmin=0;

	    for(int il = ilmin; (il <= ilmax) && (il < nx_);++il)
	      for(int jl = jlmin; (jl <= jlmax) && (jl < ny_);++jl)
		for(int kl = klmin; (kl <= klmax) && (kl < nz_);++kl) {

Phillip Lino Rall's avatar
Phillip Lino Rall committed
770
		  ploc = D3vector(il * hx, jl * hy, kl * hz) + pWSD;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
771
772
773

		  //		  cout << "HI" << endl;

Phillip Lino Rall's avatar
Phillip Lino Rall committed
774
		  typ = -1;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
775

Phillip Lino Rall's avatar
Phillip Lino Rall committed
776
		  lam = lambda_of_p_in_tet(ploc,cWND,cWNT,cWST,cEST);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
		  if(contained_in_tet(lam)) typ=0;
		  else {
		    lam = lambda_of_p_in_tet(ploc,cEST,cWND,cWST,cESD);
		    if(contained_in_tet(lam)) typ=1;
		    else {
		      lam = lambda_of_p_in_tet(ploc,cWND,cWSD,cWST,cESD);
		      if(contained_in_tet(lam)) typ=2;
		      else {
			lam = lambda_of_p_in_tet(ploc,cEST,cWND,cESD,cEND);
			if(contained_in_tet(lam)) typ=3;
			else {
			  lam = lambda_of_p_in_tet(ploc,cENT,cWNT,cEST,cEND);
			  if(contained_in_tet(lam)) typ=4;
			  else {
			    lam = lambda_of_p_in_tet(ploc,cWNT,cWND,cEST,cEND);
			    if(contained_in_tet(lam)) typ=5;
			  }
			}
		      }
		    }
		  }

		  /*		  
		  cout << " typ " << typ << id_hex 
		       << " il: " << il 
		       << " jl: " << jl 
		       << " kl: " << kl 
		       << endl; 
		  */

		  if(typ!=-1) {
		    int ind_global;
		    ind_global = il+nx*(jl+ny*kl);      
		    bool stop;
		    stop=false;

		    if(ids_hex[ind_global]!=-1) {
		      stop=new_lam_worse(lambda[ind_global],lam);
		    }

		    if(stop==false) {
		      ids_hex[ind_global] = id_hex;
		      ids_i[ind_global] = i;
		      ids_j[ind_global] = j;
		      ids_k[ind_global] = k;
		      
		      typ_tet[ind_global] = typ;
		      
		      lambda[ind_global] = lam;
		    }
		    //go_on = false;
		  }

		  /*
		  cout << " out "
		       << " ilmin: " << ilmin
		       << " ilmax: " << ilmax
		       << " jlmin: " << jlmin
		       << " jlmax: " << jlmax
		       << " klmin: " << klmin
		       << " klmax: " << klmax;
		  cout << "\n   "; cWSD.Print();
		  cout << "\n   "; cESD.Print();
		  cout << "\n   "; cWND.Print();
		  cout << "\n   "; cEND.Print();
		  cout << "\n   "; cWST.Print();
		  cout << "\n   "; cEST.Print();
		  cout << "\n   "; cWNT.Print();
		  cout << "\n   "; cENT.Print();
		  cout << "\n   p: "; ploc.Print();

		  cout << "\n   : ";  boxWSD.Print();
		  cout << "\n   : ";  boxENT.Print();

		  cout << endl;
		  */
		}
	  }
  }


  for(int i=0;i<num_total;++i) {
    if(ids_hex[i]==-1) {
      // wir nehmen default value!!
      /*
      cout << i 
	   << " Error: Interpolate_on_structured_grid: I cannot interpolate all data!"
	   << endl;
      ids_hex[i] = 0;
      */
    }
    else {
      //cout << i << " Interpolate_on_structured_grid: o.k.!" << endl;
    }
  }
}

/////////////////////////////////////////////////////////////
// 2. Interpolate from  blockgrid  to  blockgrid
/////////////////////////////////////////////////////////////


Interpolate_on_block_grid::Interpolate_on_block_grid(int nx_, int ny_, int nz_,
				                     Blockgrid* blockgrid_from, Blockgrid* blockgrid_to_) {
    nx = nx_;
    ny = ny_;
    nz = nz_;
 
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
     
    blockgrid_to = blockgrid_to_;
    
    //Variable<double> coordXYZ(*blockgrid);
    X_coordinate Xc(*blockgrid_to);
    Y_coordinate Yc(*blockgrid_to);
    Z_coordinate Zc(*blockgrid_to);
    
    pWSD.x = Minimum(Xc);    pWSD.y = Minimum(Yc);    pWSD.z = Minimum(Zc);
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);  

    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
    data = new double[nx*ny*nz];
    
    
903
904
905
    hx = (pENT.x - pWSD.x) / (nx-1);
    hy = (pENT.y - pWSD.y) / (ny-1);
    hz = (pENT.z - pWSD.z) / (nz-1);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
906
907
908
909
910
911
912
913
914
915
916
917
    
    
    /*
    
    // test GGGG
    cout << "\n WSD: " ; pWSD.Print();
    cout << "\n ENT: " ; pENT.Print();
    cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;
    */
}
  

918

Phillip Lino Rall's avatar
Phillip Lino Rall committed
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
  
void Interpolate_on_block_grid::interpolate(Variable<double>* U_from, Variable<double>* U_to,
					    double defaultInterpolation) {
/*
   //test GGGG
   X_coordinate Xfrom(*U_from->Give_blockgrid());
  (*U_from) = Xfrom;  
*/  
    interpolatorStructured->interpolate<double>(*U_from,data,defaultInterpolation);

    /*
 //test GGGG
    for(int i=0;i<Nx;++i) for(int j=0;j<nz;++j) for(int k=0;k<nz;++k) 
       data[i    +nx*(j    +ny* k)] = hx * i;
      */
      
    Functor3<double,double,Interpolate_on_block_grid> myFunctor(this);
    
    X_coordinate Xc(*blockgrid_to);
    Y_coordinate Yc(*blockgrid_to);
    Z_coordinate Zc(*blockgrid_to);
    
    (*U_to) = myFunctor(Xc,Yc,Zc);
}
943
944


Phillip Lino Rall's avatar
Phillip Lino Rall committed
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
 
double Interpolate_on_block_grid::evaluate(double coord_x, double coord_y, double coord_z) {  
  if(coord_x > pENT.x) return 0.0;
  if(coord_x < pWSD.x) return 0.0;
  if(coord_y > pENT.y) return 0.0;
  if(coord_y < pWSD.y) return 0.0;
  if(coord_z > pENT.z) return 0.0;
  if(coord_z < pWSD.z) return 0.0;
  
  int i = (coord_x - pWSD.x) / hx;   
  int j = (coord_y - pWSD.y) / hy;   
  int k = (coord_z - pWSD.z) / hz;   
    
  if(i < 0)   i=0;     if(j <0   ) j=0;     if(k<0)     k=0;
  if(i>=nx-1) i=nx-2;  if(j>=ny-1) j=ny-2;  if(k>=nz-1) k=nz-2;
  
  //cout << "i: " << i << " j: " << j << " k: " << k << endl;
962

Phillip Lino Rall's avatar
Phillip Lino Rall committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
  double uWSD = data[i    +nx*(j    +ny* k)];
  double uESD = data[(i+1)+nx*(j    +ny* k)];
  double uWND = data[i    +nx*((j+1)+ny* k)];
  double uEND = data[(i+1)+nx*((j+1)+ny* k)];
  double uWST = data[i    +nx*(j    +ny*(k+1))];
  double uEST = data[(i+1)+nx*(j    +ny*(k+1))];
  double uWNT = data[i    +nx*((j+1)+ny*(k+1))];
  double uENT = data[(i+1)+nx*((j+1)+ny*(k+1))];
  
  
  // assert( (i+1)+nx*((j+1)+ny*(k+1)) < nx*ny*nz);
  
  double locX = (coord_x - pWSD.x) / hx - i;
  double locY = (coord_y - pWSD.y) / hy - j;
  double locZ = (coord_z - pWSD.z) / hz - k;
  
Phillip Lino Rall's avatar
Phillip Lino Rall committed
979

Phillip Lino Rall's avatar
Phillip Lino Rall committed
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
  return uWSD * (1.0 - locX) * (1.0 - locY) * (1.0 - locZ) +
         uESD *        locX  * (1.0 - locY) * (1.0 - locZ) +
         uWND * (1.0 - locX) *        locY  * (1.0 - locZ) +
         uEND *        locX  *        locY  * (1.0 - locZ) +
         uWST * (1.0 - locX) * (1.0 - locY) *        locZ  +
         uEST *        locX  * (1.0 - locY) *        locZ  +
         uWNT * (1.0 - locX) *        locY  *        locZ  +
         uENT *        locX  *        locY  *        locZ;
}
 
Interpolate_on_block_grid::~Interpolate_on_block_grid() {
    delete interpolatorStructured;
    delete[] data;
}


Phillip Lino Rall's avatar
Phillip Lino Rall committed
996
997
998
/////////////////////////////////////////////////////////////
// 3. Interpolate from Variable on a blockgrid to any point using structured intermediate grid  
/////////////////////////////////////////////////////////////
Phillip Lino Rall's avatar
Phillip Lino Rall committed
999

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1000
   
For faster browsing, not all history is shown. View entire blame