source: issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp@ 13695

Last change on this file since 13695 was 13695, checked in by jschierm, 12 years ago

NEW: Implemented RiftStruct for Python WriteData.

File size: 13.1 KB
RevLine 
[12011]1/* \file WriteData.c:
2 * \brief: general interface for writing data
3 */
4
5#ifdef HAVE_CONFIG_H
6 #include <config.h>
7#else
8#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9#endif
10
[13242]11#include <mex.h>
[12011]12#include "../../include/include.h"
13#include "../../shared/shared.h"
[12013]14#include "./matlabio.h"
[12011]15
[12043]16/*Primitive data types*/
[12365]17/*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{*/
[12011]18void WriteData(mxArray** pdataref,double* matrix, int M,int N){
[12040]19
20 mxArray *dataref = NULL;
21 double *tmatrix = NULL;
[13622]22
[12011]23 if(matrix){
[12040]24 /*create the matlab matrixwith Matlab's memory manager */
25 tmatrix=(double*)mxMalloc(M*N*sizeof(double));
26 for(int i=0;i<M;i++){
27 for(int j=0;j<N;j++){
[12046]28 tmatrix[j*M+i]=matrix[i*N+j];
[12040]29 }
30 }
31 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
[12041]32 mxSetM(dataref,(mwSize)M);
33 mxSetN(dataref,(mwSize)N);
[12040]34 mxSetPr(dataref,(double*)tmatrix);
[12011]35 }
36 else{
37 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
38 }
39 *pdataref=dataref;
40}
41/*}}}*/
[12365]42/*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{*/
[12011]43void WriteData(mxArray** pdataref,int* matrix, int M,int N){
44
[12040]45 mxArray* dataref = NULL;
46 double* tmatrix = NULL;
[12011]47
48 if(matrix){
[12040]49 /*convert to double matrix using Matlab's memory manager*/
50 double* tmatrix=(double*)mxMalloc(M*N*sizeof(double));
51 for(int i=0;i<M;i++){
52 for(int j=0;j<N;j++){
[12046]53 tmatrix[j*M+i]=(double)matrix[i*N+j];
[12040]54 }
55 }
56 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
[12041]57 mxSetM(dataref,(mwSize)M);
58 mxSetN(dataref,(mwSize)N);
[12040]59 mxSetPr(dataref,(double*)tmatrix);
[12011]60 }
61 else{
62 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
63 }
64 *pdataref=dataref;
65}
66/*}}}*/
[12365]67/*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{*/
[12011]68void WriteData(mxArray** pdataref,double* vector, int M){
[13622]69
[12040]70 mxArray* dataref = NULL;
71 double* vector_matlab = NULL;
[12011]72
73 if(vector){
74
[12040]75 /*create the matlab vector with Matlab's memory manager */
76 vector_matlab=(double*)mxMalloc(M*sizeof(double));
77 for(int i=0;i<M;i++) vector_matlab[i]=vector[i];
[12011]78 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
79 mxSetM(dataref,(mwSize)M);
80 mxSetN(dataref,(mwSize)1);
[12040]81 mxSetPr(dataref,vector_matlab);
[12011]82 }
83 else{
84 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
85 }
86
87 *pdataref=dataref;
88}
89/*}}}*/
[12365]90/*FUNCTION WriteData(mxArray** pdataref,double scalar){{{*/
[12011]91void WriteData(mxArray** pdataref,double scalar){
92
93 *pdataref=mxCreateDoubleScalar(scalar);
94}
95/*}}}*/
[12365]96/*FUNCTION WriteData(mxArray** pdataref,int integer){{{*/
[12011]97void WriteData(mxArray** pdataref,int integer){
98
99 *pdataref=mxCreateDoubleScalar((double)integer);
100
101}
102/*}}}*/
[12365]103/*FUNCTION WriteData(mxArray** pdataref,int boolean){{{*/
[12011]104void WriteData(mxArray** pdataref,bool boolean){
105
106 *pdataref=mxCreateDoubleScalar((double)boolean);
107
108}
109/*}}}*/
[12365]110/*FUNCTION WriteData(mxArray** pdataref,char* string){{{*/
[12011]111void WriteData(mxArray** pdataref,char* string){
112
113 *pdataref=mxCreateString(string);
114}
115/*}}}*/
[13447]116/*FUNCTION WriteData(mxArray** pdataref){{{*/
117void WriteData(mxArray** pdataref){
[12043]118
[13447]119 ;
120
121}
122/*}}}*/
123
[12043]124/*ISSM objects*/
[12365]125/*FUNCTION WriteData(mxArray** pdataref,BamgGeom* bamggeom){{{*/
[12043]126void WriteData(mxArray** pdataref,BamgGeom* bamggeom){
127
128 /*Intermediary*/
129 int i;
130 mxArray *dataref = NULL;
[13324]131 const int numfields = 8;
[12043]132 const char *fnames[numfields];
133 mwSize ndim = 2;
134 mwSize dimensions[2] = {1,1};
135
136 /*Initialize field names*/
137 i=0;
138 fnames[i++] = "Vertices";
139 fnames[i++] = "Edges";
140 fnames[i++] = "TangentAtEdges";
[13322]141 fnames[i++] = "Corners";
[12043]142 fnames[i++] = "RequiredVertices";
143 fnames[i++] = "RequiredEdges";
144 fnames[i++] = "CrackedEdges";
145 fnames[i++] = "SubDomains";
146 _assert_(i==numfields);
147
148 /*Initialize Matlab structure*/
149 dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
150
151 /*set each matlab each field*/
152 i=0;
153 i++; SetStructureField(dataref,"Vertices", bamggeom->VerticesSize[0], bamggeom->VerticesSize[1], bamggeom->Vertices);
154 i++; SetStructureField(dataref,"Edges", bamggeom->EdgesSize[0], bamggeom->EdgesSize[1], bamggeom->Edges);
155 i++; SetStructureField(dataref,"TangentAtEdges", bamggeom->TangentAtEdgesSize[0], bamggeom->TangentAtEdgesSize[1], bamggeom->TangentAtEdges);
[13322]156 i++; SetStructureField(dataref,"Corners", bamggeom->CornersSize[0], bamggeom->CornersSize[1], bamggeom->Corners);
[12043]157 i++; SetStructureField(dataref,"RequiredVertices",bamggeom->RequiredVerticesSize[0],bamggeom->RequiredVerticesSize[1],bamggeom->RequiredVertices);
158 i++; SetStructureField(dataref,"RequiredEdges", bamggeom->RequiredEdgesSize[0], bamggeom->RequiredEdgesSize[1], bamggeom->RequiredEdges);
159 i++; SetStructureField(dataref,"CrackedEdges", bamggeom->CrackedEdgesSize[0], bamggeom->CrackedEdgesSize[1], bamggeom->CrackedEdges);
160 i++; SetStructureField(dataref,"SubDomains", bamggeom->SubDomainsSize[0], bamggeom->SubDomainsSize[1], bamggeom->SubDomains);
161 _assert_(i==numfields);
162
163 /*Assign output*/
164 *pdataref=dataref;
165}
166/*}}}*/
[12365]167/*FUNCTION WriteData(mxArray** pdataref,BamgMesh* bamgmesh){{{*/
[12043]168void WriteData(mxArray** pdataref,BamgMesh* bamgmesh){
169
170 /*Intermediary*/
171 int i;
172 mxArray *dataref = NULL;
173 const int numfields = 16;
174 const char *fnames[numfields];
175 mwSize ndim = 2;
176 mwSize dimensions[2] = {1,1};
177
178 /*Initialize field names*/
179 i=0;
180 fnames[i++] = "Vertices";
181 fnames[i++] = "Edges";
[13322]182 fnames[i++] = "Triangles";
183 fnames[i++] = "Quadrilaterals";
184 fnames[i++] = "IssmEdges";
[12043]185 fnames[i++] = "IssmSegments";
186 fnames[i++] = "VerticesOnGeomVertex";
187 fnames[i++] = "VerticesOnGeomEdge";
188 fnames[i++] = "EdgesOnGeomEdge";
189 fnames[i++] = "SubDomains";
190 fnames[i++] = "SubDomainsFromGeom";
191 fnames[i++] = "ElementConnectivity";
192 fnames[i++] = "NodalConnectivity";
193 fnames[i++] = "NodalElementConnectivity";
194 fnames[i++] = "CrackedVertices";
195 fnames[i++] = "CrackedEdges";
196 _assert_(i==numfields);
197
198 /*Initialize Matlab structure*/
199 dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
200
201 /*set each matlab each field*/
202 i=0;
203 i++; SetStructureField(dataref,"Vertices",bamgmesh->VerticesSize[0], bamgmesh->VerticesSize[1],bamgmesh->Vertices);
204 i++; SetStructureField(dataref,"Edges", bamgmesh->EdgesSize[0],bamgmesh->EdgesSize[1], bamgmesh->Edges);
[13322]205 i++; SetStructureField(dataref,"Triangles", bamgmesh->TrianglesSize[0],bamgmesh->TrianglesSize[1], bamgmesh->Triangles);
206 i++; SetStructureField(dataref,"Quadrilaterals",bamgmesh->QuadrilateralsSize[0], bamgmesh->QuadrilateralsSize[1],bamgmesh->Quadrilaterals);
207 i++; SetStructureField(dataref,"IssmEdges", bamgmesh->IssmEdgesSize[0],bamgmesh->IssmEdgesSize[1], bamgmesh->IssmEdges);
[12043]208 i++; SetStructureField(dataref,"IssmSegments",bamgmesh->IssmSegmentsSize[0], bamgmesh->IssmSegmentsSize[1],bamgmesh->IssmSegments);
209 i++; SetStructureField(dataref,"VerticesOnGeomVertex",bamgmesh->VerticesOnGeomVertexSize[0],bamgmesh->VerticesOnGeomVertexSize[1], bamgmesh->VerticesOnGeomVertex);
210 i++; SetStructureField(dataref,"VerticesOnGeomEdge",bamgmesh->VerticesOnGeomEdgeSize[0],bamgmesh->VerticesOnGeomEdgeSize[1], bamgmesh->VerticesOnGeomEdge);
211 i++; SetStructureField(dataref,"EdgesOnGeomEdge", bamgmesh->EdgesOnGeomEdgeSize[0], bamgmesh->EdgesOnGeomEdgeSize[1],bamgmesh->EdgesOnGeomEdge);
212 i++; SetStructureField(dataref,"SubDomains",bamgmesh->SubDomainsSize[0], bamgmesh->SubDomainsSize[1],bamgmesh->SubDomains);
213 i++; SetStructureField(dataref,"SubDomainsFromGeom", bamgmesh->SubDomainsFromGeomSize[0], bamgmesh->SubDomainsFromGeomSize[1],bamgmesh->SubDomainsFromGeom);
214 i++; SetStructureField(dataref,"ElementConnectivity",bamgmesh->ElementConnectivitySize[0],bamgmesh->ElementConnectivitySize[1], bamgmesh->ElementConnectivity);
215 i++; SetStructureField(dataref,"NodalConnectivity",bamgmesh->NodalConnectivitySize[0],bamgmesh->NodalConnectivitySize[1], bamgmesh->NodalConnectivity);
216 i++; SetStructureField(dataref,"NodalElementConnectivity", bamgmesh->NodalElementConnectivitySize[0], bamgmesh->NodalElementConnectivitySize[1],bamgmesh->NodalElementConnectivity);
217 i++; SetStructureField(dataref,"CrackedVertices", bamgmesh->CrackedVerticesSize[0],bamgmesh->CrackedVerticesSize[1], bamgmesh->CrackedVertices);
218 i++; SetStructureField(dataref,"CrackedEdges",bamgmesh->CrackedEdgesSize[0], bamgmesh->CrackedEdgesSize[1],bamgmesh->CrackedEdges);
219 _assert_(i==numfields);
220
221 /*Assign output*/
222 *pdataref=dataref;
223}
224/*}}}*/
[13227]225/*FUNCTION WriteData(mxArray** pdataref,SeqMat<double>* matrix){{{*/
226void WriteData(mxArray** pdataref,SeqMat<double>* matrix){
[13622]227
[12043]228 int i,j;
229 int rows,cols;
230 mxArray *dataref = NULL;
231 double *matrix_ptr = NULL;
232 double *tmatrix_ptr = NULL;
[13622]233
[12043]234 if(matrix){
[13622]235
[12850]236 matrix_ptr=matrix->ToSerial();
237 matrix->GetSize(&rows,&cols);
[12043]238
239 /*Now transpose the matrix and allocate with Matlab's memory manager: */
240 tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
[12046]241 for(i=0;i<rows;i++){
242 for(j=0;j<cols;j++){
243 tmatrix_ptr[j*rows+i]=matrix_ptr[i*cols+j];
[12043]244 }
245 }
[13622]246
[12043]247 /*create matlab matrix: */
248 dataref=mxCreateDoubleMatrix(0,0,mxREAL);
249 mxSetM(dataref,rows);
250 mxSetN(dataref,cols);
251 mxSetPr(dataref,tmatrix_ptr);
252
253 /*Free ressources:*/
[12434]254 xDelete<double>(matrix_ptr);
[12043]255 }
256 else{
257 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
258 }
259
260 *pdataref=dataref;
261}
262/*}}}*/
[13227]263/*FUNCTION WriteData(mxArray** pdataref,SeqVec<double>* vector){{{*/
264void WriteData(mxArray** pdataref,SeqVec<double>* vector){
[13622]265
[12043]266 mxArray* dataref=NULL;
267 double* vector_ptr=NULL;
268 double* vector_matlab=NULL;
269 int rows;
[13622]270
[12043]271 if(vector){
272 /*call toolkit routine: */
[12850]273 vector_ptr=vector->ToMPISerial();
274 vector->GetSize(&rows);
[13622]275
[12043]276 /*now create the matlab vector with Matlab's memory manager */
277 vector_matlab=(double*)mxMalloc(rows*sizeof(double));
278 for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
279
280 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
281 mxSetM(dataref,rows);
282 mxSetN(dataref,1);
283 mxSetPr(dataref,vector_matlab);
284 }
285 else{
286 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
287 }
288
289 /*Clean-up and return*/
[12434]290 xDelete<double>(vector_ptr);
[12043]291 *pdataref=dataref;
292}
293/*}}}*/
[13638]294/*FUNCTION WriteData(mxArray** pdataref,RiftStruct* riftstruct){{{*/
295void WriteData(mxArray** pdataref,RiftStruct* riftstruct){
[12043]296
[13638]297 /*Intermediary*/
298 int i;
299 mxArray *dataref = NULL;
300 const int numfields = 10;
301 const char *fnames[numfields];
302 mwSize ndim = 2;
303 mwSize dimensions[2] = {1,1};
304
305 /*Initialize field names*/
306 i=0;
307 fnames[i++] = "numsegs";
308 fnames[i++] = "segments";
309 fnames[i++] = "pairs";
310 fnames[i++] = "tips";
311 fnames[i++] = "penaltypairs";
312 fnames[i++] = "fill";
313 fnames[i++] = "friction";
314 fnames[i++] = "fraction";
315 fnames[i++] = "fractionincrement";
316 fnames[i++] = "state";
317 _assert_(i==numfields);
318
319 /*Initialize matlab structure of dimension numrifts*/
320 dimensions[0]=riftstruct->numrifts;
321 dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
322
323 /*set each matlab each field*/
324 for(int i=0;i<riftstruct->numrifts;i++){
325 SetStructureFieldi(dataref,i,"numsegs" ,riftstruct->riftsnumsegments[i]);
326 SetStructureFieldi(dataref,i,"segments" ,riftstruct->riftsnumsegments[i] ,3,riftstruct->riftssegments[i]);
327 SetStructureFieldi(dataref,i,"pairs" ,riftstruct->riftsnumpairs[i] ,2,riftstruct->riftspairs[i]);
328 SetStructureFieldi(dataref,i,"tips" ,1 ,2,&riftstruct->riftstips[2*i]);
329 SetStructureFieldi(dataref,i,"penaltypairs" ,riftstruct->riftsnumpenaltypairs[i],7,riftstruct->riftspenaltypairs[i]);
[13695]330 SetStructureFieldi(dataref,i,"fill" ,IceEnum);
[13638]331 SetStructureFieldi(dataref,i,"friction" ,0);
332 SetStructureFieldi(dataref,i,"fraction" ,0.);
333 SetStructureFieldi(dataref,i,"fractionincrement",0.1);
334 SetStructureFieldi(dataref,i,"state" ,riftstruct->riftsnumpenaltypairs[i],1,riftstruct->state[i]);
335 }
336
337 /*Assign output*/
338 *pdataref=dataref;
339}
340/*}}}*/
341
[12043]342/*Toolkit*/
[12365]343/*FUNCTION SetStructureField{{{*/
[12046]344void SetStructureField(mxArray* dataref,const char* fieldname,int M,int N,double* fieldpointer){
[12043]345
[12046]346 mxArray* field = NULL;
[12043]347
[12046]348 /*Convert field*/
349 WriteData(&field,fieldpointer,M,N);
350
351 /*Assign to structure*/
352 mxSetField(dataref,0,fieldname,field);
[12043]353}
354/*}}}*/
[13638]355/*FUNCTION SetStructureFieldi(mxArray* dataref,int i,const char* fieldname,int M,int N,double* fieldpointer){{{*/
356void SetStructureFieldi(mxArray* dataref,int i,const char* fieldname,int M,int N,double* fieldpointer){
357
358 mxArray* field = NULL;
359
360 /*Convert field*/
361 WriteData(&field,fieldpointer,M,N);
362
363 /*Assign to structure*/
364 mxSetField(dataref,i,fieldname,field);
365}
366/*}}}*/
367/*FUNCTION SetStructureFieldi(mxArray* dataref,int i,const char* fieldname,int field){{{*/
368void SetStructureFieldi(mxArray* dataref,int i,const char* fieldname,int fieldin){
369
370 mxArray* field = NULL;
371
372 /*Convert field*/
373 WriteData(&field,fieldin);
374
375 /*Assign to structure*/
376 mxSetField(dataref,i,fieldname,field);
377}
378/*}}}*/
379/*FUNCTION SetStructureFieldi(mxArray* dataref,int i,const char* fieldname,double field){{{*/
380void SetStructureFieldi(mxArray* dataref,int i,const char* fieldname,double fieldin){
381
382 mxArray* field = NULL;
383
384 /*Convert field*/
385 WriteData(&field,fieldin);
386
387 /*Assign to structure*/
388 mxSetField(dataref,i,fieldname,field);
389}
390/*}}}*/
Note: See TracBrowser for help on using the repository browser.