interpol.h 22.6 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
/**********************************************************************************
 * 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.
 **********************************************************************************/

// ------------------------------------------------------------
//
Phillip Lino Rall's avatar
Phillip Lino Rall committed
21
// variable.h
Phillip Lino Rall's avatar
Phillip Lino Rall committed
22
23
24
25
26
27
28
29
30
//
// ------------------------------------------------------------

#ifndef INTERPOL_H
#define INTERPOL_H

/////////////////////////////////////////////////////////////
// 1. Interpolate from  blockgrid to rectangular blockgrid
// 2. Interpolate from  blockgrid  to  blockgrid
31
32
// 3. Interpolate from Variable on a blockgrid to any point using structured intermediate grid
// 4. Interpolate from blockgrid direct to any point
Phillip Lino Rall's avatar
Phillip Lino Rall committed
33
34
35
36
37
38
/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
// 1. Interpolate from  blockgrid to rectangular blockgrid
/////////////////////////////////////////////////////////////

Christoph Pflaum's avatar
Christoph Pflaum committed
39
40
41
42
/** \addtogroup InterpolationOperators **/
/* @{ */  
/**
 * Interpolation 3D:
Phillip Lino Rall's avatar
Phillip Lino Rall committed
43
data structure on structured grid
Christoph Pflaum's avatar
Christoph Pflaum committed
44
 \verbatim
Phillip Lino Rall's avatar
Phillip Lino Rall committed
45
46
ny    *  *  *  *  *  *
ny-1  *  +  +  +  +  *
Phillip Lino Rall's avatar
Phillip Lino Rall committed
47
48
49
50
3     *  +  +  +  +  *
2     *  +  +  +  +  *
1     *  +  +  +  +  *
0     *  *  *  *  *  *
Phillip Lino Rall's avatar
Phillip Lino Rall committed
51
      0  1  2  3 nx-1 nx
Christoph Pflaum's avatar
Christoph Pflaum committed
52
  \endverbatim      
Phillip Lino Rall's avatar
Phillip Lino Rall committed
53
 * interpolates data from blockgrid_ to rectangular block [pWSD, pENT]
Christoph Pflaum's avatar
Christoph Pflaum committed
54
**/
55
class Interpolate_direct;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
56
57
class Interpolate_on_structured_grid {
 public:
Christoph Pflaum's avatar
Christoph Pflaum committed
58
/**
Phillip Lino Rall's avatar
Phillip Lino Rall committed
59
60
61
62
63
64
65
 * preparation for interpolation
 @param  nx_ number of structured grid points x-direction
 @param  ny_ number of structured grid points y-direction
 @param  nz_ number of structured grid points z-direction
 @param  pWSD Corner WSD of structured grid
 @param  pENT Corner WSD of structured grid
 @param  blockgrid_ of unstructured grid
Christoph Pflaum's avatar
Christoph Pflaum committed
66
**/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
67
68
  Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
				 D3vector pWSD, D3vector pENT,
69
                 Blockgrid& blockgrid_, bool trilinearInterpolationFlag_ = false);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
70

Phillip Lino Rall's avatar
Phillip Lino Rall committed
71
  Interpolate_on_structured_grid(int nx_, int ny_, int nz_,
72
                 Blockgrid& blockgrid_, bool trilinearInterpolationFlag_ = false);
73

74
  ~Interpolate_on_structured_grid();
Christoph Pflaum's avatar
Christoph Pflaum committed
75
/**
Phillip Lino Rall's avatar
Phillip Lino Rall committed
76
77
78
 * interpolates from Variable u(of unstructured blockgrid) to structured data
 @param  u:  Variable on unstructured blockgrid
 @param  data: pointer to structured grid data i+nx*(j+ny*k)
Christoph Pflaum's avatar
Christoph Pflaum committed
79
**/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
80
81
  template <class DTyp>
    void interpolate(Variable<DTyp>& u, DTyp* data, DTyp defaultInterpolation);
82

83
    void update_Interpolate_on_structured_grid(Blockgrid& blockgrid_, bool onlyOnSurfaceZ = false);
84
    void setInterpolateDirect(Interpolate_direct *id_){id = id_;}
85

86
87
88
89
90
91
92
93
    int getNx(){return nx;}
    int getNy(){return ny;}
    int getNz(){return nz;}

    double getHx(){return hx;}
    double getHy(){return hy;}
    double getHz(){return hz;}
