interpol.cc 140 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
28
29
30
31
32
/**********************************************************************************
 * 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"
#include "../grid/blockgrid.h"
#include "../grid/marker.h"
#include "../extemp/extemp.h"
#include "../extemp/parallel.h"
#include "../extemp/variable.h"
33
#include "../extemp/cellvar.h"
Phillip Lino Rall's avatar
Phillip Lino Rall committed
34
35
36
#include "../extemp/co_fu.h"
#include "../extemp/functor.h"
#include "interpol.h"
Christoph Pflaum's avatar
Christoph Pflaum committed
37
//#include "customtime.h"
38
#include <iomanip>
Phillip Lino Rall's avatar
Phillip Lino Rall committed
39
40
#include "assert.h"

41

Phillip Lino Rall's avatar
Phillip Lino Rall committed
42
43
44
45
46
47
48
49
50
51
52
53
54
/////////////////////////////////////////////////////////////
// 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;
}

55
bool contained_in_tet_strong(D3vector lam) {
56
57
58
59
60
    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;
61
62
63
64
65
  return true;
}

bool new_lam_better(D3vector lam_old, D3vector lam_new) {

66
67
    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;
68

69
70
    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;
71
    if (MAX(lam_new) < MAX(lam_old)) return true;
72
    return false;
73
74
}

Phillip Lino Rall's avatar
Phillip Lino Rall committed
75
bool new_lam_worse(D3vector lam_old, D3vector lam_new) {
76
77
  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
78
79
80
  return false;
}

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

Phillip Lino Rall's avatar
Phillip Lino Rall committed
83
84
85
86
87
88
89
90
91
92
93
94
95
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_;
 
    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
101

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

  delete[] typ_tet;
  delete[] lambda;
}
Phillip Lino Rall's avatar
Phillip Lino Rall committed
116
117

Interpolate_on_structured_grid::Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
118
119
                                   D3vector pWSD, D3vector pENT,
                                   Blockgrid& blockgrid_) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
120
  int Nx, Ny, Nz;
121
  // int typ;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
122
123
124
125
126

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

127
128
  //int ilmin, jlmin, klmin;
  //int ilmax, jlmax, klmax;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
129
130
131
132

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

133
  // D3vector lam;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
134
135
136
137
138
139
140
141
142
143

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

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

  if(nx_>1)
    hx = (pENT.x - pWSD.x) / (nx_-1);
144
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
145
146
147
    hx = 1.0;
  if(ny_>1)
    hy = (pENT.y - pWSD.y) / (ny_-1);
148
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
149
150
151
    hy = 1.0;
  if(nz_>1)
    hz = (pENT.z - pWSD.z) / (nz_-1);
152
  else
Phillip Lino Rall's avatar
Phillip Lino Rall committed
153
154
155
156
    hz = 1.0;

  int num_total = nx * ny * nz;

157
  /*
Phillip Lino Rall's avatar
Phillip Lino Rall committed
158
159
160
161
162
  D3vector cWSD, cESD;
  D3vector cWND, cEND;

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

165
  // D3vector boxWSD, boxENT;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
166

167
  // D3vector ploc;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

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

186
#pragma omp parallel for num_threads(UGBlocks::numThreadsToTake) if(UGBlocks::useOpenMP)
Phillip Lino Rall's avatar
Phillip Lino Rall committed
187
      for(int k=0;k<Nz;++k)
188
189
    for(int j=0;j<Ny;++j)
      for(int i=0;i<Nx;++i) {
Phillip Lino Rall's avatar
Phillip Lino Rall committed
190
            // corner points of general hex-cell
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
        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
241
242
243
244
245
246
247
248
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;

249

Phillip Lino Rall's avatar
Phillip Lino Rall committed
250
251
252
253
254
255
256
257
 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;
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
        */

        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
354
355
356
357
358
359
360
  }


  for(int i=0;i<num_total;++i) {
    if(ids_hex[i]==-1) {
      // wir nehmen default value!!
      /*
361
362
363
      cout << i
       << " Error: Interpolate_on_structured_grid: I cannot interpolate all data!"
       << endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
364
365
366
367
368
369
370
371
372
      ids_hex[i] = 0;
      */
    }
    else {
      //cout << i << " Interpolate_on_structured_grid: o.k.!" << endl;
    }
  }
}

373
374
375
376
377
/////////////////////////////////////////////////////////////
// 2. Interpolate from  blockgrid  to  blockgrid
/////////////////////////////////////////////////////////////


Phillip Lino Rall's avatar
Phillip Lino Rall committed
378
379
380
381
382
383
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
384
  
Phillip Lino Rall's avatar
Phillip Lino Rall committed
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
  
  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
427

Phillip Lino Rall's avatar
Phillip Lino Rall committed
428
429
430
431
432
433
434
435
436
437
438
  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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

  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
456

Phillip Lino Rall's avatar
Phillip Lino Rall committed
457
458
459
460
      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
461
462
463
464
	    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
465

Phillip Lino Rall's avatar
Phillip Lino Rall committed
466
467
468
469
	    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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

            // 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
