Skip to content

Commit

Permalink
qrotor.plot.convergence()
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogila committed Jan 31, 2025
1 parent b82814d commit d6f48c3
Show file tree
Hide file tree
Showing 41 changed files with 541 additions and 716 deletions.
2 changes: 1 addition & 1 deletion aton/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"""

__version__ = 'v0.0.12'
__version__ = 'v0.0.13'

148 changes: 34 additions & 114 deletions aton/qrotor/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
| `reduced_energies()` | Reduced energies E/B as a function of the reduced potential V/B |
| `potential()` | Potential values as a function of the angle |
| `energies()` | Calculated eigenvalues |
| `convergence_DEV()` | NOT IMPLEMENTED |
| `convergence()` | Energy convergence |
| `eigenvectors_DEV()` | NOT IMPLEMENTED |
---
Expand Down Expand Up @@ -110,121 +110,41 @@ def energies(data) -> None:
plt.show()


## TODO: Implement the following functions


def convergence_DEV(data:list):
'''Plots the energy convergence of the system. NOT YET IMPLEMENTED'''
fig, ax1 = plt.subplots(figsize=(10, 6))

E_color = 'C0'
runtime_color = 'C3'
yaxes_color = E_color

converged_color_line = 'lightgrey'

units = data.variables[0].units
E_units = 'meV'
if 'meV' in units or 'mev' in units:
E_units = 'meV'
elif 'eV' in units or 'ev' in units:
E_units = 'eV'

title = data.comment
ylabel_text = f'Energy / {E_units}'
xlabel_text = 'Grid Size'
runtime_text = 'Runtime / s'

legend_title = data.legend_title
legend_title_position = data.legend_title_position
check_E_threshold = data.check_E_threshold
check_E_diff = data.check_E_diff
check_E_level = data.check_E_level
ideal_E = data.ideal_E
if check_E_level is None:
data.check_E_level = len(data.solutions[0].eigenvalues) - 1
check_E_level = data.check_E_level
ideal_E = data.get_ideal_E()


textbox = dict(boxstyle='round', facecolor='white', edgecolor='lightgrey', alpha=0.5)
textstr = ''

textstr_position_x = 0.88
textstr_position_y = 0.15
textstr_alignment_h = 'right'
textstr_alignment_v = 'bottom'

if legend_title_position and isinstance(legend_title_position, list):
textstr_position_x = data.legend_title_position[0]
textstr_position_y = data.legend_title_position[1]
textstr_alignment_h = data.legend_title_position[2]
textstr_alignment_v = data.legend_title_position[3]

energies = data.energies()
energies_transposed = np.transpose(energies)
plotted_energies = energies_transposed[check_E_level]
gridsizes = data.gridsizes()
runtimes = data.runtimes()

if check_E_diff:
plotted_energies = np.abs(plotted_energies - ideal_E)
ylabel_text = 'Energy offset / |meV|'
textstr_position_x = 0.5
textstr_position_y = 0.85
textstr_alignment_v = 'top'
textstr_alignment_h = 'center'

if not any(runtimes):
yaxes_color = 'black'

ax1.plot(gridsizes, plotted_energies, marker='o', linestyle='-', color=E_color)
ax1.set_xlabel(xlabel_text)
ax1.set_ylabel(ylabel_text, color=yaxes_color)
ax1.tick_params(axis='y', labelcolor=yaxes_color)

if ideal_E is not None:
if check_E_diff:
ax1.axhline(y=0, color='grey', linestyle='--')
else:
ax1.axhline(y=ideal_E, color='grey', linestyle='--')
textstr += f'Ideal E={ideal_E:.4f}\n'

if check_E_threshold and (ideal_E is not None):
if check_E_diff:
abs_energies = energy
else:
abs_energies = np.abs(plotted_energies - ideal_E)
for i, energy in enumerate(abs_energies):
if energy < check_E_threshold:
#ax1.plot(gridsizes[i], plotted_energies[i], marker=converged_marker, color=E_converged_color)
ax1.axvline(x=gridsizes[i], color=converged_color_line, linestyle='--')
textstr += f'Convergence threshold: {check_E_threshold}\n'
lower_limit, _ = ax1.get_ylim()
ax1.text(gridsizes[i], lower_limit, str(gridsizes[i]), fontsize=10, verticalalignment='bottom', horizontalalignment='center')
break

if any(runtimes):
ax2 = ax1.twinx() # instantiate a second y-axis that shares the same x-axis
ax2.set_ylabel(runtime_text, color=runtime_color) # we already handled the x-label with ax1
ax2.plot(gridsizes, runtimes, marker='o', linestyle='-', color=runtime_color)
ax2.tick_params(axis='y', labelcolor=runtime_color)
for i, energy in enumerate(plotted_energies):
textstr += f'N={gridsizes[i]} E={round(energy,4):.04f} t={round(runtimes[i],2):.02f}'
if i < len(plotted_energies) - 1:
textstr += '\n'

else:
for i, energy in enumerate(plotted_energies):
textstr += f'N={gridsizes[i]} E={round(energy,4):.04f}\n'
def convergence(data:list) -> None:
"""Plot the energy convergence of a list of Systems as a function of the gridsize."""
systems.check(data)
gridsizes = [system.gridsize for system in data]
runtimes = [system.runtime for system in data]
deviations = [] # List of lists, containing all eigenvalue deviations for every system
E_levels = data[0].E_levels
for system in data:
deviation_list = []
for i, eigenvalue in enumerate(system.eigenvalues):
ideal_E = systems.get_ideal_E(i)
deviation = abs(ideal_E - eigenvalue)
deviation_list.append(deviation)
deviation_list = deviation_list[1:] # Remove ground state
deviations.append(deviation_list)
# Plotting
fig, ax1 = plt.subplots()
ax1.set_xlabel('Grid size')
ax1.set_ylabel('Error / meV')
ax1.set_xscale('log')
ax1.set_yscale('log')
ax2 = ax1.twinx()
ax2.set_ylabel('Runtime [s]')
ax2.set_yscale('log')
ax2.plot(gridsizes, runtimes, color='tab:red', label='Runtime', linestyle='--')
for i in range(E_levels-1):
if i % 2 == 0: # Ignore even numbers, since those levels are degenerated.
continue
ax1.plot(gridsizes, [dev[i] for dev in deviations], label=f'$E_{int((i+1)/2)}$')
fig.legend(loc='upper right', bbox_to_anchor=(0.9, 0.88), fontsize='small')
plt.title(data[0].comment if data[0].comment else 'Energy convergence vs gridsize')
plt.show()

if legend_title is not False:
if isinstance(legend_title, str):
textstr = legend_title + '\n' + textstr
fig.text(textstr_position_x, textstr_position_y, textstr, fontsize=10, verticalalignment=textstr_alignment_v, horizontalalignment=textstr_alignment_h, bbox=textbox)

plt.title(title)
plt.show()
## TODO: Implement the following functions


def eigenvectors_DEV(data:System, levels=None, squared=False, scaling_factor=1):
Expand Down
20 changes: 6 additions & 14 deletions aton/qrotor/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,11 @@ def set_group(self, group:str=None, B:float=None):
self.group = group # No match was found
return self

def get_ideal_E(self, E_level):
"""Calculates the ideal energy for a specified `E_level` for a convergence test.
To use with `potential_name = 'zero'`.
def solve(self):
"""Solves the quantum system.
Same as `aton.qrotor.solve.energies(System)`.
"""
real_E_level = None
if self.potential_name != 'zero':
print("WARNING: System.get_ideal_E() only valid for potential_name = 'zero'")
return
if E_level % 2 == 0:
real_E_level = E_level / 2
else:
real_E_level = (E_level + 1) / 2
ideal_E = int(real_E_level ** 2)
return ideal_E
from .solve import energies
return energies(self)

