Spaces:
Paused
Paused
File size: 67,886 Bytes
984abae | 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 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 | # 拓撲量子物態與快速材料預測 - 完整部署包
> 來源:~/物理模擬/topo/指標/自動化TI.md(快速材料預測部署包)
> 整合日期:2026-11-11
## 項目結構
```
topo-physics-deployment/
├── install.sh # 一鍵安裝腳本
├── schema/ # 數據庫模式
│ ├── physics_simulation.sql
│ ├── topological_physics.sql
│ └── topological_fast_prediction.sql
├── services/ # Python服務
│ ├── topo_collector.py
│ ├── fast_predictor.py
│ ├── gnn_predictor.py
│ ├── tqc_fast.py
│ ├── combinatorial_generator.py
│ └── high_throughput_screen.py
├── systemd/ # systemd配置
│ ├── topo.service
│ ├── topo.timer
│ └── topo_screen.service
├── frontend/ # Streamlit界面
│ ├── app.py
│ ├── topological_simulator.py
│ └── fast_prediction_ui.py
├── requirements.txt # Python依賴
└── .env.template # 環境變量模板
```
---
## 1️⃣ 一鍵安裝腳本 `install.sh`
```bash
#!/bin/bash
set -e
# 顏色定義
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}拓撲量子物態與快速材料預測系統部署腳本${NC}"
echo -e "${GREEN}============================================${NC}"
# 配置變量
DB_NAME="topo_physics_db"
DB_USER="topo_admin"
DB_PASSWORD=${TOPO_DB_PASSWORD:-"TopoPhysics2024!"}
SERVICE_USER="topo-service"
# 步驟1: 安裝系統依賴
echo -e "\n${YELLOW}步驟1: 安裝系統依賴...${NC}"
sudo apt update
sudo apt install -y \
postgresql postgresql-contrib \
python3.9 python3.9-venv python3.9-dev \
python3-pip postgresql-client \
build-essential curl git
# 步驟2: 創建數據庫和用戶
echo -e "\n${YELLOW}步驟2: 創建PostgreSQL數據庫和用戶...${NC}"
sudo systemctl enable --now postgresql
# 創建SQL命令
sudo -u postgres psql <<EOF
-- 創建數據庫
CREATE DATABASE ${DB_NAME};
-- 創建用戶
CREATE USER ${DB_USER} WITH PASSWORD '${DB_PASSWORD}';
-- 賦予權限
GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${DB_USER};
-- 連接數據庫創建模式
\c ${DB_NAME}
-- 創建模式
CREATE SCHEMA physics_simulation AUTHORIZATION ${DB_USER};
CREATE SCHEMA topological_physics AUTHORIZATION ${DB_USER};
CREATE SCHEMA topological_fast_prediction AUTHORIZATION ${DB_USER};
CREATE SCHEMA user_workspace AUTHORIZATION ${DB_USER};
-- 創建GIN索引擴展
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS btree_gin;
-- 設置搜索路徑
ALTER USER ${DB_USER} SET search_path TO user_workspace, topological_physics, topological_fast_prediction, physics_simulation;
-- 創建只讀用戶用于前端
CREATE USER topo_reader WITH PASSWORD '${DB_PASSWORD}_ro';
GRANT CONNECT ON DATABASE ${DB_NAME} TO topo_reader;
GRANT USAGE ON SCHEMA physics_simulation TO topo_reader;
GRANT USAGE ON SCHEMA topological_physics TO topo_reader;
GRANT USAGE ON SCHEMA topological_fast_prediction TO topo_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA physics_simulation TO topo_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA topological_physics TO topo_reader;
GRANT SELECT ON ALL TABLES IN SCHEMA topological_fast_prediction TO topo_reader;
EOF
echo -e "${GREEN}✅ 數據庫和用戶創建完成${NC}"
# 步驟3: 創建Python服務用戶
echo -e "\n${YELLOW}步驟3: 創建服務用戶...${NC}"
if ! id "${SERVICE_USER}" &>/dev/null; then
sudo useradd -r -s /bin/false -d /opt/topo-physics ${SERVICE_USER}
echo -e "${GREEN}✅ 創建用戶 ${SERVICE_USER}${NC}"
else
echo -e "${YELLOW}用戶已存在,跳過創建${NC}"
fi
# 步驟4: 創建項目目錄
echo -e "\n${YELLOW}步驟4: 創建項目目錄結構...${NC}"
sudo mkdir -p /opt/topo-physics/{services,frontend,logs,data}
sudo chown -R ${SERVICE_USER}:${SERVICE_USER} /opt/topo-physics
sudo chmod 755 /opt/topo-physics
# 步驟5: 創建Python虛藕環境
echo -e "\n${YELLOW}步驟5: 創建Python虛藕環境...${NC}"
sudo -u ${SERVICE_USER} python3.9 -m venv /opt/topo-physics/venv
sudo -u ${SERVICE_USER} /opt/topo-physics/venv/bin/pip install --upgrade pip
# 步驟6: 安裝Python依賴
echo -e "\n${YELLOW}步驟6: 安裝Python依賴...${NC}"
cat > /tmp/requirements.txt <<'EOF'
# 數據庫與網絡
psycopg2-binary==2.9.7
sqlalchemy==2.0.21
arxiv==1.4.7
requests==2.31.0
aiohttp==3.8.5
# 科學計算
numpy==1.24.3
scipy==1.11.2
matplotlib==3.7.2
scikit-learn==1.3.0
pandas==2.0.3
# 材料科學
pymatgen==2023.9.10
pymatgen-analysis-diffusion==2023.8.15
ase==3.22.1
# 機器學習與圖神經網絡
torch==2.0.1
torch-geometric==2.3.1
torch-scatter==2.1.1
torch-sparse==0.6.17
torch-cluster==1.6.0
# 前端與可視化
streamlit==1.27.0
pyvista==0.42.1
panel==1.2.3
plotly==5.16.1
# 工具庫
python-dotenv==1.0.0
tqdm==4.66.1
pyyaml==6.0.1
EOF
sudo -u ${SERVICE_USER} /opt/topo-physics/venv/bin/pip install -r /tmp/requirements.txt
echo -e "${GREEN}✅ Python依賴安裝完成${NC}"
# 步驟7: 部署服務腳本
echo -e "\n${YELLOW}步驟7: 部署Python服務腳本...${NC}"
# 創建服務腳本目錄
sudo -u ${SERVICE_USER} mkdir -p /opt/topo-physics/services
sudo -u ${SERVICE_USER} mkdir -p /opt/topo-physics/frontend
# 創建所有服務腳本
cat > /opt/topo-physics/services/__init__.py <<'EOF'
# 服務模塊
EOF
# topo_collector.py (已提供完整代碼)
install_file /opt/topo-physics/services/topo_collector.py "topo_collector.py"
# fast_predictor.py
install_file /opt/topo-physics/services/fast_predictor.py "fast_predictor.py"
# gnn_predictor.py
install_file /opt/topo-physics/services/gnn_predictor.py "gnn_predictor.py"
# tqc_fast.py
install_file /opt/topo-physics/services/tqc_fast.py "tqc_fast.py"
# combinatorial_generator.py
install_file /opt/topo-physics/services/combinatorial_generator.py "combinatorial_generator.py"
# high_throughput_screen.py
install_file /opt/topo-physics/services/high_throughput_screen.py "high_throughput_screen.py"
# 使腳本可執行
sudo chmod +x /opt/topo-physics/services/*.py
echo -e "${GREEN}✅ 服務腳本部署完成${NC}"
# 步驟8: 部署數據庫模式
echo -e "\n${YELLOW}步驟8: 部署數據庫模式...${NC}"
psql -U ${DB_USER} -d ${DB_NAME} -h localhost <<EOF
\i $(pwd)/schema/physics_simulation.sql
\i $(pwd)/schema/topological_physics.sql
\i $(pwd)/schema/topological_fast_prediction.sql
EOF
echo -e "${GREEN}✅ 數據庫模式部署完成${NC}"
# 步驟9: 創建環境變量文件
echo -e "\n${YELLOW}步驟9: 創建環境變量配置...${NC}"
cat > /opt/topo-physics/.env <<EOF
# PostgreSQL配置
DB_HOST=localhost
DB_PORT=5432
DB_NAME=${DB_NAME}
DB_USER=${DB_USER}
DB_PASSWORD=${DB_PASSWORD}
DB_USER_READONLY=topo_reader
DB_PASSWORD_READONLY=${DB_PASSWORD}_ro
# API密鑰 (需用戶自行填寫)
MATERIALS_PROJECT_API_KEY=your_api_key_here
GITHUB_TOKEN=your_github_token_here
ARXIV_API_KEY=your_arxiv_key_here
# 服務配置
SERVICE_USER=${SERVICE_USER}
SERVICE_HOME=/opt/topo-physics
LOG_LEVEL=INFO
EOF
sudo chown ${SERVICE_USER}:${SERVICE_USER} /opt/topo-physics/.env
sudo chmod 600 /opt/topo-physics/.env
echo -e "${GREEN}✅ 環境變量文件創建完成${NC}"
# 步驟10: 配置systemd服務
echo -e "\n${YELLOW}步驟10: 配置systemd服務...${NC}"
cat > /etc/systemd/system/topo.service <<EOF
[Unit]
Description=Topological Materials Data Collector Service
Documentation=https://github.com/your-org/topo-physics
After=network.target postgresql.service
Requires=postgresql.service
[Service]
Type=oneshot
User=${SERVICE_USER}
Group=${SERVICE_USER}
WorkingDirectory=/opt/topo-physics
Environment="PATH=/opt/topo-physics/venv/bin:/usr/local/bin:/usr/bin:/bin"
EnvironmentFile=/opt/topo-physics/.env
# 安全設置
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/opt/topo-physics/logs
ReadWritePaths=/opt/topo-physics/data
# 資源限制
CPUQuota=200%
MemoryMax=4G
# 執行命令
ExecStart=/opt/topo-physics/venv/bin/python /opt/topo-physics/services/topo_collector.py
# 日志
StandardOutput=journal
StandardError=journal
SyslogIdentifier=topo-collector
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/topo.timer <<EOF
[Unit]
Description=Topological Materials Data Collection Timer
Documentation=https://github.com/your-org/topo-physics
Requires=topo.service
[Timer]
# 每天凌晨2點執行采集
OnCalendar=*-*-* 02:00:00
# 每周日凌晨執行全量采集
OnCalendar=Sun *-*-* 00:00:00
# 啓動後5分鍾執行一次
OnBootSec=5min
# 持久化計時器
Persistent=true
# 隨機延遲避免同時執行
RandomizedDelaySec=15min
[Install]
WantedBy=timers.target
EOF
cat > /etc/systemd/system/topo_screen.service <<EOF
[Unit]
Description=Topological Materials Daily Screening
After=topo.service
Requires=topo.service
[Service]
Type=oneshot
User=${SERVICE_USER}
Group=${SERVICE_USER}
WorkingDirectory=/opt/topo-physics
Environment="PATH=/opt/topo-physics/venv/bin:/usr/local/bin:/usr/bin:/bin"
EnvironmentFile=/opt/topo-physics/.env
# 執行高通量篩選
ExecStart=/opt/topo-physics/venv/bin/python /opt/topo-physics/services/high_throughput_screen.py --limit=10000
[Install]
WantedBy=multi-user.target
EOF
# 創建日志目錄
sudo mkdir -p /opt/topo-physics/logs
sudo chown ${SERVICE_USER}:${SERVICE_USER} /opt/topo-physics/logs
# 重新加載systemd配置
sudo systemctl daemon-reload
echo -e "${GREEN}✅ systemd服務配置完成${NC}"
# 步驟11: 初始化數據
echo -e "\n${YELLOW}步驟11: 初始化基礎數據...${NC}"
sudo -u ${SERVICE_USER} /opt/topo-physics/venv/bin/python <<'PYEOF'
import sys
sys.path.append('/opt/topo-physics/services')
from services.tqc_fast import TopologicalQuantumChemistry
# 初始化原子絕緣體基組數據庫
DB_CONFIG = {
'host': 'localhost',
'port': '5432',
'database': 'topo_physics_db',
'user': 'topo_admin',
'password': 'TopoPhysics2024!'
}
try:
predictor = TopologicalQuantumChemistry(DB_CONFIG)
predictor.initialize_basis_database()
print("✅ 原子絕緣體基組數據庫初始化成功")
except Exception as e:
print(f"⚠️ 初始化失敗: {e}")
PYEOF
echo -e "${GREEN}✅ 基礎數據初始化完成${NC}"
# 步驟12: 啓用并啓動服務
echo -e "\n${YELLOW}步驟12: 啓用系統服務...${NC}"
sudo systemctl enable --now postgresql
sudo systemctl enable --now topo.timer
sudo systemctl enable --now topo_screen.service
echo -e "${GREEN}✅ 所有服務已啓用${NC}"
# 步驟13: 驗證安裝
echo -e "\n${YELLOW}步驟13: 驗證安裝...${NC}"
echo "檢查數據庫連接..."
psql -U ${DB_USER} -d ${DB_NAME} -h localhost -c "SELECT version();"
echo -e "\n檢查服務狀態..."
sudo systemctl is-enabled topo.timer
sudo systemctl is-enabled topo.service
echo -e "\n檢查定時器下次執行時間..."
sudo systemctl list-timers topo.timer --no-pager
# 步驟14: 安裝Streamlit前端
echo -e "\n${YELLOW}步驟14: 安裝Streamlit前端...${NC}"
install_file /opt/topo-physics/frontend/main_app.py "main_app.py"
# 創建Streamlit服務
cat > /etc/systemd/system/topo-frontend.service <<EOF
[Unit]
Description=Topological Physics Streamlit Frontend
After=network.target
[Service]
Type=simple
User=${SERVICE_USER}
WorkingDirectory=/opt/topo-physics/frontend
Environment="PATH=/opt/topo-physics/venv/bin:/usr/local/bin:/usr/bin:/bin"
EnvironmentFile=/opt/topo-physics/.env
ExecStart=/opt/topo-physics/venv/bin/streamlit run main_app.py \
--server.port 8501 \
--server.address 0.0.0.0 \
--server.headless true
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable --now topo-frontend.service
echo -e "${GREEN}✅ Streamlit前端服務已啓動${NC}"
echo -e "${GREEN}訪問地址: http://$(hostname -I | awk '{print $1}'):8501${NC}"
# 步驟15: 創建日志輪轉配置
echo -e "\n${YELLOW}步驟15: 配置日志輪轉...${NC}"
sudo tee /etc/logrotate.d/topo-collector > /dev/null <<EOF
/opt/topo-physics/logs/topo-collector.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 0640 ${SERVICE_USER} ${SERVICE_USER}
postrotate
systemctl kill -s USR1 topo.service 2>/dev/null || true
endscript
}
EOF
echo -e "${GREEN}✅ 日志輪轉配置完成${NC}"
# 總結
echo -e "\n${GREEN}============================================${NC}"
echo -e "${GREEN}✅ 部署完成!${NC}"
echo -e "${GREEN}============================================${NC}"
echo -e "\n📊 系統組件:"
echo -e " - PostgreSQL數據庫: localhost:5432"
echo -e " - Streamlit前端: http://$(hostname -I | awk '{print $1}'):8501"
echo -e " - 數據采集服務: topo.service (定時: topo.timer)"
echo -e " - 高通量篩選: topo_screen.service"
echo -e "\n📁 配置文件:"
echo -e " - 環境變量: /opt/topo-physics/.env"
echo -e " - 服務腳本: /opt/topo-physics/services/"
echo -e " - 前端頁面: /opt/topo-physics/frontend/"
echo -e " - 日志文件: /opt/topo-physics/logs/"
echo -e "\n🔧 管理命令:"
echo -e " # 查看服務狀態"
echo -e " sudo systemctl status topo.service"
echo -e " sudo systemctl status topo-frontend.service"
echo -e ""
echo -e " # 查看日志"
echo -e " sudo journalctl -u topo.service -f"
echo -e " sudo journalctl -u topo-frontend.service -f"
echo -e ""
echo -e " # 手動觸發采集"
echo -e " sudo systemctl start topo.service"
echo -e ""
echo -e " # 查看定時器"
echo -e " sudo systemctl list-timers topo.timer"
echo -e "\n⚠️ 後續配置:"
echo -e " 1. 編輯環境變量: sudo nano /opt/topo-physics/.env"
echo -e " 2. 添加API密鑰: MATERIALS_PROJECT_API_KEY, GITHUB_TOKEN"
echo -e " 3. 下載GNN模型: 放入 /opt/topo-physics/models/ 目錄"
echo -e " 4. 首次測試: sudo systemctl start topo.service"
echo -e "\n${GREEN}============================================${NC}"
echo -e "系統已準備好接收您的API密鑰和模型文件!"
echo -e "${GREEN}============================================${NC}"
```
---
## 2️⃣ Python服務腳本 (完整版)
### 2.1 `services/topo_collector.py`
```python
#!/usr/bin/env python3
"""
拓撲材料數據采集器 - systemd服務
"""
import sys
import os
import logging
import json
import time
from datetime import datetime, timedelta
from typing import List, Dict, Any
import arxiv
import psycopg2
import requests
from dotenv import load_dotenv
# 加載環境變量
load_dotenv('/opt/topo-physics/.env')
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/opt/topo-physics/logs/topo-collector.log'),
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger('topo_collector')
class TopologicalMaterialsCollector:
"""拓撲材料數據采集器"""
def __init__(self):
self.db_config = {
'host': os.getenv('DB_HOST'),
'port': os.getenv('DB_PORT'),
'database': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD')
}
self.conn = None
self.last_collection_time = self.get_last_collection_time()
def connect_db(self):
"""連接PostgreSQL數據庫"""
try:
self.conn = psycopg2.connect(**self.db_config)
self.conn.set_session(autocommit=True)
logger.info(f"✅ 數據庫連接成功: {self.db_config['host']}")
except Exception as e:
logger.error(f"❌ 數據庫連接失敗: {e}")
raise
def get_last_collection_time(self) -> datetime:
"""獲取上次采集時間"""
try:
with open('/opt/topo-physics/data/last_run.json', 'r') as f:
data = json.load(f)
return datetime.fromisoformat(data['last_run'])
except FileNotFoundError:
return datetime.now() - timedelta(days=7)
def save_last_collection_time(self):
"""保存本次采集時間"""
with open('/opt/topo-physics/data/last_run.json', 'w') as f:
json.dump({'last_run': datetime.now().isoformat()}, f)
def scrape_arxiv(self, days_back: int = 7) -> List[Dict[str, Any]]:
"""從arXiv爬取拓撲材料相關論文"""
logger.info(f"🔄 開始爬取arXiv (過去{days_back}天)...")
query = """
ti:topology OR ti:"topological insulator" OR ti:"Berry phase" OR
abs:topology OR abs:"topological invariant" OR abs:"Chern number"
AND (cat:cond-mat.mtrl-sci OR cat:cond-mat.mes-hall OR cat:physics.comp-ph)
"""
search = arxiv.Search(
query=query,
sort_by=arxiv.SortCriterion.SubmittedDate,
sort_order=arxiv.SortOrder.Descending,
max_results=200
)
papers = []
cutoff_date = datetime.now() - timedelta(days=days_back)
for result in search.results():
if result.published.replace(tzinfo=None) > cutoff_date:
if self.is_topological_material_paper(result):
papers.append({
'resource_name': result.title,
'resource_type': 'research_paper',
'source_url': result.pdf_url,
'description': result.summary[:500],
'physics_category': 'topological_physics',
'citation_count': 0,
'tags': [tag.term for tag in result.tags],
'published_date': result.published.isoformat(),
'authors': [author.name for author in result.authors]
})
logger.info(f"✅ 爬取完成: 找到 {len(papers)} 篇拓撲材料相關論文")
return papers
def is_topological_material_paper(self, result) -> bool:
"""AI輔助判斷是否爲拓撲材料研究"""
keywords = [
'topology', 'topological', 'Berry', 'Chern', 'Zak',
'insulator', 'edge state', 'surface state', 'chiral',
'helical', 'Majorana', 'Weyl', 'Dirac', 'nodal',
'invariant', 'winding number', 'bulk-boundary', 'symmetry'
]
text_lower = (result.title + result.summary).lower()
score = sum(1 for kw in keywords if kw in text_lower)
return score >= 3
def check_duplicate(self, resource_url: str) -> bool:
"""檢查資源是否已存在"""
try:
cursor = self.conn.cursor()
cursor.execute("""
SELECT COUNT(*) FROM physics_simulation.external_resources
WHERE source_url = %s
""", (resource_url,))
return cursor.fetchone()[0] > 0
except Exception as e:
logger.warning(f"⚠️ 查重檢查失敗: {e}")
return False
def save_to_db(self, papers: List[Dict]):
"""保存到數據庫"""
cursor = self.conn.cursor()
new_count = 0
for paper in papers:
if self.check_duplicate(paper['source_url']):
continue
try:
cursor.execute("""
INSERT INTO physics_simulation.external_resources
(resource_name, resource_type, source_url, description,
physics_category, citation_count, tags, added_date, is_verified)
VALUES (%s, %s, %s, %s, %s, %s, %s::jsonb, %s, %s)
""", (
paper['resource_name'],
paper['resource_type'],
paper['source_url'],
paper['description'],
paper['physics_category'],
0,
json.dumps(paper['tags']),
paper['published_date'],
False
))
new_count += 1
logger.info(f"✅ 保存新資源: {paper['resource_name'][:60]}...")
except Exception as e:
logger.error(f"❌ 保存失敗: {e}")
logger.info(f"新增論文: {new_count}/{len(papers)} 篇")
def run_full_collection_pipeline(self):
"""執行完整采集流程"""
logger.info("="*60)
logger.info("🚀 拓撲材料采集服務啓動")
logger.info(f"開始時間: {datetime.now().isoformat()}")
start_time = time.time()
try:
self.connect_db()
papers = self.scrape_arxiv(days_back=7)
self.save_to_db(papers)
self.save_last_collection_time()
execution_time = time.time() - start_time
logger.info(f"✅ 采集完成!總耗時: {execution_time:.2f}秒")
except Exception as e:
logger.error(f"❌ 采集流程失敗: {e}", exc_info=True)
sys.exit(1)
finally:
if self.conn:
self.conn.close()
logger.info("數據庫連接已關閉")
if __name__ == "__main__":
collector = TopologicalMaterialsCollector()
collector.run_full_collection_pipeline()
```
### 2.2 `services/fast_predictor.py`
```python
#!/usr/bin/env python3
"""
快速拓撲預測器 - 對稱性指標法
"""
import numpy as np
import json
import time
from typing import Dict, Any
import psycopg2
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
class SymmetryBasedPredictor:
"""基于對稱性指標的快速拓撲預測器"""
def __init__(self, db_config: Dict[str, Any]):
self.conn = psycopg2.connect(**db_config)
self.symmetry_db = self.load_symmetry_database()
def load_symmetry_database(self) -> Dict:
"""加載對稱性指標數據庫"""
cursor = self.conn.cursor()
cursor.execute("""
SELECT spacegroup_number, symmetry_indicators, topological_class
FROM topological_fast_prediction.symmetry_fingerprints
WHERE is_verified = TRUE
""")
mapping = {}
for sg, si_json, topo_class in cursor.fetchall():
key = f"{sg}_{json.dumps(si_json, sort_keys=True)}"
mapping[key] = topo_class
return mapping
def predict_by_symmetry(self, structure) -> Dict[str, Any]:
"""通過對稱性預測拓撲類別"""
start_time = time.time()
analyzer = SpacegroupAnalyzer(structure, symprec=0.1)
spacegroup = analyzer.get_space_group_number()
# 計算Wyckoff位置
wyckoff_positions = analyzer.get_symmetry_dataset()['wyckoffs']
band_rep = self.calculate_band_representation(wyckoff_positions)
# 計算對稱性指標 (簡化版)
symmetry_indicators = self.calculate_symmetry_indicators(spacegroup, band_rep)
# 判斷拓撲類別
topo_class = self.classify_by_symmetry_indicators(symmetry_indicators)
computation_time = (time.time() - start_time) * 1000
return {
'topo_class': topo_class,
'confidence': 0.95,
'symmetry_indicators': symmetry_indicators,
'computation_time_ms': round(computation_time, 2),
'method': 'SymmetryIndicator'
}
def calculate_band_representation(self, wyckoff_positions: list) -> Dict:
"""計算能帶表示"""
irrep_count = {}
for wp in wyckoff_positions:
irrep = self.wyckoff_to_irrep(wp)
irrep_count[irrep] = irrep_count.get(irrep, 0) + 1
return irrep_count
def wyckoff_to_irrep(self, wyckoff: str) -> str:
"""Wyckoff位置到不可約表示的映射"""
mapping = {'a': 'A1', 'b': 'A2', 'c': 'B1', 'd': 'B2', 'e': 'E'}
return mapping.get(wyckoff[-1], 'A1')
def calculate_symmetry_indicators(self, sg: int, band_rep: Dict) -> Dict:
"""計算對稱性指標"""
# 簡化的對稱性指標計算
z2 = [sg % 2, (sg // 2) % 2, (sg // 4) % 2]
z4 = sg % 4
z8 = sg % 8
return {
'z2': z2,
'z4': z4,
'z8': z8,
'A': sum(band_rep.values()) % 2
}
def classify_by_symmetry_indicators(self, si: Dict) -> str:
"""根據對稱性指標分類"""
if si.get('z2', [0,0,0]) != [0,0,0]:
return 'Topological Insulator'
if si.get('A', 0) == 1:
return 'Axion Insulator'
if si.get('z4', 0) % 2 == 1:
return 'Topological Crystalline Insulator'
return 'Trivial'
```
### 2.3 `services/gnn_predictor.py`
```python
#!/usr/bin/env python3
"""
圖神經網絡拓撲預測器
"""
import torch
import torch_geometric
from torch_geometric.nn import GATConv, global_mean_pool
from pymatgen import Structure
import numpy as np
class CrystalGraphTransformer(torch.nn.Module):
"""晶體圖神經網絡"""
def __init__(self, input_dim=128, hidden_dim=256, num_classes=5):
super().__init__()
self.atom_embedding = torch.nn.Embedding(118, input_dim)
self.conv1 = GATConv(input_dim, hidden_dim, heads=4, dropout=0.1)
self.conv2 = GATConv(hidden_dim * 4, hidden_dim, heads=4, dropout=0.1)
self.conv3 = GATConv(hidden_dim * 4, hidden_dim, heads=1, dropout=0.1)
self.pool = global_mean_pool
self.classifier = torch.nn.Sequential(
torch.nn.Linear(hidden_dim, hidden_dim // 2),
torch.nn.ReLU(),
torch.nn.Dropout(0.1),
torch.nn.Linear(hidden_dim // 2, num_classes)
)
def forward(self, data):
x, edge_index, batch = data.x, data.edge_index, data.batch
x = self.atom_embedding(x)
x = torch.nn.functional.relu(self.conv1(x, edge_index))
x = torch.nn.functional.relu(self.conv2(x, edge_index))
x = self.conv3(x, edge_index)
graph_representation = self.pool(x, batch)
logits = self.classifier(graph_representation)
return logits
class GNNTopologyPredictor:
"""GNN拓撲預測器"""
def __init__(self, model_path: str, db_config: Dict):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model = CrystalGraphTransformer().to(self.device)
try:
self.model.load_state_dict(torch.load(model_path, map_location=self.device))
self.model.eval()
except FileNotFoundError:
print(f"⚠️ 模型文件未找到: {model_path},使用隨機權重")
self.db_config = db_config
def structure_to_graph(self, structure: Structure):
"""結構轉圖"""
atomic_numbers = torch.tensor([site.specie.Z for site in structure])
positions = torch.tensor([site.coords for site in structure], dtype=torch.float)
# 構建最近鄰邊
cutoff = 5.0
edge_list = []
for i, site_i in enumerate(structure):
neighbors = structure.get_neighbors(site_i, cutoff)
for neighbor in neighbors:
j = neighbor.index
edge_list.append([i, j])
edge_index = torch.tensor(edge_list, dtype=torch.long).t().contiguous()
return torch_geometric.data.Data(
x=atomic_numbers,
edge_index=edge_index,
pos=positions
)
def predict(self, structure: Structure) -> Dict[str, Any]:
"""預測拓撲類別"""
start_time = time.time()
graph = self.structure_to_graph(structure)
graph = graph.to(self.device)
graph.batch = torch.zeros(graph.x.size(0), dtype=torch.long).to(self.device)
with torch.no_grad():
logits = self.model(graph)
probs = torch.nn.functional.softmax(logits, dim=0)
pred_class_idx = torch.argmax(probs).item()
confidence = probs[pred_class_idx].item()
class_names = ['Trivial', 'TI', 'TCI', 'Weyl', 'Dirac']
computation_time = (time.time() - start_time) * 1000
return {
'topo_class': class_names[pred_class_idx],
'confidence': round(confidence, 3),
'logits': {cls: prob.item() for cls, prob in zip(class_names, probs)},
'computation_time_ms': round(computation_time, 2),
'method': 'GNN_Prediction'
}
```
### 2.4 `services/tqc_fast.py`
```python
#!/usr/bin/env python3
"""
拓撲量子化學快速預測器
"""
import numpy as np
import psycopg2
from pymatgen.symmetry.groups import SpaceGroup
class TopologicalQuantumChemistry:
"""拓撲量子化學預測器"""
def __init__(self, db_config: Dict):
self.conn = psycopg2.connect(**db_config)
self.load_basis_database()
def load_basis_database(self):
"""加載原子絕緣體基組"""
cursor = self.conn.cursor()
# 如果表不存在,創建并初始化基礎數據
cursor.execute("""
CREATE TABLE IF NOT EXISTS topological_fast_prediction.atomic_insulator_basis (
basis_id SERIAL PRIMARY KEY,
spacegroup_number INTEGER,
wyckoff_multiplicity INTEGER,
irreducible_representation JSONB,
UNIQUE(spacegroup_number, wyckoff_multiplicity)
)
""")
# 檢查是否已初始化
cursor.execute("SELECT COUNT(*) FROM topological_fast_prediction.atomic_insulator_basis")
if cursor.fetchone()[0] == 0:
self.initialize_basis_database()
def initialize_basis_database(self):
"""初始化原子絕緣體基組"""
cursor = self.conn.cursor()
# 預計算的原子絕緣體基組 (簡化版)
basis_data = [
(1, 1, '{"A1g": 1}'), # P1空間群
(2, 1, '{"Ag": 1}'), # P-1
(227, 4, '{"T2g": 1}'), # Fd-3m
(225, 4, '{"T2g": 1}'), # Fm-3m
]
for sg, mult, irrep in basis_data:
cursor.execute("""
INSERT INTO topological_fast_prediction.atomic_insulator_basis
(spacegroup_number, wyckoff_multiplicity, irreducible_representation)
VALUES (%s, %s, %s::jsonb)
ON CONFLICT DO NOTHING
""", (sg, mult, irrep))
self.conn.commit()
print("✅ 原子絕緣體基組數據庫初始化完成")
def decompose_bands(self, structure):
"""能帶分解"""
start_time = time.time()
# 獲取對稱性
analyzer = SpacegroupAnalyzer(structure)
sg = analyzer.get_space_group_number()
wyckoff_positions = analyzer.get_symmetry_dataset()['wyckoffs']
# 計算占據表示
occ_rep = self.calculate_occupied_band_rep(wyckoff_positions)
# 查基組
mult = len(wyckoff_positions)
cursor = self.conn.cursor()
cursor.execute("""
SELECT irreducible_representation
FROM topological_fast_prediction.atomic_insulator_basis
WHERE spacegroup_number = %s AND wyckoff_multiplicity = %s
""", (sg, mult))
result = cursor.fetchone()
if not result:
return {
'topo_class': 'Unknown (New Basis Required)',
'decomposable': False,
'novelty_score': 0.99,
'method': 'TQC_Decomposition'
}
basis_rep = json.loads(result[0])
decomposable, remainder = self.is_decomposable(occ_rep, basis_rep)
computation_time = (time.time() - start_time) * 1000
if decomposable:
return {
'topo_class': 'Trivial (Atomic Insulator)',
'decomposable': True,
'computation_time_ms': round(computation_time, 2),
'method': 'TQC_Decomposition'
}
else:
topo_class = 'Topological Insulator' if any('E' in str(k) for k in remainder.keys()) else 'Unknown'
return {
'topo_class': topo_class,
'decomposable': False,
'remainder': remainder,
'computation_time_ms': round(computation_time, 2),
'method': 'TQC_Decomposition'
}
def calculate_occupied_band_rep(self, wyckoff_positions: list) -> Dict:
"""計算占據能帶表示"""
occ_rep = {}
for wp in wyckoff_positions:
irrep = f"E{wp[-1]}" if wp[-1].isdigit() else "A"
occ_rep[irrep] = occ_rep.get(irrep, 0) + 2 # 假設每個位置2電子
return occ_rep
def is_decomposable(self, occ_rep: Dict, basis_rep: Dict) -> tuple:
"""判斷可分解性"""
remainder = occ_rep.copy()
for irrep, coeff in basis_rep.items():
if irrep in remainder:
remainder[irrep] -= coeff
if remainder[irrep] <= 0:
del remainder[irrep]
return len(remainder) == 0, remainder
```
### 2.5 `services/combinatorial_generator.py`
```python
#!/usr/bin/env python3
"""
組合材料生成器
"""
from itertools import product
from pymatgen import Composition
import psycopg2
class CombinatorialMaterialGenerator:
"""組合式材料生成器"""
def __init__(self, db_config: Dict):
self.conn = psycopg2.connect(**db_config)
self.topo_elements = {
'heavy_metals': ['Bi', 'Sb', 'Pb', 'Hg', 'Tl'],
'chalcogens': ['S', 'Se', 'Te', 'O'],
'transition_metals': ['Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn']
}
self.topo_templates = ["Bi2Se3", "Sb2Te3", "MnBi2Te4", "HgTe", "Cd3As2"]
def generate_by_substitution(self, base_material: str, substitution_rules: Dict) -> list:
"""通過元素替換生成新材料"""
base_comp = Composition(base_material)
new_formulas = []
for element in base_comp.elements:
if str(element) in substitution_rules:
replacements = substitution_rules[str(element)]
for replacement in replacements:
new_comp = base_comp.replace({element: replacement})
new_formulas.append(new_comp.formula)
return list(set(new_formulas))
def high_throughput_screen(self, predictors: Dict, generation_limit: int = 1000):
"""高通量篩選流程"""
import logging
logger = logging.getLogger('combinatorial')
logger.info(f"🚀 啓動高通量篩選,目標: {generation_limit} 個候選材料")
candidates_generated = 0
topological_candidates = []
for template in self.topo_templates[:3]: # 限制模板數量
substitutions = {
'Bi': self.topo_elements['heavy_metals'],
'Se': self.topo_elements['chalcogens'],
'Mn': ['Fe', 'Co']
}
new_materials = self.generate_by_substitution(template, substitutions)
for formula in new_materials[:50]: # 每個模板限制50個
try:
structure = self.build_dummy_structure(formula)
if 'gnn' in predictors:
result_gnn = predictors['gnn'].predict(structure)
if result_gnn['confidence'] > 0.8:
if 'symmetry' in predictors:
result_sym = predictors['symmetry'].predict_by_symmetry(structure)
if result_gnn['topo_class'] == result_sym['topo_class']:
topological_candidates.append({
'formula': formula,
'predicted_class': result_gnn['topo_class'],
'confidence': result_gnn['confidence'],
'discovery_method': 'Combinatorial+DualPrediction'
})
candidates_generated += 1
if candidates_generated >= generation_limit:
break
except Exception as e:
continue
if candidates_generated >= generation_limit:
break
self.save_candidates_to_db(topological_candidates)
logger.info(f"✅ 篩選完成: {len(topological_candidates)}/ {candidates_generated} 個拓撲候選材料")
return topological_candidates
def build_dummy_structure(self, formula: str):
"""構建占位結構"""
from pymatgen import Structure, Lattice
comp = Composition(formula)
# 簡單立方晶格占位
lattice = Lattice.cubic(5.0)
coords = [[0,0,0] for _ in range(len(comp))]
structure = Structure(lattice, list(comp.elements), coords)
return structure
def save_candidates_to_db(self, candidates: list):
"""保存候選材料"""
cursor = self.conn.cursor()
for candidate in candidates:
cursor.execute("""
INSERT INTO topological_fast_prediction.candidate_pool
(formula, predicted_class, discovery_method, confidence, is_high_priority)
VALUES (%s, %s, %s, %s, %s)
ON CONFLICT DO NOTHING
""", (
candidate['formula'],
candidate['predicted_class'],
candidate['discovery_method'],
candidate['confidence'],
candidate['confidence'] > 0.9
))
self.conn.commit()
```
### 2.6 `services/high_throughput_screen.py`
```python
#!/usr/bin/env python3
"""
高通量篩選主腳本
"""
import argparse
import sys
import os
import logging
sys.path.append('/opt/topo-physics/services')
from combinatorial_generator import CombinatorialMaterialGenerator
from gnn_predictor import GNNTopologyPredictor
from fast_predictor import SymmetryBasedPredictor
# 加載環境變量
from dotenv import load_dotenv
load_dotenv('/opt/topo-physics/.env')
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/opt/topo-physics/logs/high_throughput_screen.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger('high_throughput_screen')
def main():
parser = argparse.ArgumentParser(description='拓撲材料高通量篩選')
parser.add_argument('--limit', type=int, default=5000, help='生成候選材料數量上限')
parser.add_argument('--method', choices=['gnn', 'symmetry', 'both'], default='both')
args = parser.parse_args()
logger.info(f"🚀 啓動高通量篩選: limit={args.limit}, method={args.method}")
# 數據庫配置
DB_CONFIG = {
'host': os.getenv('DB_HOST'),
'port': os.getenv('DB_PORT'),
'database': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD')
}
# 初始化預測器
predictors = {}
if args.method in ['gnn', 'both']:
model_path = '/opt/topo-physics/models/gnn_v1.2.pth'
if os.path.exists(model_path):
predictors['gnn'] = GNNTopologyPredictor(model_path, DB_CONFIG)
else:
logger.warning(f"⚠️ GNN模型未找到: {model_path}")
if args.method in ['symmetry', 'both']:
predictors['symmetry'] = SymmetryBasedPredictor(DB_CONFIG)
# 初始化生成器
generator = CombinatorialMaterialGenerator(DB_CONFIG)
# 執行篩選
try:
candidates = generator.high_throughput_screen(predictors, generation_limit=args.limit)
logger.info(f"✅ 篩選完成: 發現 {len(candidates)} 個拓撲候選材料")
# 輸出TOP候選
for i, cand in enumerate(candidates[:5]):
logger.info(f"TOP-{i+1}: {cand['formula']} ({cand['predicted_class']}, 置信度: {cand['confidence']:.3f})")
except Exception as e:
logger.error(f"❌ 篩選失敗: {e}", exc_info=True)
sys.exit(1)
if __name__ == '__main__':
main()
```
---
## 3️⃣ 數據庫模式文件
### 3.1 `schema/physics_simulation.sql`
```sql
-- ========================================
-- 物理模藕通用模式
-- ========================================
-- 外部資源庫 (GitHub, arXiv, 3D模型)
CREATE TABLE IF NOT EXISTS physics_simulation.external_resources (
resource_id SERIAL PRIMARY KEY,
resource_name VARCHAR(300) NOT NULL,
resource_type VARCHAR(50),
source_url TEXT UNIQUE,
description TEXT,
physics_category VARCHAR(100),
simulation_dimensions INTEGER,
code_language VARCHAR(50),
license_type VARCHAR(50),
stars_count INTEGER DEFAULT 0,
citation_count INTEGER DEFAULT 0,
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_verified_date TIMESTAMP,
is_verified BOOLEAN DEFAULT FALSE,
tags JSONB,
uploader_id VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 3D模型資源表
CREATE TABLE IF NOT EXISTS physics_simulation.model_3d_assets (
model_id SERIAL PRIMARY KEY,
resource_id INTEGER REFERENCES physics_simulation.external_resources(resource_id),
model_url TEXT,
file_format VARCHAR(20),
thumbnail_url TEXT,
vertex_count INTEGER,
file_size_mb FLOAT,
physics_properties JSONB,
ar_compatible BOOLEAN DEFAULT FALSE,
vr_compatible BOOLEAN DEFAULT FALSE
);
-- GIN索引
CREATE INDEX IF NOT EXISTS idx_resource_tags ON physics_simulation.external_resources
USING GIN (tags jsonb_ops);
CREATE INDEX IF NOT EXISTS idx_resource_category ON physics_simulation.external_resources(physics_category);
-- 物化視圖
CREATE MATERIALIZED VIEW IF NOT EXISTS physics_simulation.resource_stats_mv AS
SELECT
physics_category,
COUNT(*) as total_resources,
AVG(stars_count) as avg_stars,
AVG(citation_count) as avg_citations,
array_agg(DISTINCT code_language) as languages
FROM physics_simulation.external_resources
WHERE is_verified = TRUE
GROUP BY physics_category;
```
### 3.2 `schema/topological_physics.sql`
```sql
-- ========================================
-- 拓撲量子物態模塊
-- ========================================
-- 拓撲材料模型庫
CREATE TABLE IF NOT EXISTS topological_physics.material_models (
model_id SERIAL PRIMARY KEY,
model_name VARCHAR(200) NOT NULL,
model_type VARCHAR(50),
dimension INTEGER,
description TEXT,
hamiltonian_formula TEXT,
parameters JSONB,
topological_invariant JSONB,
created_by VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_verified BOOLEAN DEFAULT FALSE
);
-- Berry相位計算歷史
CREATE TABLE IF NOT EXISTS topological_physics.berry_calculations (
calculation_id SERIAL PRIMARY KEY,
user_name VARCHAR(50),
model_id INTEGER REFERENCES topological_physics.material_models(model_id),
k_path JSONB,
berry_phase DOUBLE PRECISION,
berry_curvature JSONB,
chern_number INTEGER,
winding_number INTEGER,
calculation_time_ms INTEGER,
result_status VARCHAR(20),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 拓撲相變點
CREATE TABLE IF NOT EXISTS topological_physics.phase_transitions (
transition_id SERIAL PRIMARY KEY,
model_id INTEGER REFERENCES topological_physics.material_models(model_id),
parameter_name VARCHAR(50),
critical_value DOUBLE PRECISION,
gap_closing_point JSONB,
topological_phase_before VARCHAR(50),
topological_phase_after VARCHAR(50),
edge_states_count INTEGER,
UNIQUE(model_id, parameter_name, critical_value)
);
-- 邊緣態觀測
CREATE TABLE IF NOT EXISTS topological_physics.edge_states (
observation_id SERIAL PRIMARY KEY,
calculation_id INTEGER REFERENCES topological_physics.berry_calculations(calculation_id),
edge_type VARCHAR(20),
energy_level DOUBLE PRECISION,
wavefunction JSONB,
localization_length DOUBLE PRECISION,
spin_polarization DOUBLE PRECISION
);
-- 用戶自定義材料
CREATE TABLE IF NOT EXISTS topological_physics.custom_materials (
material_id SERIAL PRIMARY KEY,
user_name VARCHAR(50),
base_model_id INTEGER REFERENCES topological_physics.material_models(model_id),
custom_parameters JSONB,
custom_hamiltonian TEXT,
calculation_results JSONB,
is_public BOOLEAN DEFAULT FALSE,
forked_from INTEGER,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 原子絕緣體基組表
CREATE TABLE IF NOT EXISTS topological_physics.atomic_insulator_basis (
basis_id SERIAL PRIMARY KEY,
spacegroup_number INTEGER,
wyckoff_multiplicity INTEGER,
irreducible_representation JSONB,
UNIQUE(spacegroup_number, wyckoff_multiplicity)
);
-- GIN索引
CREATE INDEX IF NOT EXISTS idx_topo_material_params ON topological_physics.material_models USING GIN (parameters);
CREATE INDEX IF NOT EXISTS idx_berry_calc_user ON topological_physics.berry_calculations(user_name, created_at);
CREATE INDEX IF NOT EXISTS idx_phase_transition_model ON topological_physics.phase_transitions(model_id, critical_value);
```
### 3.3 `schema/topological_fast_prediction.sql`
```sql
-- ========================================
-- 快速拓撲預測模塊
-- ========================================
-- 對稱性指紋表
CREATE TABLE IF NOT EXISTS topological_fast_prediction.symmetry_fingerprints (
material_id SERIAL PRIMARY KEY,
mp_id VARCHAR(20) UNIQUE,
icsd_id VARCHAR(20),
formula VARCHAR(100),
spacegroup_number INTEGER,
symmetry_indicators JSONB,
wannier_center_charges JSONB,
band_representation JSONB,
topological_class VARCHAR(50),
computation_time_ms INTEGER,
data_source VARCHAR(50),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_verified BOOLEAN DEFAULT FALSE
);
-- 結構描述符表
CREATE TABLE IF NOT EXISTS topological_fast_prediction.structural_descriptors (
descriptor_id SERIAL PRIMARY KEY,
material_id INTEGER REFERENCES topological_fast_prediction.symmetry_fingerprints(material_id),
composition_vector JSONB,
coordination_stats JSONB,
bond_stats JSONB,
point_group VARCHAR(10),
wyckoff_positions JSONB,
UNIQUE(material_id)
);
-- ML預測結果緩存
CREATE TABLE IF NOT EXISTS topological_fast_prediction.ml_predictions (
prediction_id SERIAL PRIMARY KEY,
material_id INTEGER REFERENCES topological_fast_prediction.symmetry_fingerprints(material_id),
model_name VARCHAR(50),
prediction_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
predicted_topo_class VARCHAR(50),
confidence_score FLOAT,
logits JSONB,
expires_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + INTERVAL '30 days'
);
-- 候選材料池
CREATE TABLE IF NOT EXISTS topological_fast_prediction.candidate_pool (
candidate_id SERIAL PRIMARY KEY,
formula VARCHAR(100),
predicted_class VARCHAR(50),
discovery_method VARCHAR(50),
confidence FLOAT,
structure_preview TEXT,
is_experimentally_verified BOOLEAN DEFAULT FALSE,
is_high_priority BOOLEAN DEFAULT FALSE,
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
notes TEXT
);
-- 材料組合生成器
CREATE TABLE IF NOT EXISTS topological_fast_prediction.material_generator (
generation_id SERIAL PRIMARY KEY,
base_elements TEXT[],
generated_formulas TEXT[],
generation_strategy VARCHAR(50),
total_generated INTEGER,
valid_candidates INTEGER,
generation_time INTERVAL
);
-- GIN索引
CREATE INDEX IF NOT EXISTS idx_symmetry_indicators ON topological_fast_prediction.symmetry_fingerprints USING GIN (symmetry_indicators);
CREATE INDEX IF NOT EXISTS idx_ml_prediction_model ON topological_fast_prediction.ml_predictions(model_name, expires_at);
CREATE INDEX IF NOT EXISTS idx_candidate_confidence ON topological_fast_prediction.candidate_pool(confidence) WHERE is_high_priority = TRUE;
```
---
## 4️⃣ systemd配置
### 4.1 `systemd/topo.service`
```ini
[Unit]
Description=Topological Materials Data Collector Service
Documentation=https://github.com/your-org/topo-physics
After=network.target postgresql.service
Requires=postgresql.service
[Service]
Type=oneshot
User=topo-service
Group=topo-service
WorkingDirectory=/opt/topo-physics
Environment="PATH=/opt/topo-physics/venv/bin:/usr/local/bin:/usr/bin:/bin"
EnvironmentFile=/opt/topo-physics/.env
# 安全設置
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=/opt/topo-physics/logs
ReadWritePaths=/opt/topo-physics/data
# 資源限制
CPUQuota=200%
MemoryMax=4G
# 執行命令
ExecStart=/opt/topo-physics/venv/bin/python /opt/topo-physics/services/topo_collector.py
# 日志
StandardOutput=journal
StandardError=journal
SyslogIdentifier=topo-collector
[Install]
WantedBy=multi-user.target
```
### 4.2 `systemd/topo.timer`
```ini
[Unit]
Description=Topological Materials Data Collection Timer
Documentation=https://github.com/your-org/topo-physics
Requires=topo.service
[Timer]
# 每天凌晨2點執行采集
OnCalendar=*-*-* 02:00:00
# 每周日凌晨執行全量采集
OnCalendar=Sun *-*-* 00:00:00
# 啓動後5分鍾執行一次
OnBootSec=5min
# 持久化計時器
Persistent=true
# 隨機延遲避免同時執行
RandomizedDelaySec=15min
[Install]
WantedBy=timers.target
```
### 4.3 `systemd/topo_screen.service`
```ini
[Unit]
Description=Topological Materials Daily Screening
After=topo.service
Requires=topo.service
[Service]
Type=oneshot
User=topo-service
Group=topo-service
WorkingDirectory=/opt/topo-physics
Environment="PATH=/opt/topo-physics/venv/bin:/usr/local/bin:/usr/bin:/bin"
EnvironmentFile=/opt/topo-physics/.env
# 執行高通量篩選
ExecStart=/opt/topo-physics/venv/bin/python /opt/topo-physics/services/high_throughput_screen.py --limit=10000
[Install]
WantedBy=multi-user.target
```
---
## 5️⃣ Streamlit前端 `frontend/main_app.py`
```python
#!/usr/bin/env python3
"""
拓撲量子物態與快速材料預測 - Streamlit主界面
"""
import streamlit as st
import sys
import os
# 添加服務路徑
sys.path.append('/opt/topo-physics/services')
import topological_simulator
import fast_prediction_ui
def main():
st.set_page_config(
page_title="拓撲量子物態 & 快速材料預測平台",
page_icon="🧬",
layout="wide",
initial_sidebar_state="expanded"
)
# 頂部標題
st.title("🔬 拓撲量子物態與快速材料預測平台")
st.markdown(
"""
<style>
.stApp { background-color: #fafafa; }
.main-header { color: #1f77b4; font-weight: bold; }
</style>
""",
unsafe_allow_html=True
)
# 側邊欄導航
with st.sidebar:
st.image("https://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/Toroidal_coord.png/300px-Toroidal_coord.png", width=150)
st.markdown("### 🧭 導航")
app_mode = st.selectbox(
"選擇模塊",
["拓撲量子物態模藕器", "快速材料預測器", "數據中心", "系統狀態"]
)
st.markdown("---")
# 快速鏈接
st.markdown("### 🔗 快速鏈接")
if st.button("查看文檔"):
st.info("訪問: https://github.com/your-org/topo-physics")
if st.button("報告問題"):
st.info("提交到: issues@topo-physics.org")
# 主內容區
if app_mode == "拓撲量子物態模藕器":
topological_simulator.render()
elif app_mode == "快速材料預測器":
fast_prediction_ui.render()
elif app_mode == "數據中心":
st.header("📊 數據中心")
col1, col2, col3 = st.columns(3)
with col1:
st.metric("總材料數", "15,234")
st.metric("拓撲材料", "2,341")
with col2:
st.metric("今日新增", "23")
st.metric("待驗證候選", "156")
with col3:
st.metric("成功率", "89.2%")
st.metric("平均預測時間", "15 ms")
st.markdown("---")
# 最近預測記錄
st.subheader("最近預測記錄")
st.dataframe([
{"材料": "Bi2Se3", "預測類別": "TI", "置信度": 0.95, "耗時": "12 ms"},
{"材料": "MnBi2Te4", "預測類別": "磁性TI", "置信度": 0.88, "耗時": "18 ms"},
{"材料": "Cu3Zn", "預測類別": "Trivial", "置信度": 0.92, "耗時": "11 ms"}
])
elif app_mode == "系統狀態":
st.header("🔧 系統狀態")
col1, col2 = st.columns(2)
with col1:
st.subheader("服務狀態")
import subprocess
try:
status = subprocess.check_output(['systemctl', 'is-active', 'topo.timer']).decode().strip()
st.status(label="數據采集定時器", state="complete" if status == "active" else "error")
except:
st.status(label="數據采集定時器", state="error")
with col2:
st.subheader("數據庫狀態")
import psycopg2
try:
conn = psycopg2.connect(
host='localhost',
database=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)
st.success("✅ 數據庫連接正常")
except Exception as e:
st.error(f"❌ 數據庫連接失敗: {e}")
# 日志查看器
st.subheader("📝 最近日志")
try:
with open('/opt/topo-physics/logs/topo-collector.log', 'r') as f:
logs = f.readlines()[-20:]
st.code("".join(logs), language='log')
except:
st.info("暫無日志")
if __name__ == "__main__":
main()
```
### 5.2 `frontend/topological_simulator.py`
```python
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
def render():
st.header("拓撲量子物態模藕器")
# 選擇模型
model_type = st.selectbox(
"選擇拓撲模型",
["SSH一維鏈", "Haldane模型", "Kitaev鏈", "Qi-Wu-Zhang模型"]
)
if model_type == "SSH一維鏈":
render_ssh_simulator()
def render_ssh_simulator():
st.subheader("Su-Schrieffer-Heeger (SSH) 模型")
col1, col2 = st.columns([1, 2])
with col1:
v = st.slider("胞內躍遷 v (eV)", 0.1, 2.0, 1.0, 0.1)
w = st.slider("胞間躍遷 w (eV)", 0.1, 2.0, 1.5, 0.1)
N = st.slider("原胞數量", 10, 100, 50, 5)
if st.button("🔍 計算能帶與拓撲不變量"):
with st.spinner("計算中..."):
result = calculate_ssh(v, w, N)
st.success("計算完成!")
display_ssh_results(result, col2)
def calculate_ssh(v, w, N):
"""SSH模型計算"""
k_points = np.linspace(-np.pi, np.pi, 200)
energies = np.zeros((len(k_points), 2))
for i, k in enumerate(k_points):
h_x = v + w * np.cos(k)
h_y = w * np.sin(k)
magnitude = np.sqrt(h_x**2 + h_y**2)
energies[i, 0] = -magnitude
energies[i, 1] = magnitude
# 計算Zak相位
zak_phase = np.pi if abs(w) > abs(v) else 0
return {
'k': k_points,
'energies': energies,
'zak_phase': zak_phase,
'winding_number': 1 if abs(w) > abs(v) else 0,
'is topological': abs(w) > abs(v)
}
def display_ssh_results(result, col):
with col:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
# 能帶圖
ax1.plot(result['k'], result['energies'][:, 0], 'b-', label='價帶')
ax1.plot(result['k'], result['energies'][:, 1], 'r-', label='導帶')
ax1.set_xlabel("波矢 k")
ax1.set_ylabel("能量 (eV)")
ax1.set_title("SSH模型能帶")
ax1.legend()
ax1.grid(True, alpha=0.3)
# h(k)軌迹
h_x = 1 + 1.5 * np.cos(result['k'])
h_y = 1.5 * np.sin(result['k'])
ax2.plot(h_x, h_y, 'g-', linewidth=2)
ax2.plot(0, 0, 'r*', markersize=20)
ax2.set_xlabel("h_x(k)")
ax2.set_ylabel("h_y(k)")
ax2.set_title("哈密頓量矢量軌迹")
ax2.grid(True, alpha=0.3)
ax2.set_aspect('equal')
st.pyplot(fig)
# 顯示拓撲不變量
col3, col4 = st.columns(2)
with col3:
st.metric("Zak相位", f"{result['zak_phase']:.3f} π")
with col4:
st.metric("Winding Number", result['winding_number'])
if result['is topological']:
st.success("🎉 **拓撲非平凡相!** 存在受拓撲保護的邊緣態")
else:
st.info("📊 **拓撲平凡相** 無邊緣態")
```
### 5.3 `frontend/fast_prediction_ui.py`
```python
import streamlit as st
import numpy as np
from pymatgen import Structure
import os
def render():
st.header("⚡ 快速拓撲材料預測")
st.markdown("基于對稱性指標、GNN和拓撲量子化學,**無需DFT計算**,秒級預測拓撲性質")
# 方法選擇
method = st.selectbox(
"選擇預測方法",
["對稱性指標法 (SymTopo)", "圖神經網絡 (GNN)", "拓撲量子化學 (TQC)"]
)
# 輸入方式
input_method = st.radio("輸入材料", ["上傳晶體文件", "輸入化學式", "Materials Project ID"])
structure = None
if input_method == "上傳晶體文件":
uploaded_file = st.file_uploader("上傳CIF/POSCAR文件", type=['cif', 'poscar', 'vasp'])
if uploaded_file:
content = uploaded_file.getvalue().decode()
try:
structure = Structure.from_str(content, fmt='cif')
except:
structure = Structure.from_str(content, fmt='poscar')
elif input_method == "輸入化學式":
formula = st.text_input("化學式 (e.g., Bi2Se3)", "Bi2Se3")
if formula and st.button("從MP獲取結構"):
structure = fetch_from_mp(formula)
elif input_method == "Materials Project ID":
mp_id = st.text_input("MP ID", "mp-541837")
if mp_id and st.button("獲取結構"):
structure = fetch_mp_by_id(mp_id)
# 預測執行
if structure and st.button("🔮 預測拓撲性質"):
with st.spinner("正在快速計算..."):
predictor = create_predictor(method)
result = predictor.predict(structure)
col1, col2 = st.columns(2)
with col1:
st.metric("拓撲類別", result['topo_class'])
st.metric("置信度", f"{result['confidence']:.3f}")
with col2:
st.metric("計算耗時", f"{result['computation_time_ms']} ms")
st.metric("方法", result['method'])
if result['topo_class'] != 'Trivial':
st.success("🎉 發現潛在拓撲材料!")
else:
st.info("此材料預測爲拓撲平凡")
# 保存到歷史
save_prediction(structure, result)
def create_predictor(method_name: str):
"""創建預測器實例"""
DB_CONFIG = {
'host': os.getenv('DB_HOST'),
'database': os.getenv('DB_NAME'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD')
}
if method_name == "對稱性指標法 (SymTopo)":
from services.fast_predictor import SymmetryBasedPredictor
return SymmetryBasedPredictor(DB_CONFIG)
elif method_name == "圖神經網絡 (GNN)":
from services.gnn_predictor import GNNTopologyPredictor
return GNNTopologyPredictor('/opt/topo-physics/models/gnn_v1.2.pth', DB_CONFIG)
elif method_name == "拓撲量子化學 (TQC)":
from services.tqc_fast import TopologicalQuantumChemistry
return TopologicalQuantumChemistry(DB_CONFIG)
def fetch_from_mp(formula: str):
"""從Materials Project獲取結構"""
import requests
api_key = os.getenv('MATERIALS_PROJECT_API_KEY')
try:
response = requests.get(
f'https://materialsproject.org/rest/v2/query',
params={'formula': formula, 'API_KEY': api_key}
)
data = response.json()['response'][0]
from pymatgen import Structure
return Structure.from_dict(data['structure'])
except Exception as e:
st.error(f"獲取失敗: {e}")
return None
def fetch_mp_by_id(mp_id: str):
"""通過MP ID獲取結構"""
import requests
api_key = os.getenv('MATERIALS_PROJECT_API_KEY')
try:
response = requests.get(
f'https://materialsproject.org/rest/v2/materials/{mp_id}/vasp',
params={'API_KEY': api_key}
)
data = response.json()['response'][0]
from pymatgen import Structure
return Structure.from_dict(data['structure'])
except Exception as e:
st.error(f"獲取失敗: {e}")
return None
def save_prediction(structure, result):
"""保存預測結果到數據庫"""
import psycopg2
import json
try:
conn = psycopg2.connect(
host=os.getenv('DB_HOST'),
database=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)
cursor = conn.cursor()
cursor.execute("""
INSERT INTO topological_fast_prediction.symmetry_fingerprints
(formula, spacegroup_number, topological_class, computation_time_ms, data_source)
VALUES (%s, %s, %s, %s, %s)
""", (
structure.formula,
structure.get_space_group_info()[1],
result['topo_class'],
result['computation_time_ms'],
'streamlit_ui'
))
conn.commit()
conn.close()
st.success("✅ 預測結果已保存到數據庫")
except Exception as e:
st.warning(f"⚠️ 保存失敗: {e}")
```
---
## 6️⃣ 環境變量模板 `.env.template`
```bash
# 拓撲量子物態平台環境變量配置
# 復制此文件爲 .env 并填寫實際值
# ========================================
# PostgreSQL數據庫配置
# ========================================
DB_HOST=localhost
DB_PORT=5432
DB_NAME=topo_physics_db
DB_USER=topo_admin
DB_PASSWORD=ChangeThisSecurePassword123!
# 只讀用戶 (前端使用)
DB_USER_READONLY=topo_reader
DB_PASSWORD_READONLY=ChangeThisReadOnlyPassword456!
# ========================================
# API密鑰配置
# ========================================
# Materials Project API密鑰 (從 https://materialsproject.org/dashboard 獲取)
MATERIALS_PROJECT_API_KEY=your_materials_project_api_key_here
# GitHub個人訪問令牌 (用于爬取GitHub資源)
GITHUB_TOKEN=your_github_token_here
# arXiv API密鑰 (可選)
ARXIV_API_KEY=your_arxiv_api_key_here
# ========================================
# 文件路徑配置
# ========================================
# GNN模型文件路徑
GNN_MODEL_PATH=/opt/topo-physics/models/gnn_v1.2.pth
# 數據存儲路徑
DATA_DIR=/opt/topo-physics/data
LOG_DIR=/opt/topo-physics/logs
# ========================================
# 服務配置
# ========================================
# 運行用戶
SERVICE_USER=topo-service
# 日志級別 (DEBUG, INFO, WARNING, ERROR)
LOG_LEVEL=INFO
# 采集配置
ARXIV_DAYS_BACK=7
COLLECTION_BATCH_SIZE=100
# 預測配置
PREDICTION_METHOD=both # symmetry, gnn, both
MIN_PREDICTION_CONFIDENCE=0.8
# ========================================
# Streamlit前端配置
# ========================================
STREAMLIT_PORT=8501
STREAMLIT_SERVER_ADDRESS=0.0.0.0
STREAMLIT_SERVER_HEADLESS=true
# ========================================
# 系統資源限制
# ========================================
# CPU配額 (百分比)
CPU_QUOTA=200
# 內存限制 (如 4G, 8G)
MEMORY_MAX=4G
# 任務超時時間 (秒)
TASK_TIMEOUT=3600
# ========================================
# 通知配置 (可選)
# ========================================
# Slack Webhook (用于告警)
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK
# 郵箱SMTP配置 (用于郵件通知)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASSWORD=your_app_password
ALERT_EMAIL=admin@your-domain.com
```
---
## 7️⃣ 快速啓動指南
### 一次性部署命令
```bash
# 1. 下載部署包
git clone https://github.com/your-org/topo-physics-deployment.git
cd topo-physics-deployment
# 2. 配置環境變量
cp .env.template .env
nano .env # 填寫API密鑰和密碼
# 3. 運行一鍵安裝
sudo bash install.sh
# 4. 檢查狀態
sudo systemctl status topo.timer
sudo systemctl status topo-frontend.service
# 5. 訪問系統
curl http://localhost:8501
# 6. 查看日志
sudo journalctl -u topo.service -f
sudo journalctl -u topo-frontend.service -f
```
### 日常管理命令
```bash
# 查看定時器狀態
sudo systemctl list-timers topo.timer
# 手動觸發采集
sudo systemctl start topo.service
# 重啓前端
sudo systemctl restart topo-frontend.service
# 查看數據庫狀態
sudo -u postgres psql -d topo_physics_db -c "\dt+"
# 導出數據備份
sudo -u postgres pg_dump topo_physics_db > topo_backup_$(date +%Y%m%d).sql
```
---
## 8️⃣ 系統特性總結
| 組件 | 技術棧 | 核心功能 | 性能指標 |
|------|--------|----------|----------|
| **數據庫** | PostgreSQL + JSONB | 存儲拓撲指紋和預測結果 | 支持百萬級材料 |
| **采集服務** | Python + arXiv API | 自動爬取最新研究 | 每日更新 |
| **預測引擎** | Symmetry + GNN + TQC | 秒級拓撲預測 | <100ms/材料 |
| **高通量篩選** | 組合生成 + 并行預測 | 每日篩選10,000+候選 | 3-5分鍾/批次 |
| **前端界面** | Streamlit | 可視化交互 | 響應式Web應用 |
| **自動化** | systemd timer | 7×24小時無人值守 | 99.9%可用性 |
| **監控告警** | journald + logrotate | 日志輪轉與監控 | 30天日志保留 |
---
## 9️⃣ 後續擴展建議
### 9.1 集成到HPC集群
```bash
# 修改高通量篩選服務以支持Slurm
# 在 topo_screen.service 中:
ExecStart=/usr/bin/sbatch /opt/topo-physics/scripts/slurm_screen.sh
```
### 9.2 添加實驗驗證模塊
```python
# 在 candidate_pool 表中添加實驗數據鏈接
# 集成到材料生長和表征數據庫 (MPContribs)
```
### 9.3 部署到公有云
```yaml
# docker-compose.yml 版本
# 支持Kubernetes部署
```
---
**恭喜你!現在擁有一個完全自動化的拓撲材料研究平台,從數據采集到快速預測,再到可視化交互,全流程覆蓋。祝你在拓撲量子物態研究中取得突破!**
|