public:
94
95
96

    D3vector trilinarInterpolation(D3vector X, int id_Hex, int i,  int j,  int k );
    bool trilinearInterpolationFlag{true};
Phillip Lino Rall's avatar
Phillip Lino Rall committed
97
98
99
  int nx, ny, nz;
  D3vector pENT,pWSD;
  private:
Phillip Lino Rall's avatar
Phillip Lino Rall committed
100
101
102
103
104
105
  int* ids_hex;
  int *ids_i, *ids_j, *ids_k;
  int *typ_tet;

  D3vector *lambda;

Phillip Lino Rall's avatar
Phillip Lino Rall committed
106
  
Phillip Lino Rall's avatar
Phillip Lino Rall committed
107
108
109
110
  double hx, hy, hz;

  Blockgrid*        blockgrid;
  Unstructured_grid*       ug;
111
112

  Interpolate_direct* id{NULL};
Phillip Lino Rall's avatar
Phillip Lino Rall committed
113
};
Christoph Pflaum's avatar
Christoph Pflaum committed
114
/* @} */
115
116


Phillip Lino Rall's avatar
Phillip Lino Rall committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
template <class DTyp>
class Give_corner_data_of_cube {
 public:
  Give_corner_data_of_cube(Variable<DTyp>& u_, int hex_, int i_, int j_, int k_);
  
  DTyp WSD() { return u->template Give_data<hexahedronEl>(id, i,   j,   k,   Nx, Ny); }
  DTyp ESD() { return u->template Give_data<hexahedronEl>(id, i+1, j,   k,   Nx, Ny); }
  DTyp WND() { return u->template Give_data<hexahedronEl>(id, i,   j+1, k,   Nx, Ny); }
  DTyp END() { return u->template Give_data<hexahedronEl>(id, i+1, j+1, k,   Nx, Ny); }
  DTyp WST() { return u->template Give_data<hexahedronEl>(id, i,   j,   k+1, Nx, Ny); }
  DTyp EST() { return u->template Give_data<hexahedronEl>(id, i+1, j,   k+1, Nx, Ny); }
  DTyp WNT() { return u->template Give_data<hexahedronEl>(id, i,   j+1, k+1, Nx, Ny); }
  DTyp ENT() { return u->template Give_data<hexahedronEl>(id, i+1, j+1, k+1, Nx, Ny); }

 private:
  int id, i, j, k;
  int Nx, Ny;
  Variable<DTyp>* u;
};

template <class DTyp>
Give_corner_data_of_cube<DTyp>::Give_corner_data_of_cube(Variable<DTyp>& u_,
							 int hex_, int i_, int j_, int k_) {
  Blockgrid*        blockgrid;

  u = &u_;
  blockgrid = u->Give_blockgrid();
  id = hex_; i = i_;  j = j_;  k = k_;
  
  Nx = blockgrid->Give_Nx_hexahedron(id);
  Ny = blockgrid->Give_Ny_hexahedron(id);
}


