forked from code-saturne/code_saturne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcs_case_domain.py
More file actions
2108 lines (1561 loc) · 69.3 KB
/
Copy pathcs_case_domain.py
File metadata and controls
2108 lines (1561 loc) · 69.3 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of code_saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2022 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.
#-------------------------------------------------------------------------------
import configparser
import datetime
import fnmatch
import os
import os.path
import sys
import shutil
import stat
from code_saturne.base import cs_compile
from code_saturne.base import cs_xml_reader
from code_saturne.base.cs_exec_environment import run_command
from code_saturne.base.cs_exec_environment import enquote_arg, separate_args
from code_saturne.base.cs_exec_environment import get_ld_library_path_additions
from code_saturne.base.cs_exec_environment import source_syrthes_env
from code_saturne.base.cs_meg_to_c import meg_to_c_interpreter
#===============================================================================
# Utility functions
#===============================================================================
def any_to_str(arg):
"""Transform single values or lists to a whitespace-separated string"""
s = ''
if type(arg) == tuple or type(arg) == list:
for e in arg:
s += ' ' + str(e)
return s[1:]
else:
return str(arg)
#-------------------------------------------------------------------------------
def make_clean_dir(path):
"""
Create a directory, or remove files (not directories) in existing directory
"""
if not os.path.isdir(path):
os.mkdir(path)
else:
l = os.listdir(path)
for f in l:
os.remove(os.path.join(path, f))
#-------------------------------------------------------------------------------
class RunCaseError(Exception):
"""Base class for exception handling."""
def __init__(self, *args):
self.args = args
def __str__(self):
if len(self.args) == 1:
return str(self.args[0])
else:
return str(self.args)
# def __repr__(self):
# return "%s(*%s)" % (self.__class__.__name__, repr(self.args))
#===============================================================================
# Classes
#===============================================================================
class base_domain:
"""
Base class from which classes handling running case should inherit.
"""
#---------------------------------------------------------------------------
def __init__(self,
package, # main package
code_name = '<undefined>', # code name
name = None, # domain name
n_procs_weight = None, # recommended number of processes
n_procs_min = 1, # min. number of processes
n_procs_max = None): # max. number of processes
# Package specific information
self.package = package
self.code_name = code_name
# User functions
self.user = {}
# Names, directories, and files in case structure
self.case_dir = None
self.case_root_dir = None
self.name = name # used for multiple domains only
self.data_dir = None
self.result_dir = None
self.src_dir = None
# Notebook and parametric definitions and additional user arguments
self.notebook = None
self.parametric_args = None
self.kw_args = None
# Working directory and executable
self.exec_dir = None
self.solver_path = None
# Is the case already staged ?
self.data_is_staged = False
# Execution options
self.n_procs = n_procs_weight
if not n_procs_min:
n_procs_min = 1
self.n_procs_min = max(1, n_procs_min)
self.n_procs_max = n_procs_max
if self.n_procs is None:
self.n_procs = 1
self.n_procs = max(self.n_procs, self.n_procs_min)
if self.n_procs_max != None:
self.n_procs = min(self.n_procs, self.n_procs_max)
# Error reporting
self.error = ''
self.error_long = ''
#---------------------------------------------------------------------------
def __input_path_abs_dir__(self, path):
"""
Determine root directory based on given path.
Paths including RESU should be based on dest_root_dir,
those including MESH based on case_root_dir,
others in case_dir.
"""
prefix, base = os.path.split(path)
while prefix != '':
if base in ('RESU', 'RESU_COUPLING', 'MESH'):
break
prefix, base = os.path.split(prefix)
if base in ('RESU', 'RESU_COUPLING'):
if self.dest_root_dir:
r_path = os.path.join(self.dest_root_dir, path)
if os.path.exists(r_path):
return r_path
c_path = os.path.join(self.case_root_dir, path)
if os.path.exists(c_path):
return c_path
# If path does not exist, assume it will be created
# later, in chich case we use dest_root_dir
return r_path
else:
return os.path.join(self.case_root_dir, path)
elif base == 'MESH':
return os.path.join(self.case_root_dir, path)
return os.path.join(self.case_dir, path)
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir, staging_dir = None):
# Names, directories, and files in case structure
self.case_dir = case_dir
self.case_root_dir = case_dir
self.dest_root_dir = None
if self.name != None:
self.case_dir = os.path.join(self.case_dir, self.name)
self.case_root_dir = os.path.join(case_dir, self.name)
self.data_dir = os.path.join(self.case_dir, 'DATA')
self.result_dir = os.path.join(self.case_dir, 'RESU')
self.src_dir = os.path.join(self.case_dir, 'SRC')
# If computation is already staged, avoid reading data in upstream
# case directories, as it may have changed since staging.
if staging_dir:
self.case_dir = staging_dir
if self.name != None:
self.case_dir = os.path.join(self.case_dir, self.name)
self.data_dir = self.case_dir
self.src_dir = None
#---------------------------------------------------------------------------
def set_exec_dir(self, exec_dir):
if os.path.isabs(exec_dir):
self.exec_dir = exec_dir
else:
self.exec_dir = os.path.join(self.case_dir, 'RESU', exec_dir)
if self.name != None:
self.exec_dir = os.path.join(self.exec_dir, self.name)
if not os.path.isdir(self.exec_dir):
os.makedirs(self.exec_dir)
#---------------------------------------------------------------------------
def set_result_dir(self, name, given_dir = None, dest_root_dir=None):
"""
If suffix = true, add suffix to all names in result dir.
Otherwise, create subdirectory
"""
if given_dir is None:
self.result_dir = os.path.join(self.case_dir, 'RESU', name)
else:
self.result_dir = given_dir
if self.name != None:
self.result_dir = os.path.join(self.result_dir, self.name)
self.dest_root_dir = dest_root_dir
if not os.path.isdir(self.result_dir):
os.makedirs(self.result_dir)
#---------------------------------------------------------------------------
def copy_data(self):
"""
Copy base data to the execution directory.
"""
return
#---------------------------------------------------------------------------
def init_staged_data(self):
"""
Initialize staged data in the execution directory.
"""
self.data_is_staged = True
return
#---------------------------------------------------------------------------
def prepare_data(self):
"""
Prepare data in the execution directory prior to run
"""
#---------------------------------------------------------------------------
def copy_result(self, name, purge=False):
"""
Copy a file or directory to the results directory,
optionally removing it from the source.
"""
# Determine absolute source and destination names
if os.path.isabs(name):
src = name
dest = os.path.join(self.result_dir, os.path.basename(name))
else:
src = os.path.join(self.exec_dir, name)
dest = os.path.join(self.result_dir, name)
# If source and destination are identical, return
if src == dest:
return
# Copy single file
if os.path.isfile(src):
shutil.copy2(src, dest)
if purge:
os.remove(src)
# Copy single directory (possibly recursive)
# Unlike os.path.copytree, the destination directory
# may already exist.
elif os.path.isdir(src):
if not os.path.isdir(dest):
os.mkdir(dest)
l = os.listdir(src)
for f in l:
f_src = os.path.join(src, f)
f_dest = os.path.join(dest, f)
if os.path.isfile(f_src):
shutil.copy2(f_src, f_dest)
elif os.path.isdir(f_src):
self.copy_result(f_src, f_dest)
if purge:
if os.path.islink(src):
os.remove(f)
else:
shutil.rmtree(src)
#---------------------------------------------------------------------------
def purge_result(self, name):
"""
Remove a file or directory from execution directory.
"""
# Determine absolute name
if os.path.isabs(name):
f = name
else:
f = os.path.join(self.exec_dir, name)
# Remove file or directory
if os.path.isfile(f) or os.path.islink(f):
os.remove(f)
elif os.path.isdir(f):
shutil.rmtree(f)
#---------------------------------------------------------------------------
def get_n_procs(self):
"""
Returns an array (list) containing the current number of processes
associated with a solver stage followed by the minimum and maximum
number of processes.
"""
return [self.n_procs, self.n_procs_min, self.n_procs_max]
#---------------------------------------------------------------------------
def set_n_procs(self, n_procs):
"""
Assign a number of processes to a solver stage.
"""
self.n_procs = n_procs
#---------------------------------------------------------------------------
def solver_command(self, need_abs_path=False):
"""
Returns a tuple indicating the solver's working directory,
executable path, and associated command-line arguments.
"""
exec_path = self.solver_path
if not os.path.isabs(exec_path) and need_abs_path:
exec_path = os.path.join(self.exec_dir,
os.path.basename(exec_path))
return enquote_arg(self.exec_dir), enquote_arg(exec_path), ''
#---------------------------------------------------------------------------
def summary_info(self, s):
"""
Output summary data into file s
"""
if self.name:
name = self.name
exec_dir = os.path.join(self.exec_dir, name)
result_dir = os.path.join(self.result_dir, name)
else:
name = os.path.basename(self.case_dir)
exec_dir = self.exec_dir
result_dir = self.result_dir
s.write(' Case : ' + name + '\n')
s.write(' directory : ' + self.case_dir + '\n')
s.write(' results dir. : ' + self.result_dir + '\n')
if exec_dir != result_dir:
s.write(' exec. dir. : ' + self.exec_dir + '\n')
#-------------------------------------------------------------------------------
class domain(base_domain):
"""Handle running case."""
#---------------------------------------------------------------------------
def __init__(self,
package, # main package
package_compute = None, # package for compute environment
name = None, # domain name
n_procs_weight = None, # recommended number of processes
n_procs_min = None, # min. number of processes
n_procs_max = None, # max. number of processes
logging_args = None, # command-line options for logging
param = None, # XML parameters file
prefix = None, # installation prefix
adaptation = None): # HOMARD adaptation script
base_domain.__init__(self,
package,
'code_saturne',
name,
n_procs_weight,
n_procs_min,
n_procs_max)
# Compute package if different from front-end
if package_compute:
self.package_compute = package_compute
else:
self.package_compute = self.package
# Directories, and files in case structure
self.restart_input = None
self.restart_mesh_input = None
self.mesh_input = None
self.partition_input = None
# Default executable
self.solver_path = os.path.join(self.package_compute.get_dir("pkglibexecdir"),
self.package.solver)
# Preprocessor options
self.mesh_dir = None
self.meshes = None
# Solver options
self.preprocess_on_restart = False
self.exec_solver = True
if param:
self.param = os.path.basename(param)
else:
self.param = None
self.logging_args = logging_args
self.solver_args = None
# Additional data
self.prefix = prefix
self.compile_cflags = None
self.compile_cxxflags = None
self.compile_fcflags = None
self.compile_nvccflags = None
self.compile_libs = None
# Adaptation using HOMARD
self.adaptation = adaptation
# MEG expression generator
self.mci = None
#---------------------------------------------------------------------------
def __set_case_parameters__(self):
update_xml = (self.data_is_staged == False)
# We may now import user python script functions if present.
self.user_locals = None
user_scripts = os.path.join(self.exec_dir, 'cs_user_scripts.py')
if os.path.isfile(user_scripts):
try:
exec(compile(open(user_scripts).read(), user_scripts, 'exec'),
locals(),
locals())
self.user_locals = locals()
except Exception:
execfile(user_scripts, locals(), locals())
self.user_locals = locals()
# We may now parse the optional XML parameter file
# now that its path may be built and checked.
setup_path = os.path.join(self.exec_dir, "setup.xml")
if os.path.isfile(setup_path):
# Ensure XML file is up to date as a precaution,
# and filter it in case of parametric or notebook arguments.
case = self.__xml_case_initialize__(setup_path, apply_filters=True)
case['xmlfile'] = setup_path
P = cs_xml_reader.Parser(doc=case.doc)
params = P.getParams()
for k in list(params.keys()):
self.__dict__[k] = params[k]
case.xmlSaveDocument()
if params['xml_root_name'] == 'NEPTUNE_CFD_GUI':
solver_dir = self.package_compute.get_dir("pkglibexecdir")
solver_name = "nc_solver" + self.package_compute.config.exeext
self.solver_path = os.path.join(solver_dir, solver_name)
self.code_name = 'neptune_cfd'
self.param = "setup.xml"
else:
msg = ('Remark:\n'
' No setup.xml file was provided in the DATA folder.\n'
' Default settings will be used.\n')
print(msg, file = sys.stderr)
# Now override or complete data from the XML file.
if self.user_locals:
m = 'define_domain_parameters'
if m in self.user_locals.keys():
eval(m + '(self)', globals(), self.user_locals)
del self.user_locals[m]
# Finally, ensure some fields are of the required types
if type(self.meshes) != list:
self.meshes = [self.meshes,]
#---------------------------------------------------------------------------
def __set_auto_restart__(self):
"""
Select latest valid checkpoint directory for restart, based on name
"""
self.restart_input = None
from code_saturne.base.cs_exec_environment import get_command_output
results_dir = os.path.abspath(os.path.join(self.result_dir, '..'))
results = os.listdir(results_dir)
results.sort(reverse=True)
for r in results:
m = os.path.join(results_dir, r, 'checkpoint', 'main.csc')
if not os.path.isfile(m):
m = os.path.join(results_dir, r, 'checkpoint', 'main')
if os.path.isfile(m):
try:
cmd = self.package.get_io_dump()
cmd += ' --location 0 ' + m
res = get_command_output(cmd)
except Exception:
print('checkpoint of result: ' + r + ' does not seem usable')
continue
self.restart_input = os.path.join(results_dir, r, 'checkpoint')
break
return
#---------------------------------------------------------------------------
def __xml_case_initialize__(self, path, apply_filters=False):
"""
Build XML case object
"""
from code_saturne.model.XMLengine import Case
from code_saturne.model.SolutionDomainModel import getRunType
case = Case(package=self.package, file_name=path)
case['xmlfile'] = path
case.xmlCleanAllBlank(case.xmlRootNode())
preprocess_only = (getRunType(case) != 'standard')
module_name = case.module_name()
if module_name == 'code_saturne':
from code_saturne.model.XMLinitialize import XMLinit
XMLinit(case).initialize(preprocess_only)
elif module_name == 'neptune_cfd':
from code_saturne.model.XMLinitializeNeptune import XMLinitNeptune
XMLinitNeptune(case).initialize(preprocess_only)
if not apply_filters:
return case
# Apply changes defined through notebook or parametric options.
if self.parametric_args:
from code_saturne.base import cs_parametric_setup
cs_parametric_setup.update_case_setup(case, self.parametric_args,
pkg=self.package)
if self.notebook:
from code_saturne.model.NotebookModel import NotebookModel
notebookModel = NotebookModel(case)
nbk_vars = notebookModel.getVarNameList()
n_warnings = 0
for k in self.notebook.keys():
if k in nbk_vars:
vs = self.notebook[k]
v = None
try:
v = float(vs)
except Exception:
fmt = ("Warning: notebook variable '{0}'='{1}'"
" is not a real number.")
msg = fmt.format(k, vs)
print(msg, file = sys.stderr)
n_warnings += 1
if v != None:
notebookModel.setVariableValue(val=v, var=k)
else:
fmt = ('Warning: {0} is not a known notebook variable.')
msg = fmt.format(k)
print(msg, file = sys.stderr)
n_warnings += 1
if n_warnings > 0:
print(file = sys.stderr)
return case
#---------------------------------------------------------------------------
def for_domain_str(self):
if self.name is None:
return ''
else:
return 'for domain ' + str(self.name)
#---------------------------------------------------------------------------
def read_parameter_file(self, param):
"""
Parse the optional XML parameter file.
"""
if param is None:
if os.path.isfile(os.path.join(self.data_dir, 'setup.xml')):
param = 'setup.xml'
if param != None:
version_str = '2.0'
P = cs_xml_reader.Parser(os.path.join(self.data_dir, param),
version_str = version_str)
params = P.getParams()
for k in list(params.keys()):
self.__dict__[k] = params[k]
self.param = param
#---------------------------------------------------------------------------
def copy_data(self):
"""
Copy base data to the execution directory
"""
err_str = ""
# Create the src folder if there are files to compile in source path
src_files = []
if self.src_dir != None:
if os.path.exists(self.src_dir):
src_files = cs_compile.files_to_compile(self.src_dir)
if len(src_files) > 0:
exec_src = os.path.join(self.exec_dir, 'src')
make_clean_dir(exec_src)
# Add header files to list so as not to forget to copy them
dir_files = os.listdir(self.src_dir)
src_files = src_files + ( fnmatch.filter(dir_files, '*.h')
+ fnmatch.filter(dir_files, '*.hxx')
+ fnmatch.filter(dir_files, '*.hpp'))
# Copy source files to execution directory
for f in src_files:
src_file = os.path.join(self.src_dir, f)
dest_file = os.path.join(exec_src, f)
shutil.copy2(src_file, dest_file)
# Copy data files
dir_files = []
if self.data_dir != self.exec_dir:
dir_files = os.listdir(self.data_dir)
if self.package.name in dir_files:
dir_files.remove(self.package.name)
for f in dir_files:
src = os.path.join(self.data_dir, f)
if os.path.isfile(src):
shutil.copy2(src, os.path.join(self.exec_dir, f))
# Fixed parameter name
setup_path = os.path.join(self.exec_dir, "setup.xml")
if self.param != None:
param_base = os.path.basename(self.param)
if param_base != "setup.xml":
src_path = os.path.join(self.exec_dir, param_base)
if os.path.isfile(setup_path):
self.purge_result(setup_path) # in case of previous run here
fmt = ('Warning:\n'
' Both {0} and {1} exist in\n'
' {2}.\n'
' {0} will be used for the computation.\n'
' Be aware that to follow best practices '
'only one of the two should be present.\n\n')
msg = fmt.format(os.path.basename(self.param),
os.path.basename(setup_path),
self.data_dir)
print(msg, file = sys.stderr)
try:
if os.path.islink(setup_path):
os.remove(setup_path)
os.symlink(self.param, setup_path)
except Exception:
shutil.copy2(src_path, setup_path)
# Now set parameters
self.__set_case_parameters__()
#---------------------------------------------------------------------------
def init_staged_data(self):
"""
Initialize staged data in the execution directory.
"""
self.data_is_staged = True
# Now set parameters
self.__set_case_parameters__()
# Ensure correct executable is used.
exec_src = os.path.join(self.exec_dir, 'src')
if os.path.isdir(exec_src):
if len(cs_compile.files_to_compile(exec_src)) > 0:
solver_name = os.path.basename(self.solver_path)
self.solver_path = os.path.join('.', solver_name)
# Additional precaution (in case source files were removed)
solver = os.path.basename(self.solver_path)
if os.path.isfile(os.path.join(self.exec_dir, solver)):
self.solver_path = os.path.join('.', solver)
#---------------------------------------------------------------------------
def symlink(self, target, link=None, check_type=None):
"""
Create a symbolic link to a file, or copy it if links are
not possible
"""
if target is None and link is None:
return
elif target is None:
err_str = 'No target for link: ' + link
raise RunCaseError(err_str)
elif link is None:
if self.exec_dir != None:
link = os.path.join(self.exec_dir,
os.path.basename(target))
else:
err_str = 'No path name given for link to: ' + target
raise RunCaseError(err_str)
if not os.path.exists(target):
if check_type != 'allow_future':
err_str = 'File: ' + target + ' does not exist.'
raise RunCaseError(err_str)
elif check_type == 'file':
if not os.path.isfile(target):
err_str = target + ' is not a regular file.'
raise RunCaseError(err_str)
elif check_type == 'dir':
if not os.path.isdir(target):
err_str = target + ' is not a directory.'
raise RunCaseError(err_str)
try:
os.symlink(target, link)
except AttributeError:
if not os.path.isdir(target):
shutil.copy2(target, link)
else:
if os.path.isdir(link):
shutil.rmtree(link)
shutil.copytree(target, link)
#---------------------------------------------------------------------------
def needs_compile(self):
"""
Compile and link user subroutines if necessary
"""
# Check if there are files to compile in source path
needs_comp = False
exec_src = os.path.join(self.exec_dir, 'src')
if os.path.isdir(exec_src):
src_files = cs_compile.files_to_compile(exec_src)
if self.exec_solver and len(src_files) > 0:
needs_comp = True
setup_path = os.path.join(self.exec_dir, "setup.xml")
if os.path.isfile(setup_path):
fp = os.path.join(self.exec_dir, self.param)
case = self.__xml_case_initialize__(fp)
# Do not call case.xmlSaveDocument() to avoid side effects in case
# directory; is not required as meg_to_c_interpreter works from
# case in memory
module_name = case.module_name()
self.mci = meg_to_c_interpreter(case,
module_name=module_name,
wdir = os.path.join(self.exec_dir, 'src'))
if self.mci.has_meg_code():
needs_comp = True
else:
self.mci = None
return needs_comp
#---------------------------------------------------------------------------
def compile_and_link(self):
"""
Compile and link user subroutines if necessary
"""
src_files = []
# Check if there are files to compile in source path
# or if MEG functions need to be generated
exec_src = os.path.join(self.exec_dir, 'src')
if os.path.isdir(exec_src):
src_files = cs_compile.files_to_compile(exec_src)
if len(src_files) > 0 or self.mci != None:
# Create the src folder if not done yet
if not os.path.isdir(exec_src):
os.mkdir(exec_src)
if len(src_files) > 0:
# Add header files to list so as not to forget to copy them
dir_files = os.listdir(exec_src)
src_files = src_files + ( fnmatch.filter(dir_files, '*.h')
+ fnmatch.filter(dir_files, '*.hxx')
+ fnmatch.filter(dir_files, '*.hpp'))
if self.mci != None:
mci_state = self.mci.save_all_functions()
if mci_state['state'] == -1:
self.error = 'compile or link'
self.error_long = ' missing mathematical expressions:\n\n'
for i, eme in enumerate(mci_state['exps']):
self.error_long += " (%d/%d) %s is not provided for %s for zone %s\n" % (i+1, mci_state['nexps'], eme['func'], eme['var'], eme['zone'])
return
elif mci_state['state'] == 2:
self.error = 'saving MEG generated sources'
self.error_long = ' Incorrect directory ?'
log_name = os.path.join(self.exec_dir, 'compile.log')
log = open(log_name, 'w')
solver_name = os.path.basename(self.solver_path)
retval = cs_compile.compile_and_link(self.package_compute,
solver_name,
exec_src,
self.exec_dir,
self.compile_cflags,
self.compile_cxxflags,
self.compile_fcflags,
self.compile_nvccflags,
self.compile_libs,
keep_going=True,
stdout=log,
stderr=log)
log.close()
if retval == 0:
solver_dir = '.'
self.solver_path = os.path.join(solver_dir, solver_name)
else:
# In case of error, copy source to results directory now,
# as no calculation is possible, then raise exception
for f in ['src', 'compile.log']:
self.copy_result(f)
self.error = 'compile or link'
#---------------------------------------------------------------------------
def prepare_data(self):
"""
Prepare data in the execution directory prior to run
"""
err_str = ""
if not self.exec_solver:
return
# Prepare some output directories
# (Note: LUSTRE striping could be set here)
try:
os.mkdir(os.path.join(self.exec_dir, 'checkpoint'))
except Exception:
pass
# Call user script if necessary
if self.user_locals:
m = 'domain_prepare_data_add'
if m in self.user_locals.keys():
eval(m + '(self)', globals(), self.user_locals)
del self.user_locals[m]
# Files or directories which migh be required but not available
# yet (when the matching case may be staged but not run yet).
upstream_pending = None
# Restart files
# Handle automatic case first
ignore_checkpoint = False
if self.solver_args:
for a in ('--preprocess', '--quality', '-q'):
if a in self.solver_args:
ignore_checkpoint = True
if not ignore_checkpoint:
if self.restart_input == '*':
self.__set_auto_restart__()
if self.restart_input != None:
restart_input = os.path.expanduser(self.restart_input)