pywfn 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pywfn might be problematic. Click here for more details.

Files changed (84) hide show
  1. pywfn-0.1.0/PKG-INFO +71 -0
  2. pywfn-0.1.0/README.md +64 -0
  3. pywfn-0.1.0/pyproject.toml +11 -0
  4. pywfn-0.1.0/pywfn/__init__.py +15 -0
  5. pywfn-0.1.0/pywfn/atomprop/__init__.py +45 -0
  6. pywfn-0.1.0/pywfn/atomprop/activity.py +143 -0
  7. pywfn-0.1.0/pywfn/atomprop/charge.py +216 -0
  8. pywfn-0.1.0/pywfn/atomprop/delProps.py +96 -0
  9. pywfn-0.1.0/pywfn/atomprop/dirProps.py +101 -0
  10. pywfn-0.1.0/pywfn/atomprop/direction.py +107 -0
  11. pywfn-0.1.0/pywfn/atomprop/energy.py +39 -0
  12. pywfn-0.1.0/pywfn/atomprop/freeValence.py +59 -0
  13. pywfn-0.1.0/pywfn/atomprop/lutils.py +56 -0
  14. pywfn-0.1.0/pywfn/atomprop/piProps.py +46 -0
  15. pywfn-0.1.0/pywfn/atomprop/spin.py +53 -0
  16. pywfn-0.1.0/pywfn/base/__init__.py +3 -0
  17. pywfn-0.1.0/pywfn/base/atom.py +272 -0
  18. pywfn-0.1.0/pywfn/base/bond.py +91 -0
  19. pywfn-0.1.0/pywfn/base/mol.py +332 -0
  20. pywfn-0.1.0/pywfn/bondprop/__init__.py +12 -0
  21. pywfn-0.1.0/pywfn/bondprop/bondDirect.py +31 -0
  22. pywfn-0.1.0/pywfn/bondprop/bondOrder.py +112 -0
  23. pywfn-0.1.0/pywfn/bondprop/lutils.py +117 -0
  24. pywfn-0.1.0/pywfn/bondprop/piDH.py +87 -0
  25. pywfn-0.1.0/pywfn/bondprop/piDM.py +99 -0
  26. pywfn-0.1.0/pywfn/bondprop/piSH.py +28 -0
  27. pywfn-0.1.0/pywfn/bondprop/piSM.py +52 -0
  28. pywfn-0.1.0/pywfn/bondprop/selectPI.py +13 -0
  29. pywfn-0.1.0/pywfn/chemProp.py +24 -0
  30. pywfn-0.1.0/pywfn/config.py +46 -0
  31. pywfn-0.1.0/pywfn/consts.py +7 -0
  32. pywfn-0.1.0/pywfn/data/__init__.py +14 -0
  33. pywfn-0.1.0/pywfn/data/basis.py +126 -0
  34. pywfn-0.1.0/pywfn/data/elements.py +53 -0
  35. pywfn-0.1.0/pywfn/data/radDens.py +2310 -0
  36. pywfn-0.1.0/pywfn/data/sphGrid.py +13914 -0
  37. pywfn-0.1.0/pywfn/data/temps.py +139 -0
  38. pywfn-0.1.0/pywfn/define.py +14 -0
  39. pywfn-0.1.0/pywfn/maths/__init__.py +248 -0
  40. pywfn-0.1.0/pywfn/maths/atom.py +56 -0
  41. pywfn-0.1.0/pywfn/maths/flib.py +89 -0
  42. pywfn-0.1.0/pywfn/maths/gto.py +161 -0
  43. pywfn-0.1.0/pywfn/maths/mol.py +16 -0
  44. pywfn-0.1.0/pywfn/maths/unit.py +87 -0
  45. pywfn-0.1.0/pywfn/molprop/__init__.py +82 -0
  46. pywfn-0.1.0/pywfn/molprop/aromaticity.py +19 -0
  47. pywfn-0.1.0/pywfn/molprop/obtEnergy.py +37 -0
  48. pywfn-0.1.0/pywfn/multiprop/__init__.py +59 -0
  49. pywfn-0.1.0/pywfn/multiprop/dirFukuiFun.py +19 -0
  50. pywfn-0.1.0/pywfn/multiprop/fukuiFun.py +22 -0
  51. pywfn-0.1.0/pywfn/multiprop/neEnergy.py +23 -0
  52. pywfn-0.1.0/pywfn/multiprop/parrFun.py +3 -0
  53. pywfn-0.1.0/pywfn/reader/__init__.py +35 -0
  54. pywfn-0.1.0/pywfn/reader/fch.py +93 -0
  55. pywfn-0.1.0/pywfn/reader/gjf.py +46 -0
  56. pywfn-0.1.0/pywfn/reader/log.py +511 -0
  57. pywfn-0.1.0/pywfn/reader/lutils.py +117 -0
  58. pywfn-0.1.0/pywfn/reader/mol.py +34 -0
  59. pywfn-0.1.0/pywfn/reader/xyz.py +37 -0
  60. pywfn-0.1.0/pywfn/shell.py +537 -0
  61. pywfn-0.1.0/pywfn/spaceProp/wfn.py +78 -0
  62. pywfn-0.1.0/pywfn/tools/__init__.py +3 -0
  63. pywfn-0.1.0/pywfn/tools/engCorr.py +43 -0
  64. pywfn-0.1.0/pywfn/tools/fileCreater.py +31 -0
  65. pywfn-0.1.0/pywfn/tools/getPES.py +215 -0
  66. pywfn-0.1.0/pywfn/tools/showCdxml.py +3 -0
  67. pywfn-0.1.0/pywfn/tools/siExtract.py +84 -0
  68. pywfn-0.1.0/pywfn/tools/spiltScan.py +84 -0
  69. pywfn-0.1.0/pywfn/tools/splitIrc.py +63 -0
  70. pywfn-0.1.0/pywfn/tools/splitLink.py +28 -0
  71. pywfn-0.1.0/pywfn/tools/writer.py +23 -0
  72. pywfn-0.1.0/pywfn/utils.py +218 -0
  73. pywfn-0.1.0/pywfn/writer/__init__.py +4 -0
  74. pywfn-0.1.0/pywfn/writer/cub.py +100 -0
  75. pywfn-0.1.0/pywfn/writer/gjf.py +46 -0
  76. pywfn-0.1.0/pywfn/writer/lutils.py +9 -0
  77. pywfn-0.1.0/pywfn/writer/mol.py +46 -0
  78. pywfn-0.1.0/pywfn/writer/xyz.py +30 -0
  79. pywfn-0.1.0/pywfn.egg-info/PKG-INFO +71 -0
  80. pywfn-0.1.0/pywfn.egg-info/SOURCES.txt +82 -0
  81. pywfn-0.1.0/pywfn.egg-info/dependency_links.txt +1 -0
  82. pywfn-0.1.0/pywfn.egg-info/entry_points.txt +2 -0
  83. pywfn-0.1.0/pywfn.egg-info/top_level.txt +1 -0
  84. pywfn-0.1.0/setup.cfg +4 -0
