Skip to content

Commit

Permalink
Add methods to plot tau and vtau
Browse files Browse the repository at this point in the history
  • Loading branch information
gmatteo committed Jul 21, 2024
1 parent 500b511 commit 24115c5
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 3 deletions.
37 changes: 37 additions & 0 deletions abipy/ppcodes/oncv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,43 @@ def densities(self) -> dict[str, RadialFunction]:
rhoM=RadialFunction("Model charge", rho_data[:, 0], rho_data[:, 3])
)

@lazy_property
def kin_densities(self) -> dict[str, RadialFunction]:
"""
Dictionary with Kinetic energy densities on the radial mesh.
"""
if not self.is_metapsp:
raise ValueEror("kin_densities are only available in pseudos generated with metapsp")

# Metagga taups and taumodps
#!t 0.0200249 2.9590E+02 6.4665E+02
rho_data = self._grep("!t").data

return dict(
tau_ps=RadialFunction("Tau Pseudo", rho_data[:, 0], rho_data[:, 1]),
tau_modps=RadialFunction("Tau Model + Pseudo", rho_data[:, 0], rho_data[:, 2]),
)

@lazy_property
def vtaus(self) -> dict[str, RadialFunction]:
"""
Dictionary with Vtau pototentials on the radial mesh.
"""
if not self.is_metapsp:
raise ValueEror("kin_densities are only available in pseudos generated with metapsp")

# plot "<grep '!vt' t1" using 2:3 title "VtauAE" with lines ls 1,\
# "<grep '!vt' t1" using 2:4 title "Vtau(M+PS)" with lines ls 9

# !vt 0.1000394 4.1827E-03 1.9061E-02
# !vt 0.1024657 4.5435E-03 1.9061E-02
rho_data = self._grep("!vt").data

return dict(
vtau_ae=RadialFunction("Vtau AE", rho_data[:, 0], rho_data[:, 1]),
vtau_modps=RadialFunction("VTau Model + Pseudo", rho_data[:, 0], rho_data[:, 2]),
)