487
488
489
	    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
490
491
	    

Phillip Lino Rall's avatar
Phillip Lino Rall committed
492
493
494
	    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
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

	    /*
	    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
537
		  ploc = D3vector(il * hx, jl * hy, kl * hz) + pWSD;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
538
539
540

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

Phillip Lino Rall's avatar
Phillip Lino Rall committed
541
		  typ = -1;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
542

Phillip Lino Rall's avatar
Phillip Lino Rall committed
543
		  lam = lambda_of_p_in_tet(ploc,cWND,cWNT,cWST,cEST);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
		  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];
    
    
670
671
672
    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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
    
    
    /*
    
    // test GGGG
    cout << "\n WSD: " ; pWSD.Print();
    cout << "\n ENT: " ; pENT.Print();
    cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;
    */
}
  

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

Phillip Lino Rall's avatar
Phillip Lino Rall committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
  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
761
762
763
/////////////////////////////////////////////////////////////
// 3. Interpolate from Variable on a blockgrid to any point using structured intermediate grid  
/////////////////////////////////////////////////////////////
Phillip Lino Rall's avatar
Phillip Lino Rall committed
764

Phillip Lino Rall's avatar
Phillip Lino Rall committed
765
   
Phillip Lino Rall's avatar
Phillip Lino Rall committed
766

Phillip Lino Rall's avatar
Phillip Lino Rall committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
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
PointInterpolator::PointInterpolator(int nx_, int ny_, int nz_,
				     Variable<double>* U_from, double defaultInterpolation_) {
    defaultInterpolation = defaultInterpolation_;
    shiftx = 0.0;
    shifty = 0.0;
    shiftz = 0.0;
    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);  


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

    data = new double[nx*ny*nz];
    
    
    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_);

    
    /*
    
    // test GGGG
    cout << "\n WSD: " ; pWSD.Print();
    cout << "\n ENT: " ; pENT.Print();
    cout << "nx: " << nx << " ny: " << ny << " nz: " << nz << endl;
    */
}
  
PointInterpolator::PointInterpolator(int nx_, int ny_, int nz_,
				     D3vector pWSD_, D3vector pENT_,
				     Variable<double>* U_from, double defaultInterpolation_) {
    defaultInterpolation = defaultInterpolation_;
  
    shiftx = 0.0;
    shifty = 0.0;
    shiftz = 0.0;
    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();
        
    pWSD = pWSD_;
    pENT = pENT_; 

    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
    data = new double[nx*ny*nz];
    
    
    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);
    
   
    
    
    /*
    
    // 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_;
  
    nx = intermediateGrid->nx;
    ny = intermediateGrid->ny;
    nz = intermediateGrid->nz;
    
    data = new double[nx*ny*nz];
    
    pENT = intermediateGrid->pENT;
    pWSD = intermediateGrid->pWSD;
    
    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_);
}
 
 /*
Interpolate_on_structured_grid* PointInterpolator::intermediateGrid(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);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
896
897
    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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116

    
    interpolatorStructured = new Interpolate_on_structured_grid(nx,ny,nz, pWSD, pENT, *blockgrid_from);
    
    return interpolatorStructured;
}
*/


 
double PointInterpolator::evaluate(double coord_x, double coord_y, double coord_z) {  

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

  if(coord_x > pENT.x) return defaultInterpolation;
  if(coord_x < pWSD.x) return defaultInterpolation;
  if(coord_y > pENT.y) return defaultInterpolation;
  if(coord_y < pWSD.y) return defaultInterpolation;
  if(coord_z > pENT.z) return defaultInterpolation;
  if(coord_z < pWSD.z) return defaultInterpolation;
  
  //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);
  
  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;
  
  
  //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;}
  
  if      ( (uWST != defaultInterpolation) == (uWNT != defaultInterpolation) ) {uWT = uWST * (1.0 - locY) + uWNT * locY ;}
  else if ( (uWST != defaultInterpolation) && (uWNT == defaultInterpolation) ) {uWT = uWST;}
  else     								       {uWT = uWNT;}
  
  if      ( (uESD != defaultInterpolation) == (uEND != defaultInterpolation) ) {uED = uESD * (1.0 - locY) + uEND * locY ;}
  else if ( (uESD != defaultInterpolation) && (uEND == defaultInterpolation) ) {uED = uESD;}
  else  								       {uED = uEND;}
  
  if      ( (uWSD != defaultInterpolation) == (uWND != defaultInterpolation) ) {uWD = uWSD * (1.0 - locY) + uWND * locY ;}
  else if ( (uWSD != defaultInterpolation) && (uWND == defaultInterpolation) ) {uWD = uWSD;}
  else     								       {uWD = uWND;}
    
  if      ( (uET != defaultInterpolation)  == (uWT != defaultInterpolation)  ) {uT = uWT  * (1.0 - locX) + uET  * locX ;}
  else if ( (uET != defaultInterpolation)  && (uWT == defaultInterpolation)  ) {uT = uET;}
  else     								       {uT = uWT;}
  
  if      ( (uED != defaultInterpolation)  == (uWD != defaultInterpolation)  ) {uD = uWD  * (1.0 - locX) + uED  * locX ;}
  else if ( (uED != defaultInterpolation)  && (uWD == defaultInterpolation)  ) {uD = uED;}
  else     								       {uD = uWD;}
  
  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;


  

  /*
  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;
  */
}

