 # Cube file from arbitrary density matrix

Hi,
I am currently using the Psi4 Python API and I am guessing if it would be possible to generate the cube files from an arbitrary density matrix. Let’s suppose that I need to visually compare the ground state density ( ie.
ene,wfn = psi4.energy(‘scf’,return_wfn=True) , cubeprop(wfn) ) to the density corresponding to a unitary transformed density matrix. Any suggestion is welcome and really appreciated. Thank you for your time.
Matteo

Hi Matteo,
It seems like the `cubeprop()` function takes the density matrix directly from the `wfn` object (ref: this line in the source code). You can overwrite the original density matrix in the wavefunction with the desired one like so:

``````import psi4
import numpy as np

ch4 = psi4.core.Molecule.create_molecule_from_string("""
0 1
C
H  1 1.099503
H  1 1.099503  2 109.471209
H  1 1.099503  2 109.471209    3  120.0
H  1 1.099503  2 109.471209    3  240.0
""")

psi4.set_options({'basis': 'sto-3g'})
E, wfn = psi4.energy('scf', molecule=ch4, return_wfn=True)

# get original density matrix for analysis (numpy)
Da_np = psi4.core.Matrix.to_array(wfn.Da())
Db_np = psi4.core.Matrix.to_array(wfn.Db())
# perform manipulations on the numpy matrices as needed
Da_new = psi4.core.Matrix.from_array(Da_np)
Db_new = psi4.core.Matrix.from_array(Db_np)
# overwrite density matrices in wfn
wfn.Da().copy(Da_new)
wfn.Db().copy(Db_new)

# write cube files
psi4.cubeprop(wfn)
``````

If you already have the new matrices in `psi4.core.Matrix` format, you can skip the NumPy part and do the copying directly. I think this approach should generate cube files using the new density matrices, but let me know if it doesn’t and we can try and sort out what’s wrong.

Thank you so much. Your example is nice and completely reasonable. I missed the .copy() for the wfn.Da(). I think your suggestion will fit my needs.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.