pywfn-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,71 @@
1
+ Metadata-Version: 2.1
2
+ Name: pywfn
3
+ Version: 0.1.0
4
+ Summary: A Python library for working with wavefunctions
5
+ Author-email: Xiaofei Shi <xiaofei911@gmial.com>
6
+ Description-Content-Type: text/markdown
7
+
8
+ # `pywfn` -- 基于python的波函数分析工具
9
+
10
+ 详情请查看 https://www.xiaofei911.top/mkdocs/pywfn/
11
+
12
+
13
+ ## 安装
14
+ 1. 创建虚拟环境
15
+ ```
16
+ python -m venv venv
17
+ ```
18
+ 2. 运行环境
19
+ ```
20
+ .\venv\Scripts\activate
21
+ ```
22
+ 3. 安装依赖
23
+ ```
24
+ pip install -r reqs.txt
25
+ ```
26
+ 4. 运行项目
27
+ ```
28
+ python main.py
29
+ ```
30
+
31
+ ## 开发计划
32
+ - 能够计算原子指标
33
+ - 可以读取电荷和自旋多重度 ✓
34
+ - 将项目拆分 pywfn只负责计算(带有简单的交互)以及当作包调用 ✓
35
+ - 添加日志功能 ✓
36
+ - 支持导出cube文件
37
+ - 支持分子属性的计算
38
+ - 支持绘制势能面
39
+
40
+
41
+ ## 规范
42
+ 传参的时候尽量不要使用自定义数据类型
43
+
44
+ 1,2-6,7,8
45
+ 1
46
+
47
+ 当程序作为第模块的时候,不要打印信息,因此尽量少使用print
48
+
49
+ 代码的简化和重构也会花费大量时间呀
50
+
51
+ 1.23,4.567
52
+
53
+ ## 一些心得
54
+ - 一个模块中的所有文件中的类和函数都可以在__init__.py文件中引用
55
+ - 但是一个模块中的文件互相调用彼此的类的时候就不要使用__init__.py中的引用了
56
+ - 数据保存到py文件中,可以打包成单文件
57
+
58
+ ## nuitka打包
59
+ > 自己的电脑打包有问题,可以用虚拟机来打包,使用Hyper-V
60
+
61
+ ```
62
+ python -m nuitka --mingw64 --standalone --show-progress --output-dir=out main.py
63
+ python -m nuitka --mingw64 --standalone --show-progress --output-dir=out --windows-icon-from-ico=./ico.ico main.py
64
+ ```
65
+
66
+ ## 物理量缩写
67
+ - 轨道 obt
68
+ - 能量 eng
69
+ - 电子 ele elec
70
+ - 原子 atm atom
71
+ - 电荷 chg chrg
pywfn-0.1.0/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # `pywfn` -- 基于python的波函数分析工具
2
+
3
+ 详情请查看 https://www.xiaofei911.top/mkdocs/pywfn/
4
+
5
+
6
+ ## 安装
7
+ 1. 创建虚拟环境
8
+ ```
9
+ python -m venv venv
10
+ ```
11
+ 2. 运行环境
12
+ ```
13
+ .\venv\Scripts\activate
14
+ ```
15
+ 3. 安装依赖
16
+ ```
17
+ pip install -r reqs.txt
18
+ ```
19
+ 4. 运行项目
20
+ ```
21
+ python main.py
22
+ ```
23
+
24
+ ## 开发计划
25
+ - 能够计算原子指标
26
+ - 可以读取电荷和自旋多重度 ✓
27
+ - 将项目拆分 pywfn只负责计算(带有简单的交互)以及当作包调用 ✓
28
+ - 添加日志功能 ✓
29
+ - 支持导出cube文件
30
+ - 支持分子属性的计算
31
+ - 支持绘制势能面
32
+
33
+
34
+ ## 规范
35
+ 传参的时候尽量不要使用自定义数据类型
36
+
37
+ 1,2-6,7,8
38
+ 1
39
+
40
+ 当程序作为第模块的时候,不要打印信息,因此尽量少使用print
41
+
42
+ 代码的简化和重构也会花费大量时间呀
43
+
44
+ 1.23,4.567
45
+
46
+ ## 一些心得
47
+ - 一个模块中的所有文件中的类和函数都可以在__init__.py文件中引用
48
+ - 但是一个模块中的文件互相调用彼此的类的时候就不要使用__init__.py中的引用了
49
+ - 数据保存到py文件中,可以打包成单文件
50
+
51
+ ## nuitka打包
52
+ > 自己的电脑打包有问题,可以用虚拟机来打包,使用Hyper-V
53
+
54
+ ```
55
+ python -m nuitka --mingw64 --standalone --show-progress --output-dir=out main.py
56
+ python -m nuitka --mingw64 --standalone --show-progress --output-dir=out --windows-icon-from-ico=./ico.ico main.py
57
+ ```
58
+
59
+ ## 物理量缩写
60
+ - 轨道 obt
61
+ - 能量 eng
62
+ - 电子 ele elec
63
+ - 原子 atm atom
64
+ - 电荷 chg chrg
@@ -0,0 +1,11 @@
1
+ [project]
2
+ name="pywfn"
3
+ version="0.1.0"
4
+ description="A Python library for working with wavefunctions"
5
+ authors=[{name="Xiaofei Shi", email="xiaofei911@gmial.com"}]
6
+ readme="README.md"
7
+ [build-system]
8
+ requires = ["setuptools>=42", "wheel", "numpy","rich"]
9
+ build-backend = "setuptools.build_meta"
10
+ [project.scripts]
11
+ pywfn = "pywfn.__init__:main"
@@ -0,0 +1,15 @@
1
+ """
2
+ 基于python的波函数分析程序
3
+ """
4
+ import numpy as np
5
+ np.set_printoptions(precision=4, suppress=True)
6
+
7
+ # __version__='0.0.1'
8
+ # __description__='A wave function analysis program based on python'
9
+ # __author__='XiaoFei Shi'
10
+ # __email__='1103275712@qq.com'
11
+ # __url__='https://www.xiaofei911.top/mkdocs/pywfn/'
12
+
13
+ def main():
14
+ from pywfn.shell import Shell
15
+ Shell().homePage()
@@ -0,0 +1,45 @@
1
+ """
2
+ 该模块用来计算原子属性
3
+ Mulliken电荷
4
+ """
5
+ from pywfn.utils import printer
6
+
7
+ import numpy as np
8
+
9
+ class AtomCaler:
10
+ def __init__(self) -> None:
11
+ self.logTip:str=''
12
+
13
+ def resStr(self)->np.ndarray:pass
14
+
15
+ def logRes(self):
16
+ printer.info(self.logTip)
17
+ printer.res(self.resStr())
18
+
19
+
20
+ from pywfn.shell import Shell
21
+
22
+ def onShell(shell:Shell):
23
+ printer.info('1. 原子电荷')
24
+ printer.info('2. 原子自旋')
25
+ printer.info('3. 原子能量')
26
+ printer.info('4. 原子活性')
27
+ printer.info('5. 原子活性')
28
+ opt=printer.info('请输入对应选项:')
29
+ if opt=='':return
30
+ if opt=='1':
31
+ from pywfn.atomprop import charge
32
+ caler=charge.Calculator(shell.input.Moles()[0])
33
+ caler.onShell()
34
+ if opt=='2':
35
+ from pywfn.atomprop import spin
36
+ caler=spin.Calculator(shell.input.Moles()[0])
37
+ caler.onShell()
38
+ if opt=='3':
39
+ from pywfn.atomprop import energy
40
+ caler=energy.Calculator(shell.input.Moles()[0])
41
+ caler.onShell()
42
+ if opt=='4':
43
+ from pywfn.atomprop import activity
44
+ caler=activity.Calculator(shell.input.Moles()[0])
45
+ caler.onShell()
@@ -0,0 +1,143 @@
1
+ """
2
+ 计算原子的反应活性,福井函数、parr函数等,一般需要多个分子才行
3
+ """
4
+ from pywfn.base import Mol
5
+ from pywfn.atomprop import charge, direction, spin
6
+ from pywfn.atomprop.charge import Chrgs
7
+ import numpy as np
8
+ from pywfn.shell import Shell
9
+
10
+ class Calculator:
11
+ def __init__(self) -> None:
12
+ self.mols:list[Mol]=[]
13
+
14
+ def fukui(self,chrg:Chrgs='mulliken')->np.ndarray:
15
+ """计算所有原子的福井函数[n,2]"""
16
+ assert len(self.mols)==3,"应该有三个分子"
17
+ cals=[charge.Calculator(mol) for mol in self.mols]
18
+ natm=len(self.mols[0].atoms)
19
+ chgs=np.zeros(shape=(natm,3)) # 记录所有原子的电荷
20
+ dchg=np.zeros(shape=(natm,2)) # 记录电荷差值
21
+ for c,cal in enumerate(cals):
22
+ res=cal.calculate(chrg)
23
+ chgs[:,c]=res
24
+ dchg[:,0]=-(chgs[:,0]-chgs[:,1])
25
+ dchg[:,1]=-(chgs[:,1]-chgs[:,2])
26
+ return dchg
27
+
28
+ def parr(self,chrg:Chrgs='mulliken')->np.ndarray:
29
+ """计算所有原子的parr函数[n,2]"""
30
+ assert len(self.mols)==2,"应该有两个分子"
31
+ cals=[spin.Calculator(mol) for mol in self.mols]
32
+ natm=len(self.mols[0].atoms)
33
+ spins=np.zeros(shape=(natm,3))
34
+ for c,cal in enumerate(cals):
35
+ spins[:,c]=cal.calculate(chrg)
36
+ result=np.zeros(shape=(natm,2))
37
+ result[:,0]=spins[:,0]-spins[:,1]
38
+ result[:,1]=spins[:,1]-spins[:,2]
39
+ return spins
40
+
41
+ def delEng(self):
42
+ from pywfn.atomprop import energy
43
+ cals=[energy.Calculator(mol) for mol in self.mols]
44
+ natm=len(cals[0].mol.atoms)
45
+ engs=np.zeros(shape=(natm,3))
46
+ for c,cal in enumerate(cals):
47
+ res=cal.calculate()
48
+ engs[:,c]=res
49
+ result=np.zeros(shape=(natm,2))
50
+ result[:,0]=engs[:,0]-engs[:,1]
51
+ result[:,1]=engs[:,1]-engs[:,2]
52
+ return engs
53
+
54
+ def valence(self)->list[float]:
55
+ """计算原子化合价"""
56
+ assert len(self.mols)==1,"只能算一个分子"
57
+ from pywfn.bondprop import bondOrder
58
+ mol=self.mols[0]
59
+ caler=bondOrder.Calculator(mol)
60
+ orders=caler.mayer()
61
+ orderDict={}
62
+ for a1,a2,order in orders:
63
+ a1,a2=int(a1),int(a2)
64
+ key=f'{a1}-{a2}' if a1<a2 else f'{a2}-{a1}'
65
+ orderDict[key]=order
66
+ result=np.zeros(len(mol.atoms))
67
+ for a,atom in enumerate(mol.atoms):
68
+ a1=atom.idx
69
+ valence=0
70
+ for a2 in atom.neighbors:
71
+ key=f'{a1}-{a2}' if a1<a2 else f'{a2}-{a1}'
72
+ valence+=orderDict[key]
73
+ result[a]=valence
74
+ return result
75
+
76
+ def freeValence(self,atms:list[int]):
77
+ """计算指定原子的自由价[d,5](atm,x,y,z,val)"""
78
+ assert len(self.mols)==1,"只能算一个分子"
79
+ from pywfn.bondprop import bondOrder
80
+ mol=self.mols[0]
81
+ caler=bondOrder.Calculator(mol)
82
+ # STAND=1.6494
83
+ STAND=4.0
84
+ result=[]
85
+ for atm1 in atms:
86
+ dirs=None
87
+ orders=[]
88
+ for atm2 in mol.atom(atm1).neighbors:
89
+ bond=[atm1,atm2]
90
+ res=caler.dirMayer([bond]) #[d,4](a1,a2,x,y,z,v)
91
+ if dirs is None:dirs=res[:,2:5]
92
+ vals=res[:,5]
93
+ orders.append(vals)
94
+ orderSum=np.array(orders).sum(axis=0)
95
+ for d in range(len(dirs)):
96
+ x,y,z=dirs[d]
97
+ order=STAND-orderSum[d]
98
+ result.append([atm1,x,y,z,order])
99
+ return np.array(result)
100
+
101
+ def dirFukui(self,atms:list[int])->np.ndarray:
102
+ """计算指定原子,指定方向的福井函数[n,6](atm,x,y,z,E,N)"""
103
+ assert len(self.mols)==3,"需要三个分子"
104
+ crgs=[mol.charge for mol in self.mols]
105
+ assert crgs[0]<crgs[1]<crgs[2],"电荷顺序不符"
106
+ cals=[charge.Calculator(mol) for mol in self.mols]
107
+
108
+ vals:list[np.ndarray]=[]
109
+ dirs=None
110
+ idxs=None
111
+ for cal in cals:
112
+ res=cal.dirCharge('mulliken',atms)
113
+ if dirs is None:dirs=res[:,1:4]
114
+ if idxs is None:idxs=res[:,0]
115
+ vals.append(res[:,4])
116
+ num=len(dirs)
117
+ result=np.zeros(shape=(num,6))
118
+ result[:,0]=idxs
119
+ result[:,1:4]=dirs
120
+ result[:,4]=vals[0]-vals[1]
121
+ result[:,5]=vals[1]-vals[2]
122
+ return result
123
+
124
+
125
+ def onShell(self,shell:Shell):
126
+ from pywfn.utils import printer
127
+ opts=[
128
+ 'fukui函数',
129
+ 'parr函数',
130
+ '电荷差值',
131
+ '化合价',
132
+ '自由价',
133
+ '方向fukui函数'
134
+ ]
135
+ for i,each in enumerate(opts):
136
+ printer.info(f'{i+1}.{each}')
137
+ opt=input('请选择要计算的活性:')
138
+ if opt=='1':
139
+ self.mols=shell.input.Moles()
140
+ printer.info('请输入电荷类型:')
141
+ printer.info('1.mulliken; 2.lowdin')
142
+ chrg=input()
143
+ result=self.fukui()
@@ -0,0 +1,216 @@
1
+ from pywfn.base import Mol,Atom
2
+ import numpy as np
3
+ from pywfn.data import Elements
4
+ from functools import lru_cache
5
+ elements=Elements()
6
+ from pywfn.utils import printer
7
+ from pywfn.atomprop import lutils,AtomCaler
8
+ from pywfn import maths
9
+ from typing import Literal
10
+ from pywfn.maths import CM2PM
11
+
12
+ Chrgs=Literal['mulliken','lowdin','hirshfeld']
13
+
14
+ class Calculator(AtomCaler):
15
+ def __init__(self,mol:"Mol"):
16
+ self.logTip:str=''
17
+ self.mol=mol
18
+ self.chrg:Chrgs='mulliken'
19
+
20
+ def calculate(self,chrg:Chrgs)->np.ndarray:
21
+ if chrg=='mulliken':
22
+ return self.mulliken()
23
+ if chrg=='lowdin':
24
+ return self.lowdin()
25
+ if chrg=='hirshfeld':
26
+ return self.hirshfeld()
27
+
28
+ def mulliken(self,num:bool=False,PM:np.ndarray=None):
29
+ """
30
+ 计算目录mulliken电荷
31
+ num:是否只保留电子数
32
+ """
33
+ # 计算密度矩阵
34
+ self.logTip='Mulliken电荷分布'
35
+ if PM is None:PM=self.mol.PM
36
+ # 矩阵乘法的迹的加和=矩阵对应元素乘积之和
37
+ PS=PM@self.mol.SM
38
+ EV=np.diagonal(PS)
39
+ atoms=self.mol.atoms
40
+ charges=np.zeros(len(atoms))
41
+ for a,atom in enumerate(atoms):
42
+ a1,a2=atom.obtBorder
43
+ elect=EV[a1:a2].sum()
44
+ if num:
45
+ charges[a]=elect
46
+ else:
47
+ charges[a]=atom.atomic-elect
48
+ return charges
49
+
50
+ def lowdin(self,num:bool=False,PM:np.ndarray=None):
51
+ """
52
+ 计算每个原子的lowdin电荷
53
+ """
54
+ self.logTip='Lowdin电荷分布'
55
+ if PM is None:PM=self.mol.PM
56
+ SM=self.mol.SM
57
+ # 计算矩阵的1/2次方
58
+ v, Q=np.linalg.eig(SM)
59
+ V=np.diag(v)
60
+ V_=V**0.5
61
+ Q_=np.linalg.inv(Q)
62
+ SM_half=Q@(V_@Q_)
63
+ SPS=SM_half@(PM@SM_half)
64
+ eleNums=np.diagonal(SPS)
65
+ charges=np.zeros(len(self.mol.atoms))
66
+ for a,atom in enumerate(self.mol.atoms):
67
+ u,l=atom.obtBorder
68
+ eleNum=eleNums[u:l].sum()
69
+ if num:
70
+ charges[a]=eleNum
71
+ else:
72
+ charges[a]=atom.atomic-eleNum
73
+ return charges
74
+
75
+ def hirshfeld(self):
76
+ """
77
+ 计算原子的Hirshfeld电荷,目前还未成功(分子的电子密度计算不正确)
78
+ """
79
+ from pywfn.spaceProp import wfn
80
+ wfnCaler=wfn.Calculator(self.mol)
81
+ self.mol.bohr=True
82
+ from pywfn.data import sphGrid,radDens
83
+ coords=sphGrid.gridData[:,:3]# 原点为0的坐标
84
+ weight=sphGrid.gridData[:,3]
85
+ npos=len(coords)
86
+ atoms:list[int]=self.mol.atoms.indexs
87
+ chargs=np.zeros(shape=(len(atoms)))
88
+ for a1,atom1 in enumerate(self.mol.atoms): # 计算每一个原子的电荷
89
+ pos=coords+atom1.coord # 该原子周围点的空间坐标
90
+ # molDens=self.mol.get_dens(atoms,obts,coord)*weight # 计算分子的电子密度
91
+ molDens=np.zeros(npos) # 计算分子的电子密度
92
+ proDens=np.zeros(npos)
93
+ for a2,atom2 in enumerate(self.mol.atoms): #计算前置分子密度
94
+ radius=np.linalg.norm(pos-atom2.coord,axis=1) #所有坐标对应的半径
95
+ dens1=radDens.get_radDens(atom2.atomic,radius)*weight # 插值法计算提前存储好的密度
96
+ proDens+=dens1
97
+ if a2==a1:atmDens=dens1
98
+ dens2=wfnCaler.atmDens(atom2.idx,pos-atom2.coord)*weight
99
+ molDens+=dens2
100
+ print(f'{dens1.sum():.4f},{dens2.sum():.4f}')
101
+ ratio=np.divide(atmDens,proDens,out=np.zeros_like(atmDens),where=proDens!=0)
102
+ chargs[a1]=np.sum(ratio*(molDens-proDens))
103
+ atmQ,proQ,molQ=np.sum(atmDens),np.sum(proDens),np.sum(molDens)
104
+ print(f'{atmQ=:.4f},{proQ=:.4f},{molQ=:.4f}')
105
+ chargs=[atom.atomic-val for atom,val in zip(self.mol.atoms,chargs)]
106
+ return chargs
107
+
108
+ def hirshfeld2(self):
109
+ """第二种计算hirshfeld电荷的方法"""
110
+ from pywfn.data import sphGrid,radDens
111
+ from pywfn.spaceProp import wfn
112
+ coords=sphGrid.gridData[:,:3]# 原点为0的坐标
113
+ weight=sphGrid.gridData[:,3]
114
+ pos=[]
115
+ wei=[]
116
+ for atom in self.mol.atoms:
117
+ pos.append(coords+atom.coord)
118
+ wei.append(weight)
119
+ pos=np.vstack(pos)
120
+ natm=len(self.mol.atoms)
121
+ wei=np.hstack(wei)/natm
122
+
123
+ wfnCaler=wfn.Calculator(self.mol)
124
+ chars=[]
125
+ molDens=wfnCaler.molDens(pos)
126
+ print(np.min(molDens),np.max(molDens)),print(np.sum(molDens*wei))
127
+ proDens=np.zeros_like(molDens)
128
+ for _,atom in enumerate(self.mol.atoms):
129
+ radius=np.linalg.norm(pos-atom.coord,axis=1)
130
+ proDens+=radDens.get_radDens(atom.atomic,radius)
131
+ print(np.min(proDens),np.max(proDens)),print(np.sum(proDens*wei))
132
+ pass
133
+ for _,atom in enumerate(self.mol.atoms):
134
+ atmDens=wfnCaler.atmDens(atom.idx,pos)
135
+ ratio=np.divide(atmDens,proDens,out=np.zeros_like(atmDens),where=proDens!=0)
136
+ dens=ratio*molDens
137
+ char=sum(dens*wei)
138
+ chars.append(char)
139
+ chars=np.array(chars)
140
+ k=sum(self.mol.atoms.atomics)/sum(chars)
141
+ chars*=k
142
+ chars=[atom.atomic-char for atom,char in zip(self.mol.atoms,chars)]
143
+ return np.array(chars)/20
144
+
145
+ def dirCharge(self,chrg:Chrgs,atms:list[int],dirs:list[np.ndarray]=None)->np.ndarray:
146
+ """计算不同方向的电荷[n,5](atm,x,y,z,val)"""
147
+ atms_,dirs_=fit_dirs(self.mol,atms,dirs)
148
+ assert len(atms_)==len(dirs_),"长度需要一致"
149
+ dirVal=np.zeros(shape=(len(dirs_),5))
150
+ obts=self.mol.O_obts
151
+ for d in range(len(dirs_)):
152
+ atm=atms_[d]
153
+ dir_=dirs_[d]
154
+ CMp=self.mol.projCM(obts,[atm],[dir_],False,False) # 获取投影后的轨道系数
155
+ PMp=CM2PM(CMp,obts,self.mol.oE)
156
+ if chrg=='mulliken':
157
+ val=self.mulliken(num=True,PM=PMp)[atm-1]
158
+ elif chrg=='lowdin':
159
+ val=self.lowdin(num=True,PM=PMp)[atm-1]
160
+ x,y,z=dir_
161
+ dirVal[d]=[atm,x,y,z,val]
162
+
163
+ return dirVal
164
+
165
+ def resStr(self)->str:
166
+ """获取结果的打印内容"""
167
+ satoms=lutils.atomIdxs(self.mol.atoms)
168
+ charges=self.calculate()
169
+ return lutils.atomValueStr(self.mol,satoms,charges)
170
+
171
+ def onShell(self):
172
+ from pywfn.utils import parse_atmList
173
+ printer.info('1. mulliken电荷')
174
+ printer.info('2. lowdin电荷')
175
+ printer.info('3. 方向性电荷')
176
+ while True:
177
+ opt=input('请选择电荷类型:')
178
+ if opt=='':break
179
+ if opt=='1':
180
+ charges=self.mulliken()
181
+ for i,v in enumerate(charges):
182
+ print(f'{i+1}: {v}')
183
+ if opt=='2':
184
+ charges=self.lowdin()
185
+ for i,v in enumerate(charges):
186
+ print(f'{i+1}: {v}')
187
+ if opt=='3':
188
+ printer.info('1. Mulliken[*]; 2. lowdin')
189
+ opt=input('请选择方向性电荷类型:')
190
+ if opt not in ['1','2']:return
191
+ if opt=='1':charg='mulliken'
192
+ if opt=='2':charg='lowdin'
193
+ numStr=input('请输入要计算的原子索引:')
194
+ atms=parse_atmList(numStr)
195
+ charges=self.dirCharge(charg,atms)
196
+ for a,x,y,z,v in charges:
197
+ print(f'{a}: {x},{y},{z} {v}')
198
+
199
+ def fit_dirs(mol:Mol,atms:list[int],dirs:list[np.ndarray]):
200
+ """
201
+ 矫正方向,如果没有指定方向的话,计算每个原子可能的反应方向
202
+ """
203
+ if dirs is None:
204
+ from pywfn.atomprop import direction
205
+ dirCaler=direction.Calculator(mol)
206
+ atms_=[]
207
+ dirs_=[]
208
+ for atm in atms:
209
+ resDirs=dirCaler.reaction(atm)
210
+ dirs_.append(resDirs)
211
+ atms_+=[atm]*len(resDirs)
212
+ dirs_=np.vstack(dirs_)
213
+ else:
214
+ atms_=atms
215
+ dirs_=dirs
216
+ return atms_,dirs_
@@ -0,0 +1,96 @@
1
+ """
2
+ 三个分子的差值确定的属性
3
+ - 福井函数
4
+ - parr函数
5
+ """
6
+ from pywfn.base import Mol
7
+ from pywfn.atomprop import charge,dirProps,AtomCaler,energy, spin
8
+ import numpy as np
9
+ from typing import Literal
10
+
11
+ class Calculator(AtomCaler):
12
+ def __init__(self,mols:list[Mol]) -> None:
13
+ self.mols=mols
14
+ self.natm=len(mols[0].atoms)
15
+ self.vects=None # 可以计算方向属性
16
+ self.atoms=None
17
+ self.chrg=None # 电荷类型
18
+ self.prop=None # 计算属性
19
+ self.func:Literal['fukui','parr']=None # 函数类型
20
+ self.calers=[]
21
+ self.hists={}
22
+
23
+
24
+ def calculate(self)->np.ndarray:
25
+ funcs={'fukui':self.fukuiFunc,'parr':self.parrFunc}
26
+ if self.vects:
27
+ assert self.vects is not None,'没有指定方向'
28
+ self.calers=[dirProps.Calculator(mol) for mol in self.mols]
29
+ for caler in self.calers:
30
+ caler.atoms=self.atoms
31
+ caler.vects=self.vects
32
+ caler.chrg=self.chrg
33
+ caler.prop=self.prop
34
+ return funcs[self.func]
35
+ else:
36
+ if self.func=='fukui':
37
+ self.calers=[charge.Calculator(mol) for mol in self.mols]
38
+ if self.func=='parr':
39
+ self.calers=[spin.Calculator(mol) for mol in self.mols]
40
+ return funcs[self.func]
41
+
42
+ def setProject(self,stype:bool):
43
+ """
44
+ 检查是否需要投影
45
+ stype,修改(T)或撤回修改(F)
46
+ """
47
+ if self.vects is None:return
48
+ for m,mol in enumerate(self.mols):
49
+ if stype:
50
+ CMo=mol.CM
51
+ self.hists[f'CM_{m}']=CMo # 将老的存储下来
52
+ CMn=mol.projCM(self.atoms,mol.O_obts,self.vects,True,False)
53
+ mol.datas['CM']=CMn
54
+ else:
55
+ CMo=self.hists[f'CM_{m}'] # 恢复成老的
56
+ mol.datas['CM']=CMo
57
+
58
+ def fukuiFunc(self)->np.ndarray:
59
+ self.setProject(True)
60
+ assert len(self.mols)==3,'需要三个分子'
61
+ charges=np.zeros(shape=(self.natm,3))
62
+ dcharge=np.zeros(shape=(self.natm,2))
63
+ for c,caler in enumerate(self.calers):
64
+ charge=caler.calculate()
65
+ charges[:,c]=charge
66
+ dcharge[:,0]=-(charges[:,0]-charges[:,1])
67
+ dcharge[:,1]=-(charges[:,1]-charges[:,2])
68
+ self.setProject(False)
69
+ return dcharge
70
+
71
+ def parrFunc(self)->np.ndarray:
72
+ self.setProject(True)
73
+ assert len(self.mols)==2,'需要两个分子'
74
+ spins=np.zeros(shape=(self.natm,2))
75
+ for c,caler in enumerate(self.calers):
76
+ spin=caler.calculate()
77
+ spins[:,c]=spin
78
+ self.setProject(False)
79
+ return spins
80
+
81
+ def atomEngs(self)->np.ndarray:
82
+ """计算原子轨道能"""
83
+ self.setProject(True)
84
+ self.calers=[energy.Calculator(mol) for mol in self.mols]
85
+ atmNum=len(self.mols[0].atoms)
86
+ allEngs=np.zeros(shape=(atmNum,3))
87
+ for c,caler in enumerate(self.calers):
88
+ engs=caler.calculate()
89
+ allEngs[:,c]=engs
90
+ self.allEngs=allEngs
91
+ delEngs=np.zeros(shape=(atmNum,2))
92
+ delEngs[:,0]=allEngs[:,0]-allEngs[:,1]
93
+ delEngs[:,1]=allEngs[:,1]-allEngs[:,2]
94
+ self.setProject(False)
95
+ return delEngs
96
+