-
Notifications
You must be signed in to change notification settings - Fork 24
/
asynch_interface.c
1190 lines (1027 loc) · 38 KB
/
asynch_interface.c
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
#include "asynch_interface.h"
//Initializes the asynch solver object. Also initializes MPI, if it's not set.
asynchsolver* Asynch_Init(MPI_Comm comm,int* argc,char** argv[])
{
unsigned int i;
int init_flag;
asynchsolver* asynch = (asynchsolver*) malloc(sizeof(asynchsolver));
asynch->comm = comm;
if(comm != MPI_COMM_WORLD) printf("Warning: asynchsolver object my not work fully with in a comm other than MPI_COMM_WORLD.\n");
//Initialize MPI stuff
MPI_Initialized(&init_flag);
asynch->mpi_initialized = !init_flag;
if(asynch->mpi_initialized) MPI_Init(argc,argv); //Initialize MPI
MPI_Comm_rank(comm,&(asynch->my_rank));
MPI_Comm_size(comm,&(asynch->np));
//Sets the global variables
np = asynch->np;
my_rank = asynch->my_rank;
//Initialize asynchsolver members
asynch->outputfile = NULL;
asynch->getting = NULL;
asynch->my_data = NULL;
asynch->peakfile = NULL;
asynch->peakfilename = NULL;
asynch->custom_model = NULL;
asynch->ExternalInterface = NULL;
for(i=0;i<ASYNCH_MAX_DB_CONNECTIONS;i++) asynch->db_connections[i] = NULL;
for(i=0;i<ASYNCH_MAX_DB_CONNECTIONS - ASYNCH_DB_LOC_FORCING_START;i++) asynch->forcings[i] = InitializeForcings();
asynch->setup_gbl = 0;
asynch->setup_topo = 0;
asynch->setup_params = 0;
asynch->setup_partition = 0;
asynch->setup_rkdata = 0;
asynch->setup_initmodel = 0;
asynch->setup_initconds = 0;
asynch->setup_forcings = 0;
asynch->setup_dams = 0;
asynch->setup_stepsizes = 0;
asynch->setup_savelists = 0;
asynch->setup_finalized = 0;
//Initialize data types
asynch->dt_info = Init_DataTypes();
return asynch;
}
//Creates and loads a custom model
//Return 1 if there is an error, 0 if everything is ok
int Asynch_Custom_Model(asynchsolver* asynch,void (*SetParamSizes)(UnivVars*,void*),void (*Convert)(VEC*,unsigned int,void*),void (*Routines)(Link*,unsigned int,unsigned int,unsigned short int,void*),
void (*Precalculations)(Link*,VEC*,VEC*,unsigned int,unsigned int,unsigned short int,unsigned int,void*),int (*InitializeEqs)(VEC*,VEC*,QVSData*,unsigned short int,VEC*,unsigned int,unsigned int,unsigned int,void*,void*))
{
if(!(asynch->custom_model))
{
asynch->custom_model = (model*) malloc(sizeof(model));
asynch->custom_model->Partitioning = NULL;
}
/*
// !!!! Commented for now. I might actually want functions set to NULL for interfaces... !!!!
if(!SetParamSizes || !Convert || !Routines || !Precalculations || !InitializeEqs)
{
if(my_rank == 0)
printf("[%i]: Error creating custom model: Some routines to initialize the model are NULL.\n",my_rank);
return 1;
}
*/
asynch->custom_model->SetParamSizes = SetParamSizes;
asynch->custom_model->Convert = Convert;
asynch->custom_model->Routines = Routines;
asynch->custom_model->Precalculations = Precalculations;
asynch->custom_model->InitializeEqs = InitializeEqs;
return 0;
}
//Sets a routine to use for partitioning.
int Asynch_Custom_Partitioning(asynchsolver* asynch,int* (*Partition_Routine)(Link**,unsigned int,Link**,unsigned int,unsigned int**,unsigned int*,TransData*,short int*))
{
if(!(asynch->custom_model))
{
asynch->custom_model = (model*) malloc(sizeof(model));
asynch->custom_model->SetParamSizes = NULL;
asynch->custom_model->Convert = NULL;
asynch->custom_model->Routines = NULL;
asynch->custom_model->Precalculations = NULL;
asynch->custom_model->InitializeEqs = NULL;
}
asynch->custom_model->Partitioning = Partition_Routine;
return 0;
}
// Start network setup ******************************************************************************************************
//Reads a .gbl file.
void Asynch_Parse_GBL(asynchsolver* asynch,char* gbl_filename)
{
//Read in .gbl file
asynch->GlobalVars = Read_Global_Data(gbl_filename,&(asynch->GlobalErrors),(Forcing**) &(asynch->forcings),asynch->db_connections,asynch->rkdfilename,asynch->custom_model,asynch->ExternalInterface);
if(!asynch->GlobalVars)
{
if(my_rank == 0) printf("[%i]: An error occurred reading the .gbl file. See above messages for details.\n",my_rank);
else sleep(2);
MPI_Abort(asynch->comm,1);
}
asynch->setup_gbl = 1;
}
void Asynch_Load_Network(asynchsolver* asynch)
{
if(!asynch->setup_gbl)
{
if(my_rank == 0)
printf("Error: global file must be read before loading the network topology.\n");
MPI_Abort(asynch->comm,1);
}
asynch->sys = Create_River_Network(asynch->GlobalVars,&(asynch->N),&(asynch->id_to_loc),asynch->db_connections);
if(!asynch->sys) MPI_Abort(asynch->comm,1);
asynch->setup_topo = 1;
MPI_Barrier(asynch->comm);
}
//If load_all == 1, then the parameters for every link are available on every proc.
//If load_all == 0, then the parameters are only available for links assigned to this proc.
void Asynch_Load_Network_Parameters(asynchsolver* asynch,short int load_all)
{
int i;
if(!asynch->setup_topo)
{
if(my_rank == 0)
printf("Error: Topology data must be read before loading the link parameters.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
if(!asynch->setup_partition && !load_all)
{
if(my_rank == 0)
printf("Error: Paritioning must be done before reading link parameters.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Load_Local_Parameters(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->getting,asynch->id_to_loc,asynch->GlobalVars,asynch->db_connections,load_all,asynch->custom_model,asynch->ExternalInterface);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_params = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Partition_Network(asynchsolver* asynch)
{
int i;
if(!asynch->setup_topo)
{
if(my_rank == 0)
printf("Error: Topology data must be read before partitioning the network.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Parition_Network(asynch->sys,asynch->N,asynch->GlobalVars,&(asynch->my_sys),&(asynch->my_N),&(asynch->assignments),&(asynch->my_data),&(asynch->getting),asynch->custom_model);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_partition = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Load_Numerical_Error_Data(asynchsolver* asynch)
{
int i;
if(!asynch->setup_partition)
{
if(my_rank == 0)
printf("Error: Partitioning must be done before setting up numerical error data.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Build_RKData(asynch->sys,asynch->rkdfilename,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->getting,asynch->GlobalVars,asynch->GlobalErrors,&(asynch->AllMethods),&(asynch->nummethods));
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_rkdata = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Initialize_Model(asynchsolver* asynch)
{
int i;
if(!asynch->setup_partition)
{
if(my_rank == 0)
printf("Error: Partitioning must be done before initializing model.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
if(!asynch->setup_dams)
{
if(my_rank == 0)
printf("Error: Dam parameters must be loaded before initializing model.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
if(!asynch->setup_dams)
{
if(my_rank == 0)
printf("Error: Save lists must be loaded before initializing model.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Initialize_Model(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->getting,asynch->GlobalVars,asynch->custom_model,asynch->ExternalInterface);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_initmodel = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Load_Initial_Conditions(asynchsolver* asynch)
{
int i;
if(!asynch->setup_partition)
{
if(my_rank == 0)
printf("Error: Partitioning must be done before loading initial conditions.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
if(!asynch->setup_params)
{
if(my_rank == 0)
printf("Error: Link parameters must be loaded before loading initial conditions.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
if(!asynch->setup_dams)
{
if(my_rank == 0)
printf("Error: Dam parameters must be loaded before loading initial conditions.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Load_Initial_Conditions(asynch->sys,asynch->N,asynch->assignments,asynch->getting,asynch->id_to_loc,asynch->GlobalVars,asynch->db_connections,asynch->custom_model,asynch->ExternalInterface);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_initconds = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Load_Forcings(asynchsolver* asynch)
{
int i;
if(!asynch->setup_partition)
{
if(my_rank == 0)
printf("Error: Partitioning must be done before loading forcings.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Load_Forcings(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->getting,asynch->res_list,asynch->res_size,asynch->id_to_loc,asynch->GlobalVars,asynch->forcings,asynch->db_connections);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_forcings = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Load_Dams(asynchsolver* asynch)
{
int i;
if(!asynch->setup_partition)
{
if(my_rank == 0)
printf("Error: Partitioning must be done before loading dam information.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = Load_Dams(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->getting,asynch->id_to_loc,asynch->GlobalVars,asynch->GlobalErrors,asynch->db_connections,&(asynch->res_list),&(asynch->res_size),&(asynch->my_res_size));
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_dams = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Calculate_Step_Sizes(asynchsolver* asynch)
{
int i;
if(!asynch->setup_finalized)
{
if(my_rank == 0)
printf("Error: Network must be finalized before calculating step sizes.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = CalculateInitialStepSizes(asynch->sys,asynch->my_sys,asynch->my_N,asynch->GlobalVars,asynch->workspace,1);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_stepsizes = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Load_Save_Lists(asynchsolver* asynch)
{
int i;
if(!asynch->setup_partition)
{
if(my_rank == 0)
printf("Error: Partitioning must be done before loading output data information.\n");
else
sleep(1);
MPI_Abort(asynch->comm,1);
}
i = BuildSaveLists(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->id_to_loc,asynch->GlobalVars,&(asynch->save_list),&(asynch->save_size),&(asynch->my_save_size),&(asynch->peaksave_list),&(asynch->peaksave_size),&(asynch->my_peaksave_size),asynch->db_connections);
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_savelists = 1;
MPI_Barrier(asynch->comm);
}
void Asynch_Finalize_Network(asynchsolver* asynch)
{
int i;
if(my_rank == 0)
{
if(!asynch->setup_gbl)
{
printf("Error: Cannot finalize network: Global file not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_topo)
{
printf("Error: Cannot finalize network: Topology data not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_params)
{
printf("Error: Cannot finalize network: Link parameters not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_partition)
{
printf("Error: Cannot finalize network: Network partitioning not performed.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_rkdata)
{
printf("Error: Cannot finalize network: Numerical error data not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_initmodel)
{
printf("Error: Cannot finalize network: Model initialization not performed.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_initconds)
{
printf("Error: Cannot finalize network: Initial conditions not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_forcings)
{
printf("Error: Cannot finalize network: Forcings not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_dams)
{
printf("Error: Cannot finalize network: Dam data not loaded.\n");
MPI_Abort(asynch->comm,1);
}
else if(!asynch->setup_savelists)
{
printf("Error: Cannot finalize network: Output information not loaded.\n");
MPI_Abort(asynch->comm,1);
}
}
i = FinalizeSystem(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->assignments,asynch->getting,asynch->id_to_loc,asynch->my_data,asynch->GlobalVars,asynch->db_connections,&(asynch->workspace));
if(i) MPI_Abort(asynch->comm,1);
asynch->setup_finalized = 1;
MPI_Barrier(asynch->comm);
}
// End network setup ******************************************************************************************************
//Trash an asynchsolver object
void Asynch_Free(asynchsolver* asynch)
{
int i,finalized_flag;
Free_DataTypes(&(asynch->dt_info));
TransData_Free(asynch->my_data);
for(i=0;i<ASYNCH_MAX_DB_CONNECTIONS;i++) ConnData_Free(asynch->db_connections[i]);
Destroy_ErrorData(asynch->GlobalErrors);
Destroy_Workspace(asynch->workspace,asynch->GlobalVars->max_s,asynch->GlobalVars->max_parents);
free(asynch->workspace);
free(asynch->getting);
if(asynch->outputfile) fclose(asynch->outputfile);
if(asynch->peakfilename) free(asynch->peakfilename);
for(i=0;i<asynch->N;i++) Destroy_Link(asynch->sys[i],asynch->GlobalVars->iter_limit,asynch->rkdfilename[0] != '\0',asynch->forcings,asynch->GlobalVars);
for(i=0;i<ASYNCH_MAX_DB_CONNECTIONS - ASYNCH_DB_LOC_FORCING_START;i++) FreeForcing(&(asynch->forcings[i]));
free(asynch->sys);
free(asynch->my_sys);
free(asynch->assignments);
for(i=0;i<asynch->nummethods;i++) Destroy_RKMethod(asynch->AllMethods[i]);
free(asynch->AllMethods);
if(asynch->save_list) free(asynch->save_list);
if(asynch->peaksave_list) free(asynch->peaksave_list);
if(asynch->res_list) free(asynch->res_list);
for(i=0;i<asynch->N;i++)
free(asynch->id_to_loc[i]);
free(asynch->id_to_loc);
Destroy_UnivVars(asynch->GlobalVars);
if(asynch->custom_model) free(asynch->custom_model);
//Finalize MPI
if(asynch->mpi_initialized)
{
MPI_Finalized(&finalized_flag);
if(!finalized_flag) MPI_Finalize();
}
free(asynch);
}
void Asynch_Advance(asynchsolver* asynch,short int print_flag)
{
if(print_flag && !OutputsSet(asynch->GlobalVars))
{
printf("[%i]: Warning: Solver advance requested with data output enabled, but not all outputs are initialized. Continuing solver without outputing data.\n",my_rank);
print_flag = 0;
}
AsynchSolver(asynch->sys,asynch->N,asynch->my_sys,asynch->my_N,asynch->GlobalVars,asynch->assignments,asynch->getting,asynch->res_list,asynch->res_size,
asynch->id_to_loc,asynch->workspace,asynch->forcings,asynch->db_connections,asynch->my_data,print_flag,asynch->outputfile);
}
//Returns 0 if snapshot was made, 1 if an error was encountered, -1 if no snapshot was taken
int Asynch_Take_System_Snapshot(asynchsolver* asynch,char* preface)
{
if(!(asynch->GlobalVars->output_data->CreateSnapShot)) return -1;
return asynch->GlobalVars->output_data->CreateSnapShot(asynch->sys,asynch->N,asynch->assignments,asynch->GlobalVars,preface,asynch->db_connections[ASYNCH_DB_LOC_SNAPSHOT_OUTPUT]);
}
void Asynch_Set_Database_Connection(asynchsolver* asynch,char* database_info,unsigned int conn_idx)
{
if(asynch->db_connections[conn_idx])
{
ConnData_Free(asynch->db_connections[conn_idx]);
asynch->db_connections[conn_idx] = NULL;
}
asynch->db_connections[conn_idx] = CreateConnData(database_info);
}
double Asynch_Get_Total_Simulation_Time(asynchsolver* asynch)
{
return asynch->GlobalVars->maxtime;
}
void Asynch_Set_Total_Simulation_Time(asynchsolver* asynch,double new_time)
{
asynch->GlobalVars->maxtime = new_time;
}
unsigned int Asynch_Get_Last_Rainfall_Timestamp(asynchsolver* asynch,unsigned int forcing_idx)
{
return asynch->forcings[forcing_idx]->first_file + (unsigned int) (60.0 * asynch->forcings[forcing_idx]->maxtime);
//return asynch->GlobalVars->first_file + (unsigned int) (60.0 * asynch->GlobalVars->maxtime);
}
void Asynch_Set_First_Rainfall_Timestamp(asynchsolver* asynch,unsigned int epoch_timestamp,unsigned int forcing_idx)
{
asynch->forcings[forcing_idx]->first_file = epoch_timestamp;
//asynch->GlobalVars->first_file = epoch_timestamp;
}
void Asynch_Set_Last_Rainfall_Timestamp(asynchsolver* asynch,unsigned int epoch_timestamp,unsigned int forcing_idx)
{
asynch->forcings[forcing_idx]->last_file = epoch_timestamp;
//asynch->GlobalVars->last_file = epoch_timestamp;
}
unsigned int Asynch_Get_First_Rainfall_Timestamp(asynchsolver* asynch,unsigned int forcing_idx)
{
return asynch->forcings[forcing_idx]->first_file;
//return asynch->GlobalVars->first_file;
}
void Asynch_Set_RainDB_Starttime(asynchsolver* asynch,unsigned int epoch_timestamp,unsigned int forcing_idx)
{
asynch->forcings[forcing_idx]->raindb_start_time = epoch_timestamp;
//asynch->GlobalVars->raindb_start_time = epoch_timestamp;
}
//Returns 0 if everything is good. Returns 1 if init_flag does not support a timestamp.
int Asynch_Set_Init_Timestamp(asynchsolver* asynch,unsigned int epoch_timestamp)
{
if(asynch->GlobalVars->init_flag != 3) return 1;
asynch->GlobalVars->init_timestamp = epoch_timestamp;
return 0;
}
unsigned int Asynch_Get_Init_Timestamp(asynchsolver* asynch)
{
return asynch->GlobalVars->init_timestamp;
}
void Asynch_Set_Init_File(asynchsolver* asynch,char* filename)
{
sprintf(asynch->GlobalVars->init_filename,"%s",filename);
int length;
for(length=0;length<256;length++)
if(filename[length] == '\0') break;
if(length < 3 || length == 256)
{
if(my_rank == 0) printf("Error: Bad init filename: %s.\n",filename);
MPI_Abort(asynch->comm,1);
}
//Check what type of file
if(filename[length-1] == 'c')
{
if(filename[length-2] == 'e' && filename[length-3] == 'r' && filename[length-4] == '.')
{
asynch->GlobalVars->init_flag = 2;
return;
}
}
if(filename[length-1] == 'i' && filename[length-2] == 'n' && filename[length-3] == 'i')
{
if(filename[length-4] == '.')
{
asynch->GlobalVars->init_flag = 0;
return;
}
if(filename[length-4] == 'u' && filename[length-5] == '.')
{
asynch->GlobalVars->init_flag = 1;
return;
}
}
if(my_rank == 0) printf("Error: Bad init filename: %s.\n",filename);
MPI_Abort(asynch->comm,1);
}
void Asynch_Prepare_Output(asynchsolver* asynch)
{
if(asynch->GlobalVars->output_data->PrepareOutput) asynch->GlobalVars->output_data->PrepareOutput(asynch->GlobalVars,asynch->db_connections[ASYNCH_DB_LOC_HYDRO_OUTPUT]);
}
void Asynch_Prepare_Temp_Files(asynchsolver* asynch)
{
unsigned int i;
//Check that all outputs are set
for(i=0;i<asynch->GlobalVars->num_print;i++)
{
if(asynch->GlobalVars->output_types[i] == ASYNCH_BAD_TYPE)
{
if(my_rank == 0)
printf("Error: Time series output %s is not defined.\n",asynch->GlobalVars->output_names[i]);
MPI_Abort(MPI_COMM_WORLD,1);
}
}
if(asynch->GlobalVars->output_data->PrepareTempOutput)
asynch->outputfile = asynch->GlobalVars->output_data->PrepareTempOutput(asynch->sys,asynch->N,asynch->assignments,asynch->GlobalVars,asynch->save_list,asynch->save_size,asynch->my_save_size,NULL,asynch->id_to_loc);
else
asynch->outputfile = NULL;
}
//Writes the current state to the temp files, if the current state is at a print time.
//Returns 0 if ok, 1 if no temp file is open.
int Asynch_Write_Current_Step(asynchsolver* asynch)
{
Link **sys = asynch->sys,*current;
unsigned int i,N = asynch->N,loc,save_size = asynch->save_size;
int *assignments = asynch->assignments;
double time_diff;
if(asynch->my_save_size && !(asynch->outputfile))
{
printf("[%i]: Error writting step. No temporary file is open.\n",my_rank);
return 1;
}
for(i=0;i<save_size;i++)
{
loc = find_link_by_idtoloc(asynch->save_list[i],asynch->id_to_loc,N);
current = sys[loc];
if(assignments[loc] == my_rank)
{
time_diff = fabs(current->next_save - current->last_t);
if( time_diff/current->next_save < 1e-12 || ( (fabs(current->next_save) < 1e-12) ? (time_diff < 1e-12) : 0 ) )
{
//WriteStep(current->last_t,current->list->head->y_approx,asynch->GlobalVars,current->params,current->state,asynch->outputfile,current->output_user,&(current->pos));
WriteStep(current->last_t,current->list->head->y_approx,asynch->GlobalVars,current->params,current->state,asynch->outputfile,current->output_user,&(current->pos_offset)); //!!!! Should be tail? !!!!
current->next_save += current->print_time;
(current->disk_iterations)++;
}
}
}
return 0;
}
void Asynch_Prepare_Peakflow_Output(asynchsolver* asynch)
{
if(asynch->GlobalVars->peakflow_output == NULL)
{
if(my_rank == 0) printf("Error: Peakflow function %s not defined.\n",asynch->GlobalVars->peakflow_function_name);
MPI_Abort(MPI_COMM_WORLD,1);
}
if(asynch->GlobalVars->output_data->PreparePeakflowOutput) asynch->GlobalVars->output_data->PreparePeakflowOutput(asynch->GlobalVars,asynch->peaksave_size);
}
//Return 0 means ok, -1 means no data to output
int Asynch_Create_Output(asynchsolver* asynch,char* additional_out)
{
//Flush the transfer buffers
//!!!! I'm really not sure if this should be here. Seems like either the sends in processdata should use a different tag, or
// the flush should unpack data instead of trashing it and be put in the Asynch_Advance function call. !!!!
Flush_TransData(asynch->my_data);
if(asynch->GlobalVars->output_data->CreateOutput && asynch->GlobalVars->hydrosave_flag)
return asynch->GlobalVars->output_data->CreateOutput(asynch->sys,asynch->GlobalVars,asynch->N,asynch->save_list,asynch->save_size,asynch->my_save_size,asynch->id_to_loc,asynch->assignments,NULL,additional_out,asynch->db_connections[ASYNCH_DB_LOC_HYDRO_OUTPUT],&(asynch->outputfile));
return -1;
}
//Return 0 means ok, -1 means no peakflows to output
int Asynch_Create_Peakflows_Output(asynchsolver* asynch)
{
if(asynch->GlobalVars->output_data->CreatePeakflowOutput && asynch->GlobalVars->peaksave_flag)
return asynch->GlobalVars->output_data->CreatePeakflowOutput(asynch->sys,asynch->GlobalVars,asynch->N,asynch->assignments,asynch->peaksave_list,asynch->peaksave_size,asynch->id_to_loc,asynch->db_connections[ASYNCH_DB_LOC_PEAK_OUTPUT]);
return -1;
}
//Return 0 means ok, 1 means no peakflows to output, 2 means the filename is a bad format
int Asynch_Set_Peakflow_Output_Name(asynchsolver* asynch,char* peakflowname)
{
int l;
if(asynch->GlobalVars->peaks_loc_filename)
{
l = strlen(peakflowname);
if(l > 3 && peakflowname[l-4] == '.' && peakflowname[l-3] == 'p' && peakflowname[l-2] == 'e' && peakflowname[l-1] == 'a')
{
peakflowname[l-4] = '\0';
sprintf(asynch->GlobalVars->peaks_loc_filename,peakflowname);
peakflowname[l-4] = '.';
return 0;
}
else return 2;
}
else return 1;
}
//Return 0 means ok, 1 means no peakflows to output
int Asynch_Get_Peakflow_Output_Name(asynchsolver* asynch,char* peakflowname)
{
if(asynch->GlobalVars->peaks_loc_filename)
{
sprintf(peakflowname,asynch->GlobalVars->peaks_loc_filename);
return 0;
}
else
return 1;
}
unsigned int Asynch_Get_Number_Links(asynchsolver* asynch)
{
if(!asynch) return 0;
return asynch->N;
}
unsigned int Asynch_Get_Local_Number_Links(asynchsolver* asynch)
{
if(!asynch) return 0;
return asynch->my_N;
}
/*
int Asynch_Upload_Hydrographs_Database(asynchsolver* asynch)
{
return UploadHydrosDB(asynch->sys,asynch->GlobalVars,asynch->N,asynch->save_list,asynch->save_size,asynch->my_save_size,asynch->id_to_loc,asynch->assignments,NULL,asynch->db_connections[ASYNCH_DB_LOC_HYDRO_OUTPUT]);
}
*/
int Asynch_Set_Temp_Files(asynchsolver* asynch,double set_time,void* set_value,unsigned int output_idx)
{
return SetTempFiles(set_time,set_value,asynch->GlobalVars->output_types[output_idx],output_idx,asynch->sys,asynch->N,asynch->outputfile,asynch->GlobalVars,asynch->my_save_size,asynch->id_to_loc,asynch->dt_info);
//return SetTempFiles(set_time,asynch->sys,asynch->N,asynch->outputfile,asynch->GlobalVars,asynch->my_save_size,asynch->id_to_loc);
}
int Asynch_Reset_Temp_Files(asynchsolver* asynch,double set_time)
{
return ResetTempFiles(set_time,asynch->sys,asynch->N,asynch->outputfile,asynch->GlobalVars,asynch->my_save_size,asynch->id_to_loc);
}
int Asynch_Set_Output(asynchsolver* asynch,char* name,short int data_type,void (*func)(double,VEC*,VEC*,VEC*,int,void*),int* used_states,int num_states)
{
Link **sys = asynch->sys,*current;
unsigned int *my_sys = asynch->my_sys,my_N = asynch->my_N;
unsigned int i,j,idx,loc,num_to_add,*states_to_add = NULL;
UnivVars* GlobalVars = asynch->GlobalVars;
//Find index
for(i=0;i<asynch->GlobalVars->num_print;i++)
{
if(strcmp(name,asynch->GlobalVars->output_names[i]) == 0)
{
idx = i;
break;
}
}
if(i == asynch->GlobalVars->num_print)
{
printf("[%i]: Output %s not set.\n",my_rank,name);
return 0;
}
//Add new output
switch(data_type)
{
case ASYNCH_DOUBLE:
asynch->GlobalVars->output_types[idx] = ASYNCH_DOUBLE;
asynch->GlobalVars->outputs_d[idx] = (double (*)(double,VEC*,VEC*,VEC*,int,void*)) *func;
break;
case ASYNCH_INT:
asynch->GlobalVars->output_types[idx] = ASYNCH_INT;
asynch->GlobalVars->outputs_i[idx] = (int (*)(double,VEC*,VEC*,VEC*,int,void*)) *func;
break;
default:
printf("[%i]: Error: Cannot set output. Bad function type (%hi).\n",my_rank,data_type);
MPI_Abort(MPI_COMM_WORLD,1);
}
asynch->GlobalVars->output_sizes[idx] = GetByteSize(data_type);
GetSpecifier(asynch->GlobalVars->output_specifiers[idx],data_type);
//Check if anything should be added to the dense_indices from used_states
for(loc=0;loc<my_N;loc++)
{
current = sys[my_sys[loc]];
num_to_add = 0;
states_to_add = (unsigned int*) realloc(states_to_add,num_states*sizeof(unsigned int));
for(i=0;i<num_states;i++)
{
if(used_states[i] > current->dim) continue; //State is not present at this link
for(j=0;j<current->num_dense;j++)
{
if(used_states[i] == current->dense_indices[j])
{
states_to_add[num_to_add++] = used_states[i];
break;
}
}
}
if(num_to_add)
{
current->dense_indices = (unsigned int*) realloc(current->dense_indices,(current->num_dense + num_to_add)*sizeof(unsigned int));
for(i=0;i<num_to_add;i++)
current->dense_indices[i+current->num_dense] = states_to_add[i];
current->num_dense += num_to_add;
merge_sort_1D(current->dense_indices,current->num_dense);
}
}
if(states_to_add) free(states_to_add);
return 1;
}
//Returns 1 if output function set successfully, 0 if there was a problem
int Asynch_Set_Peakflow_Output(asynchsolver* asynch,char* name,void (*func)(unsigned int,double,VEC*,VEC*,VEC*,double,unsigned int,void*,char*))
{
if(!asynch->GlobalVars->peakflow_function_name) asynch->GlobalVars->peakflow_function_name = (char*) malloc(asynch->GlobalVars->string_size*sizeof(char));
if(strcmp(name,asynch->GlobalVars->peakflow_function_name))
{
printf("[%i]: Error setting peakflow output function to %s. Function %s was previously specified.\n",my_rank,asynch->GlobalVars->peakflow_function_name,name);
return 0;
}
asynch->GlobalVars->peakflow_output = func;
return 1;
}
//Returns the link id of the link at my_sys[location]
unsigned int Asynch_Get_Local_LinkID(asynchsolver* asynch,unsigned int location)
{
return asynch->sys[asynch->my_sys[location]]->ID;
}
//Copies data dump filename to filename
//Returns 0 if filename was copied. 1 if an error occured.
//!!!! Make sure filename has enough space !!!!
int Asynch_Get_Snapshot_Output_Name(asynchsolver* asynch,char* filename)
{
if(asynch->GlobalVars->dump_loc_filename == NULL) return 1;
strcpy(filename,asynch->GlobalVars->dump_loc_filename);
return 0;
}
//Copies filename to data dump filename
//Returns 0 if filename was copied. 1 if an error occured.
int Asynch_Set_Snapshot_Output_Name(asynchsolver* asynch,char* filename)
{
if(strlen(filename) > asynch->GlobalVars->string_size) return 1;
if(!asynch->GlobalVars->dump_loc_filename)
asynch->GlobalVars->dump_loc_filename = (char*) malloc(asynch->GlobalVars->string_size * sizeof(char));
strcpy(asynch->GlobalVars->dump_loc_filename,filename);
return 0;
}
//Returns 1 if output name is set,
//0 if output name is not set,
//-1 if output name is not present
int Asynch_Check_Output(asynchsolver* asynch,char* name)
{
unsigned int i;
for(i=0;i<asynch->GlobalVars->num_print;i++)
{
if(strcmp(name,asynch->GlobalVars->output_names[i]) == 0)
{
if(asynch->GlobalVars->output_types[i] == ASYNCH_BAD_TYPE) return 0;
else return 1;
}
}
return -1;
}
//Returns 1 if using the peakflow output name is set,
//0 if the peakflow output name is not specified,
//-1 if the peakflow output name is not present
int Asynch_Check_Peakflow_Output(asynchsolver* asynch,char* name)
{
if(!asynch->GlobalVars->peakflow_function_name || !name || strcmp(name,asynch->GlobalVars->peakflow_function_name)) return -1;
return PeakflowOutputsSet(asynch->GlobalVars);
}
int Asynch_Delete_Temporary_Files(asynchsolver* asynch)
{
int ret_val = RemoveTemporaryFiles(asynch->GlobalVars,asynch->my_save_size,NULL);
//if(ret_val == 1) printf("[%i]: Error deleting temp file. File does not exist.\n");
if(ret_val == 2) printf("[%i]: Error deleting temp file.\n",my_rank);
return ret_val;
}
//Return 0 if ok, 1 if error
//!!!! This should set the current forcing to something. But what if maxtime is beyond the ceiling term? !!!!
int Asynch_Activate_Forcing(asynchsolver* asynch,unsigned int idx)
{
unsigned int i,j,l,my_N = asynch->my_N;
Link* current;
if(idx >= asynch->GlobalVars->num_forcings)
{
printf("[%i]: Cannot activate forcing %u. Not enough forcings.\n",my_rank,idx);
return 1;
}
asynch->forcings[idx]->active = 1;
/*
printf("Here\n");
getchar();
//Set the forcing value at each link
for(i=0;i<my_N;i++)
{
current = asynch->sys[asynch->my_sys[i]];
printf("rainfall buffer t = %e\n",current->last_t);
printf("*********\n");
for(l=0;l<current->forcing_buff[idx]->n_times;l++)
{
printf("%e %e\n",current->forcing_buff[idx]->rainfall[l][0],current->forcing_buff[idx]->rainfall[l][1]);
getchar();
}
printf("*********\n");
//Find the right index in rainfall
for(l=0;l<current->forcing_buff[idx]->n_times;l++)
{
printf("l = %u/%u\n",l,current->forcing_buff[idx]->n_times);
printf("last t = %e\n",current->last_t);
printf("buffer time = %e\n",current->forcing_buff[idx]->rainfall[l][0]);
getchar();
if( fabs(current->last_t - current->forcing_buff[idx]->rainfall[l][0]) < 1e-8 ) break;
}
double forcing_buffer = current->forcing_buff[idx]->rainfall[l][1];
current->forcing_values[idx] = forcing_buffer;
//Find and set the new change in rainfall
for(j=l+1;j<current->forcing_buff[idx]->n_times;j++)
{
if( fabs(current->forcing_buff[idx]->rainfall[j][1] - forcing_buffer) > 1e-8 )
{
current->forcing_change_times[idx] = current->forcing_buff[idx]->rainfall[j][0];
break;
}
}
if(j == current->forcing_buff[idx]->n_times)
current->forcing_change_times[idx] = current->forcing_buff[idx]->rainfall[j-1][0];
}
*/
return 0;
}
//Return 0 if ok, 1 if error
int Asynch_Deactivate_Forcing(asynchsolver* asynch,unsigned int idx)
{
unsigned int i,my_N = asynch->my_N,*my_sys = asynch->my_sys;
Link** sys = asynch->sys;
if(idx >= asynch->GlobalVars->num_forcings)
{
printf("[%i]: Cannot deactivate forcing %u. Not enough forcings.\n",my_rank,idx);
return 1;
}
//Deactivate forcing
asynch->forcings[idx]->active = 0;
//Clear forcing values from links
for(i=0;i<my_N;i++)
{
sys[my_sys[i]]->forcing_values[idx] = 0.0;
sys[my_sys[i]]->forcing_change_times[idx] = asynch->GlobalVars->maxtime + 1.0;
}
return 0;
}
//Sets information for a forcing.
//Returns 0 if everything is ok, 1 if there is an error.
int Asynch_Set_Forcing_State(asynchsolver* asynch,unsigned int idx,double t_0,unsigned int first_file,unsigned int last_file)
{
if(asynch->GlobalVars->num_forcings <= idx)
{
printf("[%i]: Error setting forcing state. Bad forcing index %u / %u.\n",my_rank,idx,asynch->GlobalVars->num_forcings);
return 1;
}
asynch->forcings[idx]->raindb_start_time = first_file;
asynch->forcings[idx]->maxtime = t_0;
asynch->forcings[idx]->iteration = 0;
asynch->forcings[idx]->first_file = first_file;
asynch->forcings[idx]->last_file = last_file;
return 0;
}
//Resets the peakflow information at each link.
//The current time is used for the time to peak, and the last state is used for the values.
void Asynch_Reset_Peakflow_Data(asynchsolver* asynch)
{
unsigned int i,my_N = asynch->my_N,*my_sys = asynch->my_sys;
Link *current,**sys = asynch->sys;
double t_0 = sys[my_sys[0]]->last_t;
for(i=0;i<my_N;i++)
{
current = sys[my_sys[i]];