source: issm/trunk-jpl/src/modules/InternalFront/InternalFront.cpp@ 13038

Last change on this file since 13038 was 13038, checked in by Mathieu Morlighem, 13 years ago

CHG: getting rid of xmalloc

File size: 2.9 KB
Line 
1/*\file InternalFront.c
2 *\brief: build pressureload
3 */
4
5#include "./InternalFront.h"
6
7void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){
8
9 bool* elementonwater=NULL;
10 int* elements=NULL;
11 int* connectivity=NULL;
12 int* elementconnectivity=NULL;
13 int* front=NULL;
14 double* front2=NULL;
15 bool found;
16 int numberofelements,numberofsegments;
17 int N,M;
18 int i,j,ii,jj,id;
19
20 /*Boot module: */
21 MODULEBOOT();
22
23 /*checks on arguments on the matlab side: */
24 CheckNumMatlabArguments(nlhs,NLHS,nrhs,NRHS,__FUNCT__,&InternalFrontUsage);
25
26 /*Fetch required fields*/
27 FetchData(&numberofelements,mxGetAssignedField(MODEL,0,"numberofelements"));
28 if(numberofelements<=0) _error_("No elements found in the model");
29 FetchData(&elements,&M,&N,mxGetAssignedField(MODEL,0,"elements"));
30 if(M!=numberofelements || N!=3) _error_("Field 'elements' should be of size [md.numberofelements 3]");
31 FetchData(&elementonwater,&M,&N,mxGetAssignedField(MODEL,0,"elementonwater"));
32 if(M!=numberofelements || N!=1) _error_("Field 'elementonwater' should be of size [md.numberofelements 1]");
33 FetchData(&elementconnectivity,&M,&N,mxGetAssignedField(MODEL,0,"elementconnectivity"));
34 if(M!=numberofelements || N!=3) _error_("Field 'elementconnectivity' should be of size [md.numberofelements 3]");
35
36 /*Allocate and initialize all variables*/
37 numberofsegments=0;
38 front=xNew<int>(3*numberofelements*4);
39
40 /*Loop over all elements on water*/
41 for(i=0;i<numberofelements;i++){
42
43 /*Skip if on water*/
44 if(!elementonwater[i]) continue;
45
46 /*Loop over all three adjacent elements*/
47 for(j=0;j<3;j++){
48
49 /*Skip if adjacent element does not exist or is on water*/
50 id=elementconnectivity[i*3+j];
51 if(id==0) continue;
52 if(elementonwater[id-1]) continue;
53
54 /*We have an ice front to add here, let's go!*/
55 for(ii=0;ii<3;ii++){
56
57 found=false;
58 for(jj=0;jj<3;jj++){
59 if(elements[(id-1)*3+ii]==elements[i*3+jj]){
60 found=true;
61 break;
62 }
63 }
64
65 /*OK, we just found the node of id, which is not in i. We have the segment*/
66 if(!found){
67 front[numberofsegments*4+0]=elements[(id-1)*3+(ii+1)%3];
68 front[numberofsegments*4+1]=elements[(id-1)*3+(ii+2)%3];
69 front[numberofsegments*4+2]=id;
70 front[numberofsegments*4+3]=IceEnum;
71 numberofsegments+=1;
72 break;
73 }
74 }
75
76 /*In debugging mode, check that the segment has been found*/
77 _assert_(!found);
78 }
79 }
80
81 /*Now that we know how many segments there is we can allocate the final matrix*/
82 if(numberofsegments){
83 front2=xNew<double>(4*numberofsegments);
84 for(i=0;i<4*numberofsegments;i++) front2[i]=(double)front[i];
85 }
86 xfree((void**)&front);
87
88 /*write output datasets: */
89 WriteData(FRONT,front2,numberofsegments,4);
90
91 /*end module: */
92 MODULEEND();
93}
94
95void InternalFrontUsage(void) {
96 _pprintLine_("");
97 _pprintLine_(" usage: icefront = " << __FUNCT__ << "(md);");
98 _pprintLine_("");
99}
Note: See TracBrowser for help on using the repository browser.