template <class DTyp>
void Interpolate_on_structured_grid::interpolate(Variable<DTyp>& u, DTyp* data, DTyp defaultInterpolation) {
  int i,j,k, id_hex, typ;
  int ind_global;


157

Phillip Lino Rall's avatar
Phillip Lino Rall committed
158
159
160
161
162
163
  for(int id=0;id<ug->Give_number_hexahedra();++id)
    u.template Update<hexahedronEl>(id);

  for(int ks = 0; ks < nz;++ks)
    for(int js = 0; js < ny;++js)
      for(int is = 0; is < nx;++is) {
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    ind_global = is+nx*(js+ny*ks);
    i = ids_i[ind_global];
    j = ids_j[ind_global];
    k = ids_k[ind_global];
    id_hex = ids_hex[ind_global];

    /*
    // test : das geht nicht::
    if(id_hex < 0) {
      ind_global = is+nx*((ny-1-js)+ny*ks);
      i = ids_i[ind_global];
      j = ids_j[ind_global];
      k = ids_k[ind_global];
      id_hex = ids_hex[ind_global];
    }
*/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
180
181
182
183
184
	if(id_hex < 0) data[ind_global] = defaultInterpolation;
	else {
	  Give_corner_data_of_cube<DTyp> du(u, id_hex, i,j,k);

	  typ = typ_tet[ind_global];
Phillip Lino Rall's avatar
Phillip Lino Rall committed
185
          //  std::cout << typ << std::endl;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
186
187
188
189
190
191
192
193
194
195
196
197
	
	  /*
	  if( ind_global == 3)
	    cout << " \n ind_global: " << is + nx * (js + ny * ks)
	         << " typ: " << typ
	         << " id_hex " << id_hex
	         << " i: " << i
	         << " j: " << j
	         << " k: " << k
	         << endl;
  	  */

198
      if(typ==0) data[ind_global] = interpolate_in_tet(lambda[ind_global],
Phillip Lino Rall's avatar
Phillip Lino Rall committed
199
							   du.WND(),du.WNT(),du.WST(),du.EST());
200
      if(typ==1) data[ind_global] = interpolate_in_tet(lambda[ind_global],
Phillip Lino Rall's avatar
Phillip Lino Rall committed
201
							   du.EST(),du.WND(),du.WST(),du.ESD());
202
      if(typ==2) data[ind_global] = interpolate_in_tet(lambda[ind_global],
Phillip Lino Rall's avatar
Phillip Lino Rall committed
203
							   du.WND(),du.WSD(),du.WST(),du.ESD());
204
205
206
      if(typ==3) data[ind_global] = interpolate_in_tet(lambda[ind_global],
                               du.EST(),du.WND(),du.ESD(),du.END());
      if(typ==4) data[ind_global] = interpolate_in_tet(lambda[ind_global],
Phillip Lino Rall's avatar
Phillip Lino Rall committed
207
							   du.ENT(),du.WNT(),du.EST(),du.END());
208
      if(typ==5) data[ind_global] = interpolate_in_tet(lambda[ind_global],
Phillip Lino Rall's avatar
Phillip Lino Rall committed
209
							   du.WNT(),du.WND(),du.EST(),du.END());
210
211
      if(typ==6) data[ind_global] = interpolate_in_tet_trilinear(lambda[ind_global],du.END(),du.ESD(),du.WSD(),du.WND(),
                                                                 du.WNT(),du.WST(),du.EST(),du.ENT());
Phillip Lino Rall's avatar
Phillip Lino Rall committed
212
213
214
215
216
217
218
219
	}
      }
}

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

Christoph Pflaum's avatar
Christoph Pflaum committed
220
221
222
/** \addtogroup InterpolationOperators **/
/* @{ */  
/**
Phillip Lino Rall's avatar
Phillip Lino Rall committed
223
 * interpolates data from blockgrid_from to blockgrid_to_ using an internal rectangular grid if size nx,ny,nz
Christoph Pflaum's avatar
Christoph Pflaum committed
224
**/
225
226
227
228
229
230
231
232
233
234
class PointInterpolator;

class Interpolate_on_block_grid_from_pointinterpolator {
public:
/**
* If a pointinterpolator is accesible, it can be used directly to interpolate on blockgrid_to
@param  blockgrid_ of unstructured grid
**/

 Interpolate_on_block_grid_from_pointinterpolator(PointInterpolator *interp, Blockgrid* blockgrid_to_ );
235
 //~Interpolate_on_block_grid_from_pointinterpolator();
Phillip Lino Rall's avatar
Phillip Lino Rall committed
236

Phillip Lino Rall's avatar
Phillip Lino Rall committed
237
 void interpolate(Variable<double>* U_to, Boundary_Marker *marker = NULL);
238
239
240
241
242
243
244
245

 double evaluate(double coord_x, double coord_y, double coord_z);


private:
 int nx, ny, nz;
 double hx, hy, hz;
 PointInterpolator *pointInterpolator;
246
// double* data;
247
248
249
250
251
252
253
 Blockgrid* blockgrid_to;

 D3vector pWSD;
 D3vector pENT;

};

