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

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

do not use Matlab's memory management for internal variable (big mistake)

File size: 2.9 KB
RevLine 
[7217]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;
[7331]15 bool found;
16 int numberofelements,numberofsegments;
[7217]17 int N,M;
[7331]18 int i,j,ii,jj,id;
[7217]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*/
[11933]27 FetchData(&numberofelements,mxGetAssignedField(MODEL,0,"numberofelements"));
[7217]28 if(numberofelements<=0) _error_("No elements found in the model");
[11933]29 FetchData(&elements,&M,&N,mxGetAssignedField(MODEL,0,"elements"));
[7217]30 if(M!=numberofelements || N!=3) _error_("Field 'elements' should be of size [md.numberofelements 3]");
[11933]31 FetchData(&elementonwater,&M,&N,mxGetAssignedField(MODEL,0,"elementonwater"));
[7217]32 if(M!=numberofelements || N!=1) _error_("Field 'elementonwater' should be of size [md.numberofelements 1]");
[11933]33 FetchData(&elementconnectivity,&M,&N,mxGetAssignedField(MODEL,0,"elementconnectivity"));
[7217]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;
[12060]38 front=(int*)xmalloc(3*numberofelements*4*sizeof(int));
[7217]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++){
[7331]56
57 found=false;
[7217]58 for(jj=0;jj<3;jj++){
59 if(elements[(id-1)*3+ii]==elements[i*3+jj]){
[7331]60 found=true;
61 break;
[7217]62 }
63 }
[7331]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 }
[7217]74 }
[7331]75
76 /*In debugging mode, check that the segment has been found*/
77 _assert_(!found);
[7217]78 }
79 }
80
81 /*Now that we know how many segments there is we can allocate the final matrix*/
[7305]82 if(numberofsegments){
[12060]83 front2=(double*)xmalloc(4*numberofsegments*sizeof(double));
[7305]84 for(i=0;i<4*numberofsegments;i++) front2[i]=(double)front[i];
85 }
[7217]86 xfree((void**)&front);
87
88 /*write output datasets: */
[11933]89 WriteData(FRONT,front2,numberofsegments,4);
[7217]90
91 /*end module: */
92 MODULEEND();
93}
94
95void InternalFrontUsage(void) {
96 _printf_(true,"\n");
97 _printf_(true," usage: icefront = %s(md);\n",__FUNCT__);
98 _printf_(true,"\n");
99}
Note: See TracBrowser for help on using the repository browser.