Debian Codegraph @

07p/python/auto/parseH.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # Visualization for Bifurcation Manifolds
3 # Copyright (C) 1997 Randy Paffenroth and John Maddocks
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Library General Public License for more details.
14 #
15 # You should have received a copy of the GNU Library General Public
16 # License along with this library; if not, write to the Free
17 # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 # MA 02111-1307, USA
19
20 from auto import AUTOExceptions
21
22 # The parseH class parses an AUTO parameter file
23 # THESE EXPECT THE FILE TO HAVE VERY SPECIFIC FORMAT!
24 # it provides 4 methods:
25 # read and write take as an arguement either and input or output
26 # stream (basically any object with has the method "readline"
27 # for reading and "write" for writing)
28 #
29 # readFilename and writeFilename take as an arguement a filename
30 # in which to read/write the parameters (basically it opens the
31 # file and then calles "read" or "write"
32 #
33 # Once the data is read in the class provides a dictionary
34 # interface for manipulating the file.
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