目次

Tips and Chips

「チップじゃないんだ、ティップなんだよ!」

General

macOS

iOS

Linux

Web contents

Error Handling

From NetCDF to XYZ

Mon, 09 Nov 2009 13:06:28 +0000

#!/usr/bin/env python
import os
import tempfile
from optparse import OptionParser
 
from Dacapo import Dacapo
from ASE.Trajectories.NetCDFTrajectory import NetCDFTrajectory
from ASE.IO.xyz import WriteXYZ
 
cmd = OptionParser(usage = '%prog [-r R1 R2 R3] input_nc_file output_xyz_file')
cmd.add_option('-r', '--repeat', type = 'int', nargs = 3,
               help = 'Repeat R1, R2, R3 times along the three axes',
               metavar = 'R1 R2 R3')
 
(opt, argv) = cmd.parse_args()
 
if len(argv) != 2:
    cmd.print_help()
    raise SystemExit
 
ncfile = argv[0]
xyzfile = argv[1]
 
try:
  model = Dacapo.ReadAtoms(ncfile, index = 1)
  trajectory = True
except IndexError:
  trajectory = False
 
if not trajectory:
  model = Dacapo.ReadAtoms(ncfile)
  WriteXYZ(xyzfile, atoms = model, repeat = opt.repeat, id = ncfile)
else:
  model = Dacapo.ReadAtoms(ncfile, index = 0) 
  model.SetCalculator(None)
  tmpfile = tempfile.mktemp()
  xyzpath = NetCDFTrajectory(tmpfile, model)
  xyzpath.Update()
 
  frame = 0 
  error = False
  while not error: 
    frame += 1
    try: 
        atoms = Dacapo.ReadAtoms(ncfile, index = frame) 
        model.SetCartesianPositions(atoms.GetCartesianPositions())
        xyzpath.Update()
    except IndexError: 
        error = True
 
  xyzpath.Close()    
  xyzpath = NetCDFTrajectory(tmpfile)
  WriteXYZ(xyzfile, trajectory = xyzpath, repeat = opt.repeat, id = ncfile)
  os.remove(tmpfile)

From NetCDF to DOS

Tue, 10 Nov 2009 13:51:22 +0000

#!/usr/bin/env python
from optparse import OptionParser
from Dacapo import Dacapo
from ASE.Utilities.DOS import DOS
cmd = OptionParser(usage = '%prog [-r] input_nc_file output_text_file')
cmd.add_option('-r', '--reverse', action = "store_true", default = False,
help = 'reverse out put for minor spin states')
(opt, argv) = cmd.parse_args()
 
if len(argv) != 2:
  cmd.print_help()
  raise SystemExit
 
  ncfile = argv[0]
  textfile = argv[1]
  sfmt0 = '%-15s %-15s\n'
  sfmt1 = '%-15s %-15s %-15s %-15s\n'
  efmt0 = '%15.7E %15.7E\n'
  efmt1 = '%15.7E %15.7E %15.7E %15.7E\n'
  atoms = Dacapo.ReadAtoms(ncfile)
  calc = atoms.GetCalculator()
  isSpinPolarized = calc.GetSpinPolarized()
  dos = DOS(calc)
  x0 = dos.GetEnergies()
  if isSpinPolarized:
    y0 = dos.GetDOS(0)
    y1 = dos.GetDOS(1)
    fp = open(textfile, 'w')
    if opt.reverse:
      fp.write(sfmt1 % ('Energy', 'DOS0', 'DOS1', 'DIFF'))
    else:
      fp.write(sfmt1 % ('Energy', 'DOS0', 'DOS1', 'TOTAL'))
      for i in range(len(x0)):
        if opt.reverse:
          fp.write(efmt1 % (x0[i], y0[i], -y1[i], y0[i] - y1[i]))
        else:
          fp.write(efmt1 % (x0[i], y0[i],  y1[i], y0[i] + y1[i]))
      fp.close()
  else:
    y0 = dos.GetDOS(0)
    fp = open(textfile, 'w')
    fp.write(sfmt0 % ('Energy', 'DOS'))
    for i in range(len(x0)):
      fp.write(efmt0 % (x0[i], y0[i]))
    fp.close()

From NetCDF to CUBE (1)

Tue, 10 Nov 2009 13:55:13 +0000

Total density of states

#!/usr/bin/env python
import string
from math import log10
from optparse import OptionParser
 
from Dacapo import Dacapo
from ASE.IO.Cube import WriteCube
 
cmd = OptionParser(usage = '%prog input_nc_file output_cube_file')
 
(opt, argv) = cmd.parse_args()
 
if len(argv) != 2:
  cmd.print_help()
  raise SystemExit
 
ncfile = argv[0]
cubefile = argv[1]
basename = string.split(cubefile, '.cube')
 
model = Dacapo.ReadAtoms(ncfile)
calculator = model.GetCalculator()
 
if not calculator.GetSpinPolarized():
  filename_total= basename[0] + '.cube'
else:
  filename_total= basename[0] + '_total.cube'
  filename_diff= basename[0] + '_diff.cube'
  filename_spin0= basename[0] + '_spin0.cube'
  filename_spin1= basename[0] + '_spin1.cube'
  density0 = calculator.GetDensityArray(spin = 0)
  density1 = calculator.GetDensityArray(spin = 1)
  WriteCube(model, density0, filename_spin0)
  WriteCube(model, density1, filename_spin1)
  diff = density1 - density0
  WriteCube(model, diff, filename_diff)
 
density = calculator.GetDensityArray()
WriteCube(model, density, filename_total)