| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
from auto import AUTOExceptions |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class parseH(dict): |
| 39 |
|
| 40 |
line1_comment = "NUNSTAB,NSTAB,IEQUIB,ITWIST,ISTART" |
| 41 |
line2_comment = "NREV,/,IREV(I),I=1,NDIM)" |
| 42 |
line3_comment = "NFIXED,(/,I,IFIXED(I)),I=1,NFIXED)" |
| 43 |
line4_comment = "NPSI,(/,I,IPSI(I)),I=1,NPSI)" |
| 44 |
|
| 45 |
def __init__(self, filename=None): |
| 46 |
if filename is not None and type(filename) != type(""): |
| 47 |
dict.__init__(self, filename) |
| 48 |
return |
| 49 |
dict.__init__(self) |
| 50 |
for key in ['NUNSTAB', 'NSTAB', 'IEQUIB', 'ITWIST', 'ISTART', |
| 51 |
'NREV', 'NFIXED', 'NPSI', |
| 52 |
'IREV', 'IFIXED', 'IPSI']: |
| 53 |
self[key] = None |
| 54 |
if filename: |
| 55 |
self.readFilename(filename) |
| 56 |
|
| 57 |
def readFilename(self, filename): |
| 58 |
inputfile = open(filename, "r") |
| 59 |
self.read(inputfile) |
| 60 |
inputfile.close() |
| 61 |
|
| 62 |
def writeFilename(self, filename): |
| 63 |
output = open(filename, "w") |
| 64 |
self.write(output) |
| 65 |
output.close() |
| 66 |
|
| 67 |
def read(self, inputfile): |
| 68 |
line = inputfile.readline() |
| 69 |
data = line.split() |
| 70 |
self["NUNSTAB"] = int(data[0]) |
| 71 |
self["NSTAB"] = int(data[1]) |
| 72 |
self["IEQUIB"] = int(data[2]) |
| 73 |
self["ITWIST"] = int(data[3]) |
| 74 |
self["ISTART"] = int(data[4]) |
| 75 |
|
| 76 |
line = inputfile.readline() |
| 77 |
data = line.split() |
| 78 |
nrev = int(data[0]) |
| 79 |
data = [] |
| 80 |
if nrev > 0: |
| 81 |
line = inputfile.readline() |
| 82 |
data = line.split() |
| 83 |
self["IREV"] = map(int, data) |
| 84 |
self["NREV"] = nrev |
| 85 |
|
| 86 |
line = inputfile.readline() |
| 87 |
data = line.split() |
| 88 |
nfixed = int(data[0]) |
| 89 |
data = [] |
| 90 |
if nfixed > 0: |
| 91 |
line = inputfile.readline() |
| 92 |
data = line.split() |
| 93 |
self["IFIXED"] = map(int, data[:nfixed]) |
| 94 |
self["NFIXED"] = nfixed |
| 95 |
|
| 96 |
line = inputfile.readline() |
| 97 |
data = line.split() |
| 98 |
npsi = int(data[0]) |
| 99 |
data = [] |
| 100 |
if npsi > 0: |
| 101 |
line = inputfile.readline() |
| 102 |
data = line.split() |
| 103 |
self["IPSI"] = map(int, data[:npsi]) |
| 104 |
self["NPSI"] = npsi |
| 105 |
|
| 106 |
def __str__(self): |
| 107 |
olist = ["%s %s %s %s %s %s\n" % |
| 108 |
(self["NUNSTAB"], self["NSTAB"], self["IEQUIB"], |
| 109 |
self["ITWIST"], self["ISTART"], self.line1_comment)] |
| 110 |
|
| 111 |
nrev = 0 |
| 112 |
if len(self["IREV"]) > 0: |
| 113 |
nrev = 1 |
| 114 |
olist.append("%s %s\n"%(nrev, self.line2_comment)) |
| 115 |
if nrev > 0: |
| 116 |
olist.append(" ".join(map(str, self["IREV"]))+"\n") |
| 117 |
|
| 118 |
olist.append("%s %s\n"%(len(self["IFIXED"]), |
| 119 |
self.line3_comment)) |
| 120 |
if len(self["IFIXED"]) > 0: |
| 121 |
olist.append(" ".join(map(str, self["IFIXED"]))+"\n") |
| 122 |
|
| 123 |
olist.append("%s %s\n"%(len(self["IPSI"]), |
| 124 |
self.line4_comment)) |
| 125 |
if len(self["IPSI"]) > 0: |
| 126 |
olist.append(" ".join(map(str, self["IPSI"]))+"\n") |
| 127 |
return "".join(olist) |
| 128 |
|
| 129 |
def write(self, output): |
| 130 |
output.write(str(self)) |
| 131 |
|
| 132 |
def pointtest(a): |
| 133 |
keys = ['NUNSTAB', 'NSTAB', 'IEQUIB', 'ITWIST', 'ISTART', |
| 134 |
'NREV', 'IREV', 'NFIXED', 'IFIXED', 'NPSI', 'IPSI'] |
| 135 |
for key in keys: |
| 136 |
if key not in a: |
| 137 |
raise AUTOExceptions.AUTORegressionError("No %s label"%(key,)) |
| 138 |
|
| 139 |
def test(): |
| 140 |
print("Testing reading from a filename") |
| 141 |
foo = parseH() |
| 142 |
foo.readFilename("test_data/h.cir") |
| 143 |
pointtest(foo) |
| 144 |
|
| 145 |
print("Testing reading from a stream") |
| 146 |
foo = parseH() |
| 147 |
fp = open("test_data/h.cir","r") |
| 148 |
foo.read(fp) |
| 149 |
pointtest(foo) |
| 150 |
|
| 151 |
print("parseH passed all tests") |
| 152 |
|
| 153 |
if __name__ == '__main__' : |
| 154 |
test() |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|