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

Last change on this file since 13216 was 13216, checked in by Eric.Larour, 13 years ago

NEW: large change to the code, to adapt to ADOLC requirements.

This change relates to the introduction of template classes and functions for the
Option.h abstract class. This is needed, because we want to make the Matlab
API independent from the libCore objects, which are dependent on the IssmDouble*
ADOLC type (adouble), when the Matlab API is dependent on the IssmPDouble* type (double).

To make them independent, we need to be able to specify at run time Options, Matrix and
Vector objects that hold either IssmDouble or IssmPDouble objects. The only way to do
that is through the use of templated classes for Option.h, Matrix and Vector.

The change gets rid of a lot of useless code (especially in the classes/objects/Options
directory), by introducing template versions of the same code.

The bulk of the changes to src/modules and src/mex modules is to adapt to this
new runtime declaration of templated Matrix, Vector and Option objects.

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,Matrix* matrix){{{*/
219void WriteData(mxArray** pdataref,Matrix<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 }
250 else{
251 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
252 }
253
254 *pdataref=dataref;
255}
256/*}}}*/
257/*FUNCTION WriteData(mxArray** pdataref,Vector<double>* vector){{{*/
258void WriteData(mxArray** pdataref,Vector<double>* vector){
259
260 mxArray* dataref=NULL;
261 double* vector_ptr=NULL;
262 double* vector_matlab=NULL;
263 int rows;
264
265 if(vector){
266 /*call toolkit routine: */
267 vector_ptr=vector->ToMPISerial();
268 vector->GetSize(&rows);
269
270 /*now create the matlab vector with Matlab's memory manager */
271 vector_matlab=(double*)mxMalloc(rows*sizeof(double));
272 for(int i=0;i<rows;i++) vector_matlab[i]=vector_ptr[i];
273
274 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
275 mxSetM(dataref,rows);
276 mxSetN(dataref,1);
277 mxSetPr(dataref,vector_matlab);
278 }
279 else{
280 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
281 }
282
283 /*Clean-up and return*/
284 xDelete<double>(vector_ptr);
285 *pdataref=dataref;
286}
287/*}}}*/
288
289/*Toolkit*/
290/*FUNCTION SetStructureField{{{*/
291void SetStructureField(mxArray* dataref,const char* fieldname,int M,int N,double* fieldpointer){
292
293 mxArray* field = NULL;
294
295
296 /*Convert field*/
297 WriteData(&field,fieldpointer,M,N);
298
299 /*Assign to structure*/
300 mxSetField(dataref,0,fieldname,field);
301}
302/*}}}*/
Note: See TracBrowser for help on using the repository browser.