interpol.cc 182 KB
Newer Older
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1
/**********************************************************************************
2
 * Copyright 2010 Christoph Pflaum
Phillip Lino Rall's avatar
Phillip Lino Rall committed
3
4
 * 		Department Informatik Lehrstuhl 10 - Systemsimulation
 *		Friedrich-Alexander Universität Erlangen-Nürnberg
5
 *
Phillip Lino Rall's avatar
Phillip Lino Rall committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * 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
  return true;
}
64
65
66
67
68
69
70
71
bool contained_in_tet_direct(D3vector lam) {
    double limit = 0.0; // 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;
  return true;
}
72
73

bool new_lam_better(D3vector lam_old, D3vector lam_new) {
74
75
76
77
78
79
80
81
82
83
84
    double min_new = MIN(lam_new);
    double max_new = MAX(lam_new);
    if (min_new < -0.2 || max_new > 1.2) return false;
    double min_old = MIN(lam_old);
    double max_old = MAX(lam_old);
    if (min_old < -0.2 || max_old > 1.2) return true;

    if((min_new < -1e-10 || max_new >1.0+1e-10 ) &&  (min_old >= -1e-10 && max_old <=1.0+1e-10 )) return false;
    if (min_new > min_old               && min_old<-1e-10) return true;
    if (max_new < max_old               && max_old>1.0+1e-10) return true;
    if (SUM(lam_old) > SUM(lam_new) && min_new > -1e-10) return true;
85
    return false;
86
87
}

Phillip Lino Rall's avatar
Phillip Lino Rall committed
88
bool new_lam_worse(D3vector lam_old, D3vector lam_new) {
89
90
  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
91
92
93
  return false;
}

Phillip Lino Rall's avatar
Phillip Lino Rall committed
94
/*
Phillip Lino Rall's avatar
Phillip Lino Rall committed
95

Phillip Lino Rall's avatar
Phillip Lino Rall committed
96
97
98
99
100
Intermadiate_grid_for_PointInterpolator::Intermadiate_grid_for_PointInterpolator(int nx_, int ny_, int nz_, Variable<double>* U_from)
{
    nx = nx_;
    ny = ny_;
    nz = nz_;
101

Phillip Lino Rall's avatar
Phillip Lino Rall committed
102
103
104
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
105

Phillip Lino Rall's avatar
Phillip Lino Rall committed
106
    Blockgrid* blockgrid_from = U_from->Give_blockgrid();
107

Phillip Lino Rall's avatar
Phillip Lino Rall committed
108
109
110
111
112
    //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);
113
114
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);

Phillip Lino Rall's avatar
Phillip Lino Rall committed
115

Phillip Lino Rall's avatar
Phillip Lino Rall committed
116
    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
117

Phillip Lino Rall's avatar
Phillip Lino Rall committed
118
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
119
*/
120
121
122
123
124
125
126
Interpolate_on_structured_grid::~Interpolate_on_structured_grid() {
  delete[] ids_hex;
  delete[] ids_i;
  delete[] ids_j;
  delete[] ids_k;

  delete[] typ_tet;
127
    delete[] lambda;
128
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
129

130
void Interpolate_on_structured_grid::update_Interpolate_on_structured_grid(Blockgrid &blockgrid_, bool onlyOnSurfaceZ)
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
{
        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;





        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);

193
194
195
196
197
            int limitForZ = Nz;
            if (onlyOnSurfaceZ)
            {limitForZ = 1;}

          for(int k=0;k<limitForZ;++k)
198
          for(int j=0;j<Ny;++j)
199
200
201
            for(int i=0;i<Nx;++i)
            //if (k == 0 || k == (Nz-1) || j == 0 || j == (Ny-1) || i == 0 || i == (Nx-1))
            {
202
                  // corner points of general hex-cell
203
                    findLambdaForInterpolation(id_hex,i,j,k);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

            }
        }


        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;
          }
        }
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
void Interpolate_on_structured_grid::findLambdaForInterpolation(int id_hex, int i, int j, int k)
{
    double factor = 0.1;
      // corner points of general hex-cell


  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;

//  boxWSD = D3vector(-5.926666666666667,-2.1166666666666685,12.5);
//  boxENT = boxWSD + D3vector(3);


  // 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);
//  std::cout << "ilmin " <<ilmin  << "\n";
//  std::cout << "jlmin " <<jlmin  << "\n";
//  std::cout << "klmin " <<klmin  << "\n";
//  std::cout << "ilmax " <<ilmax  << "\n";
//  std::cout << "jlmax " <<jlmax  << "\n";
//  std::cout << "klmax " <<klmax  << "\n";

  int typ;
  D3vector lam;

  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;

    typ = -1;


    int ind_global;
    ind_global = il+nx*(jl+ny*kl);

    D3vector lamTemp = D3vector(-1);
    //lamTemp = lambda[ind_global];

    lam = lambda_of_p_in_tet(ploc,cWND,cWNT,cWST,cEST);
    if (contained_in_tet_strong(lam) && new_lam_better(lamTemp,lam)){typ=0;lamTemp = lam;}

    lam = lambda_of_p_in_tet(ploc,cEST,cWND,cWST,cESD);
    if (contained_in_tet_strong(lam) && new_lam_better(lamTemp,lam)){typ=1;lamTemp = lam;}

    lam = lambda_of_p_in_tet(ploc,cWND,cWSD,cWST,cESD);
    if (contained_in_tet_strong(lam) && new_lam_better(lamTemp,lam)){typ=2;lamTemp = lam;}

    lam = lambda_of_p_in_tet(ploc,cEST,cWND,cESD,cEND);
    if (contained_in_tet_strong(lam) && new_lam_better(lamTemp,lam)){typ=3;lamTemp = lam;}

    lam = lambda_of_p_in_tet(ploc,cENT,cWNT,cEST,cEND);
    if (contained_in_tet_strong(lam) && new_lam_better(lamTemp,lam)){typ=4;lamTemp = lam;}

    lam = lambda_of_p_in_tet(ploc,cWNT,cWND,cEST,cEND);
    if (contained_in_tet_strong(lam) && new_lam_better(lamTemp,lam)){typ=5;lamTemp = lam;}



    lam = lamTemp;
    if (trilinearInterpolationFlag)
    {
        typ = -1;
    }


    //if (trilinearInterpolationFlag && MIN(lam)>= -0.1 && MAX(lam) <= 1.1 && SUM(lam) <= 1.2 && SUM(lam)>=-0.2)
    if (trilinearInterpolationFlag && MIN(lam)>= -0.1 && MAX(lam) <= 1.1 && SUM(lam) <= 1.1 && SUM(lam)>=-0.1)
    {
        lam = trilinarInterpolation(ploc, id_hex,i,  j,  k);
        typ = 6;
    }


    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_better(lambda[ind_global],lam);
        if (trilinearInterpolationFlag)
        {
            if (MIN(lam) >= -1e-10 && MAX(lam)<1.0+1e-10)// || ( MAX(lambda[ind_global])>1.0 || MIN(lambda[ind_global])<0.0 ))
            {
                stop = false;
            }
            if ( MAX(lambda[ind_global])>1.0+1e-10 || MIN(lambda[ind_global])<-1e-10 )
            {
                stop = false;
            }

        }

      }

      if(stop==false)// && ((typ_tet[ind_global] != 6) || (typ_tet[ind_global] == 6 & (MIN(lambda[ind_global])<-1e-10)|| MAX(lambda[ind_global])>(1+1e-10))))
      {
        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;
      }
    }
  }
}
373
374
375
376
377
378
379
380
381
382
383
384
D3vector Interpolate_on_structured_grid::trilinarInterpolation(D3vector X, int id_Hex, int i, int j, int k)
{
    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);

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
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 B = cESD - cEND; // 100 - 000
//    D3vector C = cWND - cEND; // 010 - 000
//    D3vector D = cENT - cEND; // 001 - 000
//    //std::cout << "to be implemented" << std::endl;