std::vector<double> PointInterpolator::evaluateGradient(double coord_x, double coord_y, double coord_z){
    coord_x-=shiftx;
    coord_y-=shifty;
    coord_z-=shiftz;
    //cout << "coord_x y z " << coord_x << " " << coord_y << " " << coord_z << endl;
    if(coord_x > pENT.x) return std::vector<double>({0.0 , 0.0 , 0.0});
    if(coord_x < pWSD.x) return std::vector<double>({0.0 , 0.0 , 0.0});
    if(coord_y > pENT.y) return std::vector<double>({0.0 , 0.0 , 0.0});
    if(coord_y < pWSD.y) return std::vector<double>({0.0 , 0.0 , 0.0});
    if(coord_z > pENT.z) return std::vector<double>({0.0 , 0.0 , 0.0});
    if(coord_z < pWSD.z) return std::vector<double>({0.0 , 0.0 , 0.0});


    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 << "coord_i j k " << i << " " << j << " " << k << 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))];

    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;

1117

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168

    //cout << "coord_locX locY locZ " << locX << " " << locY << " " << locZ << endl;

    if (uWSD == 0.0 || uESD == 0.0 || uWND == 0.0 || uEND == 0.0 || uWST == 0.0 || uEST == 0.0 || uWNT == 0.0 || uENT == 0.0)
    {
        std::vector<double> gradient = { 0.0 , 0.0 , 0.0};
        return gradient;
    }
    double uTOT(0);
    double uET, uWT, uWD, uED;
    double uT, uD, uN, uS, uW, uE;

    double uGradientZ, uGradientX, uGradientY;

    //assume: all values are != defaultInterpolation
    //does not hold for curved interfaces
    /*
    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;
    */
    uGradientX =    uWSD * (0.0 - 1.0) * (1.0 - locY) * (1.0 - locZ) +
                    uESD *        1.0  * (1.0 - locY) * (1.0 - locZ) +
                    uWND * (0.0 - 1.0) *        locY  * (1.0 - locZ) +
                    uEND *        1.0  *        locY  * (1.0 - locZ) +
                    uWST * (0.0 - 1.0) * (1.0 - locY) *        locZ  +
                    uEST *        1.0  * (1.0 - locY) *        locZ  +
                    uWNT * (0.0 - 1.0) *        locY  *        locZ  +
                    uENT *        1.0  *        locY  *        locZ;
    uGradientY =    uWSD * (1.0 - locX) * (0.0 - 1.0) * (1.0 - locZ) +
                    uESD *        locX  * (0.0 - 1.0) * (1.0 - locZ) +
                    uWND * (1.0 - locX) *        1.0  * (1.0 - locZ) +
                    uEND *        locX  *        1.0  * (1.0 - locZ) +
                    uWST * (1.0 - locX) * (0.0 - 1.0) *        locZ  +
                    uEST *        locX  * (0.0 - 1.0) *        locZ  +
                    uWNT * (1.0 - locX) *        1.0  *        locZ  +
                    uENT *        locX  *        1.0  *        locZ;

    uGradientZ =    uWSD * (1.0 - locX) * (1.0 - locY) * (0.0 - 1.0) +
                    uESD *        locX  * (1.0 - locY) * (0.0 - 1.0) +
                    uWND * (1.0 - locX) *        locY  * (0.0 - 1.0) +
                    uEND *        locX  *        locY  * (0.0 - 1.0) +
                    uWST * (1.0 - locX) * (1.0 - locY) *        1.0  +
                    uEST *        locX  * (1.0 - locY) *        1.0  +
                    uWNT * (1.0 - locX) *        locY  *        1.0  +
                    uENT *        locX  *        locY  *        1.0;
1169

Phillip Lino Rall's avatar
Phillip Lino Rall committed
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
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
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
    std::vector<double> gradient = {uGradientX / hx, uGradientY / hy, uGradientZ / hz};

    return gradient;

}

void PointInterpolator::smoothGrid()
{
    for (int i = 1 ; i < nx -1 ; i++)
    {
        for (int j = 1 ; j < ny -1 ; j++)
        {
            for (int k = 1 ; k < nz -1 ; k++)
            {
                data[i    +nx*(j    +ny* k)] =  (2.0 * data[i    +nx*(j    +ny* k)]
                                                    + data[i+1  +nx*(j    +ny* k)]
                                                    + data[i-1  +nx*(j    +ny* k)]
                                                    + data[i+1  +nx*(j+1  +ny* k)]
                                                    + data[i+1  +nx*(j-1  +ny* k)]
                                                    + data[i+1  +nx*(j    +ny* k+1)]
                                                    + data[i+1  +nx*(j    +ny* k-1)]) / 8.0;

            }
        }
    }
}

