Ice Sheet System Model  4.18
Code documentation
Data Structures | Functions
InterpFromMesh2dx.h File Reference

: header file for Data interpolation routines. More...

#include "../../classes/classes.h"
#include "../../toolkits/toolkits.h"

Go to the source code of this file.

Data Structures

struct  InterpFromMesh2dxThreadStruct
 

Functions

int InterpFromMesh2dx (IssmSeqVec< IssmPDouble > **pdata_prime, int *index_data, double *x_data, double *y_data, int nods_data, int nels_data, double *data, int data_length, double *x_prime, double *y_prime, int nods_prime, double *default_values, int num_default_values, Contour< IssmPDouble > **contours, int numcontours)
 
void * InterpFromMesh2dxt (void *vInterpFromMesh2dxThreadStruct)
 

Detailed Description

: header file for Data interpolation routines.

Definition in file InterpFromMesh2dx.h.

Function Documentation

◆ InterpFromMesh2dx()

int InterpFromMesh2dx ( IssmSeqVec< IssmPDouble > **  pdata_prime,
int *  index_data,
double *  x_data,
double *  y_data,
int  nods_data,
int  nels_data,
double *  data,
int  data_length,
double *  x_prime,
double *  y_prime,
int  nods_prime,
double *  default_values,
int  num_default_values,
Contour< IssmPDouble > **  contours,
int  numcontours 
)

Definition at line 11 of file InterpFromMesh2dx.cpp.

14  {
15 
16  /*Output*/
17  IssmSeqVec<IssmPDouble>* data_prime=NULL;
18 
19  /*Intermediary*/
20  int i;
21  int interpolation_type;
22  bool debug;
23  double xmin,xmax;
24  double ymin,ymax;
25 
26  /*contours: */
27  double *incontour = NULL;
28 
29  /*some checks*/
30  if (nels_data<1 || nods_data<3 || nods_prime==0){
31  _error_("nothing to be done according to the mesh given in input");
32  }
33 
34  /*Set debug to 1 if there are lots of elements*/
35  debug=(bool)((double)nels_data*(double)nods_prime >= pow((double)10,(double)9));
36 
37  /*figure out what kind of interpolation is needed*/
38  if (data_length==nods_data){
39  interpolation_type=1;
40  }
41  else if (data_length==nels_data){
42  interpolation_type=2;
43  }
44  else{
45  _error_("length of vector data not supported yet. It should be of length (number of nodes) or (number of elements)!");
46  }
47 
48  if((numcontours) && (interpolation_type==2)){
49  _error_("element interpolation_type with contours not supported yet!");
50  }
51 
52  /*Get prime mesh extrema coordinates*/
53  xmin=x_prime[0]; xmax=x_prime[0];ymin=y_prime[0]; ymax=y_prime[0];
54  for (i=1;i<nods_prime;i++){
55  if (x_prime[i]<xmin) xmin=x_prime[i];
56  if (x_prime[i]>xmax) xmax=x_prime[i];
57  if (y_prime[i]<ymin) ymin=y_prime[i];
58  if (y_prime[i]>ymax) ymax=y_prime[i];
59  }
60 
61  /*Initialize output*/
62  data_prime=new IssmSeqVec<IssmPDouble>(nods_prime);
63  if(num_default_values){
64  if(num_default_values==1)for (i=0;i<nods_prime;i++) data_prime->SetValue(i,default_values[0],INS_VAL);
65  else for (i=0;i<nods_prime;i++) data_prime->SetValue(i,default_values[i],INS_VAL);
66  }
67 
68  /*Build indices of contour: */
69  if(numcontours){
70  ContourToNodesx( &incontour,x_prime,y_prime,nods_prime,contours,numcontours,1);
71  }
72  else{
73  incontour=xNew<double>(nods_prime);
74  for (i=0;i<nods_prime;i++) incontour[i]=1.0;
75  }
76 
77  /*initialize thread parameters: */
79  gate.interpolation_type = interpolation_type;
80  gate.debug = debug;
81  gate.nels_data = nels_data;
82  gate.index_data = index_data;
83  gate.x_data = x_data;
84  gate.y_data = y_data;
85  gate.data = data;
86  gate.xmin = xmin;
87  gate.xmax = xmax;
88  gate.ymin = ymin;
89  gate.ymax = ymax;
90  gate.nods_prime = nods_prime;
91  gate.data_prime = data_prime;
92  gate.x_prime = x_prime;
93  gate.y_prime = y_prime;
94  gate.default_values = default_values;
95  gate.num_default_values = num_default_values;
96  gate.incontour = incontour;
97 
98  /*launch the thread manager with InterpFromGridToMeshxt as a core: */
99  LaunchThread(InterpFromMesh2dxt,(void*)&gate,_NUMTHREADS_);
100 
101  /*Assign output pointers:*/
102  xDelete<double>(incontour);
103  *pdata_prime=data_prime;
104  return 1;
105 }

