In [61]:
#Purpose: export the moments of selected atoms
#Author: Yue-Wen Fang
#Date: 27th September, 2017, NYU SHANGHAI
#pyinstaller -F *.py to generate the excutable binary file.
#Revision history:
#Date: 23th November, 2017, Kyoto University; output total magnetic moment of the selected ion
#Date: 2th Decemeber, 2017, Kyoto University; check if a file exsits
import math
import os
from tempfile import TemporaryFile

path = 'mag-fang.out'

###################################################
# Check if a file exsits
if os.path.exists(path):
    print('Congratuations, mag-fang.out exists.\n')
else:
    print('No mag-fang.out, please use getmag-fang to generate it.\n')
###################################################


#print('...x.........y.......z.....')
######*****for the first ion (number 13)
f = open(path, 'rt')
b = []

#c=str('13       ')
c=input('Input the number of the atom you want to analyze: ') #only an ion can be analyzed
#print(type(c))  c is a string
spaces='       '
c=c+spaces # c is a string with some spaces


for line in f:
#        if '13       ' in line:
        if c in line:
            a = str(line)
            print(a[33:40], sep=' ', end=' ')
            b.append(a[33:40])
x,y,z=b
f.close()

print('\n')
print(b)
print('\n')
print(type(x))
x=float(x)
y=float(y)
z=float(z)
total_mag_1=math.sqrt(abs(x)**2+abs(y)**2+abs(z)**2)
print('\n')
# print('the total magnetic moment is')
print(total_mag_1)

#with open(path1) as f1:
#    lis=[list(map(float,x.split()))  for x in f1 if x.strip()]
#print(lis)
##############################
#ref: https://stackoverflow.com/questions/12271503/python-read-numbers-from-text-file-and-put-into-list
# if x.strip() to skip blank lines
#use list(map(int,x.split()))  in case of python 3.x
#use map(int,x.split())  in case of python 2.x
##############################
#the usage of map
#map(int,'1 2 3 4'.split())
############################


print('\n')


Congratuations, mag-fang.out exists.

Input the number of the atom you want to analyze: 12
 -0.004   0.003   0.000 

[' -0.004', '  0.003', '  0.000']


<class 'str'>


0.005



In [17]:
from tempfile import TemporaryFile

with TemporaryFile('w+t') as f:
    #READ/WRITE to the file
    f.write('Hello World\n')
    f.write('Testing\n')
    
    #Seek back to begining and read the data
    f.seek(0)
    data = f.read()

#Temporary file is removed

In [51]:
x='xxx'
y='yyy'
z=x+y
print(z)


xxxyyy

In [94]:
NumberOfAtoms=input('Input the numbers of atoms in POSCAR, if there are several ions, please use comma.\n')
print(NumberOfAtoms)
print(type(NumberOfAtoms))


NumberOfAtoms=NumberOfAtoms.split(",")

print(type(NumberOfAtoms))
print(NumberOfAtoms)

space='       '

for ion in NumberOfAtoms:
    ion_with_space = ion + space
    print(ion_with_space)
    
# print(NumberOfAtoms[0])
# print(NumberOfAtoms[1])

# for i in NumberOfAtoms:
#     print(NumberOfAtoms[i])
    
#     ion_string=str(NumberOfAtoms[i])+space
#     print(ion_string)

# list=[int(y) for y in x]
# print(list)
# print(type(list))


# print(type(x))
# print(x)
# for i in x:
#     print(i)


Input the numbers of atoms in POSCAR, if there are several ions, please use comma.
13,14
13,14
<class 'str'>
<class 'list'>
['13', '14']
13       
14       

In [28]:
a='xyz'
a=list(a)
type(a)
print(type(a))
print(a)


<class 'list'>
['x', 'y', 'z']

In [44]:
for i in ['1','2']:
    print(i)
    print(type(i))


1
<class 'str'>
2
<class 'str'>

In [47]:
a='xxx'
b='zzzz'
c=a+b
print(c)


xxxzzzz

In [57]:
x=[1,2,3,4]
for z in x:
    print(z)


1
2
3
4

In [97]:
#Purpose: export the moments of selected atoms
#Author: Yue-Wen Fang
#Date: 27th September, 2017, NYU SHANGHAI
#pyinstaller -F *.py to generate the excutable binary file.
#Revision history:
#Date: 23th November, 2017, Kyoto University; output total magnetic moment of the selected ion
#Date: 2th December, 2017, Kyoto University; check if a file exsits
#Date: 3th December, 2017, Kyoto University; interactive with keyboard input, now we can select atoms through keyboard
import math
import os
from tempfile import TemporaryFile

path = 'mag-fang.out'

###################################################
# Check if a file exsits
if os.path.exists(path):
    print('Congratuations, mag-fang.out exists.\n')
else:
    print('No mag-fang.out, please use getmag-fang to generate it.\n')
###################################################


###################################################
# Input the atoms you are intested in, the number must be consistent to the POSCAR
NumberOfAtoms=input('Input the numbers of atoms in POSCAR, if there are several ions, please use comma.')
print('\n')
#print(NumberOfAtoms)
#print(type(NumberOfAtoms))


NumberOfAtoms=NumberOfAtoms.split(",")

#print(type(NumberOfAtoms))
#print(NumberOfAtoms)

space='       ' 
# I add space here so that I can locate the number in the mag-fang.out file
###################################################


###################################################


# this is a temp variable for stroing the magnetic information

for ion in NumberOfAtoms:
    f = open(path, 'rt')
    print(ion)
    Store_Mag = [] 
    ion_with_space = ion + space
#    print(ion_with_space)
    for line in f:
        if ion_with_space in line:
            Line_String = str(line)
#            print(Line_String[33:40], sep=' ', end=' ')
            Store_Mag.append(Line_String[33:40])
#            print(Store_Mag)

    mag_x, mag_y, mag_z = Store_Mag[:]
    mag_x=float(mag_x)
    mag_y=float(mag_y)
    mag_z=float(mag_z)
    total_mag=math.sqrt(abs(mag_x)**2+abs(mag_y)**2+abs(mag_z)**2)
    print('...x....y....z....total')
    print(mag_x, mag_y, mag_z, total_mag)
    
    f.close
    print('\n')


Congratuations, mag-fang.out exists.

Input the numbers of atoms in POSCAR, if there are several ions, please use comma.13,14,15,16


13
...x....y....z....total
-1.287 1.243 -0.0 1.7892506811511908


14
...x....y....z....total
1.287 -1.243 -0.0 1.7892506811511908


15
...x....y....z....total
-1.287 -1.243 -0.0 1.7892506811511908


16
...x....y....z....total
1.287 1.243 0.0 1.7892506811511908



In [ ]: