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

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

CHG: make sure we are writing SeqMat and SeqVec for matlab in order to disconnect petsc from modules

File size: 10.0 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){{{*/
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){{{*/
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){{{*/
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){{{*/
93void WriteData(mxArray** pdataref,double scalar){
94
95 *pdataref=mxCreateDoubleScalar(scalar);
96}
97/*}}}*/
98/*FUNCTION WriteData(mxArray** pdataref,int integer){{{*/
99void WriteData(mxArray** pdataref,int integer){
100
101 *pdataref=mxCreateDoubleScalar((double)integer);
102
103}
104/*}}}*/
105/*FUNCTION WriteData(mxArray** pdataref,int boolean){{{*/
106void WriteData(mxArray** pdataref,bool boolean){
107
108 *pdataref=mxCreateDoubleScalar((double)boolean);
109
110}
111/*}}}*/
112/*FUNCTION WriteData(mxArray** pdataref,char* string){{{*/
113void WriteData(mxArray** pdataref,char* string){
114
115 *pdataref=mxCreateString(string);
116}
117/*}}}*/
118
119/*ISSM objects*/
120/*FUNCTION WriteData(mxArray** pdataref,BamgGeom* bamggeom){{{*/
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){{{*/
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,SeqMat<double>* matrix){{{*/
219void WriteData(mxArray** pdataref,SeqMat<double>* 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 matrix_ptr=matrix->ToSerial();
230 matrix->GetSize(&rows,&cols);
231
232 /*Now transpose the matrix and allocate with Matlab's memory manager: */
233 tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
234 for(i=0;i<rows;i++){
235 for(j=0;j<cols;j++){
236 tmatrix_ptr[j*rows+i]=matrix_ptr[i*cols+j];
237 }
238 }
239
240 /*create matlab matrix: */
241 dataref=mxCreateDoubleMatrix(0,0,mxREAL);
242 mxSetM(dataref,rows);
243 mxSetN(dataref,cols);
244 mxSetPr(dataref,tmatrix_ptr);
245
246 /*Free ressources:*/
247 xDelete<double>(matrix_ptr);
248 }
249 else{
250 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
251 }
252
253 *pdataref=dataref;
254}
255/*}}}*/
256/*FUNCTION WriteData(mxArray** pdataref,SeqVec<double>* vector){{{*/
257void WriteData(mxArray** pdataref,SeqVec<double>* vector){
258
259 mxArray* dataref=NULL;
260 double* vector_ptr=NULL;
261 double* vector_matlab=NULL;
262 int rows;
263
264 if(vector){
265 /*call toolkit routine: */
266 vector_ptr=vector->ToMPISerial();
267 vector->GetSize(&rows);
268
269 /*now create the matlab vector with Matlab's memory manager */
270 vector_matlab=(double*)mxMalloc(rows*sizeof(double));
271 for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
272
273 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
274 mxSetM(dataref,rows);
275 mxSetN(dataref,1);
276 mxSetPr(dataref,vector_matlab);
277 }
278 else{
279 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
280 }
281
282 /*Clean-up and return*/
283 xDelete<double>(vector_ptr);
284 *pdataref=dataref;
285}
286/*}}}*/
287
288/*Toolkit*/
289/*FUNCTION SetStructureField{{{*/
290void SetStructureField(mxArray* dataref,const char* fieldname,int M,int N,double* fieldpointer){
291
292 mxArray* field = NULL;
293
294
295 /*Convert field*/
296 WriteData(&field,fieldpointer,M,N);
297
298 /*Assign to structure*/
299 mxSetField(dataref,0,fieldname,field);
300}
301/*}}}*/
Note: See TracBrowser for help on using the repository browser.