void PointInterpolator::writeOnInterpolatedGrid(double coord_x, double coord_y, double coord_z, double value)
{
    coord_x-=shiftx;
    coord_y-=shifty;
    coord_z-=shiftz;

    if(coord_x > pENT.x) return;
    if(coord_x < pWSD.x) return;
    if(coord_y > pENT.y) return;
    if(coord_y < pWSD.y) return;
    if(coord_z > pENT.z) return;
    if(coord_z < pWSD.z) return;

    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;

    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;



    double uWSD = value * (1.0 - locX) * (1.0 - locY) * (1.0 - locZ);
    double uESD = value *        locX  * (1.0 - locY) * (1.0 - locZ);
    double uWND = value * (1.0 - locX) *        locY  * (1.0 - locZ);
    double uEND = value *        locX  *        locY  * (1.0 - locZ);
    double uWST = value * (1.0 - locX) * (1.0 - locY) *        locZ ;
    double uEST = value *        locX  * (1.0 - locY) *        locZ ;
    double uWNT = value * (1.0 - locX) *        locY  *        locZ ;
    double uENT = value *        locX  *        locY  *        locZ ;

    data[i    +nx*(j    +ny* k)] = uWSD;
    data[(i+1)+nx*(j    +ny* k)] = uESD;
    data[i    +nx*((j+1)+ny* k)] = uWND;
    data[(i+1)+nx*((j+1)+ny* k)] = uEND;
    data[i    +nx*(j    +ny*(k+1))] = uWST;
    data[(i+1)+nx*(j    +ny*(k+1))] = uEST;
    data[i    +nx*((j+1)+ny*(k+1))] = uWNT;
    data[(i+1)+nx*((j+1)+ny*(k+1))] = uENT;

    return;


}

void PointInterpolator::subtractOnInterpolatedGrid(double coord_x, double coord_y, double coord_z, double value)
{
    coord_x+=shiftx;
    coord_y+=shifty;
    coord_z+=shiftz;
    if(coord_x > pENT.x) return;
    if(coord_x < pWSD.x) return;
    if(coord_y > pENT.y) return;
    if(coord_y < pWSD.y) return;
    if(coord_z > pENT.z) return;
    if(coord_z < pWSD.z) return;

    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;

    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;



    double uWSD = value * (1.0 - locX) * (1.0 - locY) * (1.0 - locZ);
    double uESD = value *        locX  * (1.0 - locY) * (1.0 - locZ);
    double uWND = value * (1.0 - locX) *        locY  * (1.0 - locZ);
    double uEND = value *        locX  *        locY  * (1.0 - locZ);
    double uWST = value * (1.0 - locX) * (1.0 - locY) *        locZ ;
    double uEST = value *        locX  * (1.0 - locY) *        locZ ;
    double uWNT = value * (1.0 - locX) *        locY  *        locZ ;
    double uENT = value *        locX  *        locY  *        locZ ;

    data[i    +nx*(j    +ny* k)] -= uWSD;
    data[(i+1)+nx*(j    +ny* k)] -= uESD;
    data[i    +nx*((j+1)+ny* k)] -= uWND;
    data[(i+1)+nx*((j+1)+ny* k)] -= uEND;
    data[i    +nx*(j    +ny*(k+1))] -= uWST;
    data[(i+1)+nx*(j    +ny*(k+1))] -= uEST;
    data[i    +nx*((j+1)+ny*(k+1))] -= uWNT;
    data[(i+1)+nx*((j+1)+ny*(k+1))] -= uENT;

    return;
}

void PointInterpolator::shiftInterpolatedGrid(double shift_x, double shift_y, double shift_z)
{
    shiftx = shift_x;
    shifty = shift_y;
    shiftz = shift_z;
}

void PointInterpolator::scaleInterpolatedData(double scale, double zeroVal)
{
    if (scale == 1.0)
    {
        return;
    }
    for (int k = 0; k < nz; k++)
    {
        for (int j = 0; j < ny; j++)
        {
            for (int i = 0; i < nx; i++)
            {
                if (data[i    +nx*(j    +ny* k)] != defaultInterpolation )
                {
                    data[i    +nx*(j    +ny* k)] = (data[i    +nx*(j    +ny* k)] - zeroVal) * scale + zeroVal;
                    if (data[i    +nx*(j    +ny* k)] <= 1.0 & zeroVal != 0.0)
                    {
                        cout << "Warning:  " << data[i    +nx*(j    +ny* k)] << endl;
                    }
                }
            }
        }
    }
}

 
PointInterpolator::~PointInterpolator() {
    delete interpolatorStructured;
    delete[] data;
}
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374