////    X = (cWSD+ cESD+ cWND + cEND + cWST + cEST + cWNT + cENT   ) / 8.0 ;

////    X.x = X.x +15;
////    X.y = X.y +15;
//    D3vector normalTD = cross_product(-1*B,C);
//    D3vector normalTDOPP = -1*cross_product(cWST-cEST , cWNT - cWST);
//    //D3vector normalTDOPP2 = cross_product(cENT-cEST ,cWNT - cENT);
//    D3vector normalNS = cross_product(-1*C,D);
//    D3vector normalNSOPP = -1*cross_product(cWST-cEST,cWST - cWSD);
//    //D3vector normalNSOPP2 = cross_product(cESD-cEST,cESD - cWSD);
//    D3vector normalEW = cross_product(-1*D,B);
//    D3vector normalEWOPP = -1*cross_product(cWST-cWSD,cWST - cWNT);
//    //D3vector normalEWOPP2 = cross_product(cWND-cWSD,cWND - cWNT);
//    normalTD = normalTD / D3VectorNorm(normalTD);
//    normalTDOPP = normalTDOPP / D3VectorNorm(normalTDOPP);
//    normalNS = normalNS / D3VectorNorm(normalNS);
//    normalNSOPP = normalNSOPP / D3VectorNorm(normalNSOPP);
//    normalEW = normalEW / D3VectorNorm(normalEW);
//    normalEWOPP = normalEWOPP / D3VectorNorm(normalEWOPP);
//    double eta = 0.5;
//    double xi  = 0.5;
//    double phi = 0.5;
//    bool fixEta = false;
//    bool fixXi = false;
//    bool fixPhi = false;
//    D3vector coord(eta,xi,phi);

//    double distTD0 = product(normalTD,cEND);
//    double TEST1 = product(normalTD,cWND);
//    double TEST2 = product(normalTD,cESD);
//    double TEST3 = product(normalTD,cWSD);
//    double distTD1 = product(normalTDOPP,cWST);
//    double distTDMid  = distTD0-product(normalTD,X);
//    double distTDMid2 = distTD1-product(normalTDOPP,X);

//    double distNS0 = product(normalNS,cEND);
//    double distNS1 = product(normalNSOPP,cWST);
//    double distNSMid = distNS0-product(normalNS,X);
//    double distNSMid2 = distNS1 -product(normalNSOPP,X);

//    double distEW0 = product(normalEW,cEND);
//    double distEW1 = product(normalEWOPP,cWST);
//    double distEWMid = distEW0-product(normalEW,X);
//    double distEWMid2 = distEW1 -product(normalEWOPP,X);



//    D3vector A = cEND;        // 000
//    D3vector E = cWSD - cESD - cWND + cEND;        // 110 - 100 - 010 + 000
//    D3vector F = cWNT - cWND - cENT + cEND;        // 011 - 010 - 001 + 000
//    D3vector G = cEST - cESD - cENT + cEND;        // 101 - 100 - 001 + 000
//    D3vector H = cWST + cESD + cWND + cENT - cEND - cWSD - cWNT - cEST;        // 111 + 100 + 010 + 001 - 000 - 110 - 011 - 101
//    if (fabs(angle_between_vectors(normalTD,normalTDOPP)-180*0) < 0.5)
//    {
//        double delta = (distTD1 - distTD0);
//        //phi = (distTDMid + delta - distTDMid2) /delta/ 2.0;
//        //std::cout << "phi  1 "<< phi << std::endl;
//        phi = -distTDMid / delta;
//        //std::cout << "phi  2 "<< phi << std::endl;
//        //phi = distTDMid2 / delta;
//        //std::cout << "phi  3 "<< phi << std::endl;
//        coord.z = phi;
//        fixPhi = true;
//    }
//    if (fabs(angle_between_vectors(normalNS,normalNSOPP)-180*0) < 0.5)
//    {
//        double delta = (distNS1 - distNS0);
//        //eta = (distNSMid + delta - distNSMid2) /delta / 2.0;
//        //std::cout << "eta  1 "<< eta << std::endl;
//        eta = -distNSMid / delta;
//        //std::cout << "eta  2 "<< eta << std::endl;
//        //eta = distNSMid2 / delta;
//        //std::cout << "eta  3 "<< eta << std::endl;
//        coord.x = eta;
//        fixEta = true;
//    }
//    if (fabs(angle_between_vectors(normalEW,normalEWOPP)-180*0) < 0.5)
//    {
//        double delta = (distEW1 - distEW0);
//        xi = -distEWMid / delta;
//        coord.y = xi;
//        fixXi = true;
//    }
474