◆ InterpFromMesh2dxt()

void* InterpFromMesh2dxt ( void *  vInterpFromMesh2dxThreadStruct)

Definition at line 8 of file InterpFromMesh2dxt.cpp.

8  {
9 
10  /*intermediary: */
11  int i0,i1,i,j;
12  double area,area_1,area_2,area_3;
13  double data_value;
14 
15  /*recover handle and gate: */
16  pthread_handle *handle = (pthread_handle*)vpthread_handle;
18  int my_thread = handle->id;
19  int num_threads = handle->num;
20 
21  /*recover parameters :*/
22  int interpolation_type = gate->interpolation_type;
23  bool debug = gate->debug;
24  int nels_data = gate->nels_data;
25  int *index_data = gate->index_data;
26  double *x_data = gate->x_data;
27  double *y_data = gate->y_data;
28  double *data = gate->data;
29  double xmin = gate->xmin;
30  double xmax = gate->xmax;
31  double ymin = gate->ymin;
32  double ymax = gate->ymax;
33  int nods_prime = gate->nods_prime;
34  IssmSeqVec<IssmPDouble>* data_prime = gate->data_prime;
35  double *x_prime = gate->x_prime;
36  double *y_prime = gate->y_prime;
37  double *default_values = gate->default_values;
38  int num_default_values = gate->num_default_values;
39  double *incontour = gate->incontour;
40 
41  /*partition loop across threads: */
42  PartitionRange(&i0,&i1,nels_data,num_threads,my_thread);
43 
44  /*Loop over the elements*/
45  for(i=i0;i<i1;i++){
46 
47  /*display current iteration*/
48  if (debug && my_thread==0 && fmod((double)i,(double)100)==0)
49  _printf_("\r interpolation progress: "<<setw(6)<<setprecision(2)<<double(i-i0)/double(i1-i0)*100<<"% ");
50 
51  /*if there is no point inside the domain, go to next iteration*/
52  if ( (x_data[index_data[3*i+0]-1]<xmin) && (x_data[index_data[3*i+1]-1]<xmin) && (x_data[index_data[3*i+2]-1]<xmin)) continue;
53  if ( (x_data[index_data[3*i+0]-1]>xmax) && (x_data[index_data[3*i+1]-1]>xmax) && (x_data[index_data[3*i+2]-1]>xmax)) continue;
54  if ( (y_data[index_data[3*i+0]-1]<ymin) && (y_data[index_data[3*i+1]-1]<ymin) && (y_data[index_data[3*i+2]-1]<ymin)) continue;
55  if ( (y_data[index_data[3*i+0]-1]>ymax) && (y_data[index_data[3*i+1]-1]>ymax) && (y_data[index_data[3*i+2]-1]>ymax)) continue;
56 
57  /*get area of the current element (Jacobian = 2 * area)*/
58  //area =x2 * y3 - y2*x3 + x1 * y2 - y1 * x2 + x3 * y1 - y3 * x1;
59  area=x_data[index_data[3*i+1]-1]*y_data[index_data[3*i+2]-1]-y_data[index_data[3*i+1]-1]*x_data[index_data[3*i+2]-1]
60  + x_data[index_data[3*i+0]-1]*y_data[index_data[3*i+1]-1]-y_data[index_data[3*i+0]-1]*x_data[index_data[3*i+1]-1]
61  + x_data[index_data[3*i+2]-1]*y_data[index_data[3*i+0]-1]-y_data[index_data[3*i+2]-1]*x_data[index_data[3*i+0]-1];
62 
63  /*loop over the prime nodes*/
64  for (j=0;j<nods_prime;j++){
65 
66  if(incontour[j]){
67 
68  /*Get first area coordinate = det(x-x3 x2-x3 ; y-y3 y2-y3)/area*/
69  area_1=((x_prime[j]-x_data[index_data[3*i+2]-1])*(y_data[index_data[3*i+1]-1]-y_data[index_data[3*i+2]-1])
70  - (y_prime[j]-y_data[index_data[3*i+2]-1])*(x_data[index_data[3*i+1]-1]-x_data[index_data[3*i+2]-1]))/area;
71  /*Get second area coordinate =det(x1-x3 x-x3 ; y1-y3 y-y3)/area*/
72  area_2=((x_data[index_data[3*i+0]-1]-x_data[index_data[3*i+2]-1])*(y_prime[j]-y_data[index_data[3*i+2]-1])
73  - (y_data[index_data[3*i+0]-1]-y_data[index_data[3*i+2]-1])*(x_prime[j]-x_data[index_data[3*i+2]-1]))/area;
74  /*Get third area coordinate = 1-area1-area2*/
75  area_3=1-area_1-area_2;
76 
77  /*is the current point in the current element?*/
78  if (area_1>=0 && area_2>=0 && area_3>=0){
79 
80  /*Yes ! compute the value on the point*/
81  if (interpolation_type==1){
82  /*nodal interpolation*/
83  data_value=area_1*data[index_data[3*i+0]-1]+area_2*data[index_data[3*i+1]-1]+area_3*data[index_data[3*i+2]-1];
84  }
85  else{
86  /*element interpolation*/
87  data_value=data[i];
88  }
89  if (xIsNan<IssmPDouble>(data_value)){
90  if(num_default_values==1) data_value=default_values[0];
91  else data_value=default_values[j];
92  }
93 
94  /*insert value and go to the next point*/
95  data_prime->SetValue(j,data_value,INS_VAL);
96  }
97  }
98  }
99  }
100  if(debug && my_thread==0)
101  _printf_("\r interpolation progress: "<<fixed<<setw(6)<<setprecision(2)<<100.<<"% \n");
102  return NULL;
103 }
InterpFromMesh2dxThreadStruct
Definition: InterpFromMesh2dx.h:12
InterpFromMesh2dxThreadStruct::ymin
double ymin
Definition: InterpFromMesh2dx.h:22
InterpFromMesh2dxThreadStruct::y_prime
double * y_prime
Definition: InterpFromMesh2dx.h:26
InterpFromMesh2dxThreadStruct::x_prime
double * x_prime
Definition: InterpFromMesh2dx.h:25
pthread_handle::id
int id
Definition: issm_threads.h:12
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
LaunchThread
void LaunchThread(void *function(void *), void *gate, int num_threads)
Definition: LaunchThread.cpp:25
InterpFromMesh2dxThreadStruct::default_values
double * default_values
Definition: InterpFromMesh2dx.h:27
pthread_handle::num
int num
Definition: issm_threads.h:13
InterpFromMesh2dxThreadStruct::nods_prime
int nods_prime
Definition: InterpFromMesh2dx.h:23
IssmSeqVec< IssmPDouble >
InterpFromMesh2dxThreadStruct::interpolation_type
int interpolation_type
Definition: InterpFromMesh2dx.h:14
ContourToNodesx
int ContourToNodesx(IssmPDouble **pflags, double *x, double *y, int nods, Contour< IssmPDouble > **contours, int numcontours, int edgevalue)
Definition: ContourToNodesx.cpp:6
IssmSeqVec::SetValue
void SetValue(int dof, doubletype value, InsMode mode)
Definition: IssmSeqVec.h:115
InterpFromMesh2dxThreadStruct::incontour
double * incontour
Definition: InterpFromMesh2dx.h:29
InterpFromMesh2dxThreadStruct::nels_data
int nels_data
Definition: InterpFromMesh2dx.h:16
pthread_handle
Definition: issm_threads.h:10
InterpFromMesh2dxThreadStruct::xmin
double xmin
Definition: InterpFromMesh2dx.h:21
PartitionRange
void PartitionRange(int *pi0, int *pi1, int num_el, int num_threads, int my_thread)
Definition: PartitionRange.cpp:13
INS_VAL
@ INS_VAL
Definition: toolkitsenums.h:14
InterpFromMesh2dxThreadStruct::data_prime
IssmSeqVec< IssmPDouble > * data_prime
Definition: InterpFromMesh2dx.h:24
InterpFromMesh2dxThreadStruct::ymax
double ymax
Definition: InterpFromMesh2dx.h:22
InterpFromMesh2dxThreadStruct::data
double * data
Definition: InterpFromMesh2dx.h:20
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
InterpFromMesh2dxThreadStruct::xmax
double xmax
Definition: InterpFromMesh2dx.h:21
pthread_handle::gate
void * gate
Definition: issm_threads.h:11
InterpFromMesh2dxThreadStruct::debug
bool debug
Definition: InterpFromMesh2dx.h:15
InterpFromMesh2dxThreadStruct::index_data
int * index_data
Definition: InterpFromMesh2dx.h:17
InterpFromMesh2dxThreadStruct::y_data
double * y_data
Definition: InterpFromMesh2dx.h:19
InterpFromMesh2dxThreadStruct::x_data
double * x_data
Definition: InterpFromMesh2dx.h:18
InterpFromMesh2dxThreadStruct::num_default_values
int num_default_values
Definition: InterpFromMesh2dx.h:28
InterpFromMesh2dxt
void * InterpFromMesh2dxt(void *vInterpFromMesh2dxThreadStruct)
Definition: InterpFromMesh2dxt.cpp:8