Ice Sheet System Model  4.18
Code documentation
GenericExternalResult.h
Go to the documentation of this file.
1 
5 #ifndef _GENERIC_EXTERNAL_RESULT_
6 #define _GENERIC_EXTERNAL_RESULT_
7 
8 /*Headers:{{{*/
9 #ifdef HAVE_CONFIG_H
10  #include <config.h>
11 #else
12 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
13 #endif
14 
15 #include <cstring>
16 #include "./ExternalResult.h"
17 #include "../../shared/shared.h"
18 /*}}}*/
19 
20 template <class ResultType>
22 
23  private:
24  int id;
25  char* result_name;
26  ResultType value;
27  int M;
28  int N;
29  int step;
31 
32  public:
33  /*Diverse: must be in front, as it is used in what follows*/
34  void GenericEcho(void){/*{{{*/
35  _printf_(" id : " << this->id << "\n");
36  _printf_(" result_name : " << this->result_name<< "\n");
37  _printf_(" step : " << this->step << "\n");
38  _printf_(" time : " << this->time << "\n");
39  }
40  /*}}}*/
41  void GenericWriteData(FILE* fid){/*{{{*/
42 
43  IssmPDouble passiveDouble;
44 
45  /*First write name: */
46  int length=(strlen(this->result_name)+1)*sizeof(char);
47  fwrite(&length,sizeof(int),1,fid);
48  fwrite(this->result_name,length,1,fid);
49 
50  /*Now write time and step: */
51  passiveDouble=reCast<IssmPDouble>(time);
52  fwrite(&passiveDouble,sizeof(IssmPDouble),1,fid);
53  fwrite(&step,sizeof(int),1,fid);
54  } /*}}}*/
55  void GenericMarshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
56 
58  MARSHALLING(id);
59  MARSHALLING(step);
61 
62  /*Marshal result name*/
63  int size = 0;
64  if(marshall_direction==MARSHALLING_FORWARD || marshall_direction == MARSHALLING_SIZE) size=strlen(result_name)+1;
65  MARSHALLING(size);
67 
68  /*Marshall value*/
69  this->value=0;
70  bool isnull=true;
71  if(marshall_direction==MARSHALLING_FORWARD || marshall_direction == MARSHALLING_SIZE){
72  if(value) isnull=false;
73  }
74  MARSHALLING(isnull);
75  if(!isnull){MARSHALLING(value);}
76  } /*}}}*/
77 
78  /*GenericExternalResult constructors and destructors*/
80  id = 0;
81  result_name = NULL;
82  M = 0;
83  N = 0;
84  step = 0;
85  time = 0;
86  value = 0;
87  } /*}}}*/
88  GenericExternalResult(int in_id, int in_enum_type,ResultType in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
89  id = in_id;
90  step = in_step;
91  time = in_time;
92  M = in_M;
93  N = in_N;
94 
95  /*Copy result in values*/
96  if(M*N){
97  value=xNew<IssmDouble>(M*N);
98  xMemCpy<IssmDouble>(value,in_values,M*N);
99  }
100  else value=NULL;
101 
102  /*Convert enum to name*/
103  EnumToStringx(&this->result_name,in_enum_type);
104  }
105 /*}}}*/
106  GenericExternalResult(int in_id,const char* name_in,ResultType in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
107  _error_("template GenericExternalResult(int in_id, int in_enum_type,double* in_values, int in_M,int in_N,int in_step,IssmDouble in_time) not implemented for this ResultType\n");
108  }
109  /*}}}*/
110  GenericExternalResult(int in_id, int in_enum_type,ResultType in_value,int in_step, IssmDouble in_time){ /*{{{*/
111  id = in_id;
112  value = in_value;
113  step = in_step;
114  time = in_time;
115  M = 1;
116  N = 1;
117 
118  /*Convert enum to name*/
119  EnumToStringx(&this->result_name,in_enum_type);
120  }
121  /*}}}*/
122  GenericExternalResult(int in_id, int in_enum_type,ResultType in_value){ /*{{{*/
123  id = in_id;
124  value = in_value;
125  step = UNDEF;
126  time = UNDEF;
127  M = 1;
128  N = 1;
129 
130  /*Convert enum to name*/
131  EnumToStringx(&this->result_name,in_enum_type);
132  }
133  /*}}}*/
134  GenericExternalResult(int in_id,const char* in_result_name,ResultType in_value,int in_step, IssmDouble in_time){ /*{{{*/
135  id = in_id;
136  value = in_value;
137  step = in_step;
138  time = in_time;
139  M = 1;
140  N = 1;
141 
142  /*Copy name*/
143  this->result_name = xNew<char>(strlen(in_result_name)+1);
144  xMemCpy<char>(this->result_name,in_result_name,strlen(in_result_name)+1);
145  }
146  /*}}}*/
148  xDelete<char>(result_name);
149  } /*}}}*/
150 
151  /*Object virtual functions definitions:*/
152  Object* copy(void) { /*{{{*/
153  return new GenericExternalResult<ResultType>(this->id,this->result_name,this->value,this->step,this->time);
154  } /*}}}*/
155  void Echo(void){ /*{{{*/
156  this->DeepEcho();
157  }
158  /*}}}*/
159  void DeepEcho(void){ /*{{{*/
160  _error_("template DeepEcho not implemented for this ResultType\n");
161  }
162  /*}}}*/
163  int Id(void){ /*{{{*/
164  return -1;
165  } /*}}}*/
166  int ObjectEnum(void){ /*{{{*/
167  _error_("template ObjectEnum not implemented for this ResultType\n");
168  } /*}}}*/
169  void Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
170  _error_("not implemented yet!");
171  }
172  /*}}}*/
173 
174  /*GenericExternalResult management: */
175 void WriteData(FILE* fid,bool io_gather){ /*{{{*/
176 
177  int my_rank;
178  int type;
179  int size;
180  IssmPDouble passiveDouble;
181 
182  /*recover my_rank:*/
183  my_rank=IssmComm::GetRank();
184 
185  /*return if now on cpu 0: */
186  if(my_rank)return;
187 
188  /*use generic part, same for all ResultTypes: */
189  this->GenericWriteData(fid);
190 
191  /*writing a IssmPDouble for Matlab or Python to post-process, type is 1, size is 1: */
192  type=1;
193  size=1;
194  fwrite(&type,sizeof(int),1,fid);
195  fwrite(&size,sizeof(int),1,fid);
196 
197  /*cast to a IssmPDouble: */
198  passiveDouble=reCast<IssmPDouble>(value);
199  fwrite(&passiveDouble,size*sizeof(IssmPDouble),1,fid);
200 
201 } /*}}}*/
202 void Transpose(void){ /*{{{*/
203  _error_("not implemented yet");
204 } /*}}}*/
205 char* GetResultName(void){ /*{{{*/
206  char* name = xNew<char>(strlen(this->result_name)+1);
207  xMemCpy<char>(name,this->result_name,strlen(this->result_name)+1);
208  return name;
209 } /*}}}*/
210 int GetResultEnum(void){ /*{{{*/
211  return StringToEnumx(this->result_name,false);
212 } /*}}}*/
213 int GetStep(void){ /*{{{*/
214  return this->step;
215 } /*}}}*/
216 double GetValue(void){ /*{{{*/
217  /*Only supported by IssmPDouble result, error out by default*/
218  _error_("not supported for this type of result");
219 } /*}}}*/
220 double* GetValues(void){ /*{{{*/
221  /*Only supported by IssmPDouble* result, error out by default*/
222  _error_("not supported for this type of result");
223 } /*}}}*/
224 };
225 
226 /*Specific instantiations for bool: */
227 template <> inline void GenericExternalResult<bool>::DeepEcho(void){ /*{{{*/
228 
229  _printf_("GenericExternalResult<bool>:\n");
230  this->GenericEcho();
231  _printf_(" value: " <<(this->value?"true":"false") << "\n");
232 
233 } /*}}}*/
234 template <> inline int GenericExternalResult<bool>::ObjectEnum(void){ /*{{{*/
235  return BoolExternalResultEnum;
236 } /*}}}*/
237 template <> inline void GenericExternalResult<bool>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
238 
239  this->GenericMarshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
240 
241 } /*}}}*/
242 
243 /*Specific instantiations for int: */
244 template <> inline void GenericExternalResult<int>::DeepEcho(void){ /*{{{*/
245 
246  _printf_("GenericExternalResult<int>:\n");
247  this->GenericEcho();
248  _printf_(" value: " << this->value << "\n");
249 
250 } /*}}}*/
251 template <> inline int GenericExternalResult<int>::ObjectEnum(void){ /*{{{*/
252  return IntExternalResultEnum;
253 } /*}}}*/
254 template <> inline void GenericExternalResult<int>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
255 
256  this->GenericMarshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
257 
258 } /*}}}*/
259 
260 /*Specific instantiations for double: */
261 template <> inline void GenericExternalResult<double>::DeepEcho(void){ /*{{{*/
262 
263  _printf_("GenericExternalResult<double>:\n");
264  this->GenericEcho();
265  _printf_(" value: " << this->value << "\n");
266 
267 } /*}}}*/
268 template <> inline int GenericExternalResult<double>::ObjectEnum(void){ /*{{{*/
270 } /*}}}*/
271 template <> inline double GenericExternalResult<double>::GetValue(void){ /*{{{*/
272  return value;
273 } /*}}}*/
274 template <> inline void GenericExternalResult<double>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
275 
276  this->GenericMarshall(pmarshalled_data,pmarshalled_data_size,marshall_direction);
277 
278 } /*}}}*/
279 
280 /*Specific instantiations for char*: */
281 template <> inline GenericExternalResult<char*>::GenericExternalResult(int in_id, int in_enum_type,char* in_value,int in_step, IssmDouble in_time){ /*{{{*/
282 
283  id = in_id;
284  value = xNew<char>(strlen(in_value)+1);
285  xMemCpy<char>(value,in_value,(strlen(in_value)+1));
286  step = in_step;
287  time = in_time;
288  M = 1;
289  N = 1;
290 
291  /*Convert enum to name*/
292  EnumToStringx(&this->result_name,in_enum_type);
293 
294 } /*}}}*/
295 template <> inline GenericExternalResult<char*>::GenericExternalResult(int in_id, int in_enum_type,char* in_value){ /*{{{*/
296 
297  id = in_id;
298  value = xNew<char>(strlen(in_value)+1);
299  xMemCpy<char>(value,in_value,(strlen(in_value)+1));
300  step = UNDEF;
301  time = UNDEF;
302 
303  /*Convert enum to name*/
304  EnumToStringx(&this->result_name,in_enum_type);
305 
306 } /*}}}*/
308  xDelete<char>(result_name);
309  xDelete<char>(value);
310 } /*}}}*/
311 template <> inline void GenericExternalResult<char*>::DeepEcho(void){ /*{{{*/
312 
313  _printf_("GenericExternalResult<char*>:\n");
314  this->GenericEcho();
315  _printf_(" value: " << this->value << "\n");
316 
317 } /*}}}*/
318 template <> inline void GenericExternalResult<char*>::WriteData(FILE* fid,bool io_gather){ /*{{{*/
319 
320  int my_rank;
321  int type;
322  int length;
323 
324  /*recover my_rank:*/
325  my_rank=IssmComm::GetRank();
326 
327  /*return if now on cpu 0: */
328  if(my_rank)return;
329 
330  /*use generic part, same for all ResultTypes: */
331  this->GenericWriteData(fid);
332 
333  /*writing a string, type is 2: */
334  type=2;
335  fwrite(&type,sizeof(int),1,fid);
336 
337  length=(strlen(this->value)+1)*sizeof(char);
338  fwrite(&length,sizeof(int),1,fid);
339  fwrite(this->value,length,1,fid);
340 }
341 /*}}}*/
342 template <> inline int GenericExternalResult<char*>::ObjectEnum(void){ /*{{{*/
344 } /*}}}*/
345 template <> inline void GenericExternalResult<char*>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
346 
347  int size = 0;
348 
349  if(marshall_direction==MARSHALLING_FORWARD || marshall_direction == MARSHALLING_SIZE)size=strlen(value)+1;
350 
351  MARSHALLING(id);
352  MARSHALLING(result_name);
353  MARSHALLING(size);
354  MARSHALLING_DYNAMIC(value,char,size);
355  MARSHALLING(step);
356  MARSHALLING(time);
357 
358 } /*}}}*/
359 
360 /*Specific instantiations for int*: */
361 template <> inline GenericExternalResult<int*>::GenericExternalResult(int in_id, int in_enum_type,int* in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
362 
363  id = in_id;
364  M = in_M;
365  N = in_N;
366 
367  EnumToStringx(&this->result_name,in_enum_type);
368 
369  step = in_step;
370  time = in_time;
371 
372  /*Copy result in values*/
373  if(M*N){
374  value=xNew<int>(M*N);
375  xMemCpy<int>(value,in_values,M*N);
376  }
377  else value=NULL;
378 }
379 /*}}}*/
380 template <> inline GenericExternalResult<int*>::GenericExternalResult(int in_id,const char* in_result_name,int* in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
381 
382  id = in_id;
383  M = in_M;
384  N = in_N;
385 
386  /*Copy name*/
387  this->result_name = xNew<char>(strlen(in_result_name)+1);
388  xMemCpy<char>(this->result_name,in_result_name,strlen(in_result_name)+1);
389 
390  step = in_step;
391  time = in_time;
392 
393  /*Copy result in values*/
394  if(M*N){
395  value=xNew<int>(M*N);
396  xMemCpy<int>(value,in_values,M*N);
397  }
398  else value=NULL;
399 }
400 /*}}}*/
401 template <> inline GenericExternalResult<int*>::GenericExternalResult(int in_id, int in_enum_type,int* in_value,int in_step, IssmDouble in_time){ /*{{{*/
402  _error_("you cannot initialize a GenericExternalResult<int*> without providing the dimensions of the matrix! Please use a more appropriate constructor!");
403 } /*}}}*/
405  xDelete<char>(result_name);
406  xDelete<int>(value);
407 } /*}}}*/
408 template <> inline void GenericExternalResult<int*>::Echo(void){ /*{{{*/
409 
410  _printf_("GenericExternalResult<int*>:\n");
411  this->GenericEcho();
412  _printf_(" matrix size: " << this->M << "-" << this->N << "\n");
413 
414 } /*}}}*/
415 template <> inline void GenericExternalResult<int*>::DeepEcho(void){ /*{{{*/
416 
417  int i,j;
418 
419  _printf_("GenericExternalResult<int*>:\n");
420  this->GenericEcho();
421 
422  _printf_(" matrix size: " << this->M << "-" << this->N << "\n");
423  for (i=0;i<this->M;i++){
424  _printf_(" [ ");
425  for (j=0;j<this->N;j++){
426  _printf_( " " << setw(11) << this->value[i*this->N+j]);
427  }
428  _printf_(" ]\n");
429  }
430 
431 } /*}}}*/
432 template <> inline Object* GenericExternalResult<int*>::copy(void){ /*{{{*/
433  return new GenericExternalResult<int*>(this->id,StringToEnumx(this->result_name),this->value,this->M,this->N,this->step,this->time);
434 } /*}}}*/
435 template <> inline void GenericExternalResult<int*>::WriteData(FILE* fid,bool io_gather){ /*{{{*/
436 
437  int my_rank;
438  int type;
439  int rows,cols;
440  char *name = NULL;
441  IssmPDouble passiveDouble;
442 
443  /*recover my_rank:*/
444  my_rank=IssmComm::GetRank();
445 
446  if(io_gather){
447  /*we are gathering the data on cpu 0, don't write on other cpus: */
448  if(my_rank) return;
449  }
450 
451  /*First write enum: */
452  int length=(strlen(this->result_name)+1)*sizeof(char);
453  fwrite(&length,sizeof(int),1,fid);
454  fwrite(this->result_name,length,1,fid);
455 
456  /*Now write time and step: */
457  passiveDouble=reCast<IssmPDouble>(time);
458  fwrite(&passiveDouble,sizeof(IssmPDouble),1,fid);
459  fwrite(&step,sizeof(int),1,fid);
460 
461  /*writing an int array, type is 4 (see parseresultsfromdisk.m):*/
462  type=4;
463  fwrite(&type,sizeof(int),1,fid);
464  rows=this->M;
465  fwrite(&rows,sizeof(int),1,fid);
466  cols=this->N;
467  fwrite(&cols,sizeof(int),1,fid);
468  fwrite(value,cols*rows*sizeof(int),1,fid);
469 
470 }
471 /*}}}*/
472 template <> inline int GenericExternalResult<int*>::ObjectEnum(void){ /*{{{*/
474 } /*}}}*/
475 template <> inline void GenericExternalResult<int*>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
476 
477  MARSHALLING_ENUM(this->ObjectEnum());
478 
479  MARSHALLING(id);
480  MARSHALLING(result_name);
481  MARSHALLING(M);
482  MARSHALLING(N);
483  MARSHALLING_DYNAMIC(value,int,M*N);
484  MARSHALLING(step);
485  MARSHALLING(time);
486 
487 } /*}}}*/
488 
489 /*Specific instantiations for IssmPDouble*: */
490 template <> inline GenericExternalResult<IssmPDouble*>::GenericExternalResult(int in_id, int in_enum_type,IssmPDouble* in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
491 
492  id = in_id;
493  M = in_M;
494  N = in_N;
495 
496  EnumToStringx(&this->result_name,in_enum_type);
497 
498  step = in_step;
499  time = in_time;
500 
501  /*Copy result in values*/
502  if(M*N){
503  value=xNew<IssmPDouble>(M*N);
504  xMemCpy<IssmPDouble>(value,in_values,M*N);
505  }
506  else value=NULL;
507 }
508 /*}}}*/
509 template <> inline GenericExternalResult<IssmPDouble*>::GenericExternalResult(int in_id,const char* in_result_name,IssmPDouble* in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
510 
511  id = in_id;
512  M = in_M;
513  N = in_N;
514 
515  /*Copy name*/
516  this->result_name = xNew<char>(strlen(in_result_name)+1);
517  xMemCpy<char>(this->result_name,in_result_name,strlen(in_result_name)+1);
518 
519  step = in_step;
520  time = in_time;
521 
522  /*Copy result in values*/
523  if(M*N){
524  value=xNew<IssmPDouble>(M*N);
525  xMemCpy<IssmPDouble>(value,in_values,M*N);
526  }
527  else value=NULL;
528 }
529 /*}}}*/
530 template <> inline GenericExternalResult<IssmPDouble*>::GenericExternalResult(int in_id, int in_enum_type,IssmPDouble* in_value,int in_step, IssmDouble in_time){ /*{{{*/
531  _error_("you cannot initialize a GenericExternalResult<IssmPDouble*> without providing the dimensions of the matrix! Please use a more appropriate constructor!");
532 } /*}}}*/
534  xDelete<char>(result_name);
535  xDelete<IssmPDouble>(value);
536 } /*}}}*/
537 template <> inline void GenericExternalResult<IssmPDouble*>::Echo(void){ /*{{{*/
538 
539  _printf_("GenericExternalResult<IssmPDouble*>:\n");
540  this->GenericEcho();
541  _printf_(" matrix size: " << this->M << "-" << this->N << "\n");
542 
543 } /*}}}*/
544 template <> inline void GenericExternalResult<IssmPDouble*>::DeepEcho(void){ /*{{{*/
545 
546  int i,j;
547 
548  _printf_("GenericExternalResult<IssmPDouble*>:\n");
549  this->GenericEcho();
550 
551  _printf_(" matrix size: " << this->M << "-" << this->N << "\n");
552  for (i=0;i<this->M;i++){
553  _printf_(" [ ");
554  for (j=0;j<this->N;j++){
555  _printf_( " " << setw(11) << setprecision (5) << this->value[i*this->N+j]);
556  }
557  _printf_(" ]\n");
558  }
559 
560 } /*}}}*/
561 template <> inline Object* GenericExternalResult<IssmPDouble*>::copy(void){ /*{{{*/
562  return new GenericExternalResult<IssmPDouble*>(this->id,StringToEnumx(this->result_name),this->value,this->M,this->N,this->step,this->time);
563 } /*}}}*/
564 template <> inline void GenericExternalResult<IssmPDouble*>::WriteData(FILE* fid,bool io_gather){ /*{{{*/
565 
566  int my_rank;
567  int type;
568  int rows,cols;
569  char *name = NULL;
570  IssmPDouble passiveDouble;
571 
572  /*recover my_rank:*/
573  my_rank=IssmComm::GetRank();
574 
575  if(io_gather){
576  /*we are gathering the data on cpu 0, don't write on other cpus: */
577  if(my_rank) return;
578  }
579 
580  /*First write enum: */
581  int length=(strlen(this->result_name)+1)*sizeof(char);
582  fwrite(&length,sizeof(int),1,fid);
583  fwrite(this->result_name,length,1,fid);
584 
585  /*Now write time and step: */
586  passiveDouble=reCast<IssmPDouble>(time);
587  fwrite(&passiveDouble,sizeof(IssmPDouble),1,fid);
588  fwrite(&step,sizeof(int),1,fid);
589 
590  /*writing a IssmDouble array, type is 3:*/
591  type=3;
592  fwrite(&type,sizeof(int),1,fid);
593  rows=this->M;
594  fwrite(&rows,sizeof(int),1,fid);
595  cols=this->N;
596  fwrite(&cols,sizeof(int),1,fid);
597  fwrite(value,cols*rows*sizeof(IssmPDouble),1,fid);
598 
599 }
600 /*}}}*/
601 template <> inline int GenericExternalResult<IssmPDouble*>::ObjectEnum(void){ /*{{{*/
603 } /*}}}*/
604 template <> inline double* GenericExternalResult<IssmPDouble*>::GetValues(void){ /*{{{*/
605  return value;
606 } /*}}}*/
607 template <> inline void GenericExternalResult<IssmPDouble*>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
608 
609  MARSHALLING_ENUM(this->ObjectEnum());
610 
611  MARSHALLING(id);
612  MARSHALLING(result_name);
613  MARSHALLING(M);
614  MARSHALLING(N);
615  MARSHALLING_DYNAMIC(value,IssmPDouble,M*N);
616  MARSHALLING(step);
617  MARSHALLING(time);
618 
619 } /*}}}*/
620 template <> inline void GenericExternalResult<IssmPDouble*>::Transpose(void){/*{{{*/
621 
622 
623  /*Perform transpose only if we have a matrix*/
624  if(M>1 && N>1){
625  IssmPDouble* temp=xNew<IssmPDouble>(M*N);
626  for(int i=0;i<M;i++){
627  for(int j=0;j<N;j++){
628  temp[j*M+i] = value[i*N+j];
629  }
630  }
631  xDelete<IssmPDouble>(this->value);
632  this->value = temp;
633  }
634 
635  /*Switch dimensions*/
636  int temp2 = this->N;
637  this->N = this->M;
638  this->M = temp2;
639 
640 
641 
642 } /*}}}*/
643 
644  /*Specific instantiations for IssmDouble*: */
645 #if defined(_HAVE_AD_) && !defined(_WRAPPERS_) //We hook off this specific specialization when not running ADOLC, otherwise we get a redeclaration with the next specialization.
647  xDelete<char>(result_name);
648  xDelete<IssmDouble>(value);
649 } /*}}}*/
650  template <> inline void GenericExternalResult<IssmDouble*>::WriteData(FILE* fid,bool io_gather){ /*{{{*/
651 
652  int i;
653  int my_rank;
654  int type;
655  int rows,cols;
656  char *name = NULL;
657  IssmPDouble passiveDouble;
658  IssmPDouble* passiveDoubles;
659 
660  /*recover my_rank:*/
661  my_rank=IssmComm::GetRank();
662 
663  if(io_gather){
664  /*we are gathering the data on cpu 0, don't write on other cpus: */
665  if(my_rank) return;
666  }
667 
668  /*First write enum: */
669  int length=(strlen(this->result_name)+1)*sizeof(char);
670  fwrite(&length,sizeof(int),1,fid);
671  fwrite(this->result_name,length,1,fid);
672 
673  /*Now write time and step: */
674  passiveDouble=reCast<IssmPDouble>(time);
675  fwrite(&passiveDouble,sizeof(IssmPDouble),1,fid);
676  fwrite(&step,sizeof(int),1,fid);
677 
678  /*writing a IssmDouble array, type is 3:*/
679  type=3;
680  fwrite(&type,sizeof(int),1,fid);
681  rows=this->M;
682  fwrite(&rows,sizeof(int),1,fid);
683  cols=this->N;
684  fwrite(&cols,sizeof(int),1,fid);
685 
686  passiveDoubles=xNew<IssmPDouble>(this->M*this->N);
687  for (i=0;i<this->M*this->N;i++)passiveDoubles[i]=reCast<IssmPDouble>(value[i]);
688  fwrite(passiveDoubles,cols*rows*sizeof(IssmPDouble),1,fid);
689  xDelete<IssmPDouble>(passiveDoubles);
690 
691  }
692  /*}}}*/
693 #endif
694 
695  /*Specifics instantiations for Vector*/
696  template <> inline GenericExternalResult<Vector<IssmPDouble>*>::GenericExternalResult(int in_id, int in_enum_type,Vector<IssmPDouble>* in_values, int in_M,int in_N,int in_step,IssmDouble in_time){/*{{{*/
697  _error_("instanciation not correct");
698  }
699  /*}}}*/
700  template <> inline GenericExternalResult<Vector<IssmPDouble>*>::GenericExternalResult(int in_id, int in_enum_type,Vector<IssmPDouble>* in_value,int in_step, IssmDouble in_time){ /*{{{*/
701  id = in_id;
702  M = 0;
703  N = 0;
704 
705  /*Convert enum to name*/
706  EnumToStringx(&this->result_name,in_enum_type);
707 
708  step = in_step;
709  time = in_time;
710 
711  value = in_value;
712  } /*}}}*/
713  template <> inline GenericExternalResult<Vector<IssmPDouble>*>::~GenericExternalResult(){ /*{{{*/
714  xDelete<char>(this->result_name);
715  delete value;
716  } /*}}}*/
717  template <> inline void GenericExternalResult<Vector<IssmPDouble>*>::Echo(void){ /*{{{*/
718 
719  _printf_("GenericExternalResult<Vector<IssmPDouble>*>:\n");
720  this->GenericEcho();
721  this->value->Echo();
722 
723  } /*}}}*/
724  template <> inline void GenericExternalResult<Vector<IssmPDouble>*>::DeepEcho(void){ /*{{{*/
725 
726  this->Echo();
727 
728  } /*}}}*/
729  template <> inline Object* GenericExternalResult<Vector<IssmPDouble>*>::copy(void){ /*{{{*/
730  return new GenericExternalResult<Vector<IssmPDouble>*>(this->id,StringToEnumx(this->result_name),this->value,this->step,this->time);
731  } /*}}}*/
732 #if defined(_HAVE_AD_) && !defined(_WRAPPERS_) //We hook off this specific specialization when not running ADOLC, otherwise we get a redeclaration with the next specialization.
733  template <> inline void GenericExternalResult<Vector<IssmPDouble>*>::WriteData(FILE* fid,bool io_gather){ /*{{{*/
734 
735  char *name = NULL;
736  int length,rows,cols=1;
737 
738  if(!io_gather){
739  _error_("not supported yet");
740  }
741 
742  /*Serialize vector on cpu0*/
743  IssmPDouble* serialvalues = this->value->ToMPISerial0();
744 
745  if(IssmComm::GetRank()==0){
746  this->value->GetSize(&rows);
747 
748  /*First write name: */
749  length=(strlen(this->result_name)+1)*sizeof(char);
750  fwrite(&length,sizeof(int),1,fid);
751  fwrite(this->result_name,length,1,fid);
752 
753  /*Now write time and step: */
754  IssmPDouble passiveDouble=reCast<IssmPDouble>(time);
755  fwrite(&passiveDouble,sizeof(IssmPDouble),1,fid);
756  fwrite(&step,sizeof(int),1,fid);
757 
758  /*writing a IssmDouble array, type is 3:*/
759  int type=3;
760  fwrite(&type,sizeof(int),1,fid);
761  fwrite(&rows,sizeof(int),1,fid);
762  fwrite(&cols,sizeof(int),1,fid);
763  fwrite(serialvalues,cols*rows*sizeof(IssmPDouble),1,fid);
764  }
765 
766  /*Clean up*/
767  xDelete<IssmPDouble>(serialvalues);
768 
769  }
770  /*}}}*/
771 template <> inline GenericExternalResult<Vector<IssmDouble>*>::~GenericExternalResult(){ /*{{{*/
772  xDelete<char>(this->result_name);
773  delete value;
774 } /*}}}*/
775 #endif
776  template <> inline int GenericExternalResult<Vector<IssmPDouble>*>::ObjectEnum(void){ /*{{{*/
777  return NoneEnum;
778  /*???? FIXME*/
779  } /*}}}*/
780 
781  /*Specifics instantiations for Vector<IssmDouble>*/
782  template <> inline void GenericExternalResult<Vector<IssmDouble>*>::WriteData(FILE* fid,bool io_gather){ /*{{{*/
783 
784  int i;
785  char *name = NULL;
786  int length,rows,cols=1;
787  IssmDouble* serialvalues = NULL;
788  IssmPDouble* pserialvalues = NULL;
789 
790  if(!io_gather){
791  _error_("not supported yet");
792  }
793 
794  /*Serialize vector only on cpu0*/
795  serialvalues = this->value->ToMPISerial0();
796 
797  if(IssmComm::GetRank()==0){
798 
799  /*Make it passive*/
800  this->value->GetSize(&rows);
801  pserialvalues=xNew<IssmPDouble>(rows);
802  for(i=0;i<rows;i++)pserialvalues[i]=reCast<IssmPDouble>(serialvalues[i]);
803 
804  /*First write name: */
805  length=(strlen(this->result_name)+1)*sizeof(char);
806  fwrite(&length,sizeof(int),1,fid);
807  fwrite(this->result_name,length,1,fid);
808 
809  /*Now write time and step: */
810  IssmPDouble passiveDouble=reCast<IssmPDouble>(time);
811  fwrite(&passiveDouble,sizeof(IssmPDouble),1,fid);
812  fwrite(&step,sizeof(int),1,fid);
813 
814  /*writing a IssmDouble array, type is 3:*/
815  int type=3;
816  fwrite(&type,sizeof(int),1,fid);
817  fwrite(&rows,sizeof(int),1,fid);
818  fwrite(&cols,sizeof(int),1,fid);
819  fwrite(pserialvalues,cols*rows*sizeof(IssmPDouble),1,fid);
820 
821  /*Clean up*/
822  xDelete<IssmPDouble>(pserialvalues);
823  }
824 
825  /*Clean up*/
826  xDelete<IssmDouble>(serialvalues);
827  }
828  /*}}}*/
829  template <> inline void GenericExternalResult<Vector<IssmDouble>*>::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
830 
831  _error_("GenericExternalResult instantiated for type Vector<IssmDouble>* called " << result_name << " not implemented yet");
832 
833  } /*}}}*/
834 
835 #endif /* _EXTERNAL_RESULTOBJECT_H */
GenericExternalResult::GetValue
double GetValue(void)
Definition: GenericExternalResult.h:216
GenericExternalResult::result_name
char * result_name
Definition: GenericExternalResult.h:25
GenericExternalResult::value
ResultType value
Definition: GenericExternalResult.h:26
IssmDouble
double IssmDouble
Definition: types.h:37
DoubleMatExternalResultEnum
@ DoubleMatExternalResultEnum
Definition: EnumDefinitions.h:1045
GenericExternalResult::DeepEcho
void DeepEcho(void)
Definition: GenericExternalResult.h:159
WriteData
void WriteData(IssmPDouble **pmatrix, int *pnel, int *matrix, int M, int N)
Definition: WriteJavascriptData.cpp:16
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
GenericExternalResult::GetStep
int GetStep(void)
Definition: GenericExternalResult.h:213
IntMatExternalResultEnum
@ IntMatExternalResultEnum
Definition: EnumDefinitions.h:1126
MARSHALLING_ENUM
#define MARSHALLING_ENUM(EN)
Definition: Marshalling.h:14
MARSHALLING_SIZE
@ MARSHALLING_SIZE
Definition: Marshalling.h:11
GenericExternalResult::WriteData
void WriteData(FILE *fid, bool io_gather)
Definition: GenericExternalResult.h:175
GenericExternalResult::Id
int Id(void)
Definition: GenericExternalResult.h:163
GenericExternalResult::Echo
void Echo(void)
Definition: GenericExternalResult.h:155
GenericExternalResult::step
int step
Definition: GenericExternalResult.h:29
GenericExternalResult::N
int N
Definition: GenericExternalResult.h:28
MARSHALLING_DYNAMIC
#define MARSHALLING_DYNAMIC(FIELD, TYPE, SIZE)
Definition: Marshalling.h:61
GenericExternalResult::GetResultName
char * GetResultName(void)
Definition: GenericExternalResult.h:205
ExternalResult
Definition: ExternalResult.h:21
Object
Definition: Object.h:13
GenericExternalResult::~GenericExternalResult
~GenericExternalResult()
Definition: GenericExternalResult.h:147
EnumToStringx
const char * EnumToStringx(int enum_in)
Definition: EnumToStringx.cpp:15
GenericExternalResult::M
int M
Definition: GenericExternalResult.h:27
BoolExternalResultEnum
@ BoolExternalResultEnum
Definition: EnumDefinitions.h:993
GenericExternalResult::GenericMarshall
void GenericMarshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: GenericExternalResult.h:55
ExternalResult.h
abstract class for ExternalResult object
UNDEF
#define UNDEF
Definition: constants.h:8
GenericExternalResult::Transpose
void Transpose(void)
Definition: GenericExternalResult.h:202
GenericExternalResult::GenericExternalResult
GenericExternalResult()
Definition: GenericExternalResult.h:79
MARSHALLING
#define MARSHALLING(FIELD)
Definition: Marshalling.h:29
GenericExternalResult::GetValues
double * GetValues(void)
Definition: GenericExternalResult.h:220
StringToEnumx
int StringToEnumx(const char *string_in, bool notfounderror=true)
Definition: StringToEnumx.cpp:14
IntExternalResultEnum
@ IntExternalResultEnum
Definition: EnumDefinitions.h:1120
GenericExternalResult::ObjectEnum
int ObjectEnum(void)
Definition: GenericExternalResult.h:166
GenericExternalResult
Definition: GenericExternalResult.h:21
IssmComm::GetRank
static int GetRank(void)
Definition: IssmComm.cpp:34
NoneEnum
@ NoneEnum
Definition: EnumDefinitions.h:1202
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
StringExternalResultEnum
@ StringExternalResultEnum
Definition: EnumDefinitions.h:1291
GenericExternalResult::GenericExternalResult
GenericExternalResult(int in_id, const char *name_in, ResultType in_values, int in_M, int in_N, int in_step, IssmDouble in_time)
Definition: GenericExternalResult.h:106
GenericExternalResult::GenericExternalResult
GenericExternalResult(int in_id, int in_enum_type, ResultType in_values, int in_M, int in_N, int in_step, IssmDouble in_time)
Definition: GenericExternalResult.h:88
GenericExternalResult::GetResultEnum
int GetResultEnum(void)
Definition: GenericExternalResult.h:210
GenericExternalResult::GenericEcho
void GenericEcho(void)
Definition: GenericExternalResult.h:34
MARSHALLING_FORWARD
@ MARSHALLING_FORWARD
Definition: Marshalling.h:9
GenericExternalResult::Marshall
void Marshall(char **pmarshalled_data, int *pmarshalled_data_size, int marshall_direction)
Definition: GenericExternalResult.h:169
DoubleExternalResultEnum
@ DoubleExternalResultEnum
Definition: EnumDefinitions.h:1042
GenericExternalResult::GenericExternalResult
GenericExternalResult(int in_id, const char *in_result_name, ResultType in_value, int in_step, IssmDouble in_time)
Definition: GenericExternalResult.h:134
GenericExternalResult::GenericExternalResult
GenericExternalResult(int in_id, int in_enum_type, ResultType in_value, int in_step, IssmDouble in_time)
Definition: GenericExternalResult.h:110
GenericExternalResult::GenericWriteData
void GenericWriteData(FILE *fid)
Definition: GenericExternalResult.h:41
GenericExternalResult::id
int id
Definition: GenericExternalResult.h:24
IssmPDouble
IssmDouble IssmPDouble
Definition: types.h:38
GenericExternalResult::GenericExternalResult
GenericExternalResult(int in_id, int in_enum_type, ResultType in_value)
Definition: GenericExternalResult.h:122
Vector
Definition: Vector.h:25
GenericExternalResult::time
IssmDouble time
Definition: GenericExternalResult.h:30
GenericExternalResult::copy
Object * copy(void)
Definition: GenericExternalResult.h:152