void Interpolate_direct::init()
{
    arrayBoxWSDENT.resize(blockgrid->Give_unstructured_grid()->Give_number_hexahedra());
    array_box_boundary.resize(blockgrid->Give_unstructured_grid()->Give_number_hexahedra());

    int counter = 0;
    int counterTwoNeighbours = 0;
    int counterBoxesAtBoundary = 0;

    for (int id_hex = 0 ; id_hex < blockgrid->Give_unstructured_grid()->Give_number_hexahedra() ; id_hex++)
    {
        int Nx = blockgrid->Give_Nx_hexahedron(id_hex);
        int Ny = blockgrid->Give_Ny_hexahedron(id_hex);
        int Nz = blockgrid->Give_Nz_hexahedron(id_hex);
        arrayBoxWSDENT.at(id_hex).resize(Nx*Ny*Nz);
        array_box_boundary.at(id_hex).resize(Nx*Ny*Nz);
    }
    std::vector<std::vector<std::vector<int> > >addToIndex;
    addToIndex.resize(blockgrid->Give_unstructured_grid()->Give_number_hexahedra());


    for (int id_hex = 0 ; id_hex < blockgrid->Give_unstructured_grid()->Give_number_hexahedra() ; id_hex++)
    {
        int Nx = blockgrid->Give_Nx_hexahedron(id_hex); // now without +1, since for ( n gridpoints, only n-1 surrounding blocks exist!)
        int Ny = blockgrid->Give_Ny_hexahedron(id_hex);
        int Nz = blockgrid->Give_Nz_hexahedron(id_hex);
        addToIndex.at(id_hex).resize(blockgrid->Give_unstructured_grid()->Give_number_hexahedra());
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
        std::vector<std::vector<std::vector< int > > > indexAllFacesInside(6);
        std::vector<std::vector<std::vector< int > > > indexAllFacesOutside(6);
        std::vector<std::vector< int > > indicesOfFirstFaceInside(0);
        std::vector<std::vector<std::vector< int > > > correctRelation(6);
        std::vector<int> emptyVectorInt(4);
        std::vector<std::vector<int> > emptyVector(3);
        emptyVector.at(0) = emptyVectorInt; emptyVector.at(1) = emptyVectorInt; emptyVector.at(2) = emptyVectorInt;
        //iterate each side separately;
        int i = Nx;
        int j = Ny;
        int k = Nz;
        for (j = 0 ; j < Ny+1; j = j + Ny) for (k = 0 ; k < Nz+1; k = k + Nz)
        {
            std::vector<int> temp(0);
            temp.push_back(id_hex);temp.push_back(i);temp.push_back(j);temp.push_back(k);
            int indexInner = i    +(Nx+1)*(j    +(Ny+1)* k) ;
            for (int iterIndexToAdd = 0 ; iterIndexToAdd <(int)blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).size() ; iterIndexToAdd+=4 )
1392
            {
1393
1394
1395
1396
                std::vector<int>::const_iterator first = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + iterIndexToAdd;
                std::vector<int>::const_iterator last  = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + 4 + iterIndexToAdd;
                //indicesOfFirstFaceOutside.push_back(std::vector<int>(first, last));
                indexAllFacesOutside.at(0).push_back(std::vector<int>(first, last));
1397
            }
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
            //indicesOfFirstFaceInside.push_back(temp);
            indexAllFacesInside.at(0).push_back(temp);
        }
        //filter wrong Neighbours (from other side)
        //indicesOfFirstFaceOutside = filterCorrectNeighbours(indicesOfFirstFaceOutside);
        indexAllFacesOutside.at(0) = filterCorrectNeighbours(indexAllFacesOutside.at(0));
        if (indexAllFacesOutside.at(0).empty())
        {
            //cout << "no outer neighbours here ! " << endl;
            correctRelation.at(0) = emptyVector;
        }
        else
        {
            correctRelation.at(0) = calculateNeighbourIndexRelation(indexAllFacesInside.at(0), indexAllFacesOutside.at(0));
        }
1413

1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
        i = 0;
        for (j = 0 ; j < Ny+1; j = j + Ny) for (k = 0 ; k < Nz+1; k = k + Nz)
        {
            std::vector<int> temp(0);
            temp.push_back(id_hex);temp.push_back(i);temp.push_back(j);temp.push_back(k);
            int indexInner = i    +(Nx+1)*(j    +(Ny+1)* k) ;
            for (int iterIndexToAdd = 0 ; iterIndexToAdd <(int)blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).size() ; iterIndexToAdd+=4 )
            {
                std::vector<int>::const_iterator first = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + iterIndexToAdd;
                std::vector<int>::const_iterator last  = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + 4 + iterIndexToAdd;
                indexAllFacesOutside.at(1).push_back(std::vector<int>(first, last));
1425
            }
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
            indexAllFacesInside.at(1).push_back(temp);
        }
        indexAllFacesOutside.at(1) = filterCorrectNeighbours(indexAllFacesOutside.at(1));
        if (indexAllFacesOutside.at(1).empty())
        {
            //cout << "no outer neighbours here ! " << endl;
            correctRelation.at(1) = emptyVector;
        }
        else
        {
        correctRelation.at(1) = calculateNeighbourIndexRelation(indexAllFacesInside.at(1), indexAllFacesOutside.at(1));
        }
1438

