-
Notifications
You must be signed in to change notification settings - Fork 4
/
IoModel.cpp
3203 lines (2692 loc) · 102 KB
/
IoModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file IoModel.cpp
* \brief file containing the methods that will help in processing the input data coming
* into ISSM, from Matlab, or through a binary file opened for reading.
*/
/*CODES:
* 1: boolean constant
* 2: integer constant
* 3: IssmDouble constant
* 5: boolean vector
* 6: int vector
* 7: IssmDouble vector*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include "./classes.h"
#include "../shared/io/io.h"
#include "../shared/shared.h"
#include "../classes/Inputs/TransientInput.h"
#include "../toolkits/codipack/CoDiPackGlobal.h"
/*IoConstant class and methods*/
IoConstant::IoConstant(){/*{{{*/
this->isindependent = false;
this->name = NULL;
this->constant = NULL;
}
/*}}}*/
IoConstant::~IoConstant(){/*{{{*/
xDelete<char>(this->name);
delete this->constant;
}
/*}}}*/
IoConstant::IoConstant(bool value,const char* name_in){/*{{{*/
this->isindependent = false;
this->constant = new BoolParam(IoConstantEnum,value);
_assert_(name_in);
int len=strlen(name_in);
this->name=xNew<char>(len+1);
memcpy(this->name,name_in,(len+1)*sizeof(char));
}
/*}}}*/
IoConstant::IoConstant(int value,const char* name_in){/*{{{*/
this->isindependent = false;
this->constant = new IntParam(IoConstantEnum, value);
_assert_(name_in);
int len=strlen(name_in);
this->name=xNew<char>(len+1);
memcpy(this->name,name_in,(len+1)*sizeof(char));
}
/*}}}*/
IoConstant::IoConstant(IssmDouble value,const char* name_in){/*{{{*/
this->isindependent = false;
this->constant = new DoubleParam(IoConstantEnum, value);
_assert_(name_in);
int len=strlen(name_in);
this->name=xNew<char>(len+1);
memcpy(this->name,name_in,(len+1)*sizeof(char));
}
/*}}}*/
IoConstant::IoConstant(char* value,const char* name_in){/*{{{*/
this->isindependent = false;
this->constant = new StringParam(IoConstantEnum, value);
_assert_(name_in);
int len=strlen(name_in);
this->name=xNew<char>(len+1);
memcpy(this->name,name_in,(len+1)*sizeof(char));
}
/*}}}*/
IoConstant::IoConstant(char** value,int numstrings,const char* name_in){/*{{{*/
this->isindependent = false;
this->constant = new StringArrayParam(IoConstantEnum, value, numstrings);
_assert_(name_in);
int len=strlen(name_in);
this->name=xNew<char>(len+1);
memcpy(this->name,name_in,(len+1)*sizeof(char));
}
/*}}}*/
/*IoData class and methods*/
IoData::IoData(){/*{{{*/
this->isindependent = false;
this->name = NULL;
this->code = -1;
this->layout = -1;
this->M = 0;
this->N = 0;
this->data = NULL;
}
/*}}}*/
IoData::~IoData(){/*{{{*/
xDelete<char>(this->name);
xDelete<IssmDouble>(this->data);
}
/*}}}*/
IoData::IoData(IssmDouble* matrix,int code_in,int layout_in,int M_in,int N_in,const char* name_in){/*{{{*/
this->isindependent = false;
this->code = code_in;
this->layout = layout_in;
this->M = M_in;
this->N = N_in;
this->data = matrix; /*do not copy*/
_assert_(code_in==5 || code_in==6 || code_in==7);
_assert_(name_in);
int len=strlen(name_in);
this->name=xNew<char>(len+1);
memcpy(this->name,name_in,(len+1)*sizeof(char));
}
/*}}}*/
/*IoModel constructors/destructors*/
IoModel::IoModel(){/*{{{*/
this->fid=NULL;
this->solution_enum=-1;
this->my_elements=NULL;
this->my_faces=NULL;
this->my_vfaces=NULL;
this->my_edges=NULL;
this->my_vedges=NULL;
this->my_hedges=NULL;
this->my_vertices=NULL;
this->my_vertices_lids=NULL;
this->epart=NULL;
this->domaintype=-1;
this->domaindim=-1;
this->meshelementtype=-1;
this->numberofvertices=-1;
this->numberofelements=-1;
this->numberoffaces=-1;
this->numberofverticalfaces=-1;
this->numberofedges=-1;
this->numberofverticaledges=-1;
this->numberofhorizontaledges=-1;
this->facescols=-1;
this->elements=NULL;
this->faces=NULL;
this->verticalfaces=NULL;
this->edges=NULL;
this->verticaledges=NULL;
this->horizontaledges=NULL;
this->elementtofaceconnectivity = NULL;
this->elementtoverticalfaceconnectivity = NULL;
this->elementtoedgeconnectivity = NULL;
this->elementtoverticaledgeconnectivity = NULL;
this->elementtohorizontaledgeconnectivity = NULL;
this->singlenodetoelementconnectivity = NULL;
this->numbernodetoelementconnectivity = NULL;
}/*}}}*/
IoModel::IoModel(FILE* iomodel_handle,int solution_enum_in,bool trace,IssmPDouble* X){/*{{{*/
bool autodiff=false;
bool iscontrol=false;
/*First, keep track of the file handle: */
this->fid=iomodel_handle;
/*Check that Enums are Synchronized*/
this->CheckFile();
/*Keep track of solution*/
this->solution_enum = solution_enum_in;
/*If we are running in AD mode, we need to start the trace and declare our independent variables now,
*and prevent them from being erased during successive calls to iomodel->FetchConstants, iomodel->FetchData and
iomodel->DeleteData:*/
this->StartTrace(trace);
this->DeclareIndependents(trace,X);
/*Initialize and read constants:*/
this->FetchConstants(); /*this routine goes through the input file, and fetches bool, int, IssmDouble and string only, nothing memory intensive*/
/*Is this an autodiff run?*/
this->FindConstant(&autodiff,"md.autodiff.isautodiff");
this->FindConstant(&iscontrol,"md.inversion.iscontrol");
if(trace){
autodiff=true;
}
else{
if(autodiff && !iscontrol)
autodiff=true;
else
autodiff=false;
}
this->AddConstant(new IoConstant(autodiff,"md.autodiff.isautodiff"));
/*Initialize permanent data: */
this->my_elements = NULL;
this->my_faces = NULL;
this->my_vfaces = NULL;
this->my_edges = NULL;
this->my_vedges = NULL;
this->my_hedges = NULL;
this->my_vertices = NULL;
this->my_vertices_lids = NULL;
this->epart = NULL;
FindConstant(&this->domaintype,"md.mesh.domain_type");
FindConstant(&this->meshelementtype,"md.mesh.elementtype");
FetchData(&this->domaindim,"md.mesh.domain_dimension");
FetchData(&this->numberofvertices,"md.mesh.numberofvertices");
FetchData(&this->numberofelements,"md.mesh.numberofelements");
FetchData(&this->elements,NULL,NULL,"md.mesh.elements");
this->facescols = -1;
this->faces = NULL;
this->verticalfaces = NULL;
this->edges = NULL;
this->verticaledges = NULL;
this->horizontaledges = NULL;
this->elementtofaceconnectivity = NULL;
this->elementtoverticalfaceconnectivity = NULL;
this->elementtoedgeconnectivity = NULL;
this->elementtoverticaledgeconnectivity = NULL;
this->elementtohorizontaledgeconnectivity = NULL;
this->singlenodetoelementconnectivity = NULL;
this->numbernodetoelementconnectivity = NULL;
}/*}}}*/
IoModel::~IoModel(){/*{{{*/
/*Delete constants*/
vector<IoConstant*>::iterator iter1;
for(iter1=constants.begin();iter1<constants.end();iter1++){
delete *iter1;
}
this->constants.clear();
/*Delete data*/
vector<IoData*>::iterator iter2;
for(iter2=data.begin();iter2<data.end();iter2++){
#if defined(_ISSM_DEBUG_)
if(!(*iter2)->isindependent){
_printf0_("WARNING: IoData \"" << (*iter2)->name << "\" has not been freed (DeleteData has not been called)\n");
}
#endif
delete *iter2;
}
this->data.clear();
xDelete<bool>(this->my_elements);
xDelete<bool>(this->my_faces);
xDelete<bool>(this->my_vfaces);
xDelete<bool>(this->my_edges);
xDelete<bool>(this->my_vedges);
xDelete<bool>(this->my_hedges);
xDelete<bool>(this->my_vertices);
xDelete<int>(this->my_vertices_lids);
xDelete<int>(this->epart);
xDelete<int>(this->elements);
xDelete<int>(this->faces);
xDelete<int>(this->verticalfaces);
xDelete<int>(this->edges);
xDelete<int>(this->verticaledges);
xDelete<int>(this->horizontaledges);
xDelete<int>(this->elementtofaceconnectivity);
xDelete<int>(this->elementtoverticalfaceconnectivity);
xDelete<int>(this->elementtoedgeconnectivity);
xDelete<int>(this->elementtoverticaledgeconnectivity);
xDelete<int>(this->elementtohorizontaledgeconnectivity);
xDelete<int>(this->singlenodetoelementconnectivity);
xDelete<int>(this->numbernodetoelementconnectivity);
}
/*}}}*/
/*IoModel methods*/
void IoModel::AddConstant(IoConstant* in_constant){/*{{{*/
_assert_(in_constant);
/*Go through dataset of constant and check whether it already exists */
vector<IoConstant*>::iterator iter;
for(iter=constants.begin();iter<constants.end();iter++){
if(strcmp((*iter)->name,in_constant->name)==0){
delete in_constant;
return;
}
}
this->constants.push_back(in_constant);
}
/*}}}*/
void IoModel::AddConstantIndependent(IoConstant* in_constant){/*{{{*/
_assert_(in_constant);
/*Set constant as independent*/
in_constant->isindependent = true;
/*Add to constnats*/
this->AddConstant(in_constant);
}
/*}}}*/
void IoModel::AddData(IoData* in_data){/*{{{*/
_assert_(in_data);
/*Go through dataset of data and check whether it already exists */
vector<IoData*>::iterator iter;
for(iter=data.begin();iter<data.end();iter++){
if(strcmp((*iter)->name,in_data->name)==0){
delete in_data;
return;
}
}
this->data.push_back(in_data);
}
/*}}}*/
void IoModel::AddDataIndependent(IoData* in_data){/*{{{*/
_assert_(in_data);
/*Set data as independent*/
in_data->isindependent = true;
/*Add to constnats*/
this->AddData(in_data);
}
/*}}}*/
void IoModel::CheckFile(void){/*{{{*/
bool found;
int record_enum,record_name_size;
long long record_length;
char *record_name = NULL;
const char *mddot = "md.";
/*recover my_rank:*/
int my_rank=IssmComm::GetRank();
/*Check that some fields have been allocated*/
_assert_(this->fid || my_rank);
/*Go find in the binary file, the position of the data we want to fetch: */
if(my_rank==0){ //cpu 0
/*First set FILE* position to the beginning of the file: */
fseek(this->fid,0,SEEK_SET);
for(;;){
/*Read size of first string name: */
if(fread(&record_name_size,sizeof(int),1,fid)==0){
/*we have reached the end of the file. break: */
xDelete<char>(record_name);
break;
}
if(record_name_size<3 || record_name_size>80){
_error_("error while looking in binary file. Found a string of size "<<record_name_size);
}
/*Allocate string of correct size: */
record_name=xNew<char>(record_name_size+1);
record_name[record_name_size]='\0';
/*Read record_name: */
if(fread(record_name,record_name_size*sizeof(char),1,fid)==0){
/*we have reached the end of the file. break: */
found=false;
xDelete<char>(record_name);
break;
}
if(strncmp(record_name,mddot,3)!=0){
_error_("error while reading binary file: record does not start with \"md.\": "<<record_name);
}
/*Have we found the last string?*/
if(strncmp(record_name,"md.EOF",6)==0){
found = true;
xDelete<char>(record_name);
break;
}
/*Go to next Enum*/
if(fread(&record_length,sizeof(long long),1,fid)!=1) _error_("Could not read record_length");
fseek(fid,record_length,SEEK_CUR);
xDelete<char>(record_name);
}
if(!found){
_printf0_("\n");
_printf0_("=========================================================================\n");
_printf0_(" Marshalled file is corrupted \n");
_printf0_(" \n");
_printf0_(" Last record found is: \n");
_printf0_(" the corresponding model field has probably been marshalled \n");
_printf0_(" incorrectly \n");
_printf0_(" \n");
_printf0_("=========================================================================\n");
_printf0_("\n");
_error_("Binary file corrupted (See error message above)");
}
}
}
/*}}}*/
Param* IoModel::CopyConstantObject(const char* constant_name,int param_enum){/*{{{*/
/*Intermediary*/
vector<IoConstant*>::iterator iter;
for(iter=constants.begin();iter<constants.end();iter++){
IoConstant* ioconstant=*iter;
if(strcmp(ioconstant->name,constant_name)==0){
Param* output = ioconstant->constant->copy();
output->SetEnum(param_enum);
return output;
}
}
_error_("Constant \"" << constant_name << "\" not found in iomodel");
return NULL;
}
/*}}}*/
IssmDouble* IoModel::Data(const char* data_name){/*{{{*/
/*Intermediary*/
vector<IoData*>::iterator iter;
for(iter=data.begin();iter<data.end();iter++){
IoData* iodata=*iter;
if(strcmp(iodata->name,data_name)==0) return iodata->data;
}
return NULL;
}
/*}}}*/
void IoModel::ConstantToInput(Inputs* inputs,Elements* elements,IssmDouble value, int vector_enum,int type){/*{{{*/
if (type==P1Enum){
for(Object* & object : elements->objects){
Element* element=xDynamicCast<Element*>(object);
element->InputCreateP1FromConstant(inputs,this,value,vector_enum);
}
}
else _error_("not supported yet!");
return;
}
/*}}}*/
void IoModel::DeclareIndependents(bool trace,IssmPDouble* X){/*{{{*/
bool autodiff,iscontrol;
int num_independent_objects,temp;
int Xcount=0;
char** names = NULL;
int *types = NULL;
/*Initialize array detecting whether data[i] is an independent AD mode variable: */
this->FetchData(&autodiff,"md.autodiff.isautodiff");
this->FetchData(&iscontrol,"md.inversion.iscontrol");
if(trace || (autodiff && !iscontrol)){
#ifdef _HAVE_AD_
// FIXME codi here we should be able to execute codi version as normal
this->FetchData(&num_independent_objects,"md.autodiff.num_independent_objects");
if(num_independent_objects){
this->FetchMultipleData(&names,&temp,"md.autodiff.independent_name"); _assert_(temp==num_independent_objects);
this->FetchMultipleData(&types,NULL,"md.autodiff.independent_type");
/*create independent objects, and at the same time, fetch the corresponding independent variables,
*and declare them as such in ADOLC: */
for(int i=0;i<num_independent_objects;i++){
if(types[i]==0){
/*Scalar*/
this->FetchIndependentConstant(&Xcount,X,names[i]);
}
else if(types[i]==1){
/* vector:*/
this->FetchIndependentData(&Xcount,X,names[i]);
}
else{
_error_("Independent cannot be of size " << types[i]);
}
}
for(int i=0;i<num_independent_objects;i++) xDelete<char>(names[i]);
xDelete<char*>(names);
xDelete<int>(types);
}
#else
/*if we asked for AD computations, we have a problem!: */
_error_("Cannot carry out AD mode computations without support of ADOLC or CoDiPack compiled in!");
#endif
}
}
/*}}}*/
void IoModel::DeleteData(int num,...){/*{{{*/
/*Intermediaries*/
va_list ap;
char *data_name = NULL;
const char *mddot = "md.";
vector<IoData *>::iterator iter;
/*Go through the entire list of data and delete the corresponding data from the iomodel-data dataset: */
va_start(ap,num);
for(int i=0;i<num;i++){
data_name=va_arg(ap,char*);
if(strncmp(data_name,mddot,3)!=0) _error_("String provided does not start with \"md.\" ("<<data_name<<")");
for(iter=data.begin();iter<data.end();iter++){
IoData* iodata=*iter;
if(strcmp(iodata->name,data_name)==0 && !iodata->isindependent){
delete *iter;
this->data.erase(iter);
break;
}
}
}
va_end(ap);
} /*}}}*/
void IoModel::DeleteData(IssmDouble* vector_in,const char* data_name){/*{{{*/
vector<IoData*>::iterator iter;
/*do not do anything if pointer is NULL*/
if(!vector_in) return;
/*do not delete if this is an independent variable*/
for(iter=data.begin();iter<data.end();iter++){
IoData* iodata=*iter;
if(strcmp(iodata->name,data_name)==0 && iodata->isindependent){
return;
}
}
/*Go ahead and delete*/
xDelete<IssmDouble>(vector_in);
} /*}}}*/
void IoModel::DeleteData(char*** pstringarray, int numstrings,const char* data_name){/*{{{*/
char** stringarray=*pstringarray;
if(numstrings){
for(int i=0;i<numstrings;i++){
char* string=stringarray[i];
xDelete<char>(string);
}
xDelete<char*>(stringarray);
}
*pstringarray=NULL;
} /*}}}*/
void IoModel::FetchConstants(void){/*{{{*/
/*record descriptions; */
const char* mddot = "md.";
char* record_name = NULL;
int record_name_size;
long long record_length;
int record_code; //1 to 7 number
/*records: */
int booleanint = 0;
int integer = 0;
IssmPDouble pscalar = 0;
IssmDouble scalar = 0;
char *string = NULL;
char **strings = NULL;
int string_size,numstrings;
/*recover my_rank:*/
int my_rank=IssmComm::GetRank();
/*Check that some fields have been allocated*/
_assert_(this->fid || my_rank);
/*Go find in the binary file, the position of the data we want to fetch: */
if(my_rank==0){ //cpu 0{{{
/*First set FILE* position to the beginning of the file: */
fseek(this->fid,0,SEEK_SET);
/*Now march through file looking for the correct data identifiers (bool,int,IssmDouble or string): */
for(;;){
/*Read size of first string name: */
if(fread(&record_name_size,sizeof(int),1,fid)==0){
/*we have reached the end of the file. break: */
record_code=0; //0 means bailout
ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm()); /*tell others cpus we are bailing: */
break;
}
if(record_name_size<3 || record_name_size>80){
/*we are going to error out, still try and get informatoin for the user:*/
record_name=xNew<char>(record_name_size+1);
record_name[record_name_size]='\0';
/*Read record_name: */
if(fread(record_name,record_name_size*sizeof(char),1,fid)!=0){};
_error_("error while looking in binary file. String \"" << record_name << "\" is a string of size "<<record_name_size);
}
/*Allocate string of correct size: */
record_name=xNew<char>(record_name_size+1);
record_name[record_name_size]='\0';
/*Read record_name: */
if(fread(record_name,record_name_size*sizeof(char),1,fid)==0){
_error_("Could not read record name");
}
if(strncmp(record_name,mddot,3)!=0){
_error_("error while reading binary file: record does not start with \"md.\": "<<record_name);
}
/* Read the record length and the data type code: */
if(fread(&record_length,sizeof(long long),1,this->fid)!=1) _error_("Cound not read record_length");
if(fread(&record_code ,sizeof(int),1,this->fid)!=1) _error_("Cound not read record_code");
/*Tell other cpus what we are doing: */
ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm()); /*tell other cpus what we are going to do: */
/*Tell other cpus the name of the data, then branch according to the data type: */
ISSM_MPI_Bcast(&record_name_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
ISSM_MPI_Bcast(record_name,record_name_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
ISSM_MPI_Bcast(&record_length,1,ISSM_MPI_LONG_LONG_INT,0,IssmComm::GetComm());
switch(record_code){
case 1:
/*Read the boolean and broadcast it to other cpus:*/
if(fread(&booleanint,sizeof(int),1,this->fid)!=1) _error_("could not read boolean ");
ISSM_MPI_Bcast(&booleanint,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*create BoolParam: */
this->AddConstant(new IoConstant((bool)booleanint,record_name)); //cast to boolean
break;
case 2:
/*Read the integer and broadcast it to other cpus:*/
if(fread(&integer,sizeof(int),1,this->fid)!=1) _error_("could not read integer ");
/*Convert codes to Enums if needed*/
if(strcmp(record_name,"md.smb.model")==0) integer = IoCodeToEnumSMB(integer);
if(strcmp(record_name,"md.basalforcings.model")==0) integer = IoCodeToEnumBasal(integer);
if(strcmp(record_name,"md.calving.law")==0) integer = IoCodeToEnumCalving(integer);
if(strcmp(record_name,"md.frontalforcings.parameterization")==0) integer = IoCodeToEnumFrontalforcings(integer);
if(strcmp(record_name,"md.hydrology.model")==0) integer = IoCodeToEnumHydrology(integer);
if(strcmp(record_name,"md.materials.type")==0) integer = IoCodeToEnumMaterials(integer);
if(strcmp(record_name,"md.materials.nature")==0) integer = IoCodeToEnumNature(integer);
if(strcmp(record_name,"md.timestepping.type")==0) integer = IoCodeToEnumTimestepping(integer);
if(strcmp(record_name,"md.amr.type")==0) integer = IoCodeToEnumAmr(integer);
if(strcmp(record_name,"md.solidearth.settings.grdmodel")==0) integer = IoCodeToEnumGrd(integer);
/*Broadcast to other cpus*/
ISSM_MPI_Bcast(&integer,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*create IntParam: */
this->AddConstant(new IoConstant(integer,record_name));
break;
case 3:
{
/*IssmDouble, check whether it is already there (from "declare independents")*/
bool exists = false;
vector<IoConstant*>::iterator iter;
for(iter=constants.begin();iter<constants.end();iter++){
IoConstant* ioconstant=*iter;
if(strcmp(ioconstant->name,record_name)==0){
exists = true;
break;
}
}
if(!exists){
if(fread(&pscalar,sizeof(IssmPDouble),1,this->fid)!=1) _error_("could not read scalar ");
ISSM_MPI_Bcast(&pscalar,1,ISSM_MPI_PDOUBLE,0,IssmComm::GetComm());
scalar=pscalar;
/*create DoubleParam: */
this->AddConstant(new IoConstant(scalar,record_name));
}
else{
if(fread(&pscalar,sizeof(IssmPDouble),1,this->fid)!=1) _error_("could not read scalar ");
}
}
break;
case 4:
/*We have to read a string from disk. First read the dimensions of the string, then the string: */
if(fread(&string_size,sizeof(int),1,this->fid)!=1) _error_("could not read length of string ");
ISSM_MPI_Bcast(&string_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
if(string_size){
string=xNew<char>(string_size+1);
string[string_size]='\0';
/*Read string, then broadcast: */
if(fread(string,string_size*sizeof(char),1,this->fid)!=1)_error_(" could not read string ");
ISSM_MPI_Bcast(string,string_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
}
else{
string=xNew<char>(1);
string[0]='\0';
}
/*Convert strings to enums if needed*/
if(strcmp(record_name,"md.flowequation.fe_SSA")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.flowequation.fe_HO")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.flowequation.fe_FS")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.thermal.fe")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.levelset.fe")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.groundingline.migration")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.groundingline.friction_interpolation")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.groundingline.melt_interpolation")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.masstransport.hydrostatic_adjustment")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.materials.rheology_law")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.damage.elementinterp")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.mesh.domain_type")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.mesh.elementtype")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else {
/*Add string to parameters: */
this->AddConstant(new IoConstant(string,record_name));
}
/*Free string*/
xDelete<char>(string);
break;
case 5:
case 6:
case 7:
case 8:
case 10:
/*We are not interested in this record, too memory intensive. Skip it: */
/*skip: */
fseek(fid,-sizeof(int),SEEK_CUR); //backtrak 1 integer
fseek(fid,record_length,SEEK_CUR);
break;
case 9:
/*String Array*/
if(fread(&numstrings,sizeof(int),1,fid)!=1) _error_("could not read length of string array");
ISSM_MPI_Bcast(&numstrings,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*Now allocate string array: */
if(numstrings){
strings=xNew<char*>(numstrings);
for(int i=0;i<numstrings;i++)strings[i]=NULL;
/*Go through strings, and read: */
for(int i=0;i<numstrings;i++){
if(fread(&string_size,sizeof(int),1,fid)!=1) _error_("could not read length of string ");
ISSM_MPI_Bcast(&string_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
if(string_size){
string=xNew<char>((string_size+1));
string[string_size]='\0';
if(fread(string,string_size*sizeof(char),1,fid)!=1)_error_(" could not read string ");
ISSM_MPI_Bcast(string,string_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
}
else{
string=xNew<char>(1);
string[0]='\0';
}
strings[i]=string;
}
}
/*Add strings to parameters: */
this->AddConstant(new IoConstant(strings,numstrings,record_name));
/*Free string*/
for(int i=0;i<numstrings;i++) xDelete<char>(strings[i]);
xDelete<char*>(strings);
break;
default:
_error_("unknown record type:" << record_code);
break;
}
xDelete<char>(record_name);
}
} //}}}
else{ //cpu ~0 {{{
for(;;){ //wait on cpu 0
ISSM_MPI_Bcast(&record_code,1,ISSM_MPI_INT,0,IssmComm::GetComm()); /*get from cpu 0 what we are going to do: */
if(record_code==0){
break; //we are done, break from the loop
}
else{
ISSM_MPI_Bcast(&record_name_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
_assert_(record_name_size);
record_name=xNew<char>((record_name_size+1)); record_name[record_name_size]='\0';
ISSM_MPI_Bcast(record_name,record_name_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
ISSM_MPI_Bcast(&record_length,1,ISSM_MPI_LONG_LONG_INT,0,IssmComm::GetComm());
switch(record_code){
case 1:
/*boolean. get it from cpu 0 */
ISSM_MPI_Bcast(&booleanint,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*create BoolParam: */
this->AddConstant(new IoConstant((bool)booleanint,record_name)); //cast to a boolean
break;
case 2:
/*integer. get it from cpu 0 */
ISSM_MPI_Bcast(&integer,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*create IntParam: */
this->AddConstant(new IoConstant(integer,record_name));
break;
case 3:
/*scalar. get it from cpu 0 */
{
/*IssmDouble, check whether it is already there (from "declare independents")*/
bool exists = false;
vector<IoConstant*>::iterator iter;
for(iter=constants.begin();iter<constants.end();iter++){
IoConstant* ioconstant=*iter;
if(strcmp(ioconstant->name,record_name)==0){
exists = true;
break;
}
}
if(!exists){
ISSM_MPI_Bcast(&pscalar,1,ISSM_MPI_PDOUBLE,0,IssmComm::GetComm());
scalar=pscalar;
/*create DoubleParam: */
this->AddConstant(new IoConstant(scalar,record_name));
}
}
break;
case 4:
ISSM_MPI_Bcast(&string_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
if(string_size){
string=xNew<char>((string_size+1));
string[string_size]='\0';
/*Read string from cpu 0: */
ISSM_MPI_Bcast(string,string_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
}
else{
string=xNew<char>(1);
string[0]='\0';
}
if(strcmp(record_name,"md.flowequation.fe_SSA")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.flowequation.fe_HO")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.flowequation.fe_FS")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.thermal.fe")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.levelset.fe")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.groundingline.migration")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.groundingline.friction_interpolation")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.groundingline.melt_interpolation")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.masstransport.hydrostatic_adjustment")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.materials.rheology_law")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.damage.elementinterp")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.mesh.domain_type")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else if(strcmp(record_name,"md.mesh.elementtype")==0){
this->AddConstant(new IoConstant(StringToEnumx(string),record_name));
} else {
/*Add string to parameters: */
this->AddConstant(new IoConstant(string,record_name));
}
/*Free string*/
xDelete<char>(string);
break;
case 5: break; //do nothing. not interested in this type of data, which is memory intensive.
case 6: break; //do nothing. not interested in this type of data, which is memory intensive.
case 7: break; //do nothing. not interested in this type of data, which is memory intensive.
case 8: break; //do nothing. not interested in this type of data, which is memory intensive.
case 10: break; //do nothing. not interested in this type of data, which is memory intensive.
case 9:
ISSM_MPI_Bcast(&numstrings,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*Now allocate string array: */
if(numstrings){
strings=xNew<char*>(numstrings);
for(int i=0;i<numstrings;i++)strings[i]=NULL;
/*Go through strings, and read: */
for(int i=0;i<numstrings;i++){
ISSM_MPI_Bcast(&string_size,1,ISSM_MPI_INT,0,IssmComm::GetComm());
if(string_size){
string=xNew<char>((string_size+1));
string[string_size]='\0';
ISSM_MPI_Bcast(string,string_size,ISSM_MPI_CHAR,0,IssmComm::GetComm());
}
else{
string=xNew<char>(1);
string[0]='\0';
}
strings[i]=string;
}
}
/*Add strings to parameters: */
this->AddConstant(new IoConstant(strings,numstrings,record_name));
/*Free string*/
for(int i=0;i<numstrings;i++) xDelete<char>(strings[i]);
xDelete<char*>(strings);
break;
default:
_error_("unknown record type:" << record_code);
break;
}
xDelete<char>(record_name);
}
}
} //}}}
}/*}}}*/
void IoModel::FetchData(bool* pboolean,const char* data_name){/*{{{*/
/*output: */
int booleanint;
int code;
/*recover my_rank:*/
int my_rank=IssmComm::GetRank();
/*Set file pointer to beginning of the data: */
fid=this->SetFilePointerToData(&code,NULL,data_name);
if(code!=1)_error_("expecting a boolean for \"" << data_name<<"\"");
/*We have to read a boolean from disk. */
if(my_rank==0){
if(fread(&booleanint,sizeof(int),1,fid)!=1) _error_("could not read boolean ");
}
ISSM_MPI_Bcast(&booleanint,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*cast to bool: */
/*Assign output pointers: */
*pboolean=(bool)booleanint;
}
/*}}}*/
void IoModel::FetchData(int* pinteger,const char* data_name){/*{{{*/
/*output: */
int integer;
int code;
/*recover my_rank:*/
int my_rank=IssmComm::GetRank();
/*Set file pointer to beginning of the data: */
fid=this->SetFilePointerToData(&code,NULL,data_name);
if(code!=2)_error_("expecting an integer for \"" << data_name<<"\"");
/*We have to read a integer from disk. First read the dimensions of the integer, then the integer: */
if(my_rank==0){
if(fread(&integer,sizeof(int),1,fid)!=1) _error_("could not read integer ");
}
ISSM_MPI_Bcast(&integer,1,ISSM_MPI_INT,0,IssmComm::GetComm());
/*Assign output pointers: */
*pinteger=integer;
}
/*}}}*/
void IoModel::FetchData(IssmDouble* pscalar,const char* data_name){/*{{{*/
/*output: */
IssmPDouble scalar;
int code;