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

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

Preliminary commit of new issm version, where serial code is starting to be stripped away. Will not run before some major debugging is done

File size: 5.9 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
14#if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
15#include <mex.h>
16
17/*FUNCTION WriteData(mxArray** pdataref,DataSet* dataset){{{1*/
18void WriteData(mxArray** pdataref,DataSet* dataset){
19
20 mxArray* dataref=NULL;
21 char* marshalled_dataset=NULL;
22 int marshalled_dataset_size;
23
24 /*Write a dataset: */
25 if(dataset){
26 /* marshall the dataset: */
27 marshalled_dataset=dataset->Marshall();
28 marshalled_dataset_size=dataset->MarshallSize();
29
30 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
31 mxSetM(dataref,(mwSize)(marshalled_dataset_size/sizeof(double)));
32 mxSetN(dataref,(mwSize)1);
33 mxSetPr(dataref,(double*)marshalled_dataset);
34 }
35 else{
36 /* return empty matrix: */
37 dataref=mxCreateDoubleMatrix(0,0,mxREAL);
38 }
39 *pdataref=dataref;
40
41}
42/*}}}*/
43/*FUNCTION WriteData(mxArray** pdataref,Matrix* matrix){{{1*/
44void WriteData(mxArray** pdataref,Matrix* matrix){
45
46 mxArray* dataref=NULL;
47 double* matrix_ptr=NULL;
48 int rows,cols;
49 double* tmatrix_ptr=NULL;
50
51 if(matrix){
52
53 #ifdef _HAVE_PETSC_
54 PetscMatrixToDoubleMatrix(&tmatrix_ptr,&rows,&cols);
55 #else
56 matrix_ptr=matrix->matrix->ToSerial();
57 matrix->matrix->GetSize(&rows,cols);
58 #endif
59
60 /*Now transpose the matrix: */
61 tmatrix_ptr=(double*)mxMalloc(rows*cols*sizeof(double));
62 for(i=0;i<cols;i++){
63 for(j=0;j<rows;j++){
64 tmatrix_ptr[i*rows+j]=matrix_ptr[j*cols+i];
65 }
66 }
67
68 /*create matlab matrix: */
69 dataref=mxCreateDoubleMatrix(0,0,mxREAL);
70 mxSetM(dataref,rows);
71 mxSetN(dataref,cols);
72 mxSetPr(dataref,tmatrix_ptr);
73
74 /*Free ressources:*/
75 xfree((void**)&matrix_ptr);
76
77 }
78 else{
79 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
80 }
81
82 *pdataref=dataref;
83}
84/*}}}*/
85/*FUNCTION WriteData(mxArray** pdataref,Vector* vector){{{1*/
86void WriteData(mxArray** pdataref,Vector* vector){
87
88 mxArray* dataref=NULL;
89 double* vector_ptr=NULL;
90 int rows;
91
92 if(vector){
93 /*call toolkit routine: */
94 #ifdef _HAVE_PETSC_
95 PetscVectorToDoubleVector(&vector_ptr,&rows,vector->vector);
96 #else
97 vector_ptr=vector->vector->ToMPISerial();
98 vector->vector->GetSize(&rows);
99 #endif
100
101 /*now create the matlab vector: */
102 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
103 mxSetM(dataref,rows);
104 mxSetN(dataref,1);
105 mxSetPr(dataref,vector_ptr);
106 }
107 else{
108 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
109 }
110 *pdataref=dataref;
111
112}
113/*}}}*/
114/*FUNCTION WriteData(mxArray** pdataref,double* matrix, int M,int N){{{1*/
115void WriteData(mxArray** pdataref,double* matrix, int M,int N){
116
117 mxArray* dataref=NULL;
118 mxArray* tdataref=NULL;
119
120 if(matrix){
121
122 /*data is a double* pointer. Copy into a matrix: */
123 tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
124 mxSetM(tdataref,(mwSize)N);
125 mxSetN(tdataref,(mwSize)M);
126 mxSetPr(tdataref,(double*)matrix);
127
128 //transpose
129 mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
130 }
131 else{
132 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
133 }
134 *pdataref=dataref;
135}
136/*}}}*/
137/*FUNCTION WriteData(mxArray** pdataref,int* matrix, int M,int N){{{1*/
138void WriteData(mxArray** pdataref,int* matrix, int M,int N){
139
140 mxArray* dataref=NULL;
141 mxArray* tdataref=NULL;
142
143 if(matrix){
144
145 /*convert to double matrix*/
146 double* doublematrix=(double*)mxMalloc(M*N*sizeof(double));
147 for(int i=0;i<M*N;i++) doublematrix[i]=(double)matrix[i];
148
149 /*data is a double* pointer. Copy into a matrix: */
150 tdataref = mxCreateDoubleMatrix(0,0,mxREAL);
151 mxSetM(tdataref,(mwSize)N);
152 mxSetN(tdataref,(mwSize)M);
153 mxSetPr(tdataref,(double*)doublematrix);
154
155 //transpose
156 mexCallMATLAB(1,&dataref,1,&tdataref, "transpose");
157 }
158 else{
159 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
160 }
161 *pdataref=dataref;
162}
163/*}}}*/
164/*FUNCTION WriteData(mxArray** pdataref,double* vector, int M){{{1*/
165void WriteData(mxArray** pdataref,double* vector, int M){
166
167 mxArray* dataref=NULL;
168
169 if(vector){
170
171 /*data is a double* pointer. Copy into a vector: */
172 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
173 mxSetM(dataref,(mwSize)M);
174 mxSetN(dataref,(mwSize)1);
175 mxSetPr(dataref,vector);
176 }
177 else{
178 dataref = mxCreateDoubleMatrix(0,0,mxREAL);
179 }
180
181 *pdataref=dataref;
182}
183/*}}}*/
184/*FUNCTION WriteData(mxArray** pdataref,double scalar){{{1*/
185void WriteData(mxArray** pdataref,double scalar){
186
187 *pdataref=mxCreateDoubleScalar(scalar);
188}
189/*}}}*/
190/*FUNCTION WriteData(mxArray** pdataref,int integer){{{1*/
191void WriteData(mxArray** pdataref,int integer){
192
193 *pdataref=mxCreateDoubleScalar((double)integer);
194
195}
196/*}}}*/
197/*FUNCTION WriteData(mxArray** pdataref,int boolean){{{1*/
198void WriteData(mxArray** pdataref,bool boolean){
199
200 *pdataref=mxCreateDoubleScalar((double)boolean);
201
202}
203/*}}}*/
204/*FUNCTION WriteData(mxArray** pdataref,char* string){{{1*/
205void WriteData(mxArray** pdataref,char* string){
206
207 *pdataref=mxCreateString(string);
208}
209/*}}}*/
210/*FUNCTION WriteData(mxArray** pdataref,Parameters* parameters){{{1*/
211void WriteData(mxArray** pdataref,Parameters* parameters){
212
213 int i;
214
215 /*output: */
216 mxArray *dataref = NULL;
217 mwSize nfields;
218 char **fnames = NULL;
219 mwSize onebyone[2] = {1,1};
220 mwSize ndim = 2;
221
222 /*intermediary: */
223 Param* param=NULL;
224
225 /*Recover data from the parameters dataset: */
226 nfields=(mwSize)parameters->Size();
227 fnames=(char**)mxMalloc(nfields*sizeof(char*));
228
229 /*Build structure in matlab workspace with all the parameter fields: */
230 for(i=0;i<nfields;i++){
231 param=(Param*)parameters->GetObjectByOffset(i);
232 param->GetParameterName(&fnames[i]);
233 }
234 /*Initialize structure: */
235 dataref=mxCreateStructArray( ndim,onebyone,nfields,(const char**)fnames);
236
237 /*Fill each field: */
238 for(i=0;i<nfields;i++){
239
240 param=(Param*)parameters->GetObjectByOffset(i);
241 param->SetMatlabField(dataref);
242 }
243
244 /*Assign output pointers:*/
245 *pdataref=dataref;
246
247}
248/*}}}*/
249#endif
Note: See TracBrowser for help on using the repository browser.