/
cs_restart_default.c
2802 lines (2225 loc) · 88.7 KB
/
cs_restart_default.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
/*============================================================================
* Checkpoint/restart handling for default application.
*============================================================================*/
/*
This file is part of Code_Saturne, a general-purpose CFD tool.
Copyright (C) 1998-2018 EDF S.A.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*----------------------------------------------------------------------------*/
#include "cs_defs.h"
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
* Standard C library headers
*----------------------------------------------------------------------------*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*----------------------------------------------------------------------------
* Local headers
*----------------------------------------------------------------------------*/
#include "bft_mem.h"
#include "bft_error.h"
#include "bft_printf.h"
#include "cs_field.h"
#include "cs_field_pointer.h"
#include "cs_halo.h"
#include "cs_halo_perio.h"
#include "cs_log.h"
#include "cs_map.h"
#include "cs_mesh.h"
#include "cs_parall.h"
#include "cs_mesh_location.h"
#include "cs_turbulence_model.h"
/*----------------------------------------------------------------------------
* Header for the current file
*----------------------------------------------------------------------------*/
#include "cs_restart_default.h"
/*----------------------------------------------------------------------------*/
BEGIN_C_DECLS
/*=============================================================================
* Additional doxygen documentation
*============================================================================*/
/*!
\file cs_restart_default.c
Checkpoint/restart handling for default application.
*/
/*! \cond DOXYGEN_SHOULD_SKIP_THIS */
/*=============================================================================
* Macro definitions
*============================================================================*/
/*============================================================================
* Prototypes for functions intended for use only by Fortran wrappers.
* (descriptions follow, with function bodies).
*============================================================================*/
/*============================================================================
* Type definitions
*============================================================================*/
/*============================================================================
* Static global variables
*============================================================================*/
const char *_coeff_name[] = {"bc_coeffs::a", "bc_coeffs::b",
"bc_coeffs::af", "bc_coeffs::bf",
"bc_coeffs::ad", "bc_coeffs::bd",
"bc_coeffs::ac", "bc_coeffs::bc"};
/*============================================================================
* Private function definitions
*============================================================================*/
/*----------------------------------------------------------------------------
* Read and rebuild partial field metadata from legacy checkpoint.
*
* Note that when reading legacy files (Code_Saturne version 3.3 and below),
* the old id will actually be the old scalar id (-1 for others).
*
* parameters:
* r <-- associated restart file pointer
*----------------------------------------------------------------------------*/
static void
_read_legacy_field_info(cs_restart_t *r)
{
int retcode;
int n_fields = cs_field_n_fields();
/* Initialization */
int kold = cs_field_key_id_try("old_scalar_num");
/* Now read headers */
cs_lnum_t n_old[4] = {0, 0, 0, 0}, n_cur[4] = {0, 0, 0, 0};
const char *sec_id[] = {"nombre_variables",
"nombre_scalaires",
"nombre_scalaires_us",
"nombre_scalaires_pp"};
for (int i = 0; i < 4; i++) {
retcode = cs_restart_read_section(r,
sec_id[i],
CS_MESH_LOCATION_NONE,
1,
CS_TYPE_cs_int_t,
n_old + i);
if (retcode != CS_RESTART_SUCCESS)
bft_error
(__FILE__, __LINE__, 0,
_("Error reading variable information in restart file \"%s\"."),
cs_restart_get_name(r));
}
const int kv = cs_field_key_id_try("variable_id");
const int ks = cs_field_key_id_try("scalar_id");
/* Count variables and user and non-user scalars */
for (int f_id = 0; f_id < n_fields; f_id++) {
const cs_field_t *f = cs_field_by_id(f_id);
int v_num = -1;
if (kv > -1)
v_num = cs_field_get_key_int(f, kv);
if (v_num > 0) {
int s_num = -1;
n_cur[0] += 1;
if (ks > -1)
s_num = cs_field_get_key_int(f, ks);
if (s_num > -1) {
n_cur[1] += 1;
if (f->type & CS_FIELD_USER)
n_cur[2] += 1;
else
n_cur[3] += 1;
}
}
}
/* Possible shift in old ids if temperature has been moved from
user to model scalar */
int us_shift = 0, pp_shift = 0;
/* Special case if temperature has been moved from
user to model scalar */
if ( n_cur[1] == n_old[1] && n_cur[2] == n_old[2] -1
&& n_cur[3] == 1 && n_old[3] == 0) {
if (CS_F_(t) != NULL || CS_F_(h) != NULL) {
us_shift = -1;
pp_shift = n_cur[2];
}
}
/* Warn in case of change */
if ( n_cur[0] != n_old[0] || n_cur[1] != n_old[1]
|| n_cur[2] != n_old[2] || n_cur[3] != n_old[3]) {
/* Special case if temperature has been moved from
user to model scalar */
if (n_cur[0] == n_old[0] && n_cur[1] == n_old[1] && us_shift == -1)
bft_printf
(_("\nRemark: the thermal scalar was treated as a user scalar\n"
" in the restart file, and is moved to a model scalar\n"
" in the current computation.\n"));
else {
bft_printf
(_("\n"
" Warning: the number of variables or scalars has been changed\n"
" relative to the restart file.\n\n"
" currently %d variables, of which %d scalars\n"
" previously %d variables, of which %d scalars\n\n"
" The computation continues, with a partial restart.\n"),
(int)(n_cur[0]), (int)(n_cur[1]), (int)(n_old[0]), (int)(n_old[1]));
}
}
/* Now check (and update if necessary) old scalar id */
for (int f_id = 0; f_id < n_fields; f_id++) {
cs_field_t *f = cs_field_by_id(f_id);
int old_scal_num = -1;
int s_num = -1;
if (ks > -1)
s_num = cs_field_get_key_int(f, ks);
if (s_num > -1) {
old_scal_num = -1;
if (kold > -1)
old_scal_num = cs_field_get_key_int(f, kold);
if (old_scal_num < 0) {
if (f->type & CS_FIELD_USER)
old_scal_num = s_num + us_shift;
else
old_scal_num = s_num + pp_shift;
if (old_scal_num > n_old[1])
old_scal_num = -1;
}
else {
if (old_scal_num > n_old[1])
bft_error
(__FILE__, __LINE__, 0,
_("Field \"%s\" has user-defined key \"old_scalar_num\" value %d,\n"
"but the number of available scalars in restart is %d."),
f->name, old_scal_num, (int)(n_old[1]));
}
if (kold < 0)
kold = cs_field_define_key_int("old_scalar_num",
-1,
CS_FIELD_VARIABLE);
cs_field_set_key_int(f, kold, old_scal_num);
}
}
}
/*----------------------------------------------------------------------------
* Synchronize cell-based field values.
*
* parameters:
* f <-> field whose values should be synchronized
* t_id <-- time id (0 for current, 1 for previous, ...)
*----------------------------------------------------------------------------*/
static void
_sync_field_vals(cs_field_t *f,
int t_id)
{
const cs_mesh_t *m = cs_glob_mesh;
if (m->halo != NULL) {
cs_halo_type_t halo_type = CS_HALO_EXTENDED;
cs_real_t *v = f->vals[t_id];
cs_halo_sync_var_strided(m->halo, halo_type, v, f->dim);
if (m->n_init_perio > 0) {
if (f->dim == 3)
cs_halo_perio_sync_var_vect(m->halo, halo_type, v, 3);
else if (f->dim == 6)
cs_halo_perio_sync_var_sym_tens(m->halo, halo_type, v);
else if (f->dim == 9)
cs_halo_perio_sync_var_tens(m->halo, halo_type, v);
else if (f->dim == 1 && f == CS_F_(r13)) {
cs_halo_perio_sync_var_tens_ni(m->halo,
halo_type,
CS_F_(r11)->vals[t_id],
CS_F_(r12)->vals[t_id],
CS_F_(r13)->vals[t_id],
CS_F_(r12)->vals[t_id],
CS_F_(r22)->vals[t_id],
CS_F_(r23)->vals[t_id],
CS_F_(r13)->vals[t_id],
CS_F_(r23)->vals[t_id],
CS_F_(r33)->vals[t_id]);
}
}
}
}
/*----------------------------------------------------------------------------
* Read field values from checkpoint.
*
* Values are found using the default rules based on the field's name
* postfixed by ::vals::%t_id, or its name itself.
*
* parameters:
* r <-- associated restart file pointer
* restart_name <-- base name with which read is attempted
* t_id <-- time id (0 for current, 1 for previous, ...)
* f <-> field whose values should be read
*
* returns:
* CS_RESTART_SUCCESS in case of success, CS_RESTART_ERR_... otherwise
*----------------------------------------------------------------------------*/
static int
_read_field_vals(cs_restart_t *r,
const char *r_name,
int t_id,
cs_field_t *f)
{
int retcode = CS_RESTART_SUCCESS;
char _sec_name[128];
char *sec_name = _sec_name;
if (strlen(r_name) > 96)
BFT_MALLOC(sec_name, strlen(r_name) + 64, char); /* wide margin */
/* Check for data; data will be read later, so that compatibility
checks may be done first; we really try reading the data only
at the end, so if it is not found, a warning will be logged only
once (and not once per test), and preferentially use the
base (non-compatibility) name. */
snprintf(sec_name, 127, "%s::vals::%d", r_name, t_id);
sec_name[127] = '\0';
retcode = cs_restart_check_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t);
/* Otherwise, try reading with basic (restart) name only if requested */
if ( (retcode == CS_RESTART_ERR_EXISTS || retcode == CS_RESTART_ERR_N_VALS)
&& r_name != f->name) {
snprintf(sec_name, 127, "%s", r_name);
sec_name[127] = '\0';
retcode = cs_restart_check_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t);
}
/* Read if available */
if (retcode == CS_RESTART_SUCCESS)
retcode = cs_restart_read_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t,
f->vals[t_id]);
/* Try to read anyways (with base name) to log warning */
else {
snprintf(sec_name, 127, "%s::vals::%d", r_name, t_id);
sec_name[127] = '\0';
retcode = cs_restart_read_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t,
f->vals[t_id]);
}
if (sec_name != _sec_name)
BFT_FREE(sec_name);
if ( retcode == CS_RESTART_SUCCESS
&& f->location_id == CS_MESH_LOCATION_CELLS)
_sync_field_vals(f, t_id);
return retcode;
}
/*----------------------------------------------------------------------------
* Read field values from legacy checkpoint.
*
* Values are found using older names for compatibility with older files.
* For cell-based fields, the old name base is appended automatically with
* "_ce_phase01", except for scalars, where the name uses a different scheme,
* based on "scalaire_ce_%04" % s_num.
*
* parameters:
* r <-- associated restart file pointer
* r_name <-- base name with which read is attempted
* t_id <-- time id (0 for current, 1 for previous, ...)
* f <-> file whose values should be read
*
* returns:
* CS_RESTART_SUCCESS in case of success, CS_RESTART_ERR_... otherwise
*----------------------------------------------------------------------------*/
static int
_read_field_vals_legacy(cs_restart_t *r,
const char *r_name,
int t_id,
cs_field_t *f)
{
char sec_name[128] = "";
char old_name_x[128] = "", old_name_y[128] = "", old_name_z[128] = "";
char old_name_xx[128] = "", old_name_yy[128] = "", old_name_zz[128] = "";
char old_name_xy[128] = "", old_name_yz[128] = "", old_name_xz[128] = "";
int retcode = CS_RESTART_SUCCESS;
/* Check for renaming */
char old_name[128] = "";
int ks = cs_field_key_id_try("scalar_id");
int scalar_id = cs_field_get_key_int(f, ks);
/* Special case for scalars */
if (scalar_id > -1) {
if (r_name != f->name) {
const char *name = r_name;
while (*name != '\0' && !isdigit(*name))
name++;
scalar_id = atoi(name) - 1;
}
if (scalar_id > -1)
snprintf(old_name, 127, "%04d", scalar_id+1);
else
snprintf(old_name, 127, "%s", r_name);
}
/* Other fields may need specific renaming
(old_name for partial section name, sec_name for direct section name) */
else if (r_name == f->name) {
snprintf(old_name, 127, "%s", f->name);
if (f == CS_F_(u)) {
if (t_id == 0)
strncpy(old_name, "vitesse", 127);
else if (t_id == 1)
strncpy(sec_name, "velocity_prev", 127);
}
else if (f == CS_F_(p))
strncpy(old_name, "pression", 127);
else if (f == CS_F_(r11))
strncpy(old_name, "R11", 127);
else if (f == CS_F_(r22))
strncpy(old_name, "R22", 127);
else if (f == CS_F_(r33))
strncpy(old_name, "R33", 127);
else if (f == CS_F_(r12))
strncpy(old_name, "R12", 127);
else if (f == CS_F_(r13))
strncpy(old_name, "R13", 127);
else if (f == CS_F_(r23))
strncpy(old_name, "R23", 127);
else if (f == CS_F_(rij))
strncpy(old_name, "Rij", 127);
else if (f == CS_F_(eps))
strncpy(old_name, "eps", 127);
else if (f == CS_F_(f_bar))
strncpy(old_name, "fb", 127);
else if (f == CS_F_(alpha)) {
/* Special case: "al" also possible here, depending on turbulence model;
check for either, with one test for main restart, the other for
the auxilairy restart */
int sec_code;
strncpy(old_name, "alp", 127);
sec_code = cs_restart_check_section(r, "al_ce_phase01",
1, 1, CS_TYPE_cs_real_t);
if (sec_code == CS_RESTART_SUCCESS)
strncpy(old_name, "al", 127);
else
sec_code = cs_restart_check_section(r, "fm_al_phase01",
0, 1, CS_TYPE_cs_int_t);
if (sec_code == CS_RESTART_SUCCESS)
strncpy(old_name, "al", 127);
}
else if (f == CS_F_(nusa))
strncpy(old_name, "nusa", 127);
else if (f == CS_F_(mesh_u))
strncpy(old_name, "vit_maillage", 127);
else if (f == CS_F_(rho)) {
if (t_id == 0)
strncpy(old_name, "rho", 127);
else if (t_id == 1)
strncpy(old_name, "rho_old", 127);
}
else if (f == CS_F_(rho_b))
strncpy(sec_name, "rho_fb_phase01", 127);
else if (f == CS_F_(cp))
strncpy(old_name, "cp", 127);
else if (f == CS_F_(mu))
strncpy(old_name, "viscl", 127);
else if (f == CS_F_(mu_t))
strncpy(old_name, "visct", 127);
else if (f == CS_F_(t_b))
strncpy(old_name, "tparoi_fb", 127);
else if (f == CS_F_(qinci))
strncpy(old_name, "qincid_fb", 127);
else if (f == CS_F_(hconv))
strncpy(old_name, "hfconv_fb", 127);
else if (f == CS_F_(fconv))
strncpy(old_name, "flconv_fb", 127);
else if (strcmp(f->name, "dt") == 0)
strncpy(sec_name, "dt_variable_espace_ce", 127);
else if (strcmp(f->name, "volume_forces") == 0)
strncpy(sec_name, "force_ext_ce_phase01", 127);
else if (strcmp(f->name, "hydrostatic_pressure_prd") == 0)
strncpy(sec_name, "Prhyd_pre_phase01", 127);
else if (strcmp(f->name, "void_fraction") == 0)
strncpy(sec_name, "taux_vide_ce", 127);
else if (strcmp(f->name, "mesh_viscosity") == 0)
strncpy(sec_name, "visc_maillage", 127);
else if (strcmp(f->name, "rad_st") == 0)
strncpy(sec_name, "rayexp_ce", 127);
else if (strcmp(f->name, "rad_st_implicit") == 0)
strncpy(sec_name, "rayimp_ce", 127);
else if (f == CS_F_(rad_lumin))
strncpy(sec_name, "luminance", 127);
else if (strcmp(f->name, "joule_power") == 0)
strncpy(sec_name, "tsource_sc_ce_joule", 127);
else if (strcmp(f->name, "laplace_force") == 0)
strncpy(old_name, "laplace_force", 127);
}
if (sec_name[0] == '\0') {
if (scalar_id > -1)
snprintf(sec_name, 127, "scalaire_ce_%04d", scalar_id);
else if (f->location_id == CS_MESH_LOCATION_CELLS)
snprintf(sec_name, 127, "%s_ce_phase01", old_name);
else
snprintf(sec_name, 127, "%s", old_name);
}
retcode = cs_restart_check_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t);
if (retcode == CS_RESTART_SUCCESS)
retcode = cs_restart_read_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t,
f->vals[t_id]);
/* Last chance for 3D fields */
else if (f->dim == 3 && retcode == CS_RESTART_ERR_EXISTS) {
if (strcmp(old_name, "vit_maillage") == 0) {
snprintf(old_name_x, 127, "%s_u_ce", old_name);
snprintf(old_name_y, 127, "%s_v_ce", old_name);
snprintf(old_name_z, 127, "%s_w_ce", old_name);
}
else if (strcmp(old_name, "laplace_force") == 0) {
snprintf(old_name_x, 127, "%s_1", old_name);
snprintf(old_name_y, 127, "%s_2", old_name);
snprintf(old_name_z, 127, "%s_2", old_name);
}
else {
snprintf(old_name_x, 127, "%s_u_ce_phase01", old_name);
snprintf(old_name_y, 127, "%s_v_ce_phase01", old_name);
snprintf(old_name_z, 127, "%s_w_ce_phase01", old_name);
}
retcode = cs_restart_check_section(r,
old_name_x,
f->location_id,
1,
CS_TYPE_cs_real_t);
if (retcode == CS_RESTART_SUCCESS)
retcode = cs_restart_read_real_3_t_compat(r,
sec_name,
old_name_x,
old_name_y,
old_name_z,
f->location_id,
(cs_real_3_t *)(f->vals[t_id]));
}
else if (f->dim == 6 && retcode == CS_RESTART_ERR_EXISTS) {
if (strcmp(old_name, "Rij") == 0) {
snprintf(old_name_xx, 127, "r11::vals::0");
snprintf(old_name_yy, 127, "r22::vals::0");
snprintf(old_name_zz, 127, "r33::vals::0");
snprintf(old_name_xy, 127, "r12::vals::0");
snprintf(old_name_yz, 127, "r23::vals::0");
snprintf(old_name_xz, 127, "r13::vals::0");
retcode = cs_restart_check_section(r,
old_name_xx,
f->location_id,
1,
CS_TYPE_cs_real_t);
if (retcode == CS_RESTART_SUCCESS)
retcode = cs_restart_read_real_6_t_compat(r,
"rij::vals::0",
old_name_xx,
old_name_yy,
old_name_zz,
old_name_xy,
old_name_yz,
old_name_xz,
f->location_id,
(cs_real_6_t *)(f->vals[t_id]));
}
}
if ( retcode == CS_RESTART_SUCCESS
&& f->location_id == CS_MESH_LOCATION_CELLS)
_sync_field_vals(f, t_id);
return retcode;
}
/*----------------------------------------------------------------------------
* Determine mass flux number of a given variable for legacy restart file
*
* parameters:
* r <-> associated restart file pointer
* f <-- associated field pointer
* scalar_num <-- associated scalar number, or -1
* t_id <-- associated time id (0: current, 1: previous)
*
* returns:
* number of matching mass flux in restart file (-1 if read failed)
*----------------------------------------------------------------------------*/
static int
_legacy_mass_flux_num(cs_restart_t *r,
const cs_field_t *f,
int scalar_num,
int t_id)
{
int retval = 1;
/* As of Code_Saturne 3.3, only scalars may have a different mass flux
from the "main" mass flux (in the case of scalars with drift), so for
all others, reading the associated mass flux name is of no real use. */
char sec_name[128] = "";
const char *prefix[2] = {"fm_", "fm_a_"};
if (scalar_num > 0)
snprintf(sec_name, 127, "%sscalaire%04d", prefix[t_id], scalar_num);
else if (strcmp(f->name, "void_fraction") == 0)
snprintf(sec_name, 127, "%staux_vide", prefix[t_id]);
/* Read from restart */
if (sec_name[0] != '\0') {
cs_lnum_t buf[1];
sec_name[127] = '\0';
int retcode = cs_restart_read_section(r,
sec_name,
CS_MESH_LOCATION_NONE,
1,
CS_TYPE_cs_int_t,
buf);
if (retcode == CS_RESTART_SUCCESS)
retval = buf[0];
else
retval = -1;
}
return retval;
}
/*----------------------------------------------------------------------------
* Read fields depending on others from checkpoint.
*
* This function handles legacy files (Code_Saturne version 3.3 and below).
*
* parameters:
* r <-> associated restart file pointer
* key <-> key for field association
* read_flag <-> flag to track fields read, or NULL;
* set to 1 for fields read, -1 for fields
* failed to read (size: n_fields)
*
* returns:
* number of fields read
*----------------------------------------------------------------------------*/
static int
_read_linked_fields_legacy(cs_restart_t *r,
const char *key,
int read_flag[])
{
int retcode;
int retcount = 0;
/* Initialization */
int category = 0;
const int n_fields = cs_field_n_fields();
const int key_id = cs_field_key_id(key);
const int key_flag = cs_field_key_flag(key_id);
const int kold = cs_field_key_id_try("old_scalar_num");
const int ks = cs_field_key_id_try("scalar_id");
/* Determine field type (out of possibilities in legacy files) */
if (strcmp(key, "inner_mass_flux_id") == 0)
category = 1;
else if (strcmp(key, "boundary_mass_flux_id") == 0)
category = 2;
else if (strcmp(key, "scalar_diffusivity_id") == 0)
category = 3;
for (int f_id = 0; f_id < n_fields; f_id++) {
const cs_field_t *f = cs_field_by_id(f_id);
if (key_flag == -1 || !(f->type & key_flag))
continue;
const int lnk_f_id = cs_field_get_key_int(f, key_id);
int s_num = -1;
if (lnk_f_id > -1) {
cs_field_t *f_lnk = cs_field_by_id(lnk_f_id);
if (read_flag[lnk_f_id] != 0)
continue;
read_flag[lnk_f_id] = -1;
/* check for (possibly renumbered) scalar */
if (f->type & CS_FIELD_VARIABLE) {
if (kold > -1)
s_num = cs_field_get_key_int(f, kold);
if (s_num < 0 && ks > -1)
s_num = cs_field_get_key_int(f, ks);
}
for (int t_id = 0; t_id < 2; t_id++) {
if (t_id >= f_lnk->n_time_vals)
break;
/* Build field name to read based on category */
char sec_name[128];
if (category == 1) {
int mf_num = _legacy_mass_flux_num(r, f, s_num, t_id);
if (t_id == 0)
snprintf(sec_name, 127, "flux_masse_fi_%04d", mf_num);
else
snprintf(sec_name, 127, "flux_masse_a_fi_%04d", mf_num);
}
else if (category == 2) {
int mf_num = _legacy_mass_flux_num(r, f, s_num, t_id);
if (t_id == 0)
snprintf(sec_name, 127, "flux_masse_fb_%04d", mf_num);
else
snprintf(sec_name, 127, "flux_masse_a_fb_%04d", mf_num);
}
else if (category == 3)
snprintf(sec_name, 127, "visls_ce_scalaire%04d", s_num);
/* Now we know which field name to read */
retcode = cs_restart_check_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t);
if (retcode == CS_RESTART_SUCCESS)
retcode = cs_restart_read_section(r,
sec_name,
f->location_id,
f->dim,
CS_TYPE_cs_real_t,
f->vals[t_id]);
if (retcode == CS_RESTART_SUCCESS) {
if (t_id == 0)
read_flag[lnk_f_id] = 1;
else
read_flag[lnk_f_id] += 2;
retcount += 1;
}
} /* t_id */
}
}
return retcount;
}
/*----------------------------------------------------------------------------
* Compare old and new values for a given field key
*
* parameters:
* r <-- associated restart file pointer
* key <-- associated model key
* old_field_map <-- name to id map of fields in restart file
*
* returns:
* number of values changed, or -1 if information was not found
*----------------------------------------------------------------------------*/
static int
_check_field_model(cs_restart_t *r,
const char *key,
const cs_map_name_to_id_t *old_field_map)
{
int retcode;
const int n_fields = cs_field_n_fields();
const int n_o_fields = cs_map_name_to_id_size(old_field_map);
const int key_id = cs_field_key_id(key);
const int key_flag = cs_field_key_flag(key_id);
const int kr = cs_field_key_id_try("restart_name");
int n_diff = 0;
cs_lnum_t *old_key_val;
BFT_MALLOC(old_key_val, n_o_fields, cs_lnum_t);
char *sec_name;
BFT_MALLOC(sec_name, strlen("fields:") + strlen(key) + 1, char);
strcpy(sec_name, "fields:");
strcat(sec_name, key);
/* Read metadata */
retcode = cs_restart_check_section(r,
sec_name,
CS_MESH_LOCATION_NONE,
n_o_fields,
CS_TYPE_cs_int_t);
if (retcode == CS_RESTART_SUCCESS)
retcode = cs_restart_read_section(r,
sec_name,
CS_MESH_LOCATION_NONE,
n_o_fields,
CS_TYPE_cs_int_t,
old_key_val);
/* If data is available, compare models */
if (retcode == CS_RESTART_SUCCESS) {
for (int f_id = 0; f_id < n_fields; f_id++) {
const cs_field_t *f = cs_field_by_id(f_id);
if (key_flag == -1 || !(f->type & key_flag))
continue;
const char *f_name = NULL;
if (kr > -1)
f_name = cs_field_get_key_str(f, kr);
if (f_name == NULL)
f_name = f->name;
int old_f_id = cs_map_name_to_id_try(old_field_map, f_name);
if (old_f_id > -1) {
if (cs_field_get_key_int(f, key_id) != old_key_val[old_f_id])
n_diff += 1;
}
}
}
else if (retcode == CS_RESTART_ERR_EXISTS)
n_diff = -1;
else
bft_error
(__FILE__, __LINE__, 0,
_("Error %d reading \"%s\" in restart file \"%s\"."),
retcode, sec_name, cs_restart_get_name(r));
BFT_FREE(sec_name);
BFT_FREE(old_key_val);
return n_diff;
}
/*----------------------------------------------------------------------------
* Check old turbulent flux model
*
* parameters:
* r <-- associated restart file pointer
* old_field_map <-- name to id map of fields in restart file
*----------------------------------------------------------------------------*/
static void
_check_turb_flux_model(cs_restart_t *r,
const cs_map_name_to_id_t *old_field_map)
{
const int n_fields = cs_field_n_fields();
int n_diff = _check_field_model(r,
"turbulent_flux_model",
old_field_map);
/* Read in legacy mode if required */
if (n_diff < 0) {
int kold = cs_field_key_id_try("old_scalar_num");
const int key_id = cs_field_key_id("turbulent_flux_model");
if (kold > -1) {
n_diff = 0;
for (int f_id = 0; f_id < n_fields; f_id++) {
const cs_field_t *f = cs_field_by_id(f_id);
if (!(f->type & CS_FIELD_VARIABLE))
continue;
int s_num = cs_field_get_key_int(f, kold);
if (s_num > 0) {
char sec_name[128];
cs_lnum_t old_s_model[1];
snprintf(sec_name, 127, "turbulent_flux_model%04d", s_num);
sec_name[127] = '\0';
int retcode = cs_restart_read_section(r,
sec_name,
CS_MESH_LOCATION_NONE,
1,
CS_TYPE_cs_int_t,
old_s_model);
if (retcode == CS_RESTART_SUCCESS) {
if (cs_field_get_key_int(f, key_id) != old_s_model[0])
n_diff += 1;
}
}
}
}
}
if (n_diff > 0)
bft_printf
(_("\n"
" Warning: the turbulent flux model has been changed\n"
" for %d fields relative to the restart file\n\n"
" The computation continues, with a partial restart.\n"),
n_diff);
}
/*----------------------------------------------------------------------------
* Read model option from file with compatibility for older files.
*
* parameters:
* r <-- associated restart file pointer