475
476
477
478
////    std::cout << "initial guess\n";
////    coord.Print();std::cout<<std::endl;
//    D3vector R = A    + B * coord.x + C * coord.y + D * coord.z + E * coord.x*coord.y + F * coord.y*coord.z + G * coord.x * coord.z + H * coord.x*coord.y*coord.z;
//    //
479
480


481
482
483
484
485
486
487
488
//D3vector x;
//D3vector coordPrev;
//        for (int iter = 0 ; iter < 50 ; iter++)
//        {
//            coordPrev = coord;
//            D3vector partialEta = B + E * coord.y + G * coord.z + H * coord.y*coord.z;
//            D3vector partialXi =  C + E * coord.x + F * coord.z + H * coord.x*coord.z;
//            D3vector partialPhi = D + F * coord.y + G * coord.x + H * coord.x*coord.y;
489
490
491



492
493
494
//            D3matrix jac2(partialEta,partialXi,partialPhi);
//            //jac2.transpose();
//            //jac2.invert_gauss_elimination();
495

496
//            R = A    + B * coord.x + C * coord.y + D * coord.z + E * coord.x*coord.y + F * coord.y*coord.z + G * coord.x * coord.z + H * coord.x*coord.y*coord.z -X;
497
498


499
500
501
502
503
504
505
//            //std::cout << "residuum f_xn" <<D3VectorNorm(R)<<std::endl;
////            if (D3VectorNormSquared(R) < 1e-10)
////            {
////                iter = 1000;
////            }
//           // coord.Print(); std::cout << std::flush;
//            coord = coord - jac2.invert_apply(R) * 1.0;
506

507
508
509
510
511
512
513
514
//            if (D3VectorNorm(coordPrev-coord) < 1e-15)
//            {
//                iter = 1000;
//            }
//            if (std::isnan(coord.x) || std::isnan(coord.y) || std::isnan(coord.z))
//            {
//                std::cout << "trilinear interpolation failed!!!\n"<< std::endl;
//            }
515

516
517
518
519
520
521
522
523
524
525
526
527
528
//            x = A    + B * coord.x + C * coord.y + D * coord.z + E * coord.x*coord.y + F * coord.y*coord.z + G * coord.x * coord.z + H * coord.x*coord.y*coord.z;
//            if (coord.x > 2.0)
//                coord.x = 1.1;
//            if (coord.y > 2.0)
//                coord.y = 1.1;
//            if (coord.z > 2.0)
//                coord.z = 1.1;
//            if (coord.x < -1.0)
//                coord.x = -0.1;
//            if (coord.y < -1.0)
//                coord.y = -0.1;
//            if (coord.z < -1.0)
//                coord.z = -0.1;
529

530
//        }
531
532
533