Phillip Lino Rall's avatar
Phillip Lino Rall committed
254
255
class Interpolate_on_block_grid {
 public:
Christoph Pflaum's avatar
Christoph Pflaum committed
256
/**
Phillip Lino Rall's avatar
Phillip Lino Rall committed
257
258
259
260
261
 * preparation for interpolation
 @param  nx_ number of structured grid points x-direction
 @param  ny_ number of structured grid points y-direction
 @param  nz_ number of structured grid points z-direction
 @param  blockgrid_ of unstructured grid
Christoph Pflaum's avatar
Christoph Pflaum committed
262
**/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
263
  Interpolate_on_block_grid(int nx_, int ny_, int nz_,
264
265
                    Blockgrid* blockgrid_from, Blockgrid* blockgrid_to_);
  Interpolate_on_block_grid(PointInterpolator *interp, Blockgrid* blockgrid_to_ );
Phillip Lino Rall's avatar
Phillip Lino Rall committed
266
267
268
269
270
271
272
  ~Interpolate_on_block_grid();
  
  /**
   * interpoliert Daten. Falls an einem Punkt nicht interpoliert werden kann
   * da U_from keine Zelle hat, dann wird
   *     defaultInterpolation
   * verwendet.
Christoph Pflaum's avatar
Christoph Pflaum committed
273
   **/ 
Phillip Lino Rall's avatar
Phillip Lino Rall committed
274
  void interpolate(Variable<double>* U_from, Variable<double>* U_to, double defaultInterpolation = 0.0);
275

Phillip Lino Rall's avatar
Phillip Lino Rall committed
276
  double evaluate(double coord_x, double coord_y, double coord_z);
277
278


Phillip Lino Rall's avatar
Phillip Lino Rall committed
279
280
281
282
private:
  int nx, ny, nz;
  double hx, hy, hz;
  Interpolate_on_structured_grid* interpolatorStructured;
283
  PointInterpolator *pointInterpolator;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
284
285
286
287
288
289
290
  double* data;
  Blockgrid* blockgrid_to;
  
  D3vector pWSD;
  D3vector pENT;
  
};
Christoph Pflaum's avatar
Christoph Pflaum committed
291
/* @} */
Phillip Lino Rall's avatar
Phillip Lino Rall committed
292

Phillip Lino Rall's avatar
Phillip Lino Rall committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

/////////////////////////////////////////////////////////////
// 3. Interpolate from Variable on a blockgrid to any point using structured intermediate grid  
/////////////////////////////////////////////////////////////

/*
class Intermadiate_grid_for_PointInterpolator : public Interpolate_on_structured_grid
{
public:
  Intermadiate_grid_for_PointInterpolator(int nx_, int ny_, int nz_, Variable<double>* U_from);
  int nx, ny, nz;
  Interpolate_on_structured_grid* interpolatorStructured(int nx_, int ny_, int nz_,D3vector pWSD, D3vector pENT,
		                                          Variable<double>* U_from);  
  
  D3vector pWSD;
  D3vector pENT;
};
Christoph Pflaum's avatar
Christoph Pflaum committed
310
*/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
311

Christoph Pflaum's avatar
Christoph Pflaum committed
312
313
314
/** \addtogroup InterpolationOperators **/
/* @{ */  
/**
Phillip Lino Rall's avatar
Phillip Lino Rall committed
315
316
317
318
319
 * interpolates data from blockgrid_from to blockgrid_to_ using an internal rectangular grid if size nx,ny,nz
***/
class PointInterpolator {
  friend Interpolate_on_structured_grid;
 public:
Christoph Pflaum's avatar
Christoph Pflaum committed
320
/**
Phillip Lino Rall's avatar
Phillip Lino Rall committed
321
322
323
324
325
 * preparation for interpolation
 @param  nx_ number of structured grid points x-direction
 @param  ny_ number of structured grid points y-direction
 @param  nz_ number of structured grid points z-direction
 @param  blockgrid_ of unstructured grid
Christoph Pflaum's avatar
Christoph Pflaum committed
326
**/
Phillip Lino Rall's avatar
Phillip Lino Rall committed
327
  PointInterpolator(int nx_, int ny_, int nz_,
328
            Variable<double>* U_from, double defaultInterpolation_ = 0.0, bool trilinearInterpolation_ = false);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
329
330
331
332
333
334
335
336
  
  PointInterpolator(int nx_, int ny_, int nz_,
	            D3vector pWSD, D3vector pENT,
		    Variable<double>* U_from, double defaultInterpolation_ = 0.0);  
  
