-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathensight.py
510 lines (417 loc) · 16 KB
/
ensight.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# Pizza.py toolkit, www.cs.sandia.gov/~sjplimp/pizza.html
# Steve Plimpton, [email protected], Sandia National Laboratories
#
# 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.
# ensight tool
oneline = "Convert LAMMPS snapshots or meshes to Ensight format"
docstr = """
e = ensight(d) d = object with atoms or elements (dump,data,mdump)
e.change = 1 set to 1 if element nodal xyz change with time (def = 0)
e.maxtype = 10 max particle type, set if query to data will be bad
e.one()
e.one("new")
e.one("cns","Centro","eng","Energy")
e.one("new","cns","Centro","eng","Energy")
write all snapshots as an Ensight data set
Ensight header file = tmp.case (no 1st arg) or new.case
Ensight coord file = tmp.xyz or new.xyz
additional pairs of args create auxiliary files:
tmp.cns, tmp.eng or new.cns, new.eng
cns,eng = column name in dump file and file name suffix
Centro,Energy = Ensight name for the variable
e.increment() same args as one(), but process dump out-of-core
e.many() same args as one(), but create multiple Ensight files
tmp0000.xyz, tmp0001.xyz, etc
new0000.cns, new0001.cns, etc
new0000.eng, new0001.eng, etc
e.single(N) same args as one() prepended by N, but write a single snap
"""
# History
# 10/06, Steve Plimpton (SNL): original version
# ToDo list
# binary files
# create vector or tensor variable files, not just scalar
# via pair of args like ["vx","vy","vz"],"vel"
# Variables
# data = data file to read from
# which = 0 for particles, 1 for elements
# change = 0 for unchanging mesh coords, 1 for changing mesh coords (def = 0)
# Imports and external programs
import sys, types
# Class definition
class ensight:
# --------------------------------------------------------------------
def __init__(self,data):
self.change = 0
self.maxtype = 0
self.data = data
if type(data) is types.InstanceType and ".dump" in str(data.__class__):
self.which = 0
elif type(data) is types.InstanceType and ".data" in str(data.__class__):
self.which = 0
elif type(data) is types.InstanceType and ".mdump" in str(data.__class__):
self.which = 1
elif type(data) is types.InstanceType and ".cdata" in str(data.__class__):
self.which = 1
else:
raise StandardError,"unrecognized object passed to ensight"
# --------------------------------------------------------------------
def one(self,*args):
if len(args) % 2 == 0: root = "tmp"
else:
root = args[0]
args = args[1:]
pairs = []
for i in range(0,len(args),2): pairs.append([args[i],args[i+1]])
# max # of types for all steps in Ensight files
if self.which == 0 and self.maxtype == 0:
self.maxtype = self.data.maxtype()
# write Ensight *.case header file
f = open("%s.case" % root,"w")
times = self.data.time()
self.case_file(f,root,pairs,0,len(times),times)
f.close()
# open additional files
f = open(root + ".xyz","w")
vfiles = []
for pair in pairs: vfiles.append(open(root + "." + pair[0],"w"))
# loop over snapshots
# write coords into xyz file, variables into their files
first = 1
n = flag = etype = 0
while 1:
which,time,flag = self.data.iterator(flag)
if flag == -1: break
if self.which == 0:
print >>f,"BEGIN TIME STEP"
time,box,atoms,bonds,tris,lines = self.data.viz(which)
self.coord_file_atoms(f,box,atoms)
print >>f,"END TIME STEP"
elif self.change == 0 and first:
print >>f,"BEGIN TIME STEP"
time,box,nodes,elements,nvalues,evalues = self.data.mviz(which)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
first = 0
print >>f,"END TIME STEP"
elif self.change:
print >>f,"BEGIN TIME STEP"
time,box,nodes,elements,nvalues,evalues = self.data.mviz(which)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
print >>f,"END TIME STEP"
for i in range(len(pairs)):
print >>vfiles[i],"BEGIN TIME STEP"
values = self.data.vecs(time,pairs[i][0])
if self.which == 0:
self.variable_file_atoms(vfiles[i],pairs[i][1],atoms,values)
else:
self.variable_file_elements(vfiles[i],pairs[i][1],etype,values)
print >>vfiles[i],"END TIME STEP"
print time,
sys.stdout.flush()
n += 1
# close additional files
f.close()
for f in vfiles: f.close()
print "\nwrote %s snapshots in Ensight format" % n
# --------------------------------------------------------------------
def increment(self,*args):
if len(args) % 2 == 0: root = "tmp"
else:
root = args[0]
args = args[1:]
pairs = []
for i in range(0,len(args),2): pairs.append([args[i],args[i+1]])
# max # of types for all steps in Ensight files
if self.which == 0 and self.maxtype == 0:
self.maxtype = self.data.maxtype()
# open additional files
f = open(root + ".xyz","w")
vfiles = []
for pair in pairs: vfiles.append(open(root + "." + pair[0],"w"))
# loop over snapshots
# write coords into xyz file, variables into their files
times = []
first = 1
n = etype = 0
while 1:
time = self.data.next()
if time == -1: break
times.append(time)
self.data.tselect.one(time)
self.data.delete()
if self.which == 0:
print >>f,"BEGIN TIME STEP"
time,box,atoms,bonds,tris,lines = self.data.viz(0)
self.coord_file_atoms(f,box,atoms)
print >>f,"END TIME STEP"
elif self.change == 0 and first:
print >>f,"BEGIN TIME STEP"
time,box,nodes,elements,nvalues,evalues = self.data.mviz(0)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
first = 0
print >>f,"END TIME STEP"
elif self.change:
print >>f,"BEGIN TIME STEP"
time,box,nodes,elements,nvalues,evalues = self.data.mviz(0)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
print >>f,"END TIME STEP"
for i in range(len(pairs)):
print >>vfiles[i],"BEGIN TIME STEP"
values = self.data.vecs(time,pairs[i][0])
if self.which == 0:
self.variable_file_atoms(vfiles[i],pairs[i][1],atoms,values)
else:
self.variable_file_elements(vfiles[i],pairs[i][1],etype,values)
print >>vfiles[i],"END TIME STEP"
print time,
sys.stdout.flush()
n += 1
# close additional files
f.close()
for f in vfiles: f.close()
# write Ensight *.case header file now that know all timesteps
f = open("%s.case" % root,"w")
self.case_file(f,root,pairs,0,len(times),times)
f.close()
print "\nwrote %s snapshots in Ensight format" % n
# --------------------------------------------------------------------
def many(self,*args):
if len(args) % 2 == 0: root = "tmp"
else:
root = args[0]
args = args[1:]
pairs = []
for i in range(0,len(args),2): pairs.append([args[i],args[i+1]])
# max # of types for all steps in Ensight files
if self.which == 0 and self.maxtype == 0:
self.maxtype = self.data.maxtype()
# write Ensight *.case header file
f = open("%s.case" % root,"w")
times = self.data.time()
self.case_file(f,root,pairs,1,len(times),times)
f.close()
# loop over snapshots
# generate unique filenames
# write coords into one xyz file per snapshot, variables into their files
first = 1
n = flag = etype = 0
while 1:
which,time,flag = self.data.iterator(flag)
if flag == -1: break
files = []
if n < 10:
file = root + "000" + str(n) + ".xyz"
for pair in pairs:
files.append(root + "000" + str(n) + "." + pair[0])
elif n < 100:
file = root + "00" + str(n) + ".xyz"
for pair in pairs:
files.append(root + "00" + str(n) + "." + pair[0])
elif n < 1000:
file = root + "0" + str(n) + ".xyz"
for pair in pairs:
files.append(root + "0" + str(n) + "." + pair[0])
else:
file = root + str(n) + ".xyz"
for pair in pairs:
files.append(root + str(n) + "." + pair[0])
if self.which == 0:
f = open(file,"w")
time,box,atoms,bonds,tris,lines = self.data.viz(which)
self.coord_file_atoms(f,box,atoms)
f.close()
elif self.change == 0 and first:
f = open(root + ".xyz","w")
time,box,nodes,elements,nvalues,evalues = self.data.mviz(which)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
first = 0
f.close()
elif self.change:
f = open(file,"w")
time,box,nodes,elements,nvalues,evalues = self.data.mviz(which)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
f.close()
for i in range(len(pairs)):
values = self.data.vecs(time,pairs[i][0])
f = open(files[i],"w")
if self.which == 0:
self.variable_file_atoms(f,pairs[i][1],atoms,values)
else:
self.variable_file_elements(f,pairs[i][1],etype,values)
f.close()
print time,
sys.stdout.flush()
n += 1
print "\nwrote %s snapshots in Ensight format" % n
# --------------------------------------------------------------------
def single(self,time,*args):
if len(args) % 2 == 0: root = "tmp"
else:
root = args[0]
args = args[1:]
pairs = []
for i in range(0,len(args),2): pairs.append([args[i],args[i+1]])
# max # of types for all steps in Ensight files
if self.which == 0 and self.maxtype == 0:
self.maxtype = self.data.maxtype()
# write Ensight *.case header file
f = open("%s.case" % root,"w")
self.case_file(f,root,pairs,0,1,[time])
f.close()
# write coords into xyz file, variables into their files
which = self.data.findtime(time)
etype = 0
f = open(root + ".xyz","w")
if self.which == 0:
time,box,atoms,bonds,tris,lines = self.data.viz(which)
self.coord_file_atoms(f,box,atoms)
else:
time,box,nodes,elements,nvalues,evalues = self.data.mviz(which)
self.coord_file_elements(f,box,nodes,elements)
etype = len(elements[0])
f.close()
for i in range(len(pairs)):
values = self.data.vecs(time,pairs[i][0])
f = open(root + "." + pairs[i][0],"w")
if self.which == 0:
self.variable_file_atoms(f,pairs[i][1],atoms,values)
else:
self.variable_file_elements(f,pairs[i][1],etype,values)
f.close()
# --------------------------------------------------------------------
# write Ensight case file
def case_file(self,f,root,pairs,multifile,nsnaps,times):
print >>f,"# Ensight case file\n"
print >>f,"FORMAT\ntype: ensight gold\n"
if self.which == 0:
if multifile:
# print >>f,"GEOMETRY\nmodel: %s****.xyz change_coords_only\n" % root
print >>f,"GEOMETRY\nmodel: %s****.xyz\n" % root
else:
# print >>f,"GEOMETRY\nmodel: 1 1 %s.xyz change_coords_only\n" % root
print >>f,"GEOMETRY\nmodel: 1 1 %s.xyz\n" % root
else:
if self.change == 0:
print >>f,"GEOMETRY\nmodel: %s.xyz\n" % root
elif multifile:
print >>f,"GEOMETRY\nmodel: %s****.xyz\n" % root
else:
print >>f,"GEOMETRY\nmodel: 1 1 %s.xyz\n" % root
if len(pairs):
print >>f,"VARIABLE"
for pair in pairs:
if self.which == 0:
if multifile:
print >>f,"scalar per node: %s %s****.%s" % (pair[1],root,pair[0])
else:
print >>f,"scalar per node: 1 1 %s %s.%s" % (pair[1],root,pair[0])
else:
if multifile:
print >>f,"scalar per element: %s %s****.%s" % (pair[1],root,pair[0])
else:
print >>f,"scalar per element: 1 1 %s %s.%s" % (pair[1],root,pair[0])
print >>f
print >>f,"TIME"
print >>f,"time set: 1"
print >>f,"number of steps:",nsnaps
print >>f,"filename start number: 0"
print >>f,"filename increment: 1"
print >>f,"time values:"
for i in range(nsnaps):
print >>f,times[i],
if i % 10 == 9: print >>f
print >>f
print >>f
if not multifile:
print >>f,"FILE"
print >>f,"file set: 1"
print >>f,"number of steps:",nsnaps
# --------------------------------------------------------------------
# write Ensight coordinates for atoms
# partition into "parts"
# one part = coords for all atoms of a single type
def coord_file_atoms(self,f,box,atoms):
print >>f,"Particle geometry\nfor a collection of atoms"
print >>f,"node id given"
print >>f,"element id off"
print >>f,"extents"
print >>f,"%12.5e%12.5e" % (box[0],box[3])
print >>f,"%12.5e%12.5e" % (box[1],box[4])
print >>f,"%12.5e%12.5e" % (box[2],box[5])
for type in xrange(1,self.maxtype+1):
print >>f,"part"
print >>f,"%10d" % type
print >>f,"type",type
print >>f,"coordinates"
group = [atom for atom in atoms if int(atom[1]) == type]
print >>f,"%10d" % len(group)
for atom in group: print >>f,"%10d" % int(atom[0])
for atom in group: print >>f,"%12.5e" % atom[2]
for atom in group: print >>f,"%12.5e" % atom[3]
for atom in group: print >>f,"%12.5e" % atom[4]
print >>f,"point"
print >>f,"%10d" % len(group)
for i in xrange(1,len(group)+1): print >>f,"%10d" % i
# --------------------------------------------------------------------
# write Ensight coordinates for elements
def coord_file_elements(self,f,box,nodes,elements):
print >>f,"Element geometry\nfor a collection of elements"
print >>f,"node id given"
print >>f,"element id given"
print >>f,"extents"
print >>f,"%12.5e%12.5e" % (box[0],box[3])
print >>f,"%12.5e%12.5e" % (box[1],box[4])
print >>f,"%12.5e%12.5e" % (box[2],box[5])
print >>f,"part"
print >>f,"%10d" % 1
print >>f,"all elements"
print >>f,"coordinates"
print >>f,"%10d" % len(nodes)
for node in nodes: print >>f,"%10d" % int(node[0])
for node in nodes: print >>f,"%12.5e" % node[2]
for node in nodes: print >>f,"%12.5e" % node[3]
for node in nodes: print >>f,"%12.5e" % node[4]
if len(elements[0]) == 5: print >>f,"tria3"
elif len(elements[0]) == 6: print >>f,"tetra4"
else: raise StandardError,"unrecognized element type"
print >>f,"%10d" % len(elements)
for element in elements: print >>f,"%10d" % int(element[0])
if len(elements[0]) == 5:
for element in elements:
print >>f,"%10d%10d%10d" % \
(int(element[2]),int(element[3]),int(element[4]))
elif len(elements[0]) == 6:
for element in elements:
print >>f,"%10d%10d%10d%10d" % \
(int(element[2]),int(element[3]),int(element[4]),int(element[5]))
# --------------------------------------------------------------------
# write Ensight variable values for atoms
# partition into "parts"
# one part = values for all atoms of a single type
def variable_file_atoms(self,f,name,atoms,values):
print >>f,"Particle %s" % name
for type in xrange(1,self.maxtype+1):
print >>f,"part"
print >>f,"%10d" % type
print >>f,"coordinates"
group = [values[i] for i in xrange(len(atoms))
if int(atoms[i][1]) == type]
for value in group: print >>f,"%12.5e" % value
# --------------------------------------------------------------------
# write Ensight variable values for elements
def variable_file_elements(self,f,name,etype,values):
print >>f,"Element %s" % name
print >>f,"part"
print >>f,"%10d" % 1
if etype == 5: print >>f,"tria3"
elif etype == 6: print >>f,"tetra4"
for value in values: print >>f,"%12.5e" % value