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

Last change on this file since 13322 was 13322, checked in by jschierm, 13 years ago

CHG: Changes to FetchData for BamgGeom and BamgMesh.

File size: 10.1 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 <mex.h>
12#include "../../include/include.h"
13#include "../../shared/shared.h"
14#include "./matlabio.h"
15
16/*Primitive data types*/
17/*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{*/
18void WriteData(mxArray** pdataref,double* matrix, int M,int N){
19
20 mxArray *dataref = NULL;
21 double *tmatrix = NULL;
22
23 if(matrix){
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++){
28 tmatrix[j*M+i]=matrix[i*N+j];
29 }
30 }
31 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
32 mxSetM(dataref,(mwSize)M);
33 mxSetN(dataref,(mwSize)N);
34 mxSetPr(dataref,(double*)tmatrix);
35 }
36 else{
37 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
38 }
39 *pdataref=dataref;
40}
41/*}}}*/
42/*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{*/
43void WriteData(mxArray** pdataref,int* matrix, int M,int N){
44
45 mxArray* dataref = NULL;
46 double* tmatrix = NULL;
47
48 if(matrix){
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++){
53 tmatrix[j*M+i]=(double)matrix[i*N+j];
54 }
55 }
56 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
57 mxSetM(dataref,(mwSize)M);
58 mxSetN(dataref,(mwSize)N);
59 mxSetPr(dataref,(double*)tmatrix);
60 }
61 else{
62 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
63 }
64 *pdataref=dataref;
65}
66/*}}}*/
67/*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{*/
68void WriteData(mxArray** pdataref,double* vector, int M){
69
70 mxArray* dataref = NULL;
71 double* vector_matlab = NULL;
72
73 if(vector){
74
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];
78 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
79 mxSetM(dataref,(mwSize)M);
80 mxSetN(dataref,(mwSize)1);
81 mxSetPr(dataref,vector_matlab);
82 }
83 else{
84 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
85 }
86
87 *pdataref=dataref;
88}
89/*}}}*/
90/*FUNCTION WriteData(mxArray** pdataref,double scalar){{{*/
91void WriteData(mxArray** pdataref,double scalar){
92
93 *pdataref=mxCreateDoubleScalar(scalar);
94}
95/*}}}*/
96/*FUNCTION WriteData(mxArray** pdataref,int integer){{{*/
97void WriteData(mxArray** pdataref,int integer){
98
99 *pdataref=mxCreateDoubleScalar((double)integer);
100
101}
102/*}}}*/
103/*FUNCTION WriteData(mxArray** pdataref,int boolean){{{*/
104void WriteData(mxArray** pdataref,bool boolean){
105
106 *pdataref=mxCreateDoubleScalar((double)boolean);
107
108}
109/*}}}*/
110/*FUNCTION WriteData(mxArray** pdataref,char* string){{{*/
111void WriteData(mxArray** pdataref,char* string){
112
113 *pdataref=mxCreateString(string);
114}
115/*}}}*/
116
117/*ISSM objects*/
118/*FUNCTION WriteData(mxArray** pdataref,BamgGeom* bamggeom){{{*/
119void WriteData(mxArray** pdataref,BamgGeom* bamggeom){
120
121 /*Intermediary*/
122 int i;
123 mxArray *dataref = NULL;
124 const int numfields = 7;
125 const char *fnames[numfields];
126 mwSize ndim = 2;
127 mwSize dimensions[2] = {1,1};
128
129 /*Initialize field names*/
130 i=0;
131 fnames[i++] = "Vertices";
132 fnames[i++] = "Edges";
133 fnames[i++] = "TangentAtEdges";
134 fnames[i++] = "Corners";
135 fnames[i++] = "RequiredVertices";
136 fnames[i++] = "RequiredEdges";
137 fnames[i++] = "CrackedEdges";
138 fnames[i++] = "SubDomains";
139 _assert_(i==numfields);
140
141 /*Initialize Matlab structure*/
142 dataref=mxCreateStructArray(ndim,dimensions,numfields,fnames);
143
144 /*set each matlab each field*/
145 i=0;
146 i++; SetStructureField(dataref,"Vertices", bamggeom->VerticesSize[0], bamggeom->VerticesSize[1], bamggeom->Vertices);
147 i++; SetStructureField(dataref,"Edges", bamggeom->EdgesSize[0], bamggeom->EdgesSize[1], bamggeom->Edges);
148 i++; SetStructureField(dataref,"TangentAtEdges", bamggeom->TangentAtEdgesSize[0], bamggeom->TangentAtEdgesSize[1], bamggeom->TangentAtEdges);
149 i++; SetStructureField(dataref,"Corners", bamggeom->CornersSize[0], bamggeom->CornersSize[1], bamggeom->Corners);
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++] = "Vertices";
174 fnames[i++] = "Edges";
175 fnames[i++] = "Triangles";
176 fnames[i++] = "Quadrilaterals";
177 fnames[i++] = "IssmEdges";
178 fnames[i++] = "IssmSegments";
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,"Vertices",bamgmesh->VerticesSize[0], bamgmesh->VerticesSize[1],bamgmesh->Vertices);
197 i++; SetStructureField(dataref,"Edges", bamgmesh->EdgesSize[0],bamgmesh->EdgesSize[1], bamgmesh->Edges);
198 i++; SetStructureField(dataref,"Triangles", bamgmesh->TrianglesSize[0],bamgmesh->TrianglesSize[1], bamgmesh->Triangles);
199 i++; SetStructureField(dataref,"Quadrilaterals",bamgmesh->QuadrilateralsSize[0], bamgmesh->QuadrilateralsSize[1],bamgmesh->Quadrilaterals);
200 i++; SetStructureField(dataref,"IssmEdges", bamgmesh->IssmEdgesSize[0],bamgmesh->IssmEdgesSize[1], bamgmesh->IssmEdges);
201 i++; SetStructureField(dataref,"IssmSegments",bamgmesh->IssmSegmentsSize[0], bamgmesh->IssmSegmentsSize[1],bamgmesh->IssmSegments);
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.