QuantumATK Forum

QuantumATK => General Questions and Answers => Topic started by: jerry on October 10, 2012, 13:17

Title: calculate the absolute value of spin polarized density
Post by: jerry on October 10, 2012, 13:17
Dear,

I want to calculate the absolute value of spin polarized density, which is the absolute value of (Pup-Pdown)/(Pup+Pdown). How to use python script to code this?

Thank you very much!
Title: Re: calculate the absolute value of spin polarized density
Post by: Anders Blom on October 10, 2012, 13:27
Very much like you have it in the formula. Look at the small Python script at the end of http://quantumwise.com/publications/tutorials/mini-tutorials/141 where we compute pup-pdown. You would just add the denominator, and end up with a corresponding script like

Code: python
e_down = ElectronDensity(config, spin=Spin.Down)
e_up = ElectronDensity(config, spin=Spin.Up)
nlsave("file.nc", (e_up-e_down)/(e_up+e_down), object_id="Spin Polarization Density")
Title: Re: calculate the absolute value of spin polarized density
Post by: jerry on October 10, 2012, 14:47
First of all, thank you very much for your replying.

Actually, i have known how to calculate the normalized spin density difference. But this value can be both positive and negative, I want to change the negative part to be positive (The absolute value of the spin density difference). I have try the command abs, it does bot work because the spin density difference is a function rather than a number. Can you help me on this?
Title: Re: calculate the absolute value of spin polarized density
Post by: Anders Blom on October 10, 2012, 23:26
Anything is possible in Python - but not always so easy :)

Try this:

Code: python
import NLEngine
# Compute the electron density for spin up and down
eu = ElectronDensity(config, spin=Spin.Up)
ed = ElectronDensity(config, spin=Spin.Down)
# Calculate the polarization density
e_diff = (eu-ed)/(eu+ed)
# Extract the raw numbers - and take the absolute
data = numpy.abs(e_diff.toArray().flatten())
# Insert the absolute numbers back into the object
grid_descriptor = e_diff._GridValues__grid3d.gridDescriptor()
e_diff._GridValues__grid3d = NLEngine.RealGrid3D(grid_descriptor,NLEngine.doubleSequenceToRealVector(data),True)
# Save
nlsave("file.nc", e_diff)
Title: Re: calculate the absolute value of spin polarized density
Post by: jerry on October 11, 2012, 13:59
Thank you very much! It works well :)