source: issm/trunk-jpl/src/c/objects/ExternalResults/DoubleMatExternalResult.cpp@ 11202

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

Fixed some g++ warnings: deprecated conversion from string constant to ‘char*’

File size: 7.5 KB
Line 
1/*!\file DoubleMatExternalResult.c
2 * \brief: implementation of the DoubleMatExternalResult object
3 */
4
5/*header files: */
6/*{{{1*/
7#ifdef HAVE_CONFIG_H
8 #include <config.h>
9#else
10#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
11#endif
12
13#include <stdio.h>
14#include <string.h>
15#include "../objects.h"
16#include "../../EnumDefinitions/EnumDefinitions.h"
17#include "../../shared/shared.h"
18#include "../../Container/Container.h"
19#include "../../include/include.h"
20/*}}}*/
21
22/*DoubleMatExternalResult constructors and destructor*/
23/*FUNCTION DoubleMatExternalResult::DoubleMatExternalResult(){{{1*/
24DoubleMatExternalResult::DoubleMatExternalResult(){
25 return;
26}
27/*}}}*/
28/*FUNCTION DoubleMatExternalResult::DoubleMatExternalResult(int in_id, int enum_type,IssmDoubleMat values,int M,int N,int in_step,double in_time){{{1*/
29DoubleMatExternalResult::DoubleMatExternalResult(int in_id, int in_enum_type,double* in_values, int in_M,int in_N,int in_step,double in_time){
30
31 id=in_id;
32 enum_type=in_enum_type;
33 M=in_M;
34 N=in_N;
35
36 /*Copy result in values*/
37 if(M*N){
38 values=(double*)xmalloc(M*N*sizeof(double));
39 memcpy(values,in_values,M*N*sizeof(double));
40 }
41 else values=NULL;
42
43 step=in_step;
44 time=in_time;
45}
46/*}}}*/
47/*FUNCTION DoubleMatExternalResult::~DoubleMatExternalResult(){{{1*/
48DoubleMatExternalResult::~DoubleMatExternalResult(){
49
50 xfree((void**)&this->values);
51 return;
52}
53/*}}}*/
54
55/*Object virtual functions definitions:*/
56/*FUNCTION DoubleMatExternalResult::Echo {{{1*/
57void DoubleMatExternalResult::Echo(void){
58
59 printf("DoubleMatExternalResult:\n");
60 printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
61 printf(" step: %i\n",this->step);
62 printf(" time: %g\n",this->time);
63 printf(" matrix size: %i-%i\n",this->M,this->N);
64
65}
66/*}}}*/
67/*FUNCTION DoubleMatExternalResult::DeepEcho{{{1*/
68void DoubleMatExternalResult::DeepEcho(void){
69
70 int i,j;
71
72 printf("DoubleMatExternalResult:\n");
73 printf(" id: %i\n",this->id);
74 printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
75 printf(" step: %i\n",this->step);
76 printf(" time: %g\n",this->time);
77 printf(" matrix size: %i-%i\n",this->M,this->N);
78 for (i=0;i<this->M;i++){
79 printf(" [ ");
80 for (j=0;j<this->N;j++){
81 printf(" %12.6g ",this->values[i*this->N+j]);
82 }
83 printf(" ]\n");
84 }
85 printf("\n");
86
87}
88/*}}}*/
89/*FUNCTION DoubleMatExternalResult::Id{{{1*/
90int DoubleMatExternalResult::Id(void){ return -1; }
91/*}}}*/
92/*FUNCTION DoubleMatExternalResult::MyRank{{{1*/
93int DoubleMatExternalResult::MyRank(void){
94 extern int my_rank;
95 return my_rank;
96}
97/*}}}*/
98#ifdef _SERIAL_
99/*FUNCTION DoubleMatExternalResult::Marshall{{{1*/
100void DoubleMatExternalResult::Marshall(char** pmarshalled_dataset){
101
102 char* marshalled_dataset=NULL;
103 int enum_value=0;
104
105 /*recover marshalled_dataset: */
106 marshalled_dataset=*pmarshalled_dataset;
107
108 /*get enum value of DoubleMatExternalResult: */
109 enum_value=DoubleMatExternalResultEnum;
110
111 /*marshall enum: */
112 memcpy(marshalled_dataset,&enum_value,sizeof(enum_value));marshalled_dataset+=sizeof(enum_value);
113
114 /*marshall DoubleMatExternalResult data: */
115 memcpy(marshalled_dataset,&id,sizeof(id));marshalled_dataset+=sizeof(id);
116 memcpy(marshalled_dataset,&enum_type,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
117 memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
118 memcpy(marshalled_dataset,&N,sizeof(N));marshalled_dataset+=sizeof(N);
119 memcpy(marshalled_dataset,values,M*sizeof(double));marshalled_dataset+=M*sizeof(double);
120 memcpy(marshalled_dataset,&step,sizeof(step));marshalled_dataset+=sizeof(step);
121 memcpy(marshalled_dataset,&time,sizeof(time));marshalled_dataset+=sizeof(time);
122
123 *pmarshalled_dataset=marshalled_dataset;
124}
125/*}}}*/
126/*FUNCTION DoubleMatExternalResult::MarshallSize{{{1*/
127int DoubleMatExternalResult::MarshallSize(){
128
129 return sizeof(M)
130 +sizeof(N)
131 +M*N*sizeof(double)
132 +sizeof(id)
133 +sizeof(enum_type)
134 +sizeof(step)
135 +sizeof(time)
136 +sizeof(int); //sizeof(int) for enum value
137}
138/*}}}*/
139/*FUNCTION DoubleMatExternalResult::Demarshall{{{1*/
140void DoubleMatExternalResult::Demarshall(char** pmarshalled_dataset){
141
142 char* marshalled_dataset=NULL;
143 int i;
144
145 /*recover marshalled_dataset: */
146 marshalled_dataset=*pmarshalled_dataset;
147
148 /*this time, no need to get enum type, the pointer directly points to the beginning of the
149 *object data (thanks to DataSet::Demarshall):*/
150 memcpy(&id,marshalled_dataset,sizeof(id));marshalled_dataset+=sizeof(id);
151 memcpy(&enum_type,marshalled_dataset,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
152
153 /*data: */
154 memcpy(&M,marshalled_dataset,sizeof(M));marshalled_dataset+=sizeof(M);
155 memcpy(&N,marshalled_dataset,sizeof(N));marshalled_dataset+=sizeof(N);
156 values=(double*)xmalloc(M*N*sizeof(double));
157 memcpy(values,marshalled_dataset,M*N*sizeof(double));marshalled_dataset+=M*N*sizeof(double);
158 memcpy(&step,marshalled_dataset,sizeof(step));marshalled_dataset+=sizeof(step);
159 memcpy(&time,marshalled_dataset,sizeof(time));marshalled_dataset+=sizeof(time);
160
161 /*return: */
162 *pmarshalled_dataset=marshalled_dataset;
163 return;
164}
165/*}}}*/
166#endif
167/*FUNCTION DoubleMatExternalResult::ObjectEnum{{{1*/
168int DoubleMatExternalResult::ObjectEnum(void){
169
170 return DoubleMatExternalResultEnum;
171
172}
173/*}}}*/
174/*FUNCTION DoubleMatExternalResult::copy{{{1*/
175Object* DoubleMatExternalResult::copy() {
176
177 return new DoubleMatExternalResult(this->id,this->enum_type,this->values,this->M,this->N,this->step,this->time);
178
179}
180/*}}}*/
181
182/*DoubleMatExternalResult management: */
183/*FUNCTION DoubleMatExternalResult::WriteData{{{1*/
184void DoubleMatExternalResult::WriteData(FILE* fid,bool io_gather){
185
186 int length;
187 int type;
188 int rows,cols;
189 char *name = NULL;
190 extern int my_rank;
191
192 if(io_gather){
193 /*we are gathering the data on cpu 0, don't write on other cpus: */
194 if(my_rank) return;
195 }
196
197 /*First write enum: */
198 EnumToStringx(&name,this->enum_type);
199 length=(strlen(name)+1)*sizeof(char);
200 fwrite(&length,sizeof(int),1,fid);
201 fwrite(name,length,1,fid);
202
203 /*Now write time and step: */
204 fwrite(&time,sizeof(double),1,fid);
205 fwrite(&step,sizeof(int),1,fid);
206
207 /*writing a double array, type is 3:*/
208 type=3;
209 fwrite(&type,sizeof(int),1,fid);
210 rows=this->M;
211 fwrite(&rows,sizeof(int),1,fid);
212 cols=this->N;
213 fwrite(&cols,sizeof(int),1,fid);
214 fwrite(this->values,cols*rows*sizeof(double),1,fid);
215
216}
217/*}}}1*/
218/*FUNCTION DoubleMatExternalResult::GetResultName{{{1*/
219void DoubleMatExternalResult::GetResultName(char** pname){
220 EnumToStringx(pname,this->enum_type);
221}
222/*}}}*/
223/*FUNCTION DoubleMatExternalResult::SetMatlabField{{{1*/
224#ifdef _SERIAL_
225void DoubleMatExternalResult::SetMatlabField(mxArray* dataref){
226
227 mxArray* pfield=NULL;
228 mxArray* pfield2=NULL;
229 char* name=NULL;
230 double* doublemat=NULL;
231
232 /*Make a copy of the value, to be used by matlab: */
233 doublemat=(double*)xmalloc(M*N*sizeof(double));
234 memcpy(doublemat,values,M*N*sizeof(double));
235
236 /*recover name: */
237 this->GetResultName(&name);
238
239 /*create matlab matrix: */
240 pfield=mxCreateDoubleMatrix(0,0,mxREAL);
241 mxSetM(pfield,N);
242 mxSetN(pfield,M);
243 mxSetPr(pfield,doublemat);
244
245 /*transpose the matrix, from c to matlab format */
246 mexCallMATLAB(1,&pfield2, 1, &pfield, "transpose");
247
248 /*set tranpose matrix inside the dataref structure: */
249 mxSetField( dataref, this->step-1, name,pfield2);
250 mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time));
251 mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step));
252
253}
254#endif
255/*}}}*/
256/*FUNCTION DoubleMatExternalResult::GetStep{{{1*/
257int DoubleMatExternalResult::GetStep(void){
258
259 return this->step;
260}
261/*}}}*/
Note: See TracBrowser for help on using the repository browser.