mirror of https://gitlab.com/scemama/eplf
154 lines
4.1 KiB
Python
Executable File
154 lines
4.1 KiB
Python
Executable File
#!/usr/bin/python
|
|
|
|
from math import *
|
|
|
|
import input_wrapper
|
|
InputFile = input_wrapper.InputFile
|
|
rw_data = list(input_wrapper.rw_data)
|
|
rw_data.sort()
|
|
ro_data = list(input_wrapper.rw_data)
|
|
ro_data.sort()
|
|
|
|
import sys, os
|
|
import tempfile
|
|
|
|
editor = os.getenv("EDITOR",default="vi")
|
|
|
|
## Data editing
|
|
###############
|
|
|
|
class Editor(object):
|
|
def __init__(self,inp,name):
|
|
self.dir = inp.name
|
|
self.name = name
|
|
self.inp = inp
|
|
def action(self):
|
|
raise TypeError
|
|
def edit(self):
|
|
print "Editing "+self.name
|
|
self.action()
|
|
|
|
class GeometryEditor(Editor):
|
|
def action(self):
|
|
self.charge = self.inp.nucl_charge
|
|
self.coord = self.inp.nucl_coord
|
|
edit_temp_file(self.inp,self.write_geom_file,self.read_geom_file)
|
|
|
|
def write_geom_file(self,file,inp):
|
|
print >>file, "###########################"
|
|
print >>file, "# Geometry : %s"%(inp.name)
|
|
print >>file, "###########################"
|
|
print >>file, ""
|
|
has_fitcusp = False
|
|
has_label = False
|
|
fields = "Charge X Y Z"
|
|
format = "%10.1f "+"%10.6f "*3
|
|
fields = fields.split()
|
|
print >>file, " ".join(["#"]+map(lambda x: x.center(10),fields))
|
|
print >>file, "# "+"-"*11*len(fields)+"\n"
|
|
charge = self.charge
|
|
coord = self.coord
|
|
for i in xrange(len(charge)):
|
|
buffer = [ charge[i], coord[0][i], coord[1][i], coord[2][i] ]
|
|
print >>file, " "+format%tuple(buffer)
|
|
|
|
def read_geom_file(self,file,inp):
|
|
lines = file.readlines()
|
|
lines = filter(lambda x: len(x.strip())>0,lines)
|
|
lines = filter(lambda x: not x.startswith("#"),lines)
|
|
lines = map(lambda x: x.split(),lines)
|
|
coord = [ [], [], [] ]
|
|
charge = []
|
|
for line in lines:
|
|
charge.append(float(line[-4]))
|
|
coord[0].append(float(line[-3]))
|
|
coord[1].append(float(line[-2]))
|
|
coord[2].append(float(line[-1]))
|
|
inp.coord = coord
|
|
|
|
|
|
def edit(x,inp):
|
|
d = { "geometry": GeometryEditor,
|
|
}
|
|
d[x](inp,x.replace("_"," ")).edit()
|
|
|
|
## Write temporary input file
|
|
#############################
|
|
|
|
def write_main_file(file,inp):
|
|
print >>file, "####################"
|
|
print >>file, "# %s"%(inp.name)
|
|
print >>file, "####################"
|
|
print >>file, ""
|
|
print >>file, "# Edit"
|
|
print >>file, "# --------------------\n"
|
|
print >>file, '# edit(geometry)'
|
|
print >>file, '# edit(grid_parameters)'
|
|
print >>file, ""
|
|
print >>file, "# Computation"
|
|
print >>file, "# --------------------\n"
|
|
compute = filter(lambda x: x.startswith("compute"),rw_data)
|
|
compute = filter(lambda x: not x.endswith("_grad"),compute)
|
|
compute = filter(lambda x: not x.endswith("_lapl"),compute)
|
|
for p in compute:
|
|
x = ' '
|
|
exec "if inp.%s: x = 'X'"%(p)
|
|
print >>file, "(%s) %s"%(x,p[8:])
|
|
print >>file, ""
|
|
|
|
## Execute temporary input file
|
|
###############################
|
|
|
|
def read_main_file(file,inp):
|
|
|
|
lines = file.readlines()
|
|
for line in lines:
|
|
line = line.lstrip()
|
|
if line == "":
|
|
pass
|
|
elif line[0] == "(":
|
|
line = line.replace("( ) ","=False inp.compute_")
|
|
line = line.replace("(X) ","=True inp.compute_")
|
|
line = line.replace("(x) ","=True inp.compute_")
|
|
buffer = line.split()
|
|
line = ' '.join([buffer[1].lower(),buffer[0]])
|
|
elif line.startswith("edit"):
|
|
line = line.replace("(","('").replace(")","',inp)").lower()
|
|
exec line
|
|
|
|
|
|
def edit_temp_file(input_file,write_file,read_file,saved_file=None):
|
|
if saved_file is None:
|
|
file,filename = tempfile.mkstemp()
|
|
file = open(filename,"w")
|
|
write_file(file,input_file)
|
|
file.close()
|
|
os.system("%s %s"%(editor,filename))
|
|
else:
|
|
file = open(saved_file,'r')
|
|
buffer = file.read()
|
|
file.close()
|
|
|
|
file,filename = tempfile.mkstemp()
|
|
file = open(filename,"w")
|
|
file.write(buffer)
|
|
file.close()
|
|
file = open(filename,"r")
|
|
read_file(file,input_file)
|
|
file.close()
|
|
|
|
os.remove(filename)
|
|
|
|
def main():
|
|
if len(sys.argv) not in [2,3]:
|
|
print "Syntax : %s <EZFIO_File> [input_file]"%(sys.argv[0])
|
|
sys.exit(1)
|
|
inp = InputFile(sys.argv[1])
|
|
if len(sys.argv) == 3:
|
|
saved_file = sys.argv[2]
|
|
else:
|
|
saved_file = None
|
|
edit_temp_file(inp,write_main_file,read_main_file,saved_file)
|
|
|
|
main()
|