  PointInterpolator(Interpolate_on_structured_grid* intermediateGrid,
		    Variable<double>* U_from, double defaultInterpolation_ = 0.0);  
  
337
  PointInterpolator(Interpolate_on_structured_grid* intermediateGrid, double defaultInterpolation_ = -1.0, bool counter = false);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
338
339


Phillip Lino Rall's avatar
Phillip Lino Rall committed
340
  ~PointInterpolator();
Phillip Lino Rall's avatar
Phillip Lino Rall committed
341
342
343
344
345
346

  /**
   * Calculates an intermediate Grid for above constructor
   */
  Interpolate_on_structured_grid* intermediateGrid;  

Phillip Lino Rall's avatar
Phillip Lino Rall committed
347

Phillip Lino Rall's avatar
Phillip Lino Rall committed
348
349
350
351
352
353
354
355
356
357
  
  
  
  /**
   * interpoliert Daten. Falls an einem Punkt nicht interpoliert werden kann
   * da U_from keine Zelle hat, dann wird
   *     defaultInterpolation
   * verwendet.
   * @param coord_x, coord_y, coord_z, Koordinaten des Punktes
   * @return interpolierter Wert
Christoph Pflaum's avatar
Christoph Pflaum committed
358
   **/
359

360
  void updateVariable(Variable<double>* U_from);
361
  void updatePointInterpolator(Interpolate_on_structured_grid* intermediateGrid);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
362
363
364
  double evaluate(double coord_x, double coord_y, double coord_z);
  std::vector<double> evaluateGradient(double coord_x, double coord_y, double coord_z);
  void smoothGrid();
365
  void resetInterpolator();
Phillip Lino Rall's avatar
Phillip Lino Rall committed
366

Phillip Lino Rall's avatar
Phillip Lino Rall committed
367
  void normToNumberOfWritings(bool p2norm = false);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
368
  void writeOnInterpolatedGrid(double coord_x, double coord_y, double coord_z, double value);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
369
  void writeOnInterpolatedGridPoint(int i, int j, int k, double value);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
370
371
372
373
374
  void subtractOnInterpolatedGrid(double coord_x, double coord_y, double coord_z, double value);
  void shiftInterpolatedGrid(double coord_x, double coord_y, double coord_z);
  void scaleInterpolatedData(double scale, double zeroVal = 0.0);

  void QPrint_VTK(QString DateiName);
375

Phillip Lino Rall's avatar
Phillip Lino Rall committed
376
377
378
379
  D3vector pWSD;
  D3vector pENT;
  int nx, ny, nz;
  double hx, hy, hz;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
380
private:
381
382
  double shiftx{0}, shifty{0}, shiftz{0};
  double rotationx{0}, rotationy{0}, rotationz{0};
Phillip Lino Rall's avatar
Phillip Lino Rall committed
383

Phillip Lino Rall's avatar
Phillip Lino Rall committed
384
385
  Interpolate_on_structured_grid* interpolatorStructured;
  double* data;
386
  bool dataCounterFlag{false};
387
  int* dataCounter;
Phillip Lino Rall's avatar
Phillip Lino Rall committed
388

Phillip Lino Rall's avatar
Phillip Lino Rall committed
389
390
391
392
  
  double defaultInterpolation;
  
};
Christoph Pflaum's avatar
Christoph Pflaum committed
393
/* @} */
394

395
396
397
398
399
400
401
402
403
404
405

/////////////////////////////////////////////////////////////
// 4. Interpolate from blockgrid direct to any point
/////////////////////////////////////////////////////////////


