File: cfg.py

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (189 lines) | stat: -rw-r--r-- 6,262 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Pizza.py toolkit, https://lammps.github.io/pizza
# LAMMPS development team: developers@lammps.org
#
# Copyright (2005) Sandia Corporation.  Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
# certain rights in this software.  This software is distributed under
# the GNU General Public License.

from __future__ import print_function

# cfg tool

oneline = "Convert LAMMPS snapshots to AtomEye CFG format"

docstr = """
c = cfg(d)              d = object containing atom coords (dump, data)

c.one()                 write all snapshots to tmp.cfg
c.one("new")            write all snapshots to new.cfg
c.many()                write snapshots to tmp0000.cfg, tmp0001.cfg, etc
c.many("new")           write snapshots to new0000.cfg, new0001.cfg, etc
c.single(N)             write snapshot for timestep N to tmp.cfg
c.single(N,"file")      write snapshot for timestep N to file.cfg
"""

# History
#   11/06, Aidan Thompson (SNL): original version

# ToDo list
# should decide if dump is scaled or not, since CFG prints in scaled coords
# this creates a simple AtomEye CFG format
#   there is more complex format we could write out
#   which allows for extra atom info, e.g. to do atom coloring on
# how to dump for a triclinic box, since AtomEye accepts this

# Variables
#   data = data file to read from

# Imports and external programs

import sys

# Class definition

class cfg:

  # --------------------------------------------------------------------

  def __init__(self,data):
    self.data = data

  # --------------------------------------------------------------------

  def one(self,*args):
    if len(args) == 0: file = "tmp.cfg"
    elif args[0][-4:] == ".cfg": file = args[0]
    else: file = args[0] + ".cfg"

    f = open(file,"w")
    n = flag = 0
    while 1:
      which,time,flag = self.data.iterator(flag)
      if flag == -1: break
      time,box,atoms,bonds,tris,lines = self.data.viz(which)

      xlen = box[3]-box[0]
      ylen = box[4]-box[1]
      zlen = box[5]-box[2]

      print("Number of particles = %d " % len(atoms), file=f)
      print("# Timestep %d \n#\nA = 1.0 Angstrom" % time, file=f)
      print("H0(1,1) = %20.10f A " % xlen, file=f)
      print("H0(1,2) = 0.0 A ", file=f)
      print("H0(1,3) = 0.0 A ", file=f)
      print("H0(2,1) = 0.0 A ", file=f)
      print("H0(2,2) = %20.10f A " % ylen, file=f)
      print("H0(2,3) = 0.0 A ", file=f)
      print("H0(3,1) = 0.0 A ", file=f)
      print("H0(3,2) = 0.0 A ", file=f)
      print("H0(3,3) = %20.10f A " % zlen, file=f)
      print("#", file=f)

      for atom in atoms:
        itype = int(atom[1])
        xfrac = (atom[2]-box[0])/xlen
        yfrac = (atom[3]-box[1])/ylen
        zfrac = (atom[4]-box[2])/zlen
#        print("1.0  %d   %15.10f  %15.10f  %15.10f  %15.10f  %15.10f  %15.10f " % (itype,xfrac,yfrac,zfrac,atom[5],atom[6],atom[7]), file=f)
        print("1.0  %d   %15.10f  %15.10f  %15.10f  0.0 0.0 0.0 " % (itype,xfrac,yfrac,zfrac), file=f)

      print(time)
      sys.stdout.flush()
      n += 1

    f.close()
    print("\nwrote %d snapshots to %s in CFG format" % (n,file))

  # --------------------------------------------------------------------

  def many(self,*args):
    if len(args) == 0: root = "tmp"
    else: root = args[0]

    n = flag = 0
    while 1:
      which,time,flag = self.data.iterator(flag)
      if flag == -1: break
      time,box,atoms,bonds,tris,lines = self.data.viz(which)

      if n < 10:
        file = root + "000" + str(n)
      elif n < 100:
        file = root + "00" + str(n)
      elif n < 1000:
        file = root + "0" + str(n)
      else:
        file = root + str(n)
      file += ".cfg"
      f = open(file,"w")

      xlen = box[3]-box[0]
      ylen = box[4]-box[1]
      zlen = box[5]-box[2]

      print("Number of particles = %d " % len(atoms), file=f)
      print("# Timestep %d \n#\nA = 1.0 Angstrom" % time, file=f)
      print("H0(1,1) = %20.10f A " % xlen, file=f)
      print("H0(1,2) = 0.0 A ", file=f)
      print("H0(1,3) = 0.0 A ", file=f)
      print("H0(2,1) = 0.0 A ", file=f)
      print("H0(2,2) = %20.10f A " % ylen, file=f)
      print("H0(2,3) = 0.0 A ", file=f)
      print("H0(3,1) = 0.0 A ", file=f)
      print("H0(3,2) = 0.0 A ", file=f)
      print("H0(3,3) = %20.10f A " % zlen, file=f)
      print("#", file=f)

      for atom in atoms:
        itype = int(atom[1])
        xfrac = (atom[2]-box[0])/xlen
        yfrac = (atom[3]-box[1])/ylen
        zfrac = (atom[4]-box[2])/zlen
#        print("1.0  %d   %15.10f  %15.10f  %15.10f  %15.10f  %15.10f  %15.10f " % (itype,xfrac,yfrac,zfrac,atom[5],atom[6],atom[7]), file=f)
        print("1.0  %d   %15.10f  %15.10f  %15.10f  0.0 0.0 0.0 " % (itype,xfrac,yfrac,zfrac), file=f)

      print(time)
      sys.stdout.flush()
      f.close()
      n += 1

    print("\nwrote %s snapshots in CFG format" % n)

  # --------------------------------------------------------------------

  def single(self,time,*args):
    if len(args) == 0: file = "tmp.cfg"
    elif args[0][-4:] == ".cfg": file = args[0]
    else: file = args[0] + ".cfg"

    which = self.data.findtime(time)
    time,box,atoms,bonds,tris,lines = self.data.viz(which)
    f = open(file,"w")

    xlen = box[3]-box[0]
    ylen = box[4]-box[1]
    zlen = box[5]-box[2]

    print("Number of particles = %d " % len(atoms), file=f)
    print("# Timestep %d \n#\nA = 1.0 Angstrom" % time, file=f)
    print("H0(1,1) = %20.10f A " % xlen, file=f)
    print("H0(1,2) = 0.0 A ", file=f)
    print("H0(1,3) = 0.0 A ", file=f)
    print("H0(2,1) = 0.0 A ", file=f)
    print("H0(2,2) = %20.10f A " % ylen, file=f)
    print("H0(2,3) = 0.0 A ", file=f)
    print("H0(3,1) = 0.0 A ", file=f)
    print("H0(3,2) = 0.0 A ", file=f)
    print("H0(3,3) = %20.10f A " % zlen, file=f)
    print("#", file=f)

    for atom in atoms:
      itype = int(atom[1])
      xfrac = (atom[2]-box[0])/xlen
      yfrac = (atom[3]-box[1])/ylen
      zfrac = (atom[4]-box[2])/zlen
#        print("1.0  %d   %15.10f  %15.10f  %15.10f  %15.10f  %15.10f  %15.10f " % (itype,xfrac,yfrac,zfrac,atom[5],atom[6],atom[7]), file=f)
      print("1.0  %d   %15.10f  %15.10f  %15.10f  0.0 0.0 0.0 " % (itype,xfrac,yfrac,zfrac), file=f)

    f.close()