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*/
|
---|
24 | DoubleMatExternalResult::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*/
|
---|
29 | DoubleMatExternalResult::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*/
|
---|
48 | DoubleMatExternalResult::~DoubleMatExternalResult(){
|
---|
49 |
|
---|
50 | xfree((void**)&this->values);
|
---|
51 | return;
|
---|
52 | }
|
---|
53 | /*}}}*/
|
---|
54 |
|
---|
55 | /*Object virtual functions definitions:*/
|
---|
56 | /*FUNCTION DoubleMatExternalResult::Echo {{{1*/
|
---|
57 | void 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*/
|
---|
68 | void 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*/
|
---|
90 | int DoubleMatExternalResult::Id(void){ return -1; }
|
---|
91 | /*}}}*/
|
---|
92 | /*FUNCTION DoubleMatExternalResult::MyRank{{{1*/
|
---|
93 | int DoubleMatExternalResult::MyRank(void){
|
---|
94 | extern int my_rank;
|
---|
95 | return my_rank;
|
---|
96 | }
|
---|
97 | /*}}}*/
|
---|
98 | #ifdef _SERIAL_
|
---|
99 | /*FUNCTION DoubleMatExternalResult::Marshall{{{1*/
|
---|
100 | void 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*/
|
---|
127 | int 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*/
|
---|
140 | void 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*/
|
---|
168 | int DoubleMatExternalResult::ObjectEnum(void){
|
---|
169 |
|
---|
170 | return DoubleMatExternalResultEnum;
|
---|
171 |
|
---|
172 | }
|
---|
173 | /*}}}*/
|
---|
174 | /*FUNCTION DoubleMatExternalResult::copy{{{1*/
|
---|
175 | Object* 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*/
|
---|
184 | void 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 | xfree((void**)&name);
|
---|
203 |
|
---|
204 | /*Now write time and step: */
|
---|
205 | fwrite(&time,sizeof(double),1,fid);
|
---|
206 | fwrite(&step,sizeof(int),1,fid);
|
---|
207 |
|
---|
208 | /*writing a double array, type is 3:*/
|
---|
209 | type=3;
|
---|
210 | fwrite(&type,sizeof(int),1,fid);
|
---|
211 | rows=this->M;
|
---|
212 | fwrite(&rows,sizeof(int),1,fid);
|
---|
213 | cols=this->N;
|
---|
214 | fwrite(&cols,sizeof(int),1,fid);
|
---|
215 | fwrite(this->values,cols*rows*sizeof(double),1,fid);
|
---|
216 |
|
---|
217 | }
|
---|
218 | /*}}}1*/
|
---|
219 | /*FUNCTION DoubleMatExternalResult::GetResultName{{{1*/
|
---|
220 | void DoubleMatExternalResult::GetResultName(char** pname){
|
---|
221 | EnumToStringx(pname,this->enum_type);
|
---|
222 | }
|
---|
223 | /*}}}*/
|
---|
224 | /*FUNCTION DoubleMatExternalResult::SetMatlabField{{{1*/
|
---|
225 | #if defined(_HAVE_MATLAB_) && defined(_SERIAL_)
|
---|
226 | void DoubleMatExternalResult::SetMatlabField(mxArray* dataref){
|
---|
227 |
|
---|
228 | mxArray* pfield=NULL;
|
---|
229 | mxArray* pfield2=NULL;
|
---|
230 | char* name=NULL;
|
---|
231 | double* doublemat=NULL;
|
---|
232 |
|
---|
233 | /*Make a copy of the value, to be used by matlab: */
|
---|
234 | doublemat=(double*)xmalloc(M*N*sizeof(double));
|
---|
235 | memcpy(doublemat,values,M*N*sizeof(double));
|
---|
236 |
|
---|
237 | /*recover name: */
|
---|
238 | this->GetResultName(&name);
|
---|
239 |
|
---|
240 | /*create matlab matrix: */
|
---|
241 | pfield=mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
242 | mxSetM(pfield,N);
|
---|
243 | mxSetN(pfield,M);
|
---|
244 | mxSetPr(pfield,doublemat);
|
---|
245 |
|
---|
246 | /*transpose the matrix, from c to matlab format */
|
---|
247 | mexCallMATLAB(1,&pfield2, 1, &pfield, "transpose");
|
---|
248 |
|
---|
249 | /*set tranpose matrix inside the dataref structure: */
|
---|
250 | mxSetField( dataref, this->step-1, name,pfield2);
|
---|
251 | mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time));
|
---|
252 | mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step));
|
---|
253 |
|
---|
254 | }
|
---|
255 | #endif
|
---|
256 | /*}}}*/
|
---|
257 | /*FUNCTION DoubleMatExternalResult::GetStep{{{1*/
|
---|
258 | int DoubleMatExternalResult::GetStep(void){
|
---|
259 |
|
---|
260 | return this->step;
|
---|
261 | }
|
---|
262 | /*}}}*/
|
---|