qrotor 4.0.0a1__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,167 @@
1
+ Metadata-Version: 2.4
2
+ Name: qrotor
3
+ Version: 4.0.0a1
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
+ Dynamic: author
25
+ Dynamic: author-email
26
+ Dynamic: classifier
27
+ Dynamic: description
28
+ Dynamic: description-content-type
29
+ Dynamic: keywords
30
+ Dynamic: license
31
+ Dynamic: license-file
32
+ Dynamic: requires-dist
33
+ Dynamic: requires-python
34
+ Dynamic: summary
35
+
36
+ # QRotor
37
+
38
+ QRotor is a Python package used to study molecular rotations,
39
+ such as those of methyl and amine groups.
40
+ It can calculate their quantum energy levels and wavefunctions,
41
+ along with excitations and tunnel splittings.
42
+ These quantum systems are represented by the `qrotor.System()` object.
43
+
44
+ QRotor can obtain custom potentials from DFT,
45
+ which are used to solve the quantum system.
46
+
47
+ Check the [full documentation online](https://pablogila.github.io/qrotor/).
48
+
49
+
50
+ # Index
51
+
52
+ | | |
53
+ | --- | --- |
54
+ | [qrotor.system](https://pablogila.github.io/qrotor/qrotor/system.html) | Definition of the quantum `System` object |
55
+ | [qrotor.systems](https://pablogila.github.io/qrotor/qrotor/systems.html) | Functions to manage several System objects, such as a list of systems |
56
+ | [qrotor.rotate](https://pablogila.github.io/qrotor/qrotor/rotate.html) | Rotate specific atoms from structural files |
57
+ | [qrotor.constants](https://pablogila.github.io/qrotor/qrotor/constants.html) | Common bond lengths and inertias |
58
+ | [qrotor.potential](https://pablogila.github.io/qrotor/qrotor/potential.html) | Potential definitions and loading functions |
59
+ | [qrotor.solve](https://pablogila.github.io/qrotor/qrotor/solve.html) | Solve rotation eigenvalues and eigenvectors |
60
+ | [qrotor.plot](https://pablogila.github.io/qrotor/qrotor/plot.html) | Plotting functions |
61
+
62
+
63
+ # Usage
64
+
65
+ ## Solving quantum rotational systems
66
+
67
+ A basic calculation of the eigenvalues for a zero potential goes as follows.
68
+ Note that the default energy unit is meV unless stated otherwise.
69
+
70
+ ```python
71
+ import qrotor as qr
72
+ system = qr.System()
73
+ system.gridsize = 200000 # Size of the potential grid
74
+ system.B = 1 # Rotational inertia
75
+ system.potential_name = 'zero'
76
+ system.solve()
77
+ system.eigenvalues
78
+ # [0.0, 1.0, 1.0, 4.0, 4.0, 9.0, 9.0, ...] # approx values
79
+ ```
80
+
81
+ The accuracy of the calculation increases with bigger gridsizes,
82
+ but note that the runtime increases exponentially.
83
+
84
+ The same calculation can be performed for a methyl group,
85
+ in a cosine potential of amplitude 30 meV:
86
+
87
+ ```python
88
+ import qrotor as qr
89
+ system = qr.System()
90
+ system.gridsize = 200000 # Size of the potential grid
91
+ system.B = qr.B_CH3 # Rotational inertia of a methyl group
92
+ system.potential_name = 'cosine'
93
+ system.potential_constants = [0, 30, 3, 0] # Offset, max, freq, phase (for cos pot.)
94
+ system.solve()
95
+ # Plot potential and eigenvalues
96
+ qr.plot.energies(system)
97
+ # Plot the first wavefunctions
98
+ qr.plot.wavefunction(system, levels=[0,1,2], square=True)
99
+ ```
100
+
101
+
102
+ ## Custom potentials from DFT
103
+
104
+ QRotor can be used to obtain custom rotational potentials from DFT calculations.
105
+ Using Quantum ESPRESSO, running an SCF calculation for a methyl rotation every 10 degrees:
106
+
107
+ ```python
108
+ import qrotor as qr
109
+ from aton import api
110
+ # Approx crystal positions of the atoms to rotate
111
+ atoms = [
112
+ '1.101 1.204 1.307'
113
+ '2.102 2.205 2.308'
114
+ '3.103 3.206 3.309'
115
+ ]
116
+ # Create the input SCF files, saving the filenames to a list
117
+ scf_files = qr.rotate.structure_qe('molecule.in', positions=atoms, angle=10, repeat=True)
118
+ # Run the Quantum ESPRESSO calculations
119
+ api.slurm.sbatch(files=scf_files)
120
+ ```
121
+
122
+ To load the calculated potential to a QRotor System,
123
+ ```python
124
+ # Compile a 'potential.csv' file with the calculated potential as a function of the angle
125
+ qr.potential.from_qe()
126
+ # Load to the system
127
+ system = qr.potential.load()
128
+ # Solve the system, interpolating to a bigger gridsize
129
+ system.B = qr.B_CH3
130
+ system.solve(200000)
131
+ qr.plot.energies(system)
132
+ ```
133
+
134
+
135
+ ## Tunnel splittings and excitations
136
+
137
+ Tunnel splittings, excitations and energy level degeneracy
138
+ below the potential maximum are also calculated upon solving the system:
139
+
140
+ ```python
141
+ system.solve()
142
+ system.splittings
143
+ system.excitations
144
+ system.deg
145
+ ```
146
+
147
+ An integer `System.deg` degeneracy (e.g. 3 for methyls)
148
+ indicates that the energy levels have been properly estimated.
149
+ However, if the degeneracy is a float instead,
150
+ please check the splittings and excitations manually from the system eigenvalues.
151
+
152
+ To export the energies and the tunnel splittings of several calculations to a CSV file:
153
+
154
+ ```python
155
+ calculations = [system1, system2, system3]
156
+ qr.systems.save_energies(calculations)
157
+ qr.systems.save_splittings(calculations)
158
+ ```
159
+
160
+ Excitations are calculated using the mean for each energy level
161
+ with respect to the ground state.
162
+ Tunnel splittings for each level are calculated as the difference between A and E,
163
+ considering the mean of the eigenvalues for each sublevel.
164
+ See [R. M. Dimeo, American Journal of Physics 71, 885–893 (2003)](https://doi.org/10.1119/1.1538575)
165
+ and [A. J. Horsewill, Progress in Nuclear Magnetic Resonance Spectroscopy 35, 359–389 (1999)](https://doi.org/10.1016/S0079-6565(99)00016-3)
166
+ for further reference.
167
+
@@ -0,0 +1,16 @@
1
+ qrotor/__init__.py,sha256=rG2dH4QjsVUOMBhFnv5gXs3QnrUg7fywd5pIDmMBXcQ,246
2
+ qrotor/_version.py,sha256=rKPpeKIneHbTwnQYeOQrUmp3YuE0w_IV9QL3JTPKVh4,196
3
+ qrotor/constants.py,sha256=q62GbUKy2aojQ_8epIfaJMP1Y_HrT9mVw_9zWhvdCKo,2734
4
+ qrotor/plot.py,sha256=xJbreSDIuBNBl71e5XzzVW2PBijgkVZTFl3Fe74hecU,13315
5
+ qrotor/potential.py,sha256=VpO0U9UXAfqQE8sdQymSyMfiHEnWluAFksLNJJQ2iB0,17801
6
+ qrotor/rotate.py,sha256=Wje9Q9SFhDvizz58MzNGBwsmgV-3wN9z2SnUNTIXzeg,8107
7
+ qrotor/solve.py,sha256=kR7xwIBYGazNR0VkFYjsfERlZe6mtweQRVacupycOIs,10727
8
+ qrotor/system.py,sha256=onY3HfY2zpRLDJUjBJRX-nJu6YH_qhnPnCW6Uyam_Ws,11453
9
+ qrotor/systems.py,sha256=Hcx0QvMWpaPMfC6HWpkZPPWDyHk9rxWKdAxWNnD2NMg,8184
10
+ qrotor-4.0.0a1.dist-info/licenses/LICENSE,sha256=hIahDEOTzuHCU5J2nd07LWwkLW7Hko4UFO__ffsvB-8,34523
11
+ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ tests/test_qrotor.py,sha256=44uvE1AXMAtbUILA1gLeMi2__8quqcSyv1m3R1f4AP0,3462
13
+ qrotor-4.0.0a1.dist-info/METADATA,sha256=qB-zxEAuhlp4ZFr19Ziy1ynjNH_VBxNE0OabImYi5ak,5830
14
+ qrotor-4.0.0a1.dist-info/WHEEL,sha256=Nw36Djuh_5VDukK0H78QzOX-_FQEo6V37m3nkm96gtU,91
15
+ qrotor-4.0.0a1.dist-info/top_level.txt,sha256=mLnYs07-amqX4TqbDV2_XvgdpHfgrYmzmYb7dwoh6EQ,13
16
+ qrotor-4.0.0a1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.7.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+