534
535
536
537
////    X.Print();
////    std::cout << "tirlinear interp \n";
////   x.Print();
////    std::cout << std::flush;
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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
//   //     if (MAX(coord) > 1.2 || MIN(coord) < -0.2)
//    //if (MAX(coord) > 1+1e-10 || MIN(coord) < -1e-10)
//    if (MAX(coord) > 1.1 || MIN(coord) < -0.1)
//    {std::cout << "????? coords larger 1?";
//        return D3vector{-1,-1,-1};
//    }
//    return coord;

        D3vector B = cESD - cEND; // 100 - 000
        D3vector C = cWND - cEND; // 010 - 000
        D3vector D = cENT - cEND; // 001 - 000
        //std::cout << "to be implemented" << std::endl;


    //    X = (cWSD+ cESD+ cWND + cEND + cWST + cEST + cWNT + cENT   ) / 8.0 ;

    //    X.x = X.x +15;
    //    X.y = X.y +15;
        D3vector normalTD = cross_product(-1*B,C);
        D3vector normalTDOPP = -1*cross_product(cWST-cEST , cWNT - cWST);
        //D3vector normalTDOPP2 = cross_product(cENT-cEST ,cWNT - cENT);
        D3vector normalNS = cross_product(-1*C,D);
        D3vector normalNSOPP = -1*cross_product(cWST-cEST,cWST - cWSD);
        //D3vector normalNSOPP2 = cross_product(cESD-cEST,cESD - cWSD);
        D3vector normalEW = cross_product(-1*D,B);
        D3vector normalEWOPP = -1*cross_product(cWST-cWSD,cWST - cWNT);
        //D3vector normalEWOPP2 = cross_product(cWND-cWSD,cWND - cWNT);
        normalTD = normalTD / D3VectorNorm(normalTD);
        normalTDOPP = normalTDOPP / D3VectorNorm(normalTDOPP);
        normalNS = normalNS / D3VectorNorm(normalNS);
        normalNSOPP = normalNSOPP / D3VectorNorm(normalNSOPP);
        normalEW = normalEW / D3VectorNorm(normalEW);
        normalEWOPP = normalEWOPP / D3VectorNorm(normalEWOPP);
        double eta = 0.5;
        double xi  = 0.5;
        double phi = 0.5;
        bool fixEta = false;
        bool fixXi = false;
        bool fixPhi = false;
        D3vector coord(eta,xi,phi);

        double distTD0 = product(normalTD,cEND);
        double distTD1 = product(normalTDOPP,cWST);
        double distTDMid  = distTD0-product(normalTD,X);

        double distNS0 = product(normalNS,cEND);
        double distNS1 = product(normalNSOPP,cWST);
        double distNSMid = distNS0-product(normalNS,X);

        double distEW0 = product(normalEW,cEND);
        double distEW1 = product(normalEWOPP,cWST);
        double distEWMid = distEW0-product(normalEW,X);


        D3vector A = cEND;        // 000
        D3vector E = cWSD - cESD - cWND + cEND;        // 110 - 100 - 010 + 000
        D3vector F = cWNT - cWND - cENT + cEND;        // 011 - 010 - 001 + 000
        D3vector G = cEST - cESD - cENT + cEND;        // 101 - 100 - 001 + 000
        D3vector H = cWST + cESD + cWND + cENT - cEND - cWSD - cWNT - cEST;        // 111 + 100 + 010 + 001 - 000 - 110 - 011 - 101
        if (fabs(angle_between_vectors(normalTD,normalTDOPP)-180*0) < 0.5)
        {
            double delta = (distTD1 - distTD0);
            //phi = (distTDMid + delta - distTDMid2) /delta/ 2.0;
            //std::cout << "phi  1 "<< phi << std::endl;
            phi = -distTDMid / delta;
            //std::cout << "phi  2 "<< phi << std::endl;
            //phi = distTDMid2 / delta;
            //std::cout << "phi  3 "<< phi << std::endl;
            coord.z = phi;
            fixPhi = true;
        }
        if (fabs(angle_between_vectors(normalNS,normalNSOPP)-180*0) < 0.5)
        {
            double delta = (distNS1 - distNS0);
            //eta = (distNSMid + delta - distNSMid2) /delta / 2.0;
            //std::cout << "eta  1 "<< eta << std::endl;
            eta = -distNSMid / delta;
            //std::cout << "eta  2 "<< eta << std::endl;
            //eta = distNSMid2 / delta;
            //std::cout << "eta  3 "<< eta << std::endl;
            coord.x = eta;
            fixEta = true;
        }
        if (fabs(angle_between_vectors(normalEW,normalEWOPP)-180*0) < 0.5)
        {
            double delta = (distEW1 - distEW0);
            //xi = (distEWMid + delta - distEWMid2) /delta/ 2.0;
            //std::cout << "xi  1 "<< xi << std::endl;
            xi = -distEWMid / delta;
            //std::cout << "xi  2 "<< xi << std::endl;
            //xi = distEWMid2 / delta;
            //std::cout << "xi  3 "<< xi << std::endl;
            //xi = (distEWMid + distEWMid2)/ 2.0 / (distEW1 - distEW0);
            coord.y = xi;
            fixXi = true;
        }
636

637
        D3vector R = A    + B * coord.x + C * coord.y + D * coord.z + E * coord.x*coord.y + F * coord.y*coord.z + G * coord.x * coord.z + H * coord.x*coord.y*coord.z;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
638

639
640
641
642

    D3vector x;
    D3vector coordPrev;
            for (int iter = 0 ; iter < 50 ; iter++)
643
            {
644
645
646
647
                coordPrev = coord;
                D3vector partialEta = B + E * coord.y + G * coord.z + H * coord.y*coord.z;
                D3vector partialXi =  C + E * coord.x + F * coord.z + H * coord.x*coord.z;
                D3vector partialPhi = D + F * coord.y + G * coord.x + H * coord.x*coord.y;
648
649
650



651
652
653
                D3matrix jac2(partialEta,partialXi,partialPhi);
                //jac2.transpose();
                //jac2.invert_gauss_elimination();
654

655
                R = A    + B * coord.x + C * coord.y + D * coord.z + E * coord.x*coord.y + F * coord.y*coord.z + G * coord.x * coord.z + H * coord.x*coord.y*coord.z -X;
656
657


658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
                //std::cout << "residuum f_xn" <<D3VectorNorm(R)<<std::endl;
    //            if (D3VectorNorm(R) < 1e-30)
    //            {
    //                iter = 1000;
    //            }
               // coord.Print(); std::cout << std::flush;
                coord = coord - jac2.invert_apply(R) * 1.0;
                if (D3VectorNorm(coordPrev-coord) < 1e-15)
                {
                    iter = 1000;
                }

                if (std::isnan(coord.x) || std::isnan(coord.y) || std::isnan(coord.z))
                {
                    std::cout << "trilinear interpolation failed!!!\n"<< std::endl;
                }

                x = A    + B * coord.x + C * coord.y + D * coord.z + E * coord.x*coord.y + F * coord.y*coord.z + G * coord.x * coord.z + H * coord.x*coord.y*coord.z;
                if (coord.x > 2.0)
                    coord.x = 1.1;
                if (coord.y > 2.0)
                    coord.y = 1.1;
                if (coord.z > 2.0)
                    coord.z = 1.1;
                if (coord.x < -1.0)
                    coord.x = -0.1;
                if (coord.y < -1.0)
                    coord.y = -0.1;
                if (coord.z < -1.0)
                    coord.z = -0.1;

            }


        if (MAX(coord) > 1.1 || MIN(coord) < -0.1)
        {
            return D3vector{-1,-1,-1};
        }
        return coord;
697
698

}
699

Phillip Lino Rall's avatar
Phillip Lino Rall committed
700
Interpolate_on_structured_grid::Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
701
                                   D3vector pWSD, D3vector pENT,