@lazy_property
def radial_wfs(self) -> AePsNamedTuple:
"""
Expand Down
103 changes: 100 additions & 3 deletions abipy/ppcodes/oncv_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ def _add_rc_vlines_ax(self, ax, with_lloc=False) -> None:
ax._custom_rc_lines.append((self.parser.rc5, color))

def plotly_atan_logders(self, *args, **kwargs):
mpl_fig = self.plot_atan_logders(*args, show=False, **kwargs)
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(mpl_fig)
return mpl_to_plotly(self.plot_atan_logders(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_atan_logders(self, ax=None, with_xlabel=True,
Expand Down Expand Up @@ -156,6 +156,10 @@ def _get_ae_ps_wfs(self, what) -> tuple:
raise ValueError(f"Invalid value for {what=}")
return ae_wfs, ps_wfs

def plotly_radial_wfs(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_radial_wfs(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_radial_wfs(self, ax=None, what="bound_states",
Expand All @@ -175,7 +179,7 @@ def plot_radial_wfs(self, ax=None, what="bound_states",
ps_wf, l, k = ps_wfs[nlk], nlk.l, nlk.k

if what == "bound_states":
# # Show position of the last peak.
# Show position of the last peak.
s, marker = 10, "^"
ae_peaks = ae_wf.get_peaks()
style = dict(color=self.color_l[l], s=s, marker=marker)
Expand All @@ -197,6 +201,11 @@ def plot_radial_wfs(self, ax=None, what="bound_states",

return fig

def plotly_projectors(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_projects(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_projectors(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:
"""
Expand Down Expand Up @@ -226,6 +235,11 @@ def plot_projectors(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:

return fig

def plotly_densities(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_densities(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_densities(self, ax=None, timesr2=False, fontsize: int = 8, **kwargs) -> Figure:
"""
Expand All @@ -248,6 +262,11 @@ def plot_densities(self, ax=None, timesr2=False, fontsize: int = 8, **kwargs) ->
)
return fig

def plotly_der_densities(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_der_densities(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_der_densities(self, ax=None, order=1, acc=4, fontsize=8, **kwargs) -> Figure:
"""
Expand Down Expand Up @@ -276,6 +295,11 @@ def plot_der_densities(self, ax=None, order=1, acc=4, fontsize=8, **kwargs) -> F
)
return fig

def plotly_potentials(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_potentials(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_potentials(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:
"""
Expand All @@ -298,6 +322,65 @@ def plot_potentials(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:

return fig

def plotly_vtau(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_vtau(*args, show=False, **kwargs))

def plot_vtau(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:
"""
Plot v_tau and v_tau(model+pseudo) potentials on axis ax.
Args:
ax: |matplotlib-Axes| or None if a new figure should be created.
"""
ax, fig, plt = get_ax_fig_plt(ax)

for key, pot in self.parser.vtaus.items():
mode = "ae" if key == "vtau_ae" else "ps"
ax.plot(pot.rmesh, pot.values,
label=pot.name,
**self._mpl_opts_laeps(0, mode))

self.decorate_ax(ax, xlabel="r (Bohr)", ylabel="Vtaus (Ha / a_B)",
#title="Ion Pseudopotentials",
fontsize=fontsize,
)
self._add_rc_vlines_ax(ax, with_lloc=True)

def plotly_tau(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_tau(*args, show=False, **kwargs))

def plot_tau(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:
"""
Plot kinetic energy densities tauPS and tau(M+PS) on axis ax.
Args:
ax: |matplotlib-Axes| or None if a new figure should be created.
"""
ax, fig, plt = get_ax_fig_plt(ax)

for key, den in self.parser.kin_densities.items():
#mode = "ae" if key == "tau_ps" else "ps"
ax.plot(den.rmesh, den.values,
label=den.name)
#**self._mpl_opts_laeps(0, mode))

self.decorate_ax(ax, xlabel="r (Bohr)", ylabel="Kinetic energy densities",
#title="Ion Pseudopotentials",
fontsize=fontsize,
)
self._add_rc_vlines_ax(ax, with_lloc=True)

return fig

def plotly_der_potentials(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_der_potentials(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_der_potentials(self, ax=None, order=1, acc=4, fontsize: int = 8, **kwargs) -> Figure:
"""
Expand Down Expand Up @@ -329,6 +412,11 @@ def plot_der_potentials(self, ax=None, order=1, acc=4, fontsize: int = 8, **kwar

return fig

def plotly_kene_vs_ecut(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_kene_vs_ecut(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_kene_vs_ecut(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:
"""
Expand Down Expand Up @@ -356,6 +444,11 @@ def plot_kene_vs_ecut(self, ax=None, fontsize: int = 8, **kwargs) -> Figure:

return fig

def plotly_atanlogder_econv(self, *args, **kwargs):
"""Generate plotly figure from matplotly."""
from plotly.tools import mpl_to_plotly
return mpl_to_plotly(self.plot_atan_logder_econv(*args, show=False, **kwargs))

@add_fig_kwargs
def plot_atanlogder_econv(self, ax_list=None, fontsize: int = 6, **kwargs) -> Figure:
"""
Expand Down Expand Up @@ -483,6 +576,10 @@ def yield_figs(self, **kwargs): # pragma: no cover
for order in [1, 2, 3, 4]:
yield self.plot_der_densities(order=order, show=False)

if self.parser.is_metapsp:
yield self.plot_vtau(show=False)
yield self.plot_tau(show=False)

def write_notebook(self, nbpath=None):
return oncv_make_open_notebook(self.parser.filepath)

Expand Down
1 change: 1 addition & 0 deletions abipy/scripts/oncv.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def oncv_plot(options) -> int:

out_path = _find_oncv_output(options.filepath)
plotter = OncvPlotter.from_file(out_path)

#plotter.plotly_atan_logders().show()
#return 0

Expand Down

0 comments on commit 24115c5

Please sign in to comment.