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 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2021 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
from code_saturne import cs_compile
from code_saturne import cs_xml_reader
from code_saturne.cs_exec_environment import run_command
from code_saturne.cs_exec_environment import enquote_arg, separate_args
from code_saturne.cs_exec_environment import get_ld_library_path_additions
from code_saturne.cs_exec_environment import source_syrthes_env
from code_saturne.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
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.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
# 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 = ''
self.error_long = ''
#---------------------------------------------------------------------------
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
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):
"""
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_data(self):
"""
Copy base data to the execution directory.
"""
return
#---------------------------------------------------------------------------
def init_staged_data(self):
"""
Initialize staged data in the execution directory.
"""
return
#---------------------------------------------------------------------------
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, **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 '
cs_pkg_dir = self.package.get_dir('pkgpythondir')
if self.package.name != 'code_saturne':
cs_pkg_dir = os.path.join(cs_pkg_dir, '../code_saturne')
dbg_wrapper_path = os.path.join(cs_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.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
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
# MEG expression generator
self.mci = None
#---------------------------------------------------------------------------
def __set_case_parameters__(self, update_xml=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.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.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 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 == None:
return ''
else:
return 'for domain ' + str(self.name)
#---------------------------------------------------------------------------
def read_parameter_file(self, param):
"""
Parse the optional XML parameter file.
"""
if param == 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 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 = 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__(update_xml=True)
#---------------------------------------------------------------------------
def init_staged_data(self):
"""
Initialize staged data in the execution directory.
"""
# Now set parameters
self.__set_case_parameters__(update_xml=False)
# 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)
#---------------------------------------------------------------------------
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
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:
exec_src = os.path.join(self.exec_dir, 'src')
# 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(self.src_dir)
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_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):
"""
Copy data to the execution directory
"""
err_str = ""
if not self.exec_solver:
return
# 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
# 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)
if not os.path.isabs(restart_input):
restart_input = os.path.join(self.case_dir, restart_input)
if not os.path.exists(restart_input):
err_str += restart_input + ' does not exist.\n\n'
elif not os.path.isdir(restart_input):
err_str += restart_input + ' is not a directory.\n\n.'
else:
self.symlink(restart_input,
os.path.join(self.exec_dir, 'restart'))
print(' Restart from ' + self.restart_input + '\n')
if self.restart_mesh_input != None and err_str == '':
restart_mesh_input = os.path.expanduser(self.restart_mesh_input)
if not os.path.isabs(restart_mesh_input):
restart_mesh_input = os.path.join(self.case_dir,
restart_mesh_input)
if not os.path.exists(restart_mesh_input):
err_str += restart_mesh_input + ' does not exist.\n\n'
elif not os.path.isfile(restart_mesh_input):
err_str += restart_mesh_input + ' is not a file.\n\n.'
else:
self.symlink(restart_mesh_input,
os.path.join(self.exec_dir, 'restart_mesh_input'))
print(' Restart mesh ' + self.restart_mesh_input + '\n')
# Mesh input file
restart_input_mesh = None
if self.restart_input:
restart_input_mesh = os.path.join(self.exec_dir, 'restart', 'mesh_input.csm')
if not os.path.exists(restart_input_mesh):
restart_input_mesh = None
if restart_input_mesh is None or self.preprocess_on_restart \
or self.restart_mesh_input:
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)
# Differentiate between a folder and file, since we now
# have a file extension
if os.path.isdir(mesh_input):
link_path = os.path.join(self.exec_dir, 'mesh_input')
else:
link_path = os.path.join(self.exec_dir, 'mesh_input.csm')
self.purge_result(link_path) # in case of previous run here
self.symlink(mesh_input, link_path)
else:
# use mesh from restart, with no symbolic link
self.mesh_input = restart_input_mesh
# Partition input files
if self.partition_input != None and not err_str:
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.\n\n'
else:
self.symlink(partition_input,
os.path.join(self.exec_dir, 'partition_input'))
if len(err_str) > 0:
self.error = 'data preparation'
sys.stderr.write(err_str)
else:
try:
link_path = os.path.join(self.exec_dir, "listing")
os.symlink("run_solver.log", link_path)
except Exception:
pass
#---------------------------------------------------------------------------
def preprocess(self):
"""
Runs the preprocessor in the execution directory
"""
if self.mesh_input:
return
# Check if cartesian mesh is to be used
if self.param != None:
from code_saturne.model.XMLengine import Case
from code_saturne.model.SolutionDomainModel import getMeshOriginType
fp = os.path.join(self.exec_dir, self.param)
case = Case(package=self.package, file_name=fp)
case['xmlfile'] = fp
case.xmlCleanAllBlank(case.xmlRootNode())
if getMeshOriginType(case) == 'mesh_cartesian':
return
# If no mesh is provided return, since user can define mesh_input
# using 'cs_user_mesh_input' user function.
if len(self.meshes) == 1 and self.meshes[0] == None:
return
# Study directory
study_dir = os.path.split(self.case_root_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_root_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)
# Set environment
ld_library_path_save = None
add_lib_dirs = get_ld_library_path_additions(self.package)
if add_lib_dirs:
ld_library_path_save = os.getenv('LD_LIBRARY_PATH')
ld_library_path = ""
for d in add_lib_dirs:
ld_library_path += d + ':'
ld_library_path += ld_library_path_save
os.environ['LD_LIBRARY_PATH'] = ld_library_path
# 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
# Generate output mesh name
if (mesh_id != None):
mesh_id += 1
_outputmesh = os.path.join('mesh_input',
'mesh_%02d.csm' % (mesh_id))
else:
_outputmesh = 'mesh_input.csm'
# Check if preprocessor is needed or not
if mesh_path[-4:] == ".csm":
# code_saturne mesh, no need to run preprocessor
self.symlink(mesh_path,
os.path.join(self.exec_dir, _outputmesh))
retcode = 0
else:
# run preprocessor if needed
cmd = [self.package.get_preprocessor()]
if (type(m) == tuple):
for opt in m[1:]:
cmd.append(opt)
cmd = cmd + ['--out', _outputmesh]
if (mesh_id != None):
cmd = cmd + ['--log', 'preprocessor_%02d.log' % (mesh_id)]
cmd = cmd + ['--case', 'preprocessor_%02d' % (mesh_id)]
else:
cmd = cmd + ['--log']
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
# Restore environment
if ld_library_path_save:
os.environ['LD_LIBRARY_PATH'] = ld_library_path_save
# 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.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'
# 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 = []
if not self.mesh_input and self.exec_solver:
for f in ['mesh_input', 'mesh_input.csm']:
if f in dir_files:
purge_list.append(f)
for f in ['partition_input']:
if f in dir_files:
purge_list.append(f)
# Determine files from this stage to ignore or to possibly remove
solver_name = os.path.basename(self.solver_path)
for f in [solver_name, self.package.runsolver]:
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 ['src', '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 coupling
class syrthes_domain(base_domain):
def __init__(self,
package,
cmd_line = None, # Command line to define optional syrthes4 behavior
name = None,
param = 'syrthes.data',
log_file = None,
n_procs_weight = None,
n_procs_min = 1,
n_procs_max = None,
n_procs_radiation = None,
verbose = True):
base_domain.__init__(self,
package,
name,
n_procs_weight,
n_procs_min,
n_procs_max)
self.n_procs_radiation = n_procs_radiation
self.n_procs_ref = None
self.n_procs_radiation = None
# 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
# Determine environment which should be used for Syrthes
# operations
ld_library_path_save = os.getenv('LD_LIBRARY_PATH')
source_syrthes_env(self.package, verbose)
self.ld_library_path = os.getenv('LD_LIBRARY_PATH')
self.__ld_library_path_restore__(ld_library_path_save)
#---------------------------------------------------------------------------
def __ld_library_path_restore__(self, ld_library_path_save):
if ld_library_path_save:
os.environ['LD_LIBRARY_PATH'] = ld_library_path_save
else:
try:
os.environ.pop('LD_LIBRARY_PATH')
except Exception:
pass
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir, staging_dir = None):
base_domain.set_case_dir(self, case_dir, staging_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
"""
# Now try to read additional data set by Syrthes GUI
part_tool_name = None
f = open(os.path.join(self.case_dir, self.param), 'r')
lines = f.readlines()
del(f)
for l in lines:
if l[0] == '/':
i = l.find('DOMAIN_POS=')
if i > -1:
try:
id = int(l[i + 11:].strip())
if id == 0:
part_tool_name = 'scotch'
elif id == 1:
part_tool_name = 'metis'
except Exception:
pass
i = l.find('LISTING=')
if i > -1:
logfile = l[i + 8:].strip()
if logfile:
self.logfile = logfile
i = l.find('NBPROC_COND=')
if i > -1:
try:
self.n_procs_ref = int(l[i + 12:].strip())
except Exception:
pass
i = l.find('NBPROC_RAD=')
if i > -1:
try:
self.n_procs_radiation_ref = int(l[i + 11:].strip())
except Exception:
pass
# Build command-line arguments
args = '-d ' + os.path.join(self.case_root_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 != None:
if self.n_procs_radiation > 0:
args += ' -r ' + str(self.n_procs_radiation)
if part_tool_name:
args += ' -t ' + part_tool_name
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.
ld_library_path_save = os.getenv('LD_LIBRARY_PATH')
os.environ['LD_LIBRARY_PATH'] = self.ld_library_path
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')
self.__ld_library_path_restore__(ld_library_path_save)
#---------------------------------------------------------------------------
def preprocess(self):
"""
Partition mesh for parallel run if required by user
"""
ld_library_path_save = os.getenv('LD_LIBRARY_PATH')
os.environ['LD_LIBRARY_PATH'] = self.ld_library_path
# 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)
self.__ld_library_path_restore__(ld_library_path_save)
#---------------------------------------------------------------------------
def copy_results(self):
"""
Retrieve results from the execution directory
"""
ld_library_path_save = os.getenv('LD_LIBRARY_PATH')
os.environ['LD_LIBRARY_PATH'] = self.ld_library_path
# 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)
self.__ld_library_path_restore__(ld_library_path_save)
#---------------------------------------------------------------------------
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')
#-------------------------------------------------------------------------------
# Cathare coupling
class cathare_domain(domain):
"""
Class specific to coupling with CATHARE.
"""
#---------------------------------------------------------------------------
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
cathare_case_file = None, # Cathare case .dat file
neptune_cfd_dom = None, # neptune_cfd domain
adaptation = None): # HOMARD adaptation script
domain.__init__(self,
package,
package_compute,
name,
n_procs_weight,
n_procs_min,
n_procs_max,
logging_args,
param,
prefix,
adaptation)
# If not provided, setup is automatically set to setup.xml
if self.param == None:
self.param = "setup.xml"
self.cathare_case_file = cathare_case_file
self.neptune_cfd_dom = neptune_cfd_dom
#---------------------------------------------------------------------------
def read_parameter_file(self, param):
# Getting the .xml from NEPTUNE_CFD, since all definitions are made
# within its GUI and paramfile
# First we check if code is launched using the run command,
# hence in the main folder. If using submit, then _spath
# corresponds to the execution folder, so we remove the DATA
_spath = os.path.split(self.case_dir)[0]
nept_paramfile = os.path.join(_spath,
self.neptune_cfd_dom,
"DATA",
param)
if not os.path.exists(nept_paramfile):
nept_paramfile = os.path.join(_spath,
self.neptune_cfd_dom,
param)
if not os.path.exists(nept_paramfile):
raise Exception("Could not find NEPTUNE_CFD data file.")
ofile = open(nept_paramfile,'r').readlines()
nfile = open(os.path.join(self.data_dir, param),'w')
for line in ofile:
if 'api_type' not in line:
nfile.write(line)
else:
nfile.write(' <api_type>2</api_type>')
nfile.close()
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
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)
#---------------------------------------------------------------------------
def compile_cathare2_lib(self):
"""
Copy the Cathare .dat file and the different .f files to the execution
folder. Then generate the .so lib based on the ICoCo API of
CATHARE2.
"""
msg = "Generating CATHARE2 .so file\n" \
"----------------------------\n\n"
sys.stdout.write(msg)
sys.stdout.flush()
import subprocess
orig = os.getcwd()
os.chdir(self.exec_dir)
cathare_compilation_files = [self.cathare_case_file]
dir_files = os.listdir(self.data_dir)
for f in fnmatch.filter(dir_files, '*.f'):
cathare_compilation_files.append(f)
for f in cathare_compilation_files:
src_file = os.path.join(self.data_dir, f)
dest_file = os.path.join(self.exec_dir, f)
shutil.copy2(src_file, dest_file)
# Generate the .so creation command (hence reducing dependencies)
config = configparser.ConfigParser()
config.read(self.package.get_configfiles())
shell_cmd = 'export v25_3="%s"\n' % (config.get('install', 'cathare'))
shell_cmd+= 'jdd_CATHARE="%s"\n' % (self.cathare_case_file)
shell_cmd+= 'export LD_LIBRARY_PATH=${v25_3}/lib:$LD_LIBRARY_PATH\n'
shell_cmd+= 'export LD_LIBRARY_PATH=${v25_3}/ICoCo/lib:$LD_LIBRARY_PATH\n'
shell_cmd+= 'make -f ${v25_3}/ICoCo/Makefile_gad clean\n'
shell_cmd+= '${v25_3}/unix-procedur/vers.unix\n'
shell_cmd+= 'DATAFILE=${jdd_CATHARE} make -f ${v25_3}/ICoCo/Makefile_gad lib\n'
# Shell
user_shell = os.getenv('SHELL')
if not user_shell:
user_shell = '/bin/sh'
# log
log = open('cathare2_so_generation.log', 'w')
p = subprocess.Popen(shell_cmd,
shell=True,
executable=user_shell,
stdout=log,
stderr=log,
universal_newlines=True)
log.close()
output, errors = p.communicate()
if p.returncode != 0:
self.error = 'compile cathare2 lib'
self.error_long = 'Compilation of cathare2 .so library based on '
self.error_long+= '%s file failed.' % (self.cathare_case_file)
self.error_long+= ' Check "cathare2_so_generation.log' \
os.chdir(orig)
#---------------------------------------------------------------------------
def compile_and_link(self):
"""
Compile and link user subroutines if needed.
"""
# Generate the CATHARE lib first
self.compile_cathare2_lib()
# Then compile NCFD source files if needed
super(cathare_domain, self).compile_and_link()
#---------------------------------------------------------------------------
def solver_command(self, **kw):
"""
Returns a tuple indicating the script's working directory,
executable path, and associated command-line arguments.
"""
wd, exec_path, args = super(cathare_domain, self).solver_command()
args += " --c2-wrapper"
return wd, exec_path, args
#-------------------------------------------------------------------------------
# Coupling with a general Python-based code
class python_domain(base_domain):
#---------------------------------------------------------------------------
def __init__(self,
package,
cmd_line = None,
name = None,
script_name = 'partner_script.py',
log_file = None,
n_procs_weight = None,
n_procs_min = 1,
n_procs_max = None,
n_threads = 1):
base_domain.__init__(self,
package,
name,
n_procs_weight,
n_procs_min,
n_procs_max)
self.cmd_line = cmd_line
self.logfile = log_file
if self.logfile == None:
self.logfile = 'python.log'
self.data_file = None
self.solver_path = self.package.config.python
self.exec_solver = True
self.nthreads = n_threads
self.script_name = script_name
#---------------------------------------------------------------------------
def set_case_dir(self, case_dir, staging_dir = None):
base_domain.set_case_dir(self, case_dir, staging_dir)
self.data_dir = os.path.join(self.case_dir, "DATA")
self.src_dir = None
self.result_dir = os.path.join(self.case_dir, "RESU")
#---------------------------------------------------------------------------
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 copy_data(self):
"""
Copy data to run directory
"""
dir_files = os.listdir(self.data_dir)
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))
#---------------------------------------------------------------------------
def preprocess(self):
"""
Preprocess dummy function: Does nothing for a standard python script
"""
# Nothing to do
#---------------------------------------------------------------------------
def copy_results(self):
"""
Copy results dummy function: Does nothing for a standard python script
"""
# Nothing to do
return
#---------------------------------------------------------------------------
def summary_info(self, s):
"""
output summary data into file s
"""
base_domain.summary_info(self, s)
#---------------------------------------------------------------------------
def solver_command(self, **kw):
"""
Returns a tuple indicating the script's working directory,
executable path, and associated command-line arguments.
"""
wd = enquote_arg(self.exec_dir) # Working directory
# Executable
exec_path = enquote_arg(self.solver_path) \
+ ' ' + self.script_name
# Build kernel command-line arguments
args = ''
if self.data_file:
args += ' -d ' + enquote_arg(self.data_file)
args += ' -n ' + str(self.n_procs)
args += ' --name ' + enquote_arg(self.name)
# Output to a logfile
args += ' --log ' + enquote_arg(self.logfile)
return wd, exec_path, args
#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
|