1439
1440
1441
1442
1443
1444
1445
        j = Ny;
        for (i = 0 ; i < Nx+1; i = i + Nx) for (k = 0 ; k < Nz+1; k = k + Nz)
        {
            std::vector<int> temp(0);
            temp.push_back(id_hex);temp.push_back(i);temp.push_back(j);temp.push_back(k);
            int indexInner = i    +(Nx+1)*(j    +(Ny+1)* k) ;
            for (int iterIndexToAdd = 0 ; iterIndexToAdd <(int)blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).size() ; iterIndexToAdd+=4 )
1446
            {
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
                std::vector<int>::const_iterator first = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + iterIndexToAdd;
                std::vector<int>::const_iterator last  = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + 4 + iterIndexToAdd;
                indexAllFacesOutside.at(2).push_back(std::vector<int>(first, last));
            }
            indexAllFacesInside.at(2).push_back(temp);
        }
        indexAllFacesOutside.at(2) = filterCorrectNeighbours(indexAllFacesOutside.at(2));
        if (indexAllFacesOutside.at(2).empty())
        {
            //cout << "no outer neighbours here ! " << endl;
            correctRelation.at(2) = emptyVector;
        }
        else
        {
        correctRelation.at(2) = calculateNeighbourIndexRelation(indexAllFacesInside.at(2), indexAllFacesOutside.at(2));
        }
1463

1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
        j = 0;
        for (i = 0 ; i < Nx+1; i = i + Nx) for (k = 0 ; k < Nz+1; k = k + Nz)
        {
            std::vector<int> temp(0);
            temp.push_back(id_hex);temp.push_back(i);temp.push_back(j);temp.push_back(k);
            int indexInner = i    +(Nx+1)*(j    +(Ny+1)* k) ;
            for (int iterIndexToAdd = 0 ; iterIndexToAdd <(int)blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).size() ; iterIndexToAdd+=4 )
            {
                std::vector<int>::const_iterator first = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + iterIndexToAdd;
                std::vector<int>::const_iterator last  = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + 4 + iterIndexToAdd;
                indexAllFacesOutside.at(3).push_back(std::vector<int>(first, last));
            }
            indexAllFacesInside.at(3).push_back(temp);
        }
        indexAllFacesOutside.at(3) = filterCorrectNeighbours(indexAllFacesOutside.at(3));
        if (indexAllFacesOutside.at(3).empty())
        {
            //cout << "no outer neighbours here ! " << endl;
            correctRelation.at(3) = emptyVector;
        }
        else
        {
        correctRelation.at(3) = calculateNeighbourIndexRelation(indexAllFacesInside.at(3), indexAllFacesOutside.at(3));
        }
1488

1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
        k = Nz;
        for (i = 0 ; i < Nx+1; i = i + Nx) for (j = 0 ; j < Ny+1; j = j + Ny)
        {
            std::vector<int> temp(0);
            temp.push_back(id_hex);temp.push_back(i);temp.push_back(j);temp.push_back(k);
            int indexInner = i    +(Nx+1)*(j    +(Ny+1)* k) ;
            for (int iterIndexToAdd = 0 ; iterIndexToAdd <(int)blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).size() ; iterIndexToAdd+=4 )
            {
                std::vector<int>::const_iterator first = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + iterIndexToAdd;
                std::vector<int>::const_iterator last  = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + 4 + iterIndexToAdd;
                indexAllFacesOutside.at(4).push_back(std::vector<int>(first, last));
            }
            indexAllFacesInside.at(4).push_back(temp);
        }
        indexAllFacesOutside.at(4) = filterCorrectNeighbours(indexAllFacesOutside.at(4));
        if (indexAllFacesOutside.at(4).empty())
        {
            correctRelation.at(4) = emptyVector;
        }
        else
        {
            correctRelation.at(4) = calculateNeighbourIndexRelation(indexAllFacesInside.at(4), indexAllFacesOutside.at(4));
        }
1512

1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
        k = 0;
        for (i = 0 ; i < Nx+1; i = i + Nx) for (j = 0 ; j < Ny+1; j = j + Ny)
        {
            std::vector<int> temp(0);
            temp.push_back(id_hex);temp.push_back(i);temp.push_back(j);temp.push_back(k);
            int indexInner = i    +(Nx+1)*(j    +(Ny+1)* k) ;
            for (int iterIndexToAdd = 0 ; iterIndexToAdd <(int)blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).size() ; iterIndexToAdd+=4 )
            {
                std::vector<int>::const_iterator first = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + iterIndexToAdd;
                std::vector<int>::const_iterator last  = blockgrid->Give_blockgrid_coordinates()->blockgrid_hexa_boundary.at(id_hex).at(indexInner).begin() + 4 + iterIndexToAdd;
                indexAllFacesOutside.at(5).push_back(std::vector<int>(first, last));
1524
            }
1525
1526
1527
1528
1529
1530
1531
1532
1533
            indexAllFacesInside.at(5).push_back(temp);
        }
        indexAllFacesOutside.at(5) = filterCorrectNeighbours(indexAllFacesOutside.at(5));
        if (indexAllFacesOutside.at(5).empty())
        {
            correctRelation.at(5) = emptyVector;
        }
        else
        {
1534
            correctRelation.at(5) = calculateNeighbourIndexRelation(indexAllFacesInside.at(5), indexAllFacesOutside.at(5));
1535
        }
