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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2016 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.
#-------------------------------------------------------------------------------
try:
import ConfigParser # Python2
configparser = ConfigParser
except Exception:
import configparser # Python3
import datetime
import fnmatch
import os
import os.path
import sys
import shutil
import stat
import cs_compile
import cs_xml_reader
from cs_exec_environment import run_command, source_shell_script
from cs_exec_environment import enquote_arg, separate_args
from cs_exec_environment import source_syrthes_env
#===============================================================================
# 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:
list = os.listdir(path)
for f in list:
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
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
# User functions
self.user = {}
# Names, directories, and files in case structure
self.case_dir = None
self.name = name # used for multiple domains only
self.data_dir = None
self.result_dir = None
self.src_dir = None
self.mesh_dir = None
# Working directory and executable
self.exec_dir = None
self.solver_path = None
# Execution and debugging 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 == 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)
self.debug = None
# Error reporting
self.error = ''
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir):
# Names, directories, and files in case structure
self.case_dir = case_dir
if self.name != None:
self.case_dir = os.path.join(self.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')
#---------------------------------------------------------------------------
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):
"""
If suffix = true, add suffix to all names in result dir.
Otherwise, create subdirectory
"""
if given_dir == 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)
if not os.path.isdir(self.result_dir):
os.makedirs(self.result_dir)
#---------------------------------------------------------------------------
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)
# Unkike os.path.copytree, the destination directory
# may already exist.
elif os.path.isdir(src):
if not os.path.isdir(dest):
os.mkdir(dest)
list = os.listdir(src)
for f in list:
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, **kw):
"""
Returns a tuple indicating the solver's working directory,
executable path, and associated command-line arguments.
"""
return enquote_arg(self.exec_dir), enquote_arg(self.solver_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')
#---------------------------------------------------------------------------
def debug_wrapper_args(self, debug_cmd):
"""
Additional arguments to use debug wrapper
"""
debug_args = ''
python_exec = self.package.config.python
if os.path.isfile(python_exec) or os.path.islink(python_exec):
debug_args += python_exec + ' '
else:
debug_args += 'python '
if self.package.name != 'neptune_cfd':
pkg_dir = self.package.get_dir('pkgpythondir')
else:
pkg_dir = self.package.get_cs_dir('pkgpythondir')
dbg_wrapper_path = os.path.join(pkg_dir, 'cs_debug_wrapper.py')
debug_args += dbg_wrapper_path + ' '
for a in separate_args(debug_cmd):
debug_args += enquote_arg(a) + ' '
return debug_args
#-------------------------------------------------------------------------------
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,
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.mesh_input = None
self.partition_input = None
# Default executable
self.solver_path = self.package_compute.get_solver()
# Preprocessor options
self.mesh_dir = None
self.meshes = None
# Solver options
self.exec_solver = True
self.param = param
self.logging_args = logging_args
self.solver_args = None
self.debug = None
# Additional data
self.prefix = prefix
self.compile_cflags = None
self.compile_cxxflags = None
self.compile_fcflags = None
self.compile_libs = None
# Adaptation using HOMARD
self.adaptation = adaptation
#---------------------------------------------------------------------------
def for_domain_str(self):
if self.name == None:
return ''
else:
return 'for domain ' + str(self.name)
#---------------------------------------------------------------------------
def read_parameter_file(self, param):
"""
Parse the optional XML parameter file.
"""
if param != None:
root_str = self.package.code_name + '_GUI'
version_str = '2.0'
P = cs_xml_reader.Parser(os.path.join(self.data_dir, param),
root_str = root_str,
version_str = version_str)
params = P.getParams()
for k in list(params.keys()):
self.__dict__[k] = params[k]
self.param = param
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir):
# Names, directories, and files in case structure
base_domain.set_case_dir(self, case_dir)
# We may now import user python script functions if present.
self.user_locals = None
user_scripts = os.path.join(self.data_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.
self.read_parameter_file(self.param)
# 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 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 == None and link == None:
return
elif target == None:
err_str = 'No target for link: ' + link
raise RunCaseError(err_str)
elif link == 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):
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
dir_files = os.listdir(self.src_dir)
src_files = (fnmatch.filter(dir_files, '*.c')
+ fnmatch.filter(dir_files, '*.cxx')
+ fnmatch.filter(dir_files, '*.cpp')
+ fnmatch.filter(dir_files, '*.[fF]90'))
if self.exec_solver and len(src_files) > 0:
return True
else:
return False
#---------------------------------------------------------------------------
def compile_and_link(self):
"""
Compile and link user subroutines if necessary
"""
# Check if there are files to compile in source path
dir_files = os.listdir(self.src_dir)
src_files = (fnmatch.filter(dir_files, '*.c')
+ fnmatch.filter(dir_files, '*.cxx')
+ fnmatch.filter(dir_files, '*.cpp')
+ fnmatch.filter(dir_files, '*.[fF]90'))
if len(src_files) > 0:
# Add header files to list so as not to forget to copy them
src_files = src_files + ( fnmatch.filter(dir_files, '*.h')
+ fnmatch.filter(dir_files, '*.hxx')
+ fnmatch.filter(dir_files, '*.hpp'))
exec_src = os.path.join(self.exec_dir, self.package.srcdir)
# Copy source files to execution directory
make_clean_dir(exec_src)
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)
log_name = os.path.join(self.exec_dir, 'compile.log')
log = open(log_name, 'w')
retval = cs_compile.compile_and_link(self.package_compute,
exec_src,
self.exec_dir,
self.compile_cflags,
self.compile_cxxflags,
self.compile_fcflags,
self.compile_libs,
keep_going=True,
stdout=log,
stderr=log)
log.close()
if retval == 0:
self.solver_path = os.path.join('.',
self.package_compute.solver)
else:
# In case of error, copy source to results directory now,
# as no calculation is possible, then raise exception
for f in [self.package.srcdir, 'compile.log']:
self.copy_result(f)
raise RunCaseError('Compile or link error.')
#---------------------------------------------------------------------------
def prepare_data(self):
"""
Copy data to the execution directory
"""
# If we are using a previous preprocessing, simply link to it here
if self.mesh_input:
mesh_input = os.path.expanduser(self.mesh_input)
if not os.path.isabs(mesh_input):
mesh_input = os.path.join(self.case_dir, mesh_input)
link_path = os.path.join(self.exec_dir, 'mesh_input')
self.purge_result(link_path) # in case of previous run here
self.symlink(mesh_input, link_path)
if not self.exec_solver:
return
# Copy data files
dir_files = os.listdir(self.data_dir)
if self.package.guiname in dir_files:
dir_files.remove(self.package.guiname)
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))
if f == 'cs_user_scripts.py': # Copy user script to results now
shutil.copy2(src, os.path.join(self.result_dir, f))
# 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]
# Restart files
if self.restart_input != None:
restart_input = os.path.expanduser(self.restart_input)
if not os.path.isabs(restart_input):
restart_input = os.path.join(self.case_dir, restart_input)
if os.path.exists(restart_input):
if not os.path.isdir(restart_input):
err_str = restart_input + ' is not a directory.'
raise RunCaseError(err_str)
else:
self.symlink(restart_input,
os.path.join(self.exec_dir, 'restart'))
# Partition input files
if self.partition_input != None:
partition_input = os.path.expanduser(self.partition_input)
if not os.path.isabs(partition_input):
partition_input = os.path.join(self.case_dir, partition_input)
if os.path.exists(partition_input):
if not os.path.isdir(partition_input):
err_str = partition_input + ' is not a directory.'
raise RunCaseError(err_str)
else:
self.symlink(partition_input,
os.path.join(self.exec_dir, 'partition_input'))
#---------------------------------------------------------------------------
def preprocess(self):
"""
Runs the preprocessor in the execution directory
"""
if self.mesh_input:
return
# Study directory
study_dir = os.path.split(self.case_dir)[0]
# User config file
u_cfg = configparser.ConfigParser()
u_cfg.read(self.package.get_user_configfile())
# Global config file
g_cfg = configparser.ConfigParser()
g_cfg.read(self.package.get_global_configfile())
# A mesh can be found in different mesh database directories
# (case, study, user, global -- in this order)
mesh_dirs = []
if self.mesh_dir is not None:
mesh_dir = os.path.expanduser(self.mesh_dir)
if not os.path.isabs(mesh_dir):
mesh_dir = os.path.join(self.case_dir, mesh_dir)
mesh_dirs.append(mesh_dir)
if os.path.isdir(os.path.join(study_dir, 'MESH')):
mesh_dirs.append(os.path.join(study_dir, 'MESH'))
if u_cfg.has_option('run', 'meshdir'):
add_path = u_cfg.get('run', 'meshdir').split(':')
for d in add_path:
mesh_dirs.append(d)
if g_cfg.has_option('run', 'meshdir'):
add_path = g_cfg.get('run', 'meshdir').split(':')
for d in add_path:
mesh_dirs.append(d)
# Switch to execution directory
cur_dir = os.path.realpath(os.getcwd())
if cur_dir != self.exec_dir:
os.chdir(self.exec_dir)
mesh_id = None
if len(self.meshes) > 1:
mesh_id = 0
destdir = 'mesh_input'
make_clean_dir(destdir)
# Run once per mesh
for m in self.meshes:
# Get absolute mesh paths
if m is None:
err_str = 'Preprocessing stage required but no mesh is given'
raise RunCaseError(err_str)
if (type(m) == tuple):
m0 = m[0]
else:
m0 = m
m0 = os.path.expanduser(m0)
mesh_path = m0
if (not os.path.isabs(m0)) and len(mesh_dirs) > 0:
for mesh_dir in mesh_dirs:
mesh_path = os.path.join(mesh_dir, m0)
if os.path.isfile(mesh_path):
break
if not os.path.isfile(mesh_path):
err_str = 'Mesh file ' + m0 + ' not found'
if not (os.path.isabs(mesh_path) or mesh_dirs):
err_str += '(no mesh directory given)'
raise RunCaseError(err_str)
# Build command
cmd = [self.package.get_preprocessor()]
if (type(m) == tuple):
for opt in m[1:]:
cmd.append(opt)
if (mesh_id != None):
mesh_id += 1
cmd = cmd + ['--log', 'preprocessor_%02d.log' % (mesh_id)]
cmd = cmd + ['--out', os.path.join('mesh_input',
'mesh_%02d' % (mesh_id))]
else:
cmd = cmd + ['--log']
cmd = cmd + ['--out', 'mesh_input']
cmd.append(mesh_path)
# Run command
retcode = run_command(cmd, pkg=self.package)
if retcode != 0:
err_str = \
'Error running the preprocessor.\n' \
'Check the preprocessor.log file for details.\n\n'
sys.stderr.write(err_str)
self.exec_solver = False
self.error = 'preprocess'
break
# Revert to initial directory
if cur_dir != self.exec_dir:
os.chdir(cur_dir)
return retcode
#---------------------------------------------------------------------------
def solver_command(self, **kw):
"""
Returns a tuple indicating the solver's working directory,
executable path, and associated command-line arguments.
"""
wd = enquote_arg(self.exec_dir) # Working directory
exec_path = enquote_arg(self.solver_path) # Executable
# Build kernel command-line arguments
args = ''
if self.param != None:
args += ' --param ' + enquote_arg(self.param)
if self.logging_args != None:
args += ' ' + self.logging_args
if self.solver_args != None:
args += ' ' + self.solver_args
if self.package_compute.config.features['mpi'] == 'yes':
if self.name != None:
args += ' --mpi --app-name ' + enquote_arg(self.name)
elif self.n_procs > 1:
args += ' --mpi'
# Add FSI coupling (using SALOME YACS) if required
if hasattr(self, 'fsi_aster'):
if not sys.platform.startswith('linux'):
raise RunCaseError(' Coupling with Code_Aster only avalaible on Linux.')
salome_module = os.path.join(self.package_compute.get_dir('libdir'),
'salome',
'libFSI_SATURNEExelib.so')
if os.path.isfile(salome_module):
args += ' --yacs-module=' + enquote_arg(salome_module)
# Adjust for debugger if used
if self.debug != None:
args = enquote_arg(self.solver_path) + ' ' + args
exec_path = self.debug_wrapper_args(self.debug)
return wd, exec_path, args
#---------------------------------------------------------------------------
def copy_results(self):
"""
Retrieve solver results from the execution directory
"""
# Call user script
if self.user_locals:
m = 'domain_copy_results_add'
if m in self.user_locals.keys():
eval(m + '(self)', globals(), self.user_locals)
del self.user_locals[m]
# Determine all files present in execution directory
dir_files = os.listdir(self.exec_dir)
# Only purge if we have no errors, to allow for future debugging.
valid_dir = False
purge = True
if self.error != '':
purge = False
# Determine patterns from previous stages to ignore or possibly remove
purge_list = []
f = 'mesh_input'
if not self.mesh_input and self.exec_solver:
if f in dir_files:
purge_list.append(f)
for f in ['restart', 'partition_input']:
if f in dir_files:
purge_list.append(f)
# Determine files from this stage to ignore or to possibly remove
for f in [self.package.solver, self.package.runsolver]:
if f in dir_files:
purge_list.append(f)
purge_list.extend(fnmatch.filter(dir_files, 'core*'))
f = 'scratch3.lag'
if f in dir_files:
purge_list.append(f)
for f in purge_list:
dir_files.remove(f)
if purge:
self.purge_result(f)
if len(purge_list) > 0:
valid_dir = True
# Copy user sources, compile log, and xml file if present
for f in [self.package.srcdir, 'compile.log', self.param]:
if f in dir_files:
valid_dir = True
self.copy_result(f, purge)
dir_files.remove(f)
# Copy log files
log_files = fnmatch.filter(dir_files, 'listing*')
log_files.extend(fnmatch.filter(dir_files, '*.log'))
log_files.extend(fnmatch.filter(dir_files, 'error*'))
for f in log_files:
self.copy_result(f, purge)
dir_files.remove(f)
if (len(log_files) > 0):
valid_dir = True
# Copy checkpoint files (in case of full disk, copying them
# before other large data such as postprocessing output
# increases chances of being able to continue).
cpt = 'checkpoint'
if cpt in dir_files:
valid_dir = True
self.copy_result(cpt, purge)
dir_files.remove(cpt)
# Now copy all other files
if not valid_dir:
return
for f in dir_files:
self.copy_result(f, purge)
#---------------------------------------------------------------------------
def summary_info(self, s):
"""
Output summary data into file s
"""
base_domain.summary_info(self, s)
if not self.mesh_input:
p = self.package.get_preprocessor()
s.write(' preprocessor : ' + any_to_str(p) + '\n')
if self.exec_solver:
s.write(' solver : ' + self.solver_path + '\n')
#-------------------------------------------------------------------------------
# SYRTHES 4 coupling
class syrthes_domain(base_domain):
def __init__(self,
package,
cmd_line = None, # Command line to define optional syrthes4 behaviour
name = None,
param = 'syrthes.data',
log_file = None,
n_procs_weight = None,
n_procs_min = 1,
n_procs_max = None,
n_procs_radiation = None):
base_domain.__init__(self,
package,
name,
n_procs_weight,
n_procs_min,
n_procs_max)
self.n_procs_radiation = n_procs_radiation
# Additional parameters for Code_Saturne/SYRTHES coupling
# Directories, and files in case structure
self.cmd_line = cmd_line
self.param = param
self.logfile = log_file
if self.logfile == None:
self.logfile = 'syrthes.log'
self.case_dir = None
self.exec_dir = None
self.data_dir = None
self.src_dir = None
self.result_dir = None
self.echo_comm = None
self.exec_solver = True
# Generation of SYRTHES case deferred until we know how
# many processors are really required
self.syrthes_case = None
# Try to base Syrthes version on path used to import the syrthes module,
# for consistency with case creation parameters; it this is not enough,
# use existing environment or load one based on current configuration.
try:
for p in sys.path:
if p[-14:] == '/share/syrthes' or p[-14:] == '\share\syrthes':
syr_profile = os.path.join(p[:,-14], 'bin', 'syrthes.profile')
if os.path.isfile(syr_profile):
source_shell_script(syr_profile)
except Exception:
pass
source_syrthes_env(self.package)
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir):
base_domain.set_case_dir(self, case_dir)
# Names, directories, and files in case structure
self.data_dir = self.case_dir
self.src_dir = self.case_dir
#---------------------------------------------------------------------------
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)
self.exec_dir = os.path.join(self.exec_dir, self.name)
if not os.path.isdir(self.exec_dir):
os.mkdir(self.exec_dir)
#---------------------------------------------------------------------------
def set_result_dir(self, name, given_dir = None):
if given_dir == None:
self.result_dir = os.path.join(self.result_dir,
'RESU_' + self.name,
name)
else:
self.result_dir = os.path.join(given_dir, self.name)
if not os.path.isdir(self.result_dir):
os.makedirs(self.result_dir)
#---------------------------------------------------------------------------
def solver_command(self, **kw):
"""
Returns a tuple indicating SYRTHES's working directory,
executable path, and associated command-line arguments.
"""
wd = enquote_arg(self.exec_dir) # Working directory
exec_path = enquote_arg(self.solver_path) # Executable
# Build kernel command-line arguments
args = ''
args += ' -d ' + enquote_arg(self.syrthes_case.data_file)
args += ' -n ' + str(self.syrthes_case.n_procs)
if self.syrthes_case.n_procs_ray > 0:
args += ' -r ' + str(self.n_procs_ray)
args += ' --name ' + enquote_arg(self.name)
# Output to a logfile
args += ' --log ' + enquote_arg(self.logfile)
# Adjust for Debug if used
if self.debug != None:
args = enquote_arg(self.solver_path) + ' ' + args
exec_path = self.debug_wrapper_args(self.debug)
return wd, exec_path, args
#---------------------------------------------------------------------------
def init_syrthes_case(self):
"""
Build or rebuild syrthes object
"""
# Build command-line arguments
args = '-d ' + os.path.join(self.case_dir, self.param)
args += ' --name ' + self.name
if self.n_procs != None and self.n_procs != 1:
args += ' -n ' + str(self.n_procs)
if self.n_procs_radiation > 0:
args += ' -r ' + str(self.n_procs_radiation)
if self.data_dir != None:
args += ' --data-dir ' + str(self.data_dir)
if self.src_dir != None:
args += ' --src-dir ' + str(self.src_dir)
if self.exec_dir != None:
args += ' --exec-dir ' + str(self.exec_dir)
if self.cmd_line != None and len(self.cmd_line) > 0:
args += ' ' + self.cmd_line
# Define syrthes case structure
import syrthes
self.syrthes_case = syrthes.process_cmd_line(args.split())
if self.syrthes_case.logfile == None:
self.syrthes_case.set_logfile(self.logfile)
else:
self.logfile = self.syrthes_case.logfile
# Read data file and store parameters
self.syrthes_case.read_data_file()
#---------------------------------------------------------------------------
def prepare_data(self):
"""
Fill SYRTHES domain structure
Copy data to the execution directory
Compile and link syrthes executable
"""
# Build SYRTHES case; at this stage, the number of processes
# may not be known, but this is not an issue here: for data
# preparation, SYRTHES only uses the number of processes to
# determine whether compilation should use MPI, and this is
# always true anyways in the coupled case.
self.init_syrthes_case()
# Build exec_srcdir
exec_srcdir = os.path.join(self.exec_dir, 'src')
make_clean_dir(exec_srcdir)
# Preparation of the execution directory and compile and link executable
compile_logname = os.path.join(self.exec_dir, 'compile.log')
retval = self.syrthes_case.prepare_run(exec_srcdir, compile_logname)
self.copy_result(compile_logname)
if retval != 0:
err_str = '\n Error during the SYRTHES preparation step\n'
if retval == 1:
err_str += ' Error during data copy\n'
elif retval == 2:
err_str += ' Error during syrthes compilation and link\n'
# Copy source to results directory, as no run is possible
for f in ['src', 'compile.log']:
self.copy_result(f)
raise RunCaseError(err_str)
# Set executable
self.solver_path = os.path.join('.', 'syrthes')
#---------------------------------------------------------------------------
def preprocess(self):
"""
Partition mesh for parallel run if required by user
"""
# Rebuild Syrthes case now that number of processes is known
self.init_syrthes_case()
# Sumary of the parameters
self.syrthes_case.dump()
# Initialize output file if needed
self.syrthes_case.logfile_init()
# Pre-processing (including partitioning only if SYRTHES
# computation is done in parallel)
retval = self.syrthes_case.preprocessing()
if retval != 0:
err_str = '\n Error during the SYRTHES preprocessing step\n'
raise RunCaseError(err_str)
#---------------------------------------------------------------------------
def copy_results(self):
"""
Retrieve results from the execution directory
"""
# Post-processing
if self.syrthes_case.post_mode != None:
retval = self.syrthes_case.postprocessing(mode = \
self.syrthes_case.post_mode)
else:
retval = 0
if retval != 0:
err_str = '\n Error during SYRTHES postprocessing\n'
raise RunCaseError(err_str)
if self.exec_dir == self.result_dir:
return
retval = self.syrthes_case.save_results(save_dir = self.result_dir,
horodat = False,
overwrite = True)
if retval != 0:
err_str = '\n Error saving SYRTHES results\n'
raise RunCaseError(err_str)
#---------------------------------------------------------------------------
def summary_info(self, s):
"""
Output summary data into file s
"""
base_domain.summary_info(self, s)
if self.solver_path:
s.write(' SYRTHES : ' + self.solver_path + '\n')
#-------------------------------------------------------------------------------
# Code_Aster coupling
class aster_domain(base_domain):
def __init__(self,
package,
name = None,
param = 'fsi.export'):
base_domain.__init__(self,
package,
name,
n_procs_weight = None,
n_procs_min = 1,
n_procs_max = None)
# Get Code_Aster home
config = configparser.ConfigParser()
config.read(self.package.get_configfiles())
self.aster_home = config.get('install', 'aster')
# Additional parameters for Code_Saturne/Code_Aster coupling
# Directories, and files in case structure
self.param = param
self.case_dir = None
self.result_dir = None
self.exec_solver = True
self.conf = 'fsi_config.txt'
self.comm = None
self.mesh = None
self.n_procs_max = 1
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir):
base_domain.set_case_dir(self, case_dir)
e = []
p = os.path.join(self.case_dir, self.param)
if os.path.isfile(p):
f = open(p, 'r')
e = f.readlines()
f.close
else:
err_str = 'Code_Aster export file \'' + p + '\' is not present.\n'
raise RunCaseError(err_str)
for l in e:
k = l.split()
if len(k) >= 3:
if k[1] == 'mpi_nbcpu':
self.n_procs_min = int(k[2])
self.n_procs_max = self.n_procs_min
if k[1] == 'conf':
self.conf = k[2]
if k[1] == 'comm':
self.comm = k[2]
elif k[1] == 'mail':
self.mesh = k[2]
if self.comm == None:
err_str = '\n A Code_Aster ".comm" file must be specified.\n'
raise RunCaseError(err_str)
if self.mesh == None:
err_str = '\n A Code_Aster mesh file must be specified.\n'
raise RunCaseError(err_str)
#---------------------------------------------------------------------------
def prepare_data(self):
"""
Copy solver data to the execution directory
"""
if not self.exec_solver:
return
# Copy data files
for f in [self.param, self.mesh, self.comm]:
p = os.path.join(self.case_dir, f)
if os.path.isfile(p):
shutil.copy2(p, os.path.join(self.exec_dir, f))
else:
err_str = \
'\n The Code_Aster input file: "' + f + '"\n' \
' is not present or readable.\n'
raise RunCaseError(err_str)
# Create FSI specific config.txt file
if os.path.isabs(self.conf):
s.path = self.conf
else:
s_path = os.path.join(self.aster_home, 'share', 'aster', 'config.txt')
s = open(s_path, 'r')
template = s.read()
s.close()
import re
e_re = re.compile('profile.sh')
e_subst = os.path.join(self.aster_home, 'share', 'aster', 'profile.sh')
template = re.sub(e_re, e_subst, template)
e_re = re.compile('Execution/E_SUPERV.py')
e_subst = os.path.join(self.package.get_dir('pythondir'),
'salome',
'FSI_ASTER_component.py')
template = re.sub(e_re, e_subst, template)
s_path = os.path.join(self.exec_dir, os.path.basename(self.conf))
s = open(s_path, 'w')
s.write(template)
s.close()
#---------------------------------------------------------------------------
def generate_script(self):
"""
Generate Code_Aster run script
"""
s_path = os.path.join(self.exec_dir, "aster_by_yacs")
s = open(s_path, 'w')
from cs_exec_environment import write_shell_shebang
write_shell_shebang(s)
s.write('cd ..\n')
s.write('export PATH=' + os.path.join(self.aster_home, 'bin') + ':$PATH\n')
s.write('as_run ' + self.param + '\n')
s.close()
oldmode = (os.stat(s_path)).st_mode
newmode = oldmode | (stat.S_IXUSR)
os.chmod(s_path, newmode)
#---------------------------------------------------------------------------
def copy_results(self):
"""
Retrieve results from the execution directory
"""
# Determine all files present in execution directory
dir_files = os.listdir(self.exec_dir)
# Determine if we should purge the execution directory
purge = True
if self.error != '':
purge = False
# Determine files from this stage to ignore or to possibly remove
purge_list = []
purge_list.extend(fnmatch.filter(dir_files, 'core*'))
for f in purge_list:
dir_files.remove(f)
if purge:
self.purge_result(f)
for f in dir_files:
self.copy_result(f, purge)
#---------------------------------------------------------------------------
def summary_info(self, s):
"""
Output summary data into file s
"""
base_domain.summary_info(self, s)
if self.param:
s.write(' Code_Aster : ' + self.param + '\n')
#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
|