1 | /*!\file PetscVecExternalResult.c
|
---|
2 | * \brief: implementation of the PetscVecExternalResult 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 | /*PetscVecExternalResult constructors and destructor*/
|
---|
23 | /*FUNCTION PetscVecExternalResult::PetscVecExternalResult(){{{1*/
|
---|
24 | PetscVecExternalResult::PetscVecExternalResult(){
|
---|
25 | return;
|
---|
26 | }
|
---|
27 | /*}}}*/
|
---|
28 | /*FUNCTION PetscVecExternalResult::PetscVecExternalResult(int enum_type,IssmPetscVec value){{{1*/
|
---|
29 | PetscVecExternalResult::PetscVecExternalResult(int in_id, int in_enum_type,Vec in_value,int in_step, double in_time){
|
---|
30 |
|
---|
31 | id=in_id;
|
---|
32 | enum_type=in_enum_type;
|
---|
33 |
|
---|
34 | value=NULL;
|
---|
35 |
|
---|
36 | if(in_value){
|
---|
37 | VecDuplicate(in_value,&value);
|
---|
38 | VecCopy(in_value,value);
|
---|
39 | }
|
---|
40 | else value=NULL;
|
---|
41 |
|
---|
42 | step=in_step;
|
---|
43 | time=in_time;
|
---|
44 | }
|
---|
45 | /*}}}*/
|
---|
46 | /*FUNCTION PetscVecExternalResult::~PetscVecExternalResult(){{{1*/
|
---|
47 | PetscVecExternalResult::~PetscVecExternalResult(){
|
---|
48 | VecFree(&value);
|
---|
49 | }
|
---|
50 | /*}}}*/
|
---|
51 |
|
---|
52 | /*Object virtual functions definitions:*/
|
---|
53 | /*FUNCTION PetscVecExternalResult::Echo {{{1*/
|
---|
54 | void PetscVecExternalResult::Echo(void){
|
---|
55 |
|
---|
56 | printf("PetscVecExternalResult:\n");
|
---|
57 | printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
|
---|
58 |
|
---|
59 | }
|
---|
60 | /*}}}*/
|
---|
61 | /*FUNCTION PetscVecExternalResult::DeepEcho{{{1*/
|
---|
62 | void PetscVecExternalResult::DeepEcho(void){
|
---|
63 |
|
---|
64 | int i;
|
---|
65 | printf("PetscVecExternalResult:\n");
|
---|
66 | printf(" id: %i\n",this->id);
|
---|
67 | printf(" enum: %i (%s)\n",this->enum_type,EnumToStringx(this->enum_type));
|
---|
68 | printf(" step: %i\n",this->step);
|
---|
69 | printf(" time: %g\n",this->time);
|
---|
70 | VecView(value,PETSC_VIEWER_STDOUT_WORLD);
|
---|
71 | }
|
---|
72 | /*}}}*/
|
---|
73 | /*FUNCTION PetscVecExternalResult::Id{{{1*/
|
---|
74 | int PetscVecExternalResult::Id(void){ return -1; }
|
---|
75 | /*}}}*/
|
---|
76 | /*FUNCTION PetscVecExternalResult::MyRank{{{1*/
|
---|
77 | int PetscVecExternalResult::MyRank(void){
|
---|
78 | extern int my_rank;
|
---|
79 | return my_rank;
|
---|
80 | }
|
---|
81 | /*}}}*/
|
---|
82 | #ifdef _SERIAL_
|
---|
83 | /*FUNCTION PetscVecExternalResult::Marshall{{{1*/
|
---|
84 | void PetscVecExternalResult::Marshall(char** pmarshalled_dataset){
|
---|
85 |
|
---|
86 | char* marshalled_dataset=NULL;
|
---|
87 | int enum_value=0;
|
---|
88 | int M;
|
---|
89 | double* serial_value=NULL;
|
---|
90 |
|
---|
91 | /*recover marshalled_dataset: */
|
---|
92 | marshalled_dataset=*pmarshalled_dataset;
|
---|
93 |
|
---|
94 | /*get enum value of PetscVecExternalResult: */
|
---|
95 | enum_value=PetscVecExternalResultEnum;
|
---|
96 |
|
---|
97 | /*marshall enum: */
|
---|
98 | memcpy(marshalled_dataset,&enum_value,sizeof(enum_value));marshalled_dataset+=sizeof(enum_value);
|
---|
99 |
|
---|
100 | /*marshall PetscVecExternalResult data: */
|
---|
101 | memcpy(marshalled_dataset,&id,sizeof(id));marshalled_dataset+=sizeof(id);
|
---|
102 | memcpy(marshalled_dataset,&enum_type,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
|
---|
103 | memcpy(marshalled_dataset,&step,sizeof(step));marshalled_dataset+=sizeof(step);
|
---|
104 | memcpy(marshalled_dataset,&time,sizeof(time));marshalled_dataset+=sizeof(time);
|
---|
105 |
|
---|
106 | if(value){
|
---|
107 | VecGetSize(value,&M);
|
---|
108 | VecToMPISerial(&serial_value,value);
|
---|
109 | memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
|
---|
110 | memcpy(marshalled_dataset,serial_value,M*sizeof(double));marshalled_dataset+=(M*sizeof(double));
|
---|
111 | }
|
---|
112 | else{
|
---|
113 | M=0;
|
---|
114 | memcpy(marshalled_dataset,&M,sizeof(M));marshalled_dataset+=sizeof(M);
|
---|
115 | }
|
---|
116 | /*Free ressources:*/
|
---|
117 | xfree((void**)&serial_value);
|
---|
118 |
|
---|
119 | /*return:*/
|
---|
120 | *pmarshalled_dataset=marshalled_dataset;
|
---|
121 | }
|
---|
122 | /*}}}*/
|
---|
123 | /*FUNCTION PetscVecExternalResult::MarshallSize{{{1*/
|
---|
124 | int PetscVecExternalResult::MarshallSize(){
|
---|
125 |
|
---|
126 | int M=0;
|
---|
127 | if(value)VecGetSize(value,&M);
|
---|
128 |
|
---|
129 | return sizeof(M)+M*sizeof(double)
|
---|
130 | +sizeof(id)
|
---|
131 | +sizeof(enum_type)
|
---|
132 | +sizeof(step)
|
---|
133 | +sizeof(time)
|
---|
134 | +sizeof(int); //sizeof(int) for enum value
|
---|
135 | }
|
---|
136 | /*}}}*/
|
---|
137 | /*FUNCTION PetscVecExternalResult::Demarshall{{{1*/
|
---|
138 | void PetscVecExternalResult::Demarshall(char** pmarshalled_dataset){
|
---|
139 |
|
---|
140 | char* marshalled_dataset=NULL;
|
---|
141 | int i;
|
---|
142 | int M;
|
---|
143 | double* serial_vec=NULL;
|
---|
144 | int* idxm=NULL;
|
---|
145 |
|
---|
146 | /*recover marshalled_dataset: */
|
---|
147 | marshalled_dataset=*pmarshalled_dataset;
|
---|
148 |
|
---|
149 | /*this time, no need to get enum type, the pointer directly points to the beginning of the
|
---|
150 | *object data (thanks to DataSet::Demarshall):*/
|
---|
151 | memcpy(&id,marshalled_dataset,sizeof(id));marshalled_dataset+=sizeof(id);
|
---|
152 | memcpy(&enum_type,marshalled_dataset,sizeof(enum_type));marshalled_dataset+=sizeof(enum_type);
|
---|
153 |
|
---|
154 | /*data: */
|
---|
155 | memcpy(&step,marshalled_dataset,sizeof(step));marshalled_dataset+=sizeof(step);
|
---|
156 | memcpy(&time,marshalled_dataset,sizeof(time));marshalled_dataset+=sizeof(time);
|
---|
157 |
|
---|
158 | memcpy(&M,marshalled_dataset,sizeof(M));marshalled_dataset+=sizeof(M);
|
---|
159 | if(M){
|
---|
160 | serial_vec=(double*)xmalloc(M*sizeof(double));
|
---|
161 | memcpy(serial_vec,marshalled_dataset,M*sizeof(double));marshalled_dataset+=(M*sizeof(double));
|
---|
162 |
|
---|
163 | value=NewVec(M);
|
---|
164 | idxm=(int*)xmalloc(M*sizeof(int));
|
---|
165 | for(i=0;i<M;i++)idxm[i]=i;
|
---|
166 | VecSetValues(value,M,idxm,serial_vec,INSERT_VALUES);
|
---|
167 |
|
---|
168 | VecAssemblyBegin(value);
|
---|
169 | VecAssemblyEnd(value);
|
---|
170 |
|
---|
171 |
|
---|
172 | }
|
---|
173 | else{
|
---|
174 | value=NULL;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /*Free ressources:*/
|
---|
178 | xfree((void**)&serial_vec);
|
---|
179 | xfree((void**)&idxm);
|
---|
180 |
|
---|
181 | /*return: */
|
---|
182 | *pmarshalled_dataset=marshalled_dataset;
|
---|
183 | }
|
---|
184 | /*}}}*/
|
---|
185 | #endif
|
---|
186 | /*FUNCTION PetscVecExternalResult::ObjectEnum{{{1*/
|
---|
187 | int PetscVecExternalResult::ObjectEnum(void){
|
---|
188 |
|
---|
189 | return PetscVecExternalResultEnum;
|
---|
190 |
|
---|
191 | }
|
---|
192 | /*}}}*/
|
---|
193 | /*FUNCTION PetscVecExternalResult::copy{{{1*/
|
---|
194 | Object* PetscVecExternalResult::copy() {
|
---|
195 |
|
---|
196 | return new PetscVecExternalResult(this->id,this->enum_type,this->value,this->step,this->time);
|
---|
197 |
|
---|
198 | }
|
---|
199 | /*}}}*/
|
---|
200 |
|
---|
201 | /*PetscVecExternalResult management: */
|
---|
202 | /*FUNCTION PetscVecExternalResult::WriteData{{{1*/
|
---|
203 | void PetscVecExternalResult::WriteData(FILE* fid,bool io_gather){
|
---|
204 |
|
---|
205 | int length;
|
---|
206 | int type;
|
---|
207 | int size;
|
---|
208 | char *name = NULL;
|
---|
209 | double *serialvec = NULL;
|
---|
210 | extern int my_rank;
|
---|
211 |
|
---|
212 | /*serialize: */
|
---|
213 | VecGetSize(this->value,&size);
|
---|
214 | VecToMPISerial(&serialvec,this->value);
|
---|
215 |
|
---|
216 | /*now, exit if we are not on cpu 0: */
|
---|
217 | if(my_rank)return;
|
---|
218 |
|
---|
219 | /*First write enum: */
|
---|
220 | EnumToStringx(&name,this->enum_type);
|
---|
221 | length=(strlen(name)+1)*sizeof(char);
|
---|
222 | fwrite(&length,sizeof(int),1,fid);
|
---|
223 | fwrite(name,length,1,fid);
|
---|
224 |
|
---|
225 | /*Now write time and step: */
|
---|
226 | fwrite(&time,sizeof(double),1,fid);
|
---|
227 | fwrite(&step,sizeof(int),1,fid);
|
---|
228 |
|
---|
229 | /*writing a double, type is 1, size is 1: */
|
---|
230 | type=1;
|
---|
231 |
|
---|
232 | fwrite(&type,sizeof(int),1,fid);
|
---|
233 | fwrite(&size,sizeof(int),1,fid);
|
---|
234 | fwrite(serialvec,size*sizeof(double),1,fid);
|
---|
235 |
|
---|
236 | /*Free ressources:*/
|
---|
237 | xfree((void**)&serialvec);
|
---|
238 | }
|
---|
239 | /*}}}1*/
|
---|
240 | /*FUNCTION PetscVecExternalResult::GetResultName{{{1*/
|
---|
241 | void PetscVecExternalResult::GetResultName(char**pname){
|
---|
242 | EnumToStringx(pname,this->enum_type);
|
---|
243 | }
|
---|
244 | /*}}}*/
|
---|
245 | /*FUNCTION PetscVecExternalResult::SetMatlabField{{{1*/
|
---|
246 | #ifdef _SERIAL_
|
---|
247 | void PetscVecExternalResult::SetMatlabField(mxArray* dataref){
|
---|
248 |
|
---|
249 | mxArray* pfield=NULL;
|
---|
250 | char* name=NULL;
|
---|
251 | double* doublevec=NULL;
|
---|
252 | int M;
|
---|
253 |
|
---|
254 | VecToMPISerial(&doublevec,value);
|
---|
255 | VecGetSize(value,&M);
|
---|
256 | this->GetResultName(&name);
|
---|
257 |
|
---|
258 | pfield=mxCreateDoubleMatrix(0,0,mxREAL);
|
---|
259 | mxSetM(pfield,M);
|
---|
260 | mxSetN(pfield,1);
|
---|
261 | mxSetPr(pfield,doublevec);
|
---|
262 |
|
---|
263 | mxSetField( dataref, this->step-1, name, pfield);
|
---|
264 | mxSetField( dataref, this->step-1, "time",mxCreateDoubleScalar((double)this->time));
|
---|
265 | mxSetField( dataref, this->step-1, "step",mxCreateDoubleScalar((double)this->step));
|
---|
266 |
|
---|
267 | }
|
---|
268 | #endif
|
---|
269 | /*}}}*/
|
---|
270 | /*FUNCTION PetscVecExternalResult::GetStep{{{1*/
|
---|
271 | int PetscVecExternalResult::GetStep(void){
|
---|
272 |
|
---|
273 | return this->step;
|
---|
274 | }
|
---|
275 | /*}}}*/
|
---|