17 changes: 16 additions & 1 deletion aton/qrotor/systems.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
# Description
This module contains functions to handle multiple `aton.qrotor.system` calculations.
This module contains utility functions to handle multiple `aton.qrotor.system` calculations.
# Index
Expand All @@ -15,6 +15,7 @@
| `get_groups()` | Get the chemical groups in use |
| `sort_by_gridsize()` | Sort systems by gridsize |
| `reduce_size()` | Discard data that takes too much space |
| `get_ideal_E()` | Calculate the ideal energy for a specified level |
---
"""
Expand Down Expand Up @@ -103,3 +104,17 @@ def reduce_size(systems:list) -> list:
dataset.grid = None
return systems


def get_ideal_E(E_level:int) -> int:
"""Calculates the ideal energy for a specified `E_level`.
To be used in convergence tests with `potential_name = 'zero'`.
"""
real_E_level = None
if E_level % 2 == 0:
real_E_level = E_level / 2
else:
real_E_level = (E_level + 1) / 2
ideal_E = int(real_E_level ** 2)
return ideal_E

2 changes: 1 addition & 1 deletion docs/aton.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
4 changes: 2 additions & 2 deletions docs/aton/_version.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down Expand Up @@ -116,7 +116,7 @@ <h1 class="modulename">
</span><span id="L-10"><a href="#L-10"><span class="linenos">10</span></a>
</span><span id="L-11"><a href="#L-11"><span class="linenos">11</span></a><span class="sd">&quot;&quot;&quot;</span>
</span><span id="L-12"><a href="#L-12"><span class="linenos">12</span></a>
</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="n">__version__</span> <span class="o">=</span> <span class="s1">&#39;v0.0.12&#39;</span>
</span><span id="L-13"><a href="#L-13"><span class="linenos">13</span></a><span class="n">__version__</span> <span class="o">=</span> <span class="s1">&#39;v0.0.13&#39;</span>
</span></pre></div>


Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/castep.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/phonopy.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/qe.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/interface/slurm.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys/atoms.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/phys/units.html
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/qrotor.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h2>Submodules</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
2 changes: 1 addition & 1 deletion docs/aton/qrotor/constants.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ <h2>API Documentation</h2>
</ul>


<footer>ATON v0.0.12 documentation</footer>
<footer>ATON v0.0.13 documentation</footer>

<a class="attribution" title="pdoc: Python API documentation generator" href="https://pdoc.dev" target="_blank">
built with <span class="visually-hidden">pdoc</span><img
Expand Down
Loading

0 comments on commit d6f48c3

Please sign in to comment.