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

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

Fixed Fetch and Write for Bamg objects

File size: 10.6 KB
Line 
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
11#include "../../include/include.h"
12#include "../../shared/shared.h"
13#include "./matlabio.h"
14
15#include <mex.h>
16
17
18/*Primitive data types*/
19/*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/
20void WriteData(mxArray** pdataref,double* matrix, int M,int N){
21
22 mxArray *dataref = NULL;
23 double *tmatrix = NULL;
24
25 if(matrix){
26 /*create the matlab matrixwith Matlab's memory manager */
27 tmatrix=(double*)mxMalloc(M*N*sizeof(double));
28 for(int i=0;i<M;i++){
29 for(int j=0;j<N;j++){
30 tmatrix[i*N+j]=matrix[j*M+i];
31 }
32 }
33 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
34 mxSetM(dataref,(mwSize)M);
35 mxSetN(dataref,(mwSize)N);
36 mxSetPr(dataref,(double*)tmatrix);
37 }
38 else{
39 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
40 }
41 *pdataref=dataref;
42}
43/*}}}*/
44/*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{1*/
45void WriteData(mxArray** pdataref,int* matrix, int M,int N){
46
47 mxArray* dataref = NULL;
48 double* tmatrix = NULL;
49
50 if(matrix){
51
52 /*convert to double matrix using Matlab's memory manager*/
53 double* tmatrix=(double*)mxMalloc(M*N*sizeof(double));
54 for(int i=0;i<M;i++){
55 for(int j=0;j<N;j++){
56 tmatrix[i*N+j]=(double)matrix[j*M+i];
57 }
58 }
59 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
60 mxSetM(dataref,(mwSize)M);
61 mxSetN(dataref,(mwSize)N);
62 mxSetPr(dataref,(double*)tmatrix);
63
64 }
65 else{
66 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
67 }
68 *pdataref=dataref;
69}
70/*}}}*/
71/*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{1*/
72void WriteData(mxArray** pdataref,double* vector, int M){
73
74 mxArray* dataref = NULL;
75 double* vector_matlab = NULL;
76
77 if(vector){
78
79 /*create the matlab vector with Matlab's memory manager */
80 vector_matlab=(double*)mxMalloc(M*sizeof(double));
81 for(int i=0;i<M;i++) vector_matlab[i]=vector[i];
82 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
83 mxSetM(dataref,(mwSize)M);
84 mxSetN(dataref,(mwSize)1);
85 mxSetPr(dataref,vector_matlab);
86 }
87 else{
88 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
89 }
90
91 *pdataref=dataref;
92}
93/*}}}*/
94/*FUNCTION WriteData(mxArray** pdataref,double scalar){{{1*/
95void WriteData(mxArray** pdataref,double scalar){
96
97 *pdataref=mxCreateDoubleScalar(scalar);
98}
99/*}}}*/
100/*FUNCTION WriteData(mxArray** pdataref,int integer){{{1*/
101void WriteData(mxArray** pdataref,int integer){
102
103 *pdataref=mxCreateDoubleScalar((double)integer);
104
105}
106/*}}}*/
107/*FUNCTION WriteData(mxArray** pdataref,int boolean){{{1*/
108void WriteData(mxArray** pdataref,bool boolean){
109
110 *pdataref=mxCreateDoubleScalar((double)boolean);
111
112}
113/*}}}*/
114/*FUNCTION WriteData(mxArray** pdataref,char* string){{{1*/
115void WriteData(mxArray** pdataref,char* string){
116
117 *pdataref=mxCreateString(string);
118}
119/*}}}*/
120
121/*ISSM objects*/
122/*FUNCTION WriteData(mxArray** pdataref,BamgGeom* bamggeom){{{1*/
123void WriteData(mxArray** pdataref,BamgGeom* bamggeom){
124
125 /*Intermediary*/
126 int i;
127 mxArray *dataref = NULL;
128 const int numfields = 7;
129 const char *fnames[numfields];
130 mwSize ndim = 2;
131 mwSize dimensions[2] = {1,1};
132
133 /*Initialize field names*/
134 i=0;
135 fnames[i++] = "Vertices";
136 fnames[i++] = "Edges";
137 fnames[i++] = "TangentAtEdges";
138 fnames[i++] = "RequiredVertices";
139 fnames[i++] = "RequiredEdges";
140 fnames[i++] = "CrackedEdges";
141 fnames[i++] = "SubDomains";
142 _assert_(i==numfields);
143
144 /*Initialize Matlab structure*/
145 dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
146
147 /*set each matlab each field*/
148 i=0;
149 i++; SetStructureField(dataref,"Vertices", bamggeom->VerticesSize[0], bamggeom->VerticesSize[1], bamggeom->Vertices);
150 i++; SetStructureField(dataref,"Edges", bamggeom->EdgesSize[0], bamggeom->EdgesSize[1], bamggeom->Edges);
151 i++; SetStructureField(dataref,"TangentAtEdges", bamggeom->TangentAtEdgesSize[0], bamggeom->TangentAtEdgesSize[1], bamggeom->TangentAtEdges);
152 i++; SetStructureField(dataref,"RequiredVertices",bamggeom->RequiredVerticesSize[0],bamggeom->RequiredVerticesSize[1],bamggeom->RequiredVertices);
153 i++; SetStructureField(dataref,"RequiredEdges", bamggeom->RequiredEdgesSize[0], bamggeom->RequiredEdgesSize[1], bamggeom->RequiredEdges);
154 i++; SetStructureField(dataref,"CrackedEdges", bamggeom->CrackedEdgesSize[0], bamggeom->CrackedEdgesSize[1], bamggeom->CrackedEdges);
155 i++; SetStructureField(dataref,"SubDomains", bamggeom->SubDomainsSize[0], bamggeom->SubDomainsSize[1], bamggeom->SubDomains);
156 _assert_(i==numfields);
157
158 /*Assign output*/
159 *pdataref=dataref;
160}
161/*}}}*/
162/*FUNCTION WriteData(mxArray** pdataref,BamgMesh* bamgmesh){{{1*/
163void WriteData(mxArray** pdataref,BamgMesh* bamgmesh){
164
165 /*Intermediary*/
166 int i;
167 mxArray *dataref = NULL;
168 const int numfields = 16;
169 const char *fnames[numfields];
170 mwSize ndim = 2;
171 mwSize dimensions[2] = {1,1};
172
173 /*Initialize field names*/
174 i=0;
175 fnames[i++] = "Triangles";
176 fnames[i++] = "Vertices";
177 fnames[i++] = "Edges";
178 fnames[i++] = "IssmSegments";
179 fnames[i++] = "IssmEdges";
180 fnames[i++] = "Quadrilaterals";
181 fnames[i++] = "VerticesOnGeomVertex";
182 fnames[i++] = "VerticesOnGeomEdge";
183 fnames[i++] = "EdgesOnGeomEdge";
184 fnames[i++] = "SubDomains";
185 fnames[i++] = "SubDomainsFromGeom";
186 fnames[i++] = "ElementConnectivity";
187 fnames[i++] = "NodalConnectivity";
188 fnames[i++] = "NodalElementConnectivity";
189 fnames[i++] = "CrackedVertices";
190 fnames[i++] = "CrackedEdges";
191 _assert_(i==numfields);
192
193 /*Initialize Matlab structure*/
194 dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
195
196 /*set each matlab each field*/
197 i=0;
198 i++; SetStructureField(dataref,"Triangles", bamgmesh->TrianglesSize[0],bamgmesh->TrianglesSize[1], bamgmesh->Triangles);
199 i++; SetStructureField(dataref,"Vertices",bamgmesh->VerticesSize[0], bamgmesh->VerticesSize[1],bamgmesh->Vertices);
200 i++; SetStructureField(dataref,"Edges", bamgmesh->EdgesSize[0],bamgmesh->EdgesSize[1], bamgmesh->Edges);
201 i++; SetStructureField(dataref,"IssmSegments",bamgmesh->IssmSegmentsSize[0], bamgmesh->IssmSegmentsSize[1],bamgmesh->IssmSegments);
202 i++; SetStructureField(dataref,"IssmEdges", bamgmesh->IssmEdgesSize[0],bamgmesh->IssmEdgesSize[1], bamgmesh->IssmEdges);
203 i++; SetStructureField(dataref,"Quadrilaterals",bamgmesh->QuadrilateralsSize[0], bamgmesh->QuadrilateralsSize[1],bamgmesh->Quadrilaterals);
204 i++; SetStructureField(dataref,"VerticesOnGeomVertex",bamgmesh->VerticesOnGeomVertexSize[0],bamgmesh->VerticesOnGeomVertexSize[1], bamgmesh->VerticesOnGeomVertex);
205 i++; SetStructureField(dataref,"VerticesOnGeomEdge",bamgmesh->VerticesOnGeomEdgeSize[0],bamgmesh->VerticesOnGeomEdgeSize[1], bamgmesh->VerticesOnGeomEdge);
206 i++; SetStructureField(dataref,"EdgesOnGeomEdge", bamgmesh->EdgesOnGeomEdgeSize[0], bamgmesh->EdgesOnGeomEdgeSize[1],bamgmesh->EdgesOnGeomEdge);
207 i++; SetStructureField(dataref,"SubDomains",bamgmesh->SubDomainsSize[0], bamgmesh->SubDomainsSize[1],bamgmesh->SubDomains);
208 i++; SetStructureField(dataref,"SubDomainsFromGeom", bamgmesh->SubDomainsFromGeomSize[0], bamgmesh->SubDomainsFromGeomSize[1],bamgmesh->SubDomainsFromGeom);
209 i++; SetStructureField(dataref,"ElementConnectivity",bamgmesh->ElementConnectivitySize[0],bamgmesh->ElementConnectivitySize[1], bamgmesh->ElementConnectivity);
210 i++; SetStructureField(dataref,"NodalConnectivity",bamgmesh->NodalConnectivitySize[0],bamgmesh->NodalConnectivitySize[1], bamgmesh->NodalConnectivity);
211 i++; SetStructureField(dataref,"NodalElementConnectivity", bamgmesh->NodalElementConnectivitySize[0], bamgmesh->NodalElementConnectivitySize[1],bamgmesh->NodalElementConnectivity);
212 i++; SetStructureField(dataref,"CrackedVertices", bamgmesh->CrackedVerticesSize[0],bamgmesh->CrackedVerticesSize[1], bamgmesh->CrackedVertices);
213 i++; SetStructureField(dataref,"CrackedEdges",bamgmesh->CrackedEdgesSize[0], bamgmesh->CrackedEdgesSize[1],bamgmesh->CrackedEdges);
214 _assert_(i==numfields);
215
216 /*Assign output*/
217 *pdataref=dataref;
218}
219/*}}}*/
220/*FUNCTION WriteData(mxArray** pdataref,Matrix* matrix){{{1*/
221void WriteData(mxArray** pdataref,Matrix* matrix){
222
223 int i,j;
224 int rows,cols;
225 mxArray *dataref = NULL;
226 double *matrix_ptr = NULL;
227 double *tmatrix_ptr = NULL;
228
229 if(matrix){
230
231 #ifdef _HAVE_PETSC_
232 PetscMatrixToDoubleMatrix(&matrix_ptr,&rows,&cols,matrix->matrix);
233 #else
234 matrix_ptr=matrix->matrix->ToSerial();
235 matrix->matrix->GetSize(&rows,cols);
236 #endif
237
238 /*Now transpose the matrix and allocate with Matlab's memory manager: */
239 tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
240 for(i=0;i<cols;i++){
241 for(j=0;j<rows;j++){
242 tmatrix_ptr[i*rows+j]=matrix_ptr[j*cols+i];
243 }
244 }
245
246 /*create matlab matrix: */
247 dataref=mxCreateDoubleMatrix(0,0,mxREAL);
248 mxSetM(dataref,rows);
249 mxSetN(dataref,cols);
250 mxSetPr(dataref,tmatrix_ptr);
251
252 /*Free ressources:*/
253 xfree((void**)&matrix_ptr);
254
255 }
256 else{
257 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
258 }
259
260 *pdataref=dataref;
261}
262/*}}}*/
263/*FUNCTION WriteData(mxArray** pdataref,Vector* vector){{{1*/
264void WriteData(mxArray** pdataref,Vector* vector){
265
266 mxArray* dataref=NULL;
267 double* vector_ptr=NULL;
268 double* vector_matlab=NULL;
269 int rows;
270
271 if(vector){
272 /*call toolkit routine: */
273 #ifdef _HAVE_PETSC_
274 PetscVectorToDoubleVector(&vector_ptr,&rows,vector->vector);
275 #else
276 vector_ptr=vector->vector->ToMPISerial();
277 vector->vector->GetSize(&rows);
278 #endif
279
280 /*now create the matlab vector with Matlab's memory manager */
281 vector_matlab=(double*)mxMalloc(rows*sizeof(double));
282 for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
283
284 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
285 mxSetM(dataref,rows);
286 mxSetN(dataref,1);
287 mxSetPr(dataref,vector_matlab);
288 }
289 else{
290 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
291 }
292
293 /*Clean-up and return*/
294 xfree((void**)&vector_ptr);
295 *pdataref=dataref;
296}
297/*}}}*/
298
299/*Toolkit*/
300/*FUNCTION SetStructureField{{{1*/
301void SetStructureField(mxArray* dataref,const char* fieldname,int fieldrows,int fieldcols,double* fieldpointer){
302
303 /*Intermediary*/
304 int i1,i2;
305 mxArray *pfield = NULL;
306
307 /*Copy field using Matlab's API and transpose*/
308 double* fieldcopy=NULL;
309 if (fieldrows*fieldcols){
310 fieldcopy=(double*)mxMalloc(fieldrows*fieldcols*sizeof(double));
311 for(i1=0;i1<fieldrows;i1++){
312 for(i2=0;i2<fieldcols;i2++){
313 fieldcopy[fieldcols*i1+i2]=fieldpointer[fieldrows*i2+i1];
314 }
315 }
316 }
317
318 /*Set matlab field*/
319 pfield=mxCreateDoubleMatrix(0,0,mxREAL);
320 mxSetM(pfield,fieldcols);
321 mxSetN(pfield,fieldrows);
322 mxSetPr(pfield,fieldcopy);
323 mxSetField(dataref,0,fieldname,pfield);
324}
325/*}}}*/
Note: See TracBrowser for help on using the repository browser.