1536

1537
1538
            //init surrounding boxes!
            for(int i=0;i<Nx;++i) for(int j=0;j<Ny;++j) for(int k=0;k<Nz;++k)
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
            {
                arrayBoxWSDENT.at(id_hex).at(i    +Nx*(j    +Ny* k)).resize(2);
                D3vector cWSD, cESD;
                D3vector cWND, cEND;

                D3vector cWST, cEST;
                D3vector cWNT, cENT;

                D3vector boxWSD, boxENT;
                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);
1557
                //isPlane()
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578

                    // 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;
                arrayBoxWSDENT.at(id_hex).at(i    +Nx*(j    +Ny* k)).at(0) = boxWSD;
                arrayBoxWSDENT.at(id_hex).at(i    +Nx*(j    +Ny* k)).at(1) = boxENT;



              }
1579
1580
1581
1582


            i = Nx;
            for (j = 0 ; j < Ny+1; j++) for (k = 0 ; k < Nz+1; k++)
1583
            {
1584
1585
1586
                if (!indexAllFacesOutside.at(0).empty())
                {calculateNeighbourIndex(correctRelation.at(0), indexAllFacesOutside.at(0).at(0).at(0), id_hex, i,j,k,Nx,Ny,Nz);}
            }
1587

1588
1589
1590
1591
1592
1593
1594
            i = 0;
            for (j = 0 ; j < Ny+1; j++) for (k = 0 ; k < Nz+1; k++)
            {
                if (!indexAllFacesOutside.at(1).empty())
                {calculateNeighbourIndex(correctRelation.at(1), indexAllFacesOutside.at(1).at(0).at(0), id_hex, i,j,k,Nx,Ny,Nz);}
                correctRelation.at(1);
            }
1595

1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
            j = Ny;
            for (i = 0 ; i < Nx+1; i++) for (k = 0 ; k < Nz+1; k++)
            {
                if (!indexAllFacesOutside.at(2).empty())
                {calculateNeighbourIndex(correctRelation.at(2), indexAllFacesOutside.at(2).at(0).at(0), id_hex, i,j,k,Nx,Ny,Nz);}
                correctRelation.at(2);
            }


            j = 0;
            for (i = 0 ; i < Nx+1; i++) for (k = 0 ; k < Nz+1; k++)
            {
                if (!indexAllFacesOutside.at(3).empty())
                {calculateNeighbourIndex(correctRelation.at(3), indexAllFacesOutside.at(3).at(0).at(0), id_hex, i,j,k,Nx,Ny,Nz);}
                correctRelation.at(3);
            }


            k = Nz;
            for (i = 0 ; i < Nx+1; i++) for (j = 0 ; j < Ny+1; j++)
            {
                if (!indexAllFacesOutside.at(4).empty())
                {calculateNeighbourIndex(correctRelation.at(4), indexAllFacesOutside.at(4).at(0).at(0), id_hex, i,j,k,Nx,Ny,Nz);}
                correctRelation.at(4);
            }


            k = 0;
            for (i = 0 ; i < Nx+1; i++) for (j = 0 ; j < Ny+1; j++)
            {
                if (!indexAllFacesOutside.at(5).empty())
                {calculateNeighbourIndex(correctRelation.at(5), indexAllFacesOutside.at(5).at(0).at(0), id_hex, i,j,k,Nx,Ny,Nz);}
                correctRelation.at(5);
            }



1633
    }
1634
1635
1636
1637
1638
    boxCounter = 0;
    //test for boxes: check, if neighbour is actually the neighbour!
    if (true)
    {

1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
    for (int idHex = 0 ; idHex < blockgrid->Give_unstructured_grid()->Give_number_hexahedra() ; idHex++)
    {
        int Nx = blockgrid->Give_Nx_hexahedron(idHex);
        int Ny = blockgrid->Give_Ny_hexahedron(idHex);
        int Nz = blockgrid->Give_Nz_hexahedron(idHex);
        for(int i=0;i<Nx;++i) for(int j=0;j<Ny;++j) for(int k=0;k<Nz;++k)
        {
            for (int iterHexaTemp = 0 ; iterHexaTemp < array_box_boundary.at(idHex).at(i    +(Nx)*(j    +(Ny)* k)).size() ; iterHexaTemp +=4)
            {
                int index = i    +(Nx)*(j    +(Ny)* k) ;
                int idHexTemp = array_box_boundary.at(idHex).at(index).at(iterHexaTemp+0);
                int iTemp = array_box_boundary.at(idHex).at(index).at(iterHexaTemp+1);
                int jTemp = array_box_boundary.at(idHex).at(index).at(iterHexaTemp+2);
                int kTemp = array_box_boundary.at(idHex).at(index).at(iterHexaTemp+3);

1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
                int NxOuter = blockgrid->Give_Nx_hexahedron(idHexTemp);
                int NyOuter = blockgrid->Give_Ny_hexahedron(idHexTemp);
                int NzOuter = blockgrid->Give_Nz_hexahedron(idHexTemp);

                int indexOuter = iTemp    +NxOuter*(jTemp    +NyOuter* kTemp);
                int indexInner = i    +Nx*(j    +Ny* k);
                D3vector boxWSDInner =  arrayBoxWSDENT.at(idHex).at(indexInner).at(0);
                D3vector boxENTInner =  arrayBoxWSDENT.at(idHex).at(indexInner).at(1);
                D3vector boxWSDOuter=  arrayBoxWSDENT.at(idHexTemp).at(indexOuter).at(0);
                D3vector boxENTOuter=  arrayBoxWSDENT.at(idHexTemp).at(indexOuter).at(1);

                    if (!checkOverlapOfBoxes(boxWSDInner, boxENTInner, boxWSDOuter, boxENTOuter))
                    {
                        writeBox(boxWSDInner, boxENTInner, std::string("box"));
                        writeBox(boxWSDOuter, boxENTOuter, std::string("box"));
                        cout << "surrounding1 box is wrong!" << endl;
                    }
                    else
                    {
                       // cout << "surrounding box1 is right!" << endl;
                    }
             }
        }
    }
    }