702
703
704
                                   Blockgrid& blockgrid_, bool trilinearInterpolationFlag_) {
   // id = trilinearInterpolationFlag_;
    trilinearInterpolationFlag = trilinearInterpolationFlag_;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
705
  int Nx, Ny, Nz;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
706
   int typ;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
707
708
709
710
711

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

712
713
  //int ilmin, jlmin, klmin;
  //int ilmax, jlmax, klmax;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
714
715
716
717

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

Phillip Lino Rall's avatar
Phillip Lino Rall committed
718
   D3vector lam;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
719

720
721
   this->pENT = pENT;
   this->pWSD = pWSD;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
722
723
724
725
726
727
728
729
730
  blockgrid = &blockgrid_;
  ug = blockgrid->Give_unstructured_grid();

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

  if(nx_>1)
    hx = (pENT.x - pWSD.x) / (nx_-1);
731
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
732
733
734
    hx = 1.0;
  if(ny_>1)
    hy = (pENT.y - pWSD.y) / (ny_-1);
735
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
736
737
738
    hy = 1.0;
  if(nz_>1)
    hz = (pENT.z - pWSD.z) / (nz_-1);
739
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
740
741
742
743
    hz = 1.0;

  int num_total = nx * ny * nz;

744
  /*
Phillip Lino Rall's avatar
Phillip Lino Rall committed
745
746
747
748
749
  D3vector cWSD, cESD;
  D3vector cWND, cEND;

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

752
  // D3vector boxWSD, boxENT;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
753

754
  // D3vector ploc;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
755
756
757
758
759
760
761
762
763
764
765

  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];

766
  for(int i=0;i<num_total;++i) {ids_hex[i] = -1;}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
767
768
769
770
771
772

  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);

773
//#pragma omp parallel for num_threads(UGBlocks::numThreadsToTake) if(UGBlocks::useOpenMP)
Phillip Lino Rall's avatar
Phillip Lino Rall committed
774
      for(int k=0;k<Nz;++k)
775
776
    for(int j=0;j<Ny;++j)
      for(int i=0;i<Nx;++i) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
777
            // corner points of general hex-cell
778
             findLambdaForInterpolation(id_hex,i,j,k);
779
      }
Phillip Lino Rall's avatar
Phillip Lino Rall committed
780
781
782
783
  }


  for(int i=0;i<num_total;++i) {
784
785
//      std::cout << "ids_hex[i] " << ids_hex[i] << "\n";
//      std::cout << "lambda[i] " << lambda[i].x << " " << lambda[i].y << " " << lambda[i].z << " " << "\n";
Phillip Lino Rall's avatar
Phillip Lino Rall committed
786
787
788
    if(ids_hex[i]==-1) {
      // wir nehmen default value!!
      /*
789
790
791
      cout << i
       << " Error: Interpolate_on_structured_grid: I cannot interpolate all data!"
       << endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
792
793
794
795
796
797
798
      ids_hex[i] = 0;
      */
    }
    else {
      //cout << i << " Interpolate_on_structured_grid: o.k.!" << endl;
    }
  }
799
800
801
802
803
//  for ( int iter = 0 ; iter < num_total ; iter++)
//  {
//      std::cout << "typtet " << typ_tet[iter] <<"\n";
//  }
//  std::cout << std::endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
804
805
}

806
807
808
809
810
/////////////////////////////////////////////////////////////
// 2. Interpolate from  blockgrid  to  blockgrid
/////////////////////////////////////////////////////////////


Phillip Lino Rall's avatar
Phillip Lino Rall committed
811
Interpolate_on_structured_grid::Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
812
813
814
                                   Blockgrid& blockgrid_, bool trilinearInterpolationFlag_) {
    //id = trilinearInterpolationFlag_;
    trilinearInterpolationFlag = trilinearInterpolationFlag_;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
815
816
817
  int Nx, Ny, Nz;
  int typ;

818

Phillip Lino Rall's avatar
Phillip Lino Rall committed
819
820
821
822
823
824
825
826
827
828
829
830
  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;

831
832


Phillip Lino Rall's avatar
Phillip Lino Rall committed
833
834
835
836
837
838
    //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);
839
840
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);

Phillip Lino Rall's avatar
Phillip Lino Rall committed
841
842
843
844
845
846
847
848
849
  blockgrid = &blockgrid_;
  ug = blockgrid->Give_unstructured_grid();

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

  if(nx_>1)
    hx = (pENT.x - pWSD.x) / (nx_-1);
850
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
851
852
853
    hx = 1.0;
  if(ny_>1)
    hy = (pENT.y - pWSD.y) / (ny_-1);
854
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
855
856
857
    hy = 1.0;
  if(nz_>1)
    hz = (pENT.z - pWSD.z) / (nz_-1);
858
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
859
    hz = 1.0;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
860

Phillip Lino Rall's avatar
Phillip Lino Rall committed
861
862
863
864
865
866
867
868
869
870
871
  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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888

  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
889

Phillip Lino Rall's avatar
Phillip Lino Rall committed
890
      for(int k=0;k<Nz;++k)
891
892
893
    for(int j=0;j<Ny;++j)
      for(int i=0;i<Nx;++i) {
            // corner points of general hex-cell
894
895
              findLambdaForInterpolation(id_hex,i,j,k);

Phillip Lino Rall's avatar
Phillip Lino Rall committed
896

897
      }
