The story of rubyist struggling with python :: Dict data with pycall
PyCall Ruby version Tips has an almost perfect correspondence report. In addition, numpy-specific notation
{.example}
#Access to the last line m in python[-1,:]Where to write
And so on, rubyist even has an easy-to-understand explanation of numpy.
It looks like it can be done easily. .. ..
However, when dealing with complex python data structures It's very difficult. The cause is that I don't know the key of dict. It's good when you take simple characters, but it doesn't seem to work well with slightly complicated characters.
Vasprun :: tdos or in pymatgen as follows Vasprun :: compolete \ _dos etc. can be taken.
However,
If p spd \ _dos = dosrun.complete \ _dos.get \ _spd \ _dos ()
{<OrbitalType.s: 0>: <pymatgen.electronic_structure.dos.Dos object at 0x102dd4710>, <OrbitalType.p: 1>: <pymatgen.electronic_structure.dos.Dos object at 0x102dc2dd8>, <OrbitalType.d: 2>: <pymatgen.electronic_structure.dos.Dos object at 0x102d962b0>}
Is output. I think this can be taken with spd \ _dos ('OrbitalType.s') No good. Therefore,
p s_d = spd_dos.to_a[0][1].densities
As
{.example}
array([ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00,
...
])
Make it python :: array and then
s_list = PyCall::List.(s_d.to_a[0][1]).to_a
As an array, hash is taken out, and vals is taken out, and it is converted into a data structure that can be interpreted by ruby with to \ _a.
Once you get used to it, you won't have much trouble because the same code is repeated, Maybe I'll forget it over time. I want you to incorporate it in lib, but it seems that only characters read dict directly. .. .. Would you like to write a code and try pull req?
Densities and energy can't be helped without knowing the name, You can find this by reading the source code of pymatgen. Apparently, to use pycall, you need to repeat this.
{.ruby}
require 'pycall/import'
include PyCall::Import
pyimport :sys
pyimport 'numpy', as: :np
pyfrom 'pymatgen.io.vasp.outputs', import: :Vasprun
# dos
dosrun = Vasprun.("../dos_calc_fine/vasprun.xml")
e_object = (dosrun.tdos.energies - dosrun.efermi)
energy = PyCall::List.(e_object).to_a
spd_dos = dosrun.complete_dos.get_spd_dos()
spd = PyCall::Dict.(spd_dos)
s_d = spd.to_a[0][1].densities
s_list = PyCall::List.(s_d.to_a[0][1]).to_a
p_d = spd.to_a[1][1].densities
p_list = PyCall::List.(p_d.to_a[0][1]).to_a
d_d = spd.to_a[2][1].densities
d_list = PyCall::List.(d_d.to_a[0][1]).to_a
tdos = dosrun.tdos.densities
t_list = PyCall::List.(tdos.to_a[0][1]).to_a
require 'numo/gnuplot'
Numo.gnuplot do
set yrange: '[-10:10]'
plot [s_list, energy, w: :l, title: 's'],
[p_list, energy, w: :l, title: 'p'],
[d_list, energy, w: :l, title: 'd'],
[t_list, energy, w: :l, title: 'total']
end
Recommended Posts