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

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

Fixed matrix transpose

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