Phillip Lino Rall's avatar
Phillip Lino Rall committed
898
899
900
901
902
903
904
  }


  for(int i=0;i<num_total;++i) {
    if(ids_hex[i]==-1) {
      // wir nehmen default value!!
      /*
905
906
907
      cout << i
       << " Error: Interpolate_on_structured_grid: I cannot interpolate all data!"
       << endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
      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_,
923
                                     Blockgrid* blockgrid_from, Blockgrid* blockgrid_to_) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
924
925
926
    nx = nx_;
    ny = ny_;
    nz = nz_;
927

Phillip Lino Rall's avatar
Phillip Lino Rall committed
928
929
930
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
931

Phillip Lino Rall's avatar
Phillip Lino Rall committed
932
    blockgrid_to = blockgrid_to_;
933

Phillip Lino Rall's avatar
Phillip Lino Rall committed
934
935
936
937
    //Variable<double> coordXYZ(*blockgrid);
    X_coordinate Xc(*blockgrid_to);
    Y_coordinate Yc(*blockgrid_to);
    Z_coordinate Zc(*blockgrid_to);
938

Phillip Lino Rall's avatar
Phillip Lino Rall committed
939
    pWSD.x = Minimum(Xc);    pWSD.y = Minimum(Yc);    pWSD.z = Minimum(Zc);
940
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
941
942
943

    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
    data = new double[nx*ny*nz];
944
945


946
947
948
    hx = (pENT.x - pWSD.x) / (nx-1);
    hy = (pENT.y - pWSD.y) / (ny-1);
    hz = (pENT.z - pWSD.z) / (nz-1);
949
950


Phillip Lino Rall's avatar
Phillip Lino Rall committed
951
    /*
952

Phillip Lino Rall's avatar
Phillip Lino Rall committed
953
954
955
956
957
958
959
    // test GGGG
    cout << "\n WSD: " ; pWSD.Print();
    cout << "\n ENT: " ; pENT.Print();
    cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;
    */
}

960

961
962


Phillip Lino Rall's avatar
Phillip Lino Rall committed
963
void Interpolate_on_block_grid::interpolate(Variable<double>* U_from, Variable<double>* U_to,
964
                        double defaultInterpolation) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
965
966
967
/*
   //test GGGG
   X_coordinate Xfrom(*U_from->Give_blockgrid());
968
969
  (*U_from) = Xfrom;
*/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
970
971
972
973
    interpolatorStructured->interpolate<double>(*U_from,data,defaultInterpolation);

    /*
 //test GGGG
974
    for(int i=0;i<Nx;++i) for(int j=0;j<nz;++j) for(int k=0;k<nz;++k)
Phillip Lino Rall's avatar
Phillip Lino Rall committed
975
976
       data[i    +nx*(j    +ny* k)] = hx * i;
      */
977

Phillip Lino Rall's avatar
Phillip Lino Rall committed
978
    Functor3<double,double,Interpolate_on_block_grid> myFunctor(this);
979

Phillip Lino Rall's avatar
Phillip Lino Rall committed
980
981
982
    X_coordinate Xc(*blockgrid_to);
    Y_coordinate Yc(*blockgrid_to);
    Z_coordinate Zc(*blockgrid_to);
983

Phillip Lino Rall's avatar
Phillip Lino Rall committed
984
985
    (*U_to) = myFunctor(Xc,Yc,Zc);
}
986
987


988
989

double Interpolate_on_block_grid::evaluate(double coord_x, double coord_y, double coord_z) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
990
991
992
993
994
995
  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;
996
997
998
999
1000

  int i = (coord_x - pWSD.x) / hx;
  int j = (coord_y - pWSD.y) / hy;
  int k = (coord_z - pWSD.z) / hz;

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1001
1002
  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;
1003

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1004
  //cout << "i: " << i << " j: " << j << " k: " << k << endl;
1005

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1006
1007
1008
1009
1010
1011
1012
1013
  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))];
1014
1015


Phillip Lino Rall's avatar
Phillip Lino Rall committed
1016
  // assert( (i+1)+nx*((j+1)+ny*(k+1)) < nx*ny*nz);
1017

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1018
1019
1020
  double locX = (coord_x - pWSD.x) / hx - i;
  double locY = (coord_y - pWSD.y) / hy - j;
  double locZ = (coord_z - pWSD.z) / hz - k;
1021

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1022

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
  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;
}
1032

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1033
1034
1035
1036
1037
1038
Interpolate_on_block_grid::~Interpolate_on_block_grid() {
    delete interpolatorStructured;
    delete[] data;
}


Phillip Lino Rall's avatar
Phillip Lino Rall committed
1039
/////////////////////////////////////////////////////////////
1040
// 3. Interpolate from Variable on a blockgrid to any point using structured intermediate grid
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1041
/////////////////////////////////////////////////////////////
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1042

1043

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1044

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1045
PointInterpolator::PointInterpolator(int nx_, int ny_, int nz_,
1046
                     Variable<double>* U_from, double defaultInterpolation_, bool trilinearInterpolation_ ) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1047
1048
1049
1050
1051
1052
1053
    defaultInterpolation = defaultInterpolation_;
    shiftx = 0.0;
    shifty = 0.0;
    shiftz = 0.0;
    nx = nx_;
    ny = ny_;
    nz = nz_;
1054

1055
1056
    new_lam_better(D3vector(0.033,0.799,0.316),D3vector(0.125,0.6742,0.166));

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1057
1058
1059
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
1060

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1061
    Blockgrid* blockgrid_from = U_from->Give_blockgrid();
1062

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1063
1064
1065
1066
1067
    //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);
1068
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1069
1070


1071
    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from, trilinearInterpolation_);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1072
    data = new double[nx*ny*nz];
1073
1074


Phillip Lino Rall's avatar
Phillip Lino Rall committed
1075
1076
1077
1078
1079
1080
    hx = (pENT.x - pWSD.x) / (nx-1);
    hy = (pENT.y - pWSD.y) / (ny-1);
    hz = (pENT.z - pWSD.z) / (nz-1);

    interpolatorStructured->interpolate<double>(*U_from,data,defaultInterpolation_);

1081

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1082
    /*
1083

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1084
1085
1086
1087
1088
1089
    // test GGGG
    cout << "\n WSD: " ; pWSD.Print();
    cout << "\n ENT: " ; pENT.Print();
    cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;
    */
}
1090

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1091
PointInterpolator::PointInterpolator(int nx_, int ny_, int nz_,
1092
1093
                     D3vector pWSD_, D3vector pENT_,
                     Variable<double>* U_from, double defaultInterpolation_) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1094
    defaultInterpolation = defaultInterpolation_;