/** \addtogroup InterpolationOperators **/
/* @{ */
/**
 * Interpolate from blockgrid direct to any point
***/
406
407
class Interpolate_direct {
 public:
408
    Interpolate_direct (Blockgrid* bg):blockgrid(bg){}
409

410
411
412
413
    /**
     * preparation for interpolation : calculates the neighbour index for each cell.
     * also  calculates the neighbour cell, if in other blockgrid.
    **/
414
    void init();
415
    void updateBoundaryBoxes();
416

417
418
419
420
421
422
    /**
     * Iterates throuh all cells : if D3vector v is inside of a cell, the weighting of the 8 cell points are calculated (D3vector lambda).
     * Uses previously cell indexes and its neighbours, since it's assumed that two consecutive find_cell(v) calls are close to each other.
     * Saves : idNow, idPrev, ifPrevPrev, idPrevPrevPrev
     *
    **/
423
    void find_cell(D3vector v);
424
    void find_surface_cell(D3vector v);
425
426
427
    /**
     * Print a vtk file with a box, surrounding the cell.
    **/
428
    void writeBox(D3vector vWSD, D3vector vENT, std::string str);
429
430
431
    /**
     * Print a vtk file with a point.
    **/
432
    static void writePoint(D3vector v, std::string str, int Counter);
433
434
435
    /**
     * Writes interpolation efficiency: how many direct hits, second try, iterate through all, ect...
    **/
436
   void writeInterpolationEfficiency();
437
438
439
   //   void interpolate(Variable<DTyp>& u, DTyp* data, DTyp defaultInterpolation);
    template <class DTyp>
    DTyp evaluate(Variable<DTyp>& u);
Phillip Lino Rall's avatar
Phillip Lino Rall committed
440
441
    template <class DTyp>
    DTyp evaluateSurface(Variable<DTyp>& u);
442

443
    std::vector<std::vector<std::vector<int> > > getBoundaryBox(){return array_box_boundary;}
444
445
446
447
    std::vector<std::vector<std::vector<D3vector> > > * getArrayBox() {return &arrayBoxWSDENT;}
    void setArrayBox(std::vector<std::vector<std::vector<D3vector> > > arrayBoxWSDENT_){arrayBoxWSDENT = arrayBoxWSDENT_;}


448

449
450
451
452
    int counterFast{}, counterFastest{}, counterSamePoint{}, counterSecondTry{}, counterThirdTry{}, counterSlow{}, counterHexa{}, counterCorner{}, counterEdge{};
    int checkCounter{};
    int boxCounter{};
    int typCounter0{},typCounter1{},typCounter2{},typCounter3{},typCounter4{},typCounter5{};
453
    bool debugTest;
454
455
    bool badLambdaFound;
    bool trilinearInterpolationFlag{false};
456
    D3vector lambda;
457
    D3vector vNow{1e10,1e10,1e10}, vPrev, vPrevPrev, vPrevPrevPrev;
458

459
460
461
462
463
464
    int getI(){return iNow;}
    int getJ(){return jNow;}
    int getK(){return kNow;}
    D3vector getLam(){return lambda;}
    int getTet(){return typ_tet;}
    int getHex(){return idHexNow;}
465

466
    D3vector bilinearInterpolation(D3vector v,D3vector a,D3vector b,D3vector c,D3vector d);
467
    D3vector trilinarInterpolation(D3vector v, int id_Hex,int i,  int j,  int k );
468
469
        bool vectorInBox(D3vector vWSD, D3vector vENT, D3vector v, double eps = 1e-10);
        int checkBox(int idHex, int i, int j, int k, D3vector v);
470
471
        int checkBoxSurface(int idHex, int i, int j, int k, D3vector v);

472
    private:
473
474
475
476
      int idHexPrev{-1}, iPrev{-1}, jPrev{-1}, kPrev{-1};
      int idHexPrevPrevPrev{-1}, iPrevPrevPrev{-1}, jPrevPrevPrev{-1}, kPrevPrevPrev{-1};
      int idHexPrevPrev{-1}, iPrevPrev{-1}, jPrevPrev{-1}, kPrevPrev{-1};
      int idHexNow{-1}, iNow{-1}, jNow{-1}, kNow{-1};
Phillip Lino Rall's avatar
Phillip Lino Rall committed
477
478
479
480
      int idHexPrevSurface{-1}, iPrevSurface{-1}, jPrevSurface{-1}, kPrevSurface{-1};
      int idHexPrevPrevPrevSurface{-1}, iPrevPrevPrevSurface{-1}, jPrevPrevPrevSurface{-1}, kPrevPrevPrevSurface{-1};
      int idHexPrevPrevSurface{-1}, iPrevPrevSurface{-1}, jPrevPrevSurface{-1}, kPrevPrevSurface{-1};
      int idHexNowSurface{-1}, iNowSurface{-1}, jNowSurface{-1}, kNowSurface{-1};
481

482
483
      double lamLowerLimit{-0.1};
      double lamUpperLimit{1.1};
484
485
      int typ_tet;

486
487
      D3vector cWSD, cESD;
      D3vector cWND, cEND;
488

489
490
      D3vector cWST, cEST;
      D3vector cWNT, cENT;
491
492
      Blockgrid    *blockgrid;
      std::vector<std::vector<std::vector<D3vector> > > arrayBoxWSDENT;
493
      std::vector<std::vector<bool > > isOuterBoundaryFlag;
494
495