//    cout << "counter " << counter << endl;
//    cout << "boundary boxes " << counterBoxesAtBoundary<< endl;
//    cout << "counterTwoNeighbours " << counterTwoNeighbours << endl;
//    cout << "counterTwoNeighbours " << counterTwoNeighbours << endl;

}

1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
void Interpolate_direct::updateBoundaryBoxes()
{
    for (int id_hex = 0 ; id_hex < blockgrid->Give_unstructured_grid()->Give_number_hexahedra() ; id_hex++)
    {
        int Nx = blockgrid->Give_Nx_hexahedron(id_hex);
        int Ny = blockgrid->Give_Ny_hexahedron(id_hex);
        int Nz = blockgrid->Give_Nz_hexahedron(id_hex);
        for(int i=0;i<Nx;++i) for(int j=0;j<Ny;++j) for(int k=0;k<Nz;++k)
        {
            arrayBoxWSDENT.at(id_hex).at(i    +Nx*(j    +Ny* k)).resize(2);
            D3vector cWSD, cESD;
            D3vector cWND, cEND;

            D3vector cWST, cEST;
            D3vector cWNT, cENT;

            D3vector boxWSD, boxENT;
            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;
            arrayBoxWSDENT.at(id_hex).at(i    +Nx*(j    +Ny* k)).at(0) = boxWSD;
            arrayBoxWSDENT.at(id_hex).at(i    +Nx*(j    +Ny* k)).at(1) = boxENT;
         }
    }
}


1735
1736
1737
std::vector<std::vector<int> > Interpolate_direct::calculateNeighbourIndexRelation(std::vector<std::vector<int> > inner, std::vector<std::vector<int> > outer)
{
    // 1 : find index which does not change --> not part of the boundary
1738
1739
1740
1741
1742

    if (outer.size() != inner.size())
    {
        cout << "ops " << endl;
    }
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
    std::vector<int> diffToAdd(4);
    std::vector<int> IndexToInvert(4);
    std::vector<int> IndexToSwitch(4);
    std::vector<std::vector<int> > retVal(0);
    int indexNotAtBoundaryInner;
    for ( int iter = 0 ; iter < 4 ; iter++)
    {
        bool found = true;
        for ( int iterInner = 0 ; iterInner < 3 ; iterInner++)
        {
            if (inner.at(iterInner).at(iter) == inner.at(iterInner+1).at(iter) )
            {

            }
            else
            {
                found = false;
1760
            }
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
        }
        if (found)
        {
            //first one is always the hex id, but its getting overwritten by the correct one .
            indexNotAtBoundaryInner = iter;
        }
    }
    int indexNotAtBoundaryOuter;
    for ( int iter = 0 ; iter < 4 ; iter++)
    {
        bool found = true;
        for ( int iterInner = 0 ; iterInner < 3 ; iterInner++)
        {
            if (outer.at(iterInner).at(iter) == outer.at(iterInner+1).at(iter) )
            {
1776

1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
            }
            else
            {
                found = false;
            }
        }
        if (found)
        {
            //first one is always the hex id, but its getting overwritten by the correct one .
            indexNotAtBoundaryOuter = iter;
1787
1788
        }
    }
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
    diffToAdd.at(indexNotAtBoundaryInner) = outer.front().at(indexNotAtBoundaryOuter) - inner.front().at(indexNotAtBoundaryInner);
    retVal.push_back(diffToAdd);
    //check index, which does NOT change and calculate difference

    //case 1 :: 0 invert and 0 rotation
     if (compareIndicies(inner,outer,indexNotAtBoundaryOuter))
     {
        //cout << "correct found !  No changes necessary"<< endl;
     }

    std::vector<std::vector< int > > innerModified(0);

    // case 0 : 0 invert  and 0-5 rotation
    if (compareIndicies(inner,outer,indexNotAtBoundaryOuter))