1095

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1096
1097
1098
1099
1100
1101
    shiftx = 0.0;
    shifty = 0.0;
    shiftz = 0.0;
    nx = nx_;
    ny = ny_;
    nz = nz_;
1102

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1103
1104
1105
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
1106

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1107
    Blockgrid* blockgrid_from = U_from->Give_blockgrid();
1108

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1109
    pWSD = pWSD_;
1110
    pENT = pENT_;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1111
1112
1113

    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
    data = new double[nx*ny*nz];
1114
1115


Phillip Lino Rall's avatar
Phillip Lino Rall committed
1116
1117
1118
    hx = (pENT.x - pWSD.x) / (nx-1);
    hy = (pENT.y - pWSD.y) / (ny-1);
    hz = (pENT.z - pWSD.z) / (nz-1);
1119
1120


Phillip Lino Rall's avatar
Phillip Lino Rall committed
1121
    interpolatorStructured->interpolate<double>(*U_from,data,defaultInterpolation);
1122
1123
1124
1125




Phillip Lino Rall's avatar
Phillip Lino Rall committed
1126
    /*
1127

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
    // test GGGG
    cout << "\n WSD: " ; pWSD.Print();
    cout << "\n ENT: " ; pENT.Print();
    cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;
    */
}

PointInterpolator::PointInterpolator(Interpolate_on_structured_grid* intermediateGrid, Variable<double>* U_from, double defaultInterpolation_)
{
    defaultInterpolation = defaultInterpolation_;
1138

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1139
1140
1141
    nx = intermediateGrid->nx;
    ny = intermediateGrid->ny;
    nz = intermediateGrid->nz;
1142

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1143
    data = new double[nx*ny*nz];
1144

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1145
1146
    pENT = intermediateGrid->pENT;
    pWSD = intermediateGrid->pWSD;
1147

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1148
1149
1150
1151
1152
1153
1154
1155
    shiftx = 0.0;
    shifty = 0.0;
    shiftz = 0.0;
    hx = (pENT.x - pWSD.x) / (nx-1);
    hy = (pENT.y - pWSD.y) / (ny-1);
    hz = (pENT.z - pWSD.z) / (nz-1);

    intermediateGrid->interpolate<double>(*U_from,data,defaultInterpolation_);
1156
1157
}

1158
PointInterpolator::PointInterpolator(Interpolate_on_structured_grid *intermediateGrid, double defaultInterpolation_, bool counter)
1159
{
1160
    dataCounterFlag = counter;
1161
1162
1163
1164
1165
1166
1167
    defaultInterpolation = defaultInterpolation_;

    nx = intermediateGrid->nx;
    ny = intermediateGrid->ny;
    nz = intermediateGrid->nz;

    data = new double[nx*ny*nz];
1168
1169
1170
1171
    if (counter)
    {
        dataCounter = new int[nx*ny*nz];
    }
1172
1173
1174
    for (int iter = 0 ; iter < nx*ny*nz;iter++)
    {
        data[iter]=0.0;
1175
1176
        if (counter)
        {
1177
        dataCounter[iter]=0;
1178
        }
1179
1180
1181
1182
1183
1184
1185
1186
1187
    }

    pENT = intermediateGrid->pENT;
    pWSD = intermediateGrid->pWSD;

    hx = intermediateGrid->getHx();
    hy = intermediateGrid->getHy();
    hz = intermediateGrid->getHz();

1188
1189
    interpolatorStructured = intermediateGrid;

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1190
}
1191

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1192
1193
1194
1195
1196
1197
 /*
Interpolate_on_structured_grid* PointInterpolator::intermediateGrid(int nx_, int ny_, int nz_, Variable<double>* U_from)
{
    nx = nx_;
    ny = ny_;
    nz = nz_;
1198

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1199
1200
1201
    if(nx<=2) nx = 3;
    if(ny<=2) ny = 3;
    if(nz<=2) nz = 3;
1202

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1203
    Blockgrid* blockgrid_from = U_from->Give_blockgrid();
1204

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1205
1206
1207
1208
    //Variable<double> coordXYZ(*blockgrid);
    X_coordinate Xc(*blockgrid_from);
    Y_coordinate Yc(*blockgrid_from);
    Z_coordinate Zc(*blockgrid_from);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1209
    pWSD.x = Minimum(Xc);    pWSD.y = Minimum(Yc);    pWSD.z = Minimum(Zc);
1210
1211
    pENT.x = Maximum(Xc);    pENT.y = Maximum(Yc);    pENT.z = Maximum(Zc);

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1212
1213

    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
1214

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1215
1216
1217
1218
1219
    return interpolatorStructured;
}
*/


1220
1221

double PointInterpolator::evaluate(double coord_x, double coord_y, double coord_z) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
1222
1223
1224
1225
1226

    coord_x-=shiftx;
    coord_y-=shifty;
    coord_z-=shiftz;

1227
1228
1229
1230
1231
1232
  if(coord_x > (pENT.x+(1e-9))) return defaultInterpolation;
  if(coord_x < (pWSD.x-(1e-9))) return defaultInterpolation;
  if(coord_y > (pENT.y+(1e-9))) return defaultInterpolation;
  if(coord_y < (pWSD.y-(1e-9))) return defaultInterpolation;
  if(coord_z > (pENT.z+(1e-9))) return defaultInterpolation;
  if(coord_z < (pWSD.z-(1e-9))) return defaultInterpolation;