      std::vector<std::vector<std::vector<int> > > array_box_boundary;
496
      std::vector<std::vector<std::vector<int> > > array_point_boundary;
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
private:
      /**
       * Necessary to calculate the neighbour relation.
      **/
      std::vector<std::vector<int> > calculateNeighbourIndexRelation(std::vector<std::vector<int> > inner, std::vector<std::vector<int> > outer);
      std::vector<int> calculateNeighbourIndex(std::vector<std::vector<int> > relation, int id_hex_outside,int id_hex_inside, int i, int j, int k, int Nx, int Ny, int Nz);
      std::vector<std::vector<int> > filterCorrectNeighbours(std::vector<std::vector<int> > outer);
      bool compareIndicies(std::vector<std::vector<int> > inner, std::vector<std::vector<int> > outer, int notCheck);
      std::vector<std::vector<int> > switchIJ(std::vector<std::vector<int> > v);
      std::vector<std::vector<int> > switchIK(std::vector<std::vector<int> > v);
      std::vector<std::vector<int> > switchJK(std::vector<std::vector<int> > v);
      std::vector<std::vector<int> > invertI (std::vector<std::vector<int> > v);
      std::vector<std::vector<int> > invertJ (std::vector<std::vector<int> > v);
      std::vector<std::vector<int> > invertK (std::vector<std::vector<int> > v);

      /**
       * Necessary to evaluate the neighbour cell relation.
      **/
      void setPrevIndex();
      void setPrevPrevIndex();
      void setPrevPrevPrevIndex();
Phillip Lino Rall's avatar
Phillip Lino Rall committed
518
519
520
      void setPrevIndexSurface();
      void setPrevPrevIndexSurface();
      void setPrevPrevPrevIndexSurface();
521
      int checkForHexaNeighbours(int idHex, int i, int j, int k, D3vector v);
522
      int checkForHexaNeighboursSurface(int idHex, int i, int j, int k, D3vector v);
523
      int checkBoxSurrounding(int idHex, int i, int j, int k, D3vector v);
524
      int checkBoxSurroundingOptimized(int idHex, int i, int j, int k, D3vector v, bool neglectBadLambda = false);
525
      int checkBoxSurroundingSurface(int idHex, int i, int j, int k, D3vector v, bool neglectBadLambda = false);
526
      int checkCorner(int idHex, int i, int j, int k, D3vector v);
527
      int checkCornerSurface(int idHex, int i, int j, int k, D3vector v);
528
      int checkEdge(int idHex, int i, int j, int k, D3vector v);
529
      int checkEdgeSurface(int idHex, int i, int j, int k, D3vector v);
530
531
      bool checkOverlapOfBoxes(D3vector vWSD, D3vector vENT, D3vector wWSD, D3vector wENT);
      double calculateOverlapOfBoxes(D3vector vWSD, D3vector vENT, D3vector wWSD, D3vector wENT);
532
533


534
535
536
537
538


};

