qrotor 4.0.0__py3-none-any.whl

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 qrotor might be problematic. Click here for more details.

qrotor/systems.py ADDED
@@ -0,0 +1,245 @@
1
+ """
2
+ # Description
3
+
4
+ This module contains utility functions to handle multiple `qrotor.system` calculations.
5
+ These are commonly used as a list of `System` objects.
6
+
7
+
8
+ # Index
9
+
10
+ | | |
11
+ | --- | --- |
12
+ | `as_list()` | Ensures that a list only contains System objects |
13
+ | `save_energies()` | Save the energy eigenvalues for all systems to a CSV |
14
+ | `save_splittings()` | Save the tunnel splitting energies for all systems to a CSV |
15
+ | `get_energies()` | Get the eigenvalues from all systems |
16
+ | `get_gridsizes()` | Get all gridsizes |
17
+ | `get_runtimes()` | Get all runtimes |
18
+ | `get_groups()` | Get the chemical groups in use |
19
+ | `get_ideal_E()` | Calculate the ideal energy for a specified level |
20
+ | `sort_by_gridsize()` | Sort systems by gridsize |
21
+ | `reduce_size()` | Discard data that takes too much space |
22
+ | `summary()` | Print a summary of a System or list of Systems |
23
+
24
+ ---
25
+ """
26
+
27
+
28
+ from .system import System
29
+ from aton import txt
30
+ import pandas as pd
31
+
32
+
33
+ def as_list(systems) -> None:
34
+ """Ensures that `systems` is a list of System objects.
35
+
36
+ If it is a System, returns a list with that System as the only element.
37
+ If it is neither a list nor a System,
38
+ or if the list does not contain only System objects,
39
+ it raises an error.
40
+ """
41
+ if isinstance(systems, System):
42
+ systems = [systems]
43
+ if not isinstance(systems, list):
44
+ raise TypeError(f"Must be a System object or a list of systems, found instead: {type(systems)}")
45
+ for i in systems:
46
+ if not isinstance(i, System):
47
+ raise TypeError(f"All items in the list must be System objects, found instead: {type(i)}")
48
+ return systems
49
+
50
+
51
+ def save_energies(
52
+ systems:list,
53
+ comment:str='',
54
+ filepath:str='eigenvalues.csv',
55
+ ) -> pd.DataFrame:
56
+ """Save the energy eigenvalues for all `systems` to a eigenvalues.csv file.
57
+
58
+ Returns a Pandas Dataset with `System.comment` columns and `System.eigenvalues` values.
59
+
60
+ The output file can be changed with `filepath`,
61
+ or set to null to avoid saving the dataset.
62
+ A `comment` can be included at the top of the file.
63
+ Note that `System.comment` must not include commas (`,`).
64
+ """
65
+ as_list(systems)
66
+ version = systems[0].version
67
+ E = {}
68
+ # Find max length of eigenvalues
69
+ max_len = max((len(s.eigenvalues) if s.eigenvalues is not None else 0) for s in systems)
70
+ for s in systems:
71
+ if s.eigenvalues is not None:
72
+ # Filter out None values and replace with NaN
73
+ valid_eigenvalues = [float('nan') if e is None else e for e in s.eigenvalues]
74
+ padded_eigenvalues = valid_eigenvalues + [float('nan')] * (max_len - len(s.eigenvalues))
75
+ else:
76
+ padded_eigenvalues = [float('nan')] * max_len
77
+ E[s.comment] = padded_eigenvalues
78
+ df = pd.DataFrame(E)
79
+ if not filepath:
80
+ return df
81
+ # Else save to file
82
+ df.to_csv(filepath, sep=',', index=False)
83
+ # Include a comment at the top of the file
84
+ file_comment = f'# {comment}\n' if comment else f''
85
+ file_comment += f'# Energy eigenvalues\n'
86
+ file_comment += f'# Calculated with QRotor {version}\n'
87
+ file_comment += f'# https://pablogila.github.io/qrotor\n#'
88
+ txt.edit.insert_at(filepath, file_comment, 0)
89
+ print(f'Energy eigenvalues saved to {filepath}')
90
+ return df
91
+
92
+
93
+ def save_splittings(
94
+ systems:list,
95
+ comment:str='',
96
+ filepath:str='splittings.csv',
97
+ ) -> pd.DataFrame:
98
+ """Save the tunnel splitting energies for all `systems` to a splittings.csv file.
99
+
100
+ Returns a Pandas Dataset with `System.comment` columns and `System.splittings` values.
101
+
102
+ The output file can be changed with `filepath`,
103
+ or set to null to avoid saving the dataset.
104
+ A `comment` can be included at the top of the file.
105
+ Note that `System.comment` must not include commas (`,`).
106
+ Different splitting lengths across systems are allowed - missing values will be NaN.
107
+ """
108
+ as_list(systems)
109
+ version = systems[0].version
110
+ tunnelling_E = {}
111
+ # Find max length of splittings
112
+ max_len = max(len(s.splittings) for s in systems)
113
+ for s in systems: # Pad shorter splittings with NaN
114
+ padded_splittings = s.splittings + [float('nan')] * (max_len - len(s.splittings))
115
+ tunnelling_E[s.comment] = padded_splittings
116
+ df = pd.DataFrame(tunnelling_E)
117
+ if not filepath:
118
+ return df
119
+ # Else save to file
120
+ df.to_csv(filepath, sep=',', index=False)
121
+ # Include a comment at the top of the file
122
+ file_comment = f'# {comment}\n' if comment else f''
123
+ file_comment += f'# Tunnel splitting energies\n'
124
+ file_comment += f'# Calculated with QRotor {version}\n'
125
+ file_comment += f'# https://pablogila.github.io/qrotor\n#'
126
+ txt.edit.insert_at(filepath, file_comment, 0)
127
+ print(f'Tunnel splitting energies saved to {filepath}')
128
+ return df
129
+
130
+
131
+ def get_energies(systems:list) -> list:
132
+ """Get a list with all lists of eigenvalues from all systems.
133
+
134
+ If no eigenvalues are present for a particular system, appends None.
135
+ """
136
+ as_list(systems)
137
+ energies = []
138
+ for i in systems:
139
+ if all(i.eigenvalues):
140
+ energies.append(i.eigenvalues)
141
+ else:
142
+ energies.append(None)
143
+ return energies
144
+
145
+
146
+ def get_gridsizes(systems:list) -> list:
147
+ """Get a list with all gridsize values.
148
+
149
+ If no gridsize value is present for a particular system, appends None.
150
+ """
151
+ as_list(systems)
152
+ gridsizes = []
153
+ for i in systems:
154
+ if i.gridsize:
155
+ gridsizes.append(i.gridsize)
156
+ elif any(i.potential_values):
157
+ gridsizes.append(len(i.potential_values))
158
+ else:
159
+ gridsizes.append(None)
160
+ return gridsizes
161
+
162
+
163
+ def get_runtimes(systems:list) -> list:
164
+ """Returns a list with all runtime values.
165
+
166
+ If no runtime value is present for a particular system, appends None.
167
+ """
168
+ as_list(systems)
169
+ runtimes = []
170
+ for i in systems:
171
+ if i.runtime:
172
+ runtimes.append(i.runtime)
173
+ else:
174
+ runtimes.append(None)
175
+ return runtimes
176
+
177
+
178
+ def get_groups(systems:list) -> list:
179
+ """Returns a list with all `System.group` values."""
180
+ as_list(systems)
181
+ groups = []
182
+ for i in systems:
183
+ if i.group not in groups:
184
+ groups.append(i.group)
185
+ return groups
186
+
187
+
188
+ def get_ideal_E(E_level:int) -> int:
189
+ """Calculates the ideal energy for a specified `E_level`.
190
+
191
+ To be used in convergence tests with `potential_name = 'zero'`.
192
+ """
193
+ real_E_level = None
194
+ if E_level % 2 == 0:
195
+ real_E_level = E_level / 2
196
+ else:
197
+ real_E_level = (E_level + 1) / 2
198
+ ideal_E = int(real_E_level ** 2)
199
+ return ideal_E
200
+
201
+
202
+ def sort_by_gridsize(systems:list) -> list:
203
+ """Sorts a list of System objects by `System.gridsize`."""
204
+ as_list(systems)
205
+ systems = sorted(systems, key=lambda sys: sys.gridsize)
206
+ return systems
207
+
208
+
209
+ def reduce_size(systems:list) -> list:
210
+ """Discard data that takes too much space.
211
+
212
+ Removes eigenvectors, potential values and grids,
213
+ for all System values inside the `systems` list.
214
+ """
215
+ as_list(systems)
216
+ for dataset in systems:
217
+ dataset = dataset.reduce_size()
218
+ return systems
219
+
220
+
221
+ def summary(systems, verbose:bool=False) -> None:
222
+ """Print a summary of a System or list of Systems.
223
+
224
+ Print extra info with `verbose=True`
225
+ """
226
+ print('--------------------')
227
+ systems = as_list(systems)
228
+ for system in systems:
229
+ dictionary = system.summary()
230
+ if verbose:
231
+ for key, value in dictionary.items():
232
+ print(f'{key:<24}', value)
233
+ else:
234
+ eigenvalues = system.eigenvalues if any(system.eigenvalues) else []
235
+ extra = ''
236
+ if len(system.eigenvalues) > 6:
237
+ eigenvalues = eigenvalues[:6]
238
+ extra = '...'
239
+ print('comment ' + str(system.comment))
240
+ print('B ' + str(system.B))
241
+ print('eigenvalues ' + str([float(round(e, 4)) for e in eigenvalues]) + extra)
242
+ print('version ' + str(system.version))
243
+ print('--------------------')
244
+ return None
245
+
@@ -0,0 +1,168 @@
1
+ Metadata-Version: 2.4
2
+ Name: qrotor
3
+ Version: 4.0.0
4
+ Summary: QRotor
5
+ Author: Pablo Gila-Herranz
6
+ Author-email: pgila001@ikasle.ehu.eus
7
+ License: AGPL-3.0
8
+ Keywords: QRotor,Molecular rotations,Quantum rotations,Quantum,Molecular,Rotations,Neutrons,Research,Ab-initio,DFT,Density Functional Theory,Quantum ESPRESSO,Phonons,Electronic structure
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Natural Language :: English
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Operating System :: POSIX :: Linux
14
+ Classifier: Operating System :: Microsoft :: Windows
15
+ Classifier: Operating System :: Other OS
16
+ Requires-Python: >=3
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: scipy
20
+ Requires-Dist: pandas
21
+ Requires-Dist: numpy
22
+ Requires-Dist: matplotlib
23
+ Requires-Dist: aton
24
+ Requires-Dist: periodictable
25
+ Dynamic: author
26
+ Dynamic: author-email
27
+ Dynamic: classifier
28
+ Dynamic: description
29
+ Dynamic: description-content-type
30
+ Dynamic: keywords
31
+ Dynamic: license
32
+ Dynamic: license-file
33
+ Dynamic: requires-dist
34
+ Dynamic: requires-python
35
+ Dynamic: summary
36
+
37
+ <p align="center"><img width="60.0%" src="pics/qrotor.png"></p>
38
+
39
+ QRotor is a Python package used to study molecular rotations,
40
+ such as those of methyl and amine groups.
41
+ It can calculate their quantum energy levels and wavefunctions,
42
+ along with excitations and tunnel splittings.
43
+ These quantum systems are represented by the `qrotor.System()` object.
44
+
45
+ QRotor can obtain custom potentials from DFT,
46
+ which are used to solve the quantum system.
47
+
48
+ Check the [full documentation online](https://pablogila.github.io/qrotor/).
49
+
50
+
51
+ # Index
52
+
53
+ | | |
54
+ | --- | --- |
55
+ | [qrotor.system](https://pablogila.github.io/qrotor/qrotor/system.html) | Definition of the quantum `System` object |
56
+ | [qrotor.systems](https://pablogila.github.io/qrotor/qrotor/systems.html) | Functions to manage several System objects, such as a list of systems |
57
+ | [qrotor.rotate](https://pablogila.github.io/qrotor/qrotor/rotate.html) | Rotate specific atoms from structural files |
58
+ | [qrotor.constants](https://pablogila.github.io/qrotor/qrotor/constants.html) | Common bond lengths and inertias |
59
+ | [qrotor.potential](https://pablogila.github.io/qrotor/qrotor/potential.html) | Potential definitions and loading functions |
60
+ | [qrotor.solve](https://pablogila.github.io/qrotor/qrotor/solve.html) | Solve rotation eigenvalues and eigenvectors |
61
+ | [qrotor.plot](https://pablogila.github.io/qrotor/qrotor/plot.html) | Plotting functions |
62
+
63
+
64
+ # Usage
65
+
66
+ ## Solving quantum rotational systems
67
+
68
+ A basic calculation of the eigenvalues for a zero potential goes as follows.
69
+ Note that the default energy unit is meV unless stated otherwise.
70
+
71
+ ```python
72
+ import qrotor as qr
73
+ system = qr.System()
74
+ system.gridsize = 200000 # Size of the potential grid
75
+ system.B = 1 # Rotational inertia
76
+ system.potential_name = 'zero'
77
+ system.solve()
78
+ system.eigenvalues
79
+ # [0.0, 1.0, 1.0, 4.0, 4.0, 9.0, 9.0, ...] # approx values
80
+ ```
81
+
82
+ The accuracy of the calculation increases with bigger gridsizes,
83
+ but note that the runtime increases exponentially.
84
+
85
+ The same calculation can be performed for a methyl group,
86
+ in a cosine potential of amplitude 30 meV:
87
+
88
+ ```python
89
+ import qrotor as qr
90
+ system = qr.System()
91
+ system.gridsize = 200000 # Size of the potential grid
92
+ system.B = qr.B_CH3 # Rotational inertia of a methyl group
93
+ system.potential_name = 'cosine'
94
+ system.potential_constants = [0, 30, 3, 0] # Offset, max, freq, phase (for cos pot.)
95
+ system.solve()
96
+ # Plot potential and eigenvalues
97
+ qr.plot.energies(system)
98
+ # Plot the first wavefunctions
99
+ qr.plot.wavefunction(system, levels=[0,1,2], square=True)
100
+ ```
101
+
102
+
103
+ ## Custom potentials from DFT
104
+
105
+ QRotor can be used to obtain custom rotational potentials from DFT calculations.
106
+ Using Quantum ESPRESSO, running an SCF calculation for a methyl rotation every 10 degrees:
107
+
108
+ ```python
109
+ import qrotor as qr
110
+ from aton import api
111
+ # Approx crystal positions of the atoms to rotate
112
+ atoms = [
113
+ '1.101 1.204 1.307'
114
+ '2.102 2.205 2.308'
115
+ '3.103 3.206 3.309'
116
+ ]
117
+ # Create the input SCF files, saving the filenames to a list
118
+ scf_files = qr.rotate.structure_qe('molecule.in', positions=atoms, angle=10, repeat=True)
119
+ # Run the Quantum ESPRESSO calculations
120
+ api.slurm.sbatch(files=scf_files)
121
+ ```
122
+
123
+ To load the calculated potential to a QRotor System,
124
+ ```python
125
+ # Compile a 'potential.csv' file with the calculated potential as a function of the angle
126
+ qr.potential.from_qe()
127
+ # Load to the system
128
+ system = qr.potential.load()
129
+ # Solve the system, interpolating to a bigger gridsize
130
+ system.B = qr.B_CH3
131
+ system.solve(200000)
132
+ qr.plot.energies(system)
133
+ ```
134
+
135
+
136
+ ## Tunnel splittings and excitations
137
+
138
+ Tunnel splittings, excitations and energy level degeneracy
139
+ below the potential maximum are also calculated upon solving the system:
140
+
141
+ ```python
142
+ system.solve()
143
+ system.splittings
144
+ system.excitations
145
+ system.deg
146
+ ```
147
+
148
+ An integer `System.deg` degeneracy (e.g. 3 for methyls)
149
+ indicates that the energy levels have been properly estimated.
150
+ However, if the degeneracy is a float instead,
151
+ please check the splittings and excitations manually from the system eigenvalues.
152
+
153
+ To export the energies and the tunnel splittings of several calculations to a CSV file:
154
+
155
+ ```python
156
+ calculations = [system1, system2, system3]
157
+ qr.systems.save_energies(calculations)
158
+ qr.systems.save_splittings(calculations)
159
+ ```
160
+
161
+ Excitations are calculated using the mean for each energy level
162
+ with respect to the ground state.
163
+ Tunnel splittings for each level are calculated as the difference between A and E,
164
+ considering the mean of the eigenvalues for each sublevel.
165
+ See [R. M. Dimeo, American Journal of Physics 71, 885–893 (2003)](https://doi.org/10.1119/1.1538575)
166
+ and [A. J. Horsewill, Progress in Nuclear Magnetic Resonance Spectroscopy 35, 359–389 (1999)](https://doi.org/10.1016/S0079-6565(99)00016-3)
167
+ for further reference.
168
+
@@ -0,0 +1,16 @@
1
+ qrotor/__init__.py,sha256=rG2dH4QjsVUOMBhFnv5gXs3QnrUg7fywd5pIDmMBXcQ,246
2
+ qrotor/_version.py,sha256=DMTqecCyKBSdEUmHjpS5DWtkxTyh6_wQz3QzeNRMd9E,194
3
+ qrotor/constants.py,sha256=pp3HwRPF4kvQBGBgZTR3PkeJgWrYMfHu288UcSQ9m_U,3195
4
+ qrotor/plot.py,sha256=SeQX8PM5qPdedmo0DzfGFbA8m_sH2mcK50aTuWXN8HA,13335
5
+ qrotor/potential.py,sha256=4sONrwnJkKy2zyquX6AFpNVLXrRMX9XUJUnuS6eZ9bc,17781
6
+ qrotor/rotate.py,sha256=Wje9Q9SFhDvizz58MzNGBwsmgV-3wN9z2SnUNTIXzeg,8107
7
+ qrotor/solve.py,sha256=tnkE5JYT8wW85gNbOEMgHTnzOZCb8q5xPcJbDI4uOB0,10724
8
+ qrotor/system.py,sha256=onY3HfY2zpRLDJUjBJRX-nJu6YH_qhnPnCW6Uyam_Ws,11453
9
+ qrotor/systems.py,sha256=Hcx0QvMWpaPMfC6HWpkZPPWDyHk9rxWKdAxWNnD2NMg,8184
10
+ qrotor-4.0.0.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
11
+ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ tests/test_qrotor.py,sha256=KL5i1YVFOB5cFc7BXCtbrbBKKMCHyGM-B20m4unz1q0,3457
13
+ qrotor-4.0.0.dist-info/METADATA,sha256=x4P2QdhlPMmPCFiqg6w2ndS06xshZhltnSal6enQuTE,5912
14
+ qrotor-4.0.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
+ qrotor-4.0.0.dist-info/top_level.txt,sha256=mLnYs07-amqX4TqbDV2_XvgdpHfgrYmzmYb7dwoh6EQ,13
16
+ qrotor-4.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+