1233

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
  //cout << "coord_z " << coord_z << " pWSD.z " << pWSD.z << endl;
    /*
  int i = (coord_x - pWSD.x) / hx;
  int j = (coord_y - pWSD.y) / hy;
  int k = (coord_z - pWSD.z) / hz;
  */
  double id = (coord_x - pWSD.x) / hx;
  double jd = (coord_y - pWSD.y) / hy;
  double kd = (coord_z - pWSD.z) / hz;


  int i = int(id);
  int j = int(jd);
  int k = int(kd);

  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 << "hx " << hx << " hy "<< hy << " hz " << hz << endl;
  //cout << "id: " << id << " jd: " << jd << " kd: " << kd << endl;
  //cout << "i: " << i << " j: " << j << " k: " << k << endl;
  //cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;


  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))];
  //i++;
  //j++;
  //k++;
  //k++;
  //cout << "uWSD " << uWSD << endl;
  //cout << "uESD " << uESD << endl;
  //cout << "uWND " << uWND << endl;
  //cout << "uEND " << uEND << endl;
  //cout << "uWST " << uWST << endl;
  //cout << "uEST " << uEST << endl;
  //cout << "uWNT " << uWNT << endl;
  //cout << "uENT " << uENT << endl;


  //cout << "x+1 "<< data[(i+2)+nx*(j    +ny* k)] << endl;
  //cout << "x-1 " <<data[i-1    +nx*(j    +ny* k)] << endl;
  //cout << "x-1, y-1 " <<data[i-1    +nx*(j-1    +ny* k)] << endl;

  // assert( (i+1)+nx*((j+1)+ny*(k+1)) < nx*ny*nz);
1287

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
  double posX = (coord_x - pWSD.x) ;
  double locX = posX / hx - i;
  double posY = (coord_y - pWSD.y);
  double locY = posY / hy - j;
  double posZ = (coord_z - pWSD.z);
  double locZ = posZ / hz - k;



  //cout << "locX, Y, Z: " << locX << " " << locY << " " << locZ << endl;
  //return uWSD;
1299
1300


Phillip Lino Rall's avatar
Phillip Lino Rall committed
1301
1302
1303
1304
1305
1306
1307
1308
  //cout << "uPOS : " << uWSD << " , " << uESD << " , " << uWND << " , " << uEND << " , " << uWST << " , " << uEST << " , " << uWNT << " , " << uENT << endl;
  double uTOT(0);
  double uET, uWT, uWD, uED;
  double uT, uD;

  if      ( (uEST != defaultInterpolation) == (uENT != defaultInterpolation) ) { uET = uEST * (1.0 - locY) + uENT * locY ;}
  else if ( (uEST != defaultInterpolation) && (uENT == defaultInterpolation) ) { uET = uEST;}
  else     								       { uET = uENT;}
1309

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1310
1311
1312
  if      ( (uWST != defaultInterpolation) == (uWNT != defaultInterpolation) ) {uWT = uWST * (1.0 - locY) + uWNT * locY ;}
  else if ( (uWST != defaultInterpolation) && (uWNT == defaultInterpolation) ) {uWT = uWST;}
  else     								       {uWT = uWNT;}
1313

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1314
1315
1316
  if      ( (uESD != defaultInterpolation) == (uEND != defaultInterpolation) ) {uED = uESD * (1.0 - locY) + uEND * locY ;}
  else if ( (uESD != defaultInterpolation) && (uEND == defaultInterpolation) ) {uED = uESD;}
  else  								       {uED = uEND;}
1317

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1318
1319
1320
  if      ( (uWSD != defaultInterpolation) == (uWND != defaultInterpolation) ) {uWD = uWSD * (1.0 - locY) + uWND * locY ;}
  else if ( (uWSD != defaultInterpolation) && (uWND == defaultInterpolation) ) {uWD = uWSD;}
  else     								       {uWD = uWND;}
1321

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1322
1323
1324
  if      ( (uET != defaultInterpolation)  == (uWT != defaultInterpolation)  ) {uT = uWT  * (1.0 - locX) + uET  * locX ;}
  else if ( (uET != defaultInterpolation)  && (uWT == defaultInterpolation)  ) {uT = uET;}
  else     								       {uT = uWT;}
1325

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1326
1327
1328
  if      ( (uED != defaultInterpolation)  == (uWD != defaultInterpolation)  ) {uD = uWD  * (1.0 - locX) + uED  * locX ;}
  else if ( (uED != defaultInterpolation)  && (uWD == defaultInterpolation)  ) {uD = uED;}
  else     								       {uD = uWD;}
1329

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
  if      ( (uT != defaultInterpolation)   == (uD != defaultInterpolation)   ) {uTOT = uD   * (1.0 - locZ) + uT   * locZ ;}
  else if ( (uT != defaultInterpolation)   && (uD == defaultInterpolation)   ) {uTOT = uT;}
  else    								       {uTOT = uD;}



//  if (uWSD != defaultInterpolation)
//  {    uTOT += uWSD * (1.0 - locX) * (1.0 - locY) * (1.0 - locZ);  }
//  if (uESD != defaultInterpolation)
//  {    uTOT += uESD *        locX  * (1.0 - locY) * (1.0 - locZ);  }
//  if (uWND != defaultInterpolation)
//  {    uTOT += uWND * (1.0 - locX) *        locY  * (1.0 - locZ);  }
//  if (uEND != defaultInterpolation)
//  {    uTOT += uEND *        locX  *        locY  * (1.0 - locZ);  }
//  if (uWST != defaultInterpolation)
//  {    uTOT += uWST * (1.0 - locX) * (1.0 - locY) *        locZ;  }
//  if (uEST != defaultInterpolation)
//  {    uTOT += uEST *        locX  * (1.0 - locY) *        locZ;  }
//  if (uWNT != defaultInterpolation)
//  {    uTOT += uWNT * (1.0 - locX) *        locY  *        locZ;  }
//  if (uENT != defaultInterpolation)
//  {    uTOT += uENT *        locX  *        locY  *        locZ;  }

    //cout << "my method, other method " << uTOT << " , " << 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 << endl;


  //cout <<endl<< "RESULT: " << uTOT<<endl<<endl;
  return uTOT;


1367

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385