/* @} */
539
540
541
542
543
544
545
546
template<class DTyp>
DTyp Interpolate_direct::evaluate(Variable<DTyp> &u)
{
 int i,j,k, id_hex, typ;
 int ind_global;

 DTyp returnVal;

547
548
// for(int id=0;id<5;++id)
//   u.template Update<hexahedronEl>(id);
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


   if(idHexNow < 0)
   {returnVal = -1;}
   else {
     Give_corner_data_of_cube<DTyp> du(u, idHexNow , iNow,jNow,kNow);

     int typ = typ_tet;

     if (lambda == D3vector(0,0,0))
     {
         cout << "no point found " << endl;
     }
     //cout << "lambda : " ; lambda.Print();cout<<endl;
     if(typ==0) returnVal = interpolate_in_tet(lambda,
                              du.WND(),du.WNT(),du.WST(),du.EST());
     if(typ==1) returnVal = interpolate_in_tet(lambda,
                              du.EST(),du.WND(),du.WST(),du.ESD());
     if(typ==2) returnVal = interpolate_in_tet(lambda,
                              du.WND(),du.WSD(),du.WST(),du.ESD());
     if(typ==3) returnVal = interpolate_in_tet(lambda,
                              du.EST(),du.WND(),du.ESD(),du.END());
     if(typ==4) returnVal = interpolate_in_tet(lambda,
                              du.ENT(),du.WNT(),du.EST(),du.END());
     if(typ==5) returnVal = interpolate_in_tet(lambda,
                              du.WNT(),du.WND(),du.EST(),du.END());
575
     if(typ==6) returnVal = interpolate_in_tet_trilinear(lambda,du.END(),du.ESD(),du.WSD(),du.WND(),
Phillip Lino Rall's avatar
Phillip Lino Rall committed
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
                                                                du.WNT(),du.WST(),du.EST(),du.ENT());




   }
   //    D3vector cWSD = {1,1,0};      // 1,1,0 : x2
   //    D3vector cESD = {2,0,-1};     // 1,0,0 : x1
   //    D3vector cWND = {0,1,0};      // 0,1,0 : x3
   //    D3vector cEND = {-1,0,-1.0};  // 0,0,0 : x0
   //    D3vector cWST = {1,1,1};      // 1,1,1 : x5
   //    D3vector cEST = {2,0,2};      // 1,0,1 : x6
   //    D3vector cWNT = {0,1,1};      // 0,1,1 : x4
   //    D3vector cENT = {-1,0,2};     // 0,0,1 : x7
    return returnVal;
};

template<class DTyp>
DTyp Interpolate_direct::evaluateSurface(Variable<DTyp> &u)
{
 int i,j,k, id_hex, typ;
 int ind_global;

 DTyp returnVal;

// for(int id=0;id<5;++id)
//   u.template Update<hexahedronEl>(id);


   if(idHexNowSurface < 0 )
   {returnVal = -1;}
   else {
     Give_corner_data_of_cube<DTyp> du(u, idHexNowSurface , iNowSurface,jNowSurface,kNowSurface);

     int typ = typ_tet;

     if (lambda == D3vector(0,0,0))
     {
         cout << "no point found " << endl;
     }
     //cout << "lambda : " ; lambda.Print();cout<<endl;

618
619
620
621
622
623
624
625
626
627
628
629
630
     if(typ==7) returnVal = interpolate_in_tet(lambda,du.EST(),du.ESD(),du.ENT(),0.0);
     if(typ==8) returnVal = interpolate_in_tet(lambda,du.END(),du.ESD(),du.ENT(),0.0);
     if(typ==9) returnVal = interpolate_in_tet(lambda,du.WND(),du.END(),du.WSD(),0.0);
     if(typ==10) returnVal = interpolate_in_tet(lambda,du.ESD(),du.END(),du.WSD(),0.0);
     if(typ==11) returnVal = interpolate_in_tet(lambda,du.ENT(),du.WNT(),du.EST(),0.0);
     if(typ==12) returnVal = interpolate_in_tet(lambda,du.WST(),du.WNT(),du.EST(),0.0);
     if(typ==13) returnVal = interpolate_in_tet_bilinear(lambda,du.WND(),du.END(),du.WSD(),du.ESD());
     if(typ==14) returnVal = interpolate_in_tet_bilinear(lambda,du.ENT(),du.WNT(),du.WST(),du.EST());





631
   }
632
633
634
635
636
637
638
639
   //    D3vector cWSD = {1,1,0};      // 1,1,0 : x2
   //    D3vector cESD = {2,0,-1};     // 1,0,0 : x1
   //    D3vector cWND = {0,1,0};      // 0,1,0 : x3
   //    D3vector cEND = {-1,0,-1.0};  // 0,0,0 : x0
   //    D3vector cWST = {1,1,1};      // 1,1,1 : x5
   //    D3vector cEST = {2,0,2};      // 1,0,1 : x6
   //    D3vector cWNT = {0,1,1};      // 0,1,1 : x4
   //    D3vector cENT = {-1,0,2};     // 0,0,1 : x7
640
641
642
643
    return returnVal;
};


Phillip Lino Rall's avatar
Phillip Lino Rall committed
644
645
646



Phillip Lino Rall's avatar
Phillip Lino Rall committed
647
#endif // INTERPOL_H