| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from auto import AUTOutil |
| 5 |
from auto import parseB |
| 6 |
from auto import parseC |
| 7 |
from auto import parseH |
| 8 |
from auto import parseS |
| 9 |
from auto import parseD |
| 10 |
from auto import Points |
| 11 |
from auto import AUTOExceptions |
| 12 |
import gzip |
| 13 |
import types |
| 14 |
import sys |
| 15 |
import struct |
| 16 |
|
| 17 |
class bifDiag(parseB.parseBR): |
| 18 |
|
| 19 |
|
| 20 |
nonekeys = set(["IRS", "PAR", "U", "sv", "s", "dat", "TY", "LAB", "IBR"]) |
| 21 |
|
| 22 |
def __init__(self,fort7_filename=None,fort8_filename=None, |
| 23 |
fort9_filename=None,constants=None): |
| 24 |
if (constants is not None and constants.get('sv') is not None and |
| 25 |
type(fort7_filename) in (type(""),type(None)) and |
| 26 |
type(fort8_filename) in (type(""),type(None)) and |
| 27 |
type(fort9_filename) in (type(""),type(None))): |
| 28 |
|
| 29 |
self.c = constants |
| 30 |
self.filenames = [fort7_filename,fort8_filename,fort9_filename] |
| 31 |
else: |
| 32 |
self.__realinit(fort7_filename,fort8_filename,fort9_filename, |
| 33 |
constants) |
| 34 |
|
| 35 |
def __realinit(self,fort7_filename,fort8_filename,fort9_filename,constants): |
| 36 |
ioerrors = [] |
| 37 |
try: |
| 38 |
parseB.parseBR.__init__(self,fort7_filename) |
| 39 |
for i, d in enumerate(self): |
| 40 |
self[i] = bifDiagBranch(d) |
| 41 |
except IOError: |
| 42 |
ioerrors.append(str(sys.exc_info()[1])) |
| 43 |
parseB.parseBR.__init__(self) |
| 44 |
fort7_filename = None |
| 45 |
if isinstance(fort8_filename, parseS.AUTOSolution): |
| 46 |
fort8_filename = [fort8_filename] |
| 47 |
try: |
| 48 |
solution = parseS.parseS(fort8_filename) |
| 49 |
except IOError: |
| 50 |
ioerrors.append(str(sys.exc_info()[1])) |
| 51 |
solution = None |
| 52 |
if fort7_filename is None: |
| 53 |
raise AUTOExceptions.AUTORuntimeError('\n'.join(ioerrors)) |
| 54 |
if fort7_filename is None and fort8_filename is not None: |
| 55 |
|
| 56 |
labels = {} |
| 57 |
for s in solution: |
| 58 |
br = s["Branch number"] |
| 59 |
if labels == {} or br != branch.BR: |
| 60 |
if labels != {}: |
| 61 |
branch.labels = Points.PointInfo(labels) |
| 62 |
branch = bifDiagBranch() |
| 63 |
self.append(branch) |
| 64 |
branch.BR = br |
| 65 |
branch.coordarray = [] |
| 66 |
branch.coordnames = [] |
| 67 |
branch.headernames = [] |
| 68 |
branch.headerlist = [] |
| 69 |
branch.c = constants |
| 70 |
labels = {} |
| 71 |
base = 0 |
| 72 |
i = 0 |
| 73 |
pt = s["PT"] |
| 74 |
lab = s["LAB"] |
| 75 |
ty = s["TY number"] |
| 76 |
if i >= base + pt - 1: |
| 77 |
|
| 78 |
base += 9999 |
| 79 |
i = base + pt - 1 |
| 80 |
labels[i] = {s["TY"]: {"LAB":lab,"TY number":ty,"PT":pt}} |
| 81 |
if labels != {}: |
| 82 |
branch.labels = Points.PointInfo(labels) |
| 83 |
if fort9_filename is not None and self.data != []: |
| 84 |
try: |
| 85 |
|
| 86 |
self[0].diagnostics = parseD.parseD(fort9_filename) |
| 87 |
except IOError: |
| 88 |
pass |
| 89 |
i = 0 |
| 90 |
if solution is not None: |
| 91 |
for d in self: |
| 92 |
if constants is None and d.c is not None: |
| 93 |
constants = d.c |
| 94 |
constants = parseC.parseC(constants) |
| 95 |
for k in constants: |
| 96 |
if k in self.nonekeys: |
| 97 |
constants[k] = None |
| 98 |
for ind in d.labels.getIndices(): |
| 99 |
if i >= len(solution): |
| 100 |
break |
| 101 |
x = d._gettypelabel(ind)[1] |
| 102 |
s = solution[i] |
| 103 |
if x.get("LAB",0) != 0 or s["LAB"] == 0: |
| 104 |
i = i+1 |
| 105 |
s = x["solution"] = parseS.AUTOSolution( |
| 106 |
s, constants=constants) |
| 107 |
if d.coordnames != []: |
| 108 |
s.b = d[ind] |
| 109 |
|
| 110 |
|
| 111 |
def __getattr__(self,attr): |
| 112 |
if attr == 'c': |
| 113 |
if len(self)>0 and self[0].c is not None: |
| 114 |
return self[0].c |
| 115 |
elif attr == 'data': |
| 116 |
if hasattr(self,'filenames'): |
| 117 |
self.__realinit(self.filenames[0], self.filenames[1], |
| 118 |
self.filenames[2], self.c) |
| 119 |
del self.filenames |
| 120 |
del self.c |
| 121 |
return self.data |
| 122 |
return super(bifDiag, self).__getattribute__(attr) |
| 123 |
|
| 124 |
def __repr__(self): |
| 125 |
result = id(self) |
| 126 |
if result < 0: |
| 127 |
|
| 128 |
result += 256 ** struct.calcsize('P') |
| 129 |
return "<_=%s instance at %#010x>"%(self.__class__.__name__,result) |
| 130 |
|
| 131 |
def getLabel(self,label): |
| 132 |
sols = parseS.parseS() |
| 133 |
|
| 134 |
mbr = max([abs(d["BR"]) for d in self] or [None]) |
| 135 |
mlab = max(self.getLabels() or [None]) |
| 136 |
for d in self: |
| 137 |
sols.extend(d.getLabel(None,mbr=mbr,mlab=mlab)) |
| 138 |
return sols(label) |
| 139 |
|
| 140 |
def __call__(self,label=None): |
| 141 |
return self.getLabel(label) |
| 142 |
|
| 143 |
def load(self,**kw): |
| 144 |
"""Load solution with the given AUTO constants. |
| 145 |
Returns a shallow copy with a copied set of updated constants |
| 146 |
""" |
| 147 |
return self().load(**kw) |
| 148 |
|
| 149 |
def read(self,fort7_input,fort8_input=None,fort9_input=None): |
| 150 |
parseB.parseBR.read(self,fort7_input) |
| 151 |
for i, d in enumerate(self): |
| 152 |
self[i] = bifDiagBranch(d) |
| 153 |
if fort8_input is not None and ( |
| 154 |
isinstance(fort8_input, (file, gzip.GzipFile))): |
| 155 |
solution = parseS.parseS() |
| 156 |
solution.read(fort8_input) |
| 157 |
i = 0 |
| 158 |
for d in self: |
| 159 |
for k,x in map(d._gettypelabel, d.labels.getIndices()): |
| 160 |
if x.get("LAB",0) != 0: |
| 161 |
x["solution"] = solution[i] |
| 162 |
i = i+1 |
| 163 |
if fort9_input is not None: |
| 164 |
diagnostics = parseD.parseD() |
| 165 |
diagnostics.read(fort9_input) |
| 166 |
|
| 167 |
self[0].diagnostics = diagnostics |
| 168 |
|
| 169 |
def write(self,fort7_output,fort8_output=None,fort9_output=None): |
| 170 |
parseB.parseBR.write(self,fort7_output) |
| 171 |
if fort8_output is not None: |
| 172 |
self().write(fort8_output) |
| 173 |
if fort9_output is not None: |
| 174 |
for d in self: |
| 175 |
if hasattr(d,"diagnostics"): |
| 176 |
d.diagnostics.write(fort9_output) |
| 177 |
|
| 178 |
def readFilename(self,fort7_filename,fort8_filename=None,fort9_filename=None): |
| 179 |
parseB.parseBR.readFilename(self,fort7_filename) |
| 180 |
for i, d in enumerate(self): |
| 181 |
self[i] = bifDiagBranch(d) |
| 182 |
if fort8_filename is not None and isinstance(fort8_filename, str): |
| 183 |
solution = parseS.parseS(fort8_filename) |
| 184 |
i = 0 |
| 185 |
for d in self: |
| 186 |
for k,x in map(d._gettypelabel, d.labels.getIndices()): |
| 187 |
if x.get("LAB",0) != 0: |
| 188 |
x["solution"] = solution[i] |
| 189 |
i = i+1 |
| 190 |
if not fort9_filename is None: |
| 191 |
|
| 192 |
self[0].diagnostics = parseD.parseD(fort9_filename) |
| 193 |
|
| 194 |
def writeFilename(self,fort7_filename,fort8_filename=None,fort9_filename=None,append=False): |
| 195 |
|
| 196 |
if fort8_filename is None: |
| 197 |
fort8_filename = fort7_filename |
| 198 |
elif len(self) > 0 and len(self[0]) > 0: |
| 199 |
parseB.parseBR.writeFilename(self,fort7_filename,append) |
| 200 |
if fort8_filename != '': |
| 201 |
self().writeFilename(fort8_filename,append) |
| 202 |
if not fort9_filename is None: |
| 203 |
for d in self: |
| 204 |
if hasattr(d,"diagnostics"): |
| 205 |
d.diagnostics.writeFilename(fort9_filename,append) |
| 206 |
append=True |
| 207 |
|
| 208 |
|
| 209 |
def deleteLabel(self,label=None,keepTY=0,keep=0,copy=0): |
| 210 |
|
| 211 |
if isinstance(label, types.FunctionType): |
| 212 |
deletesols = self(label) |
| 213 |
data = [] |
| 214 |
for d in self: |
| 215 |
label = [s["LAB"] for s in deletesols if s.b.branch is d] |
| 216 |
data.append(d.deleteLabel(label,keepTY,keep,copy)) |
| 217 |
if copy: |
| 218 |
return self.__class__(data) |
| 219 |
return |
| 220 |
return parseB.parseBR.deleteLabel(self,label,keepTY,keep,copy) |
| 221 |
|
| 222 |
class bifDiagBranch(parseB.AUTOBranch): |
| 223 |
def __init__(self,input=None): |
| 224 |
parseB.AUTOBranch.__init__(self,input) |
| 225 |
|
| 226 |
def __getattr__(self,attr): |
| 227 |
if attr == "diagnostics": |
| 228 |
raise AttributeError |
| 229 |
return super(bifDiagBranch, self).__getattr__(attr) |
| 230 |
|
| 231 |
def getLabel(self,label,mbr=None,mlab=None): |
| 232 |
sols = [] |
| 233 |
for idx in self.labels.getIndices(): |
| 234 |
x = self._gettypelabel(idx)[1] |
| 235 |
if "solution" in x: |
| 236 |
br = abs(self["BR"]) |
| 237 |
pt = idx%9999 + 1 |
| 238 |
lab = x["LAB"] |
| 239 |
ty = x["TY number"] |
| 240 |
sol = x["solution"] |
| 241 |
if (sol._mlab != mlab or sol._mbr != mbr or |
| 242 |
br != sol["BR"] or pt != sol["PT"] or |
| 243 |
ty != sol["TY number"] or lab != sol["LAB"]): |
| 244 |
sol = sol.__class__(sol, BR=br, PT=pt, |
| 245 |
LAB=lab, TY=ty) |
| 246 |
sol._mlab = mlab |
| 247 |
sol._mbr = mbr |
| 248 |
sols.append(sol) |
| 249 |
return parseS.parseS(sols)(label) |
| 250 |
|
| 251 |
def load(self,**kw): |
| 252 |
"""Load solution with the given AUTO constants. |
| 253 |
Returns a shallow copy with a copied set of updated constants |
| 254 |
""" |
| 255 |
return self().load(**kw) |
| 256 |
|
| 257 |
def write(self,fort7_output,fort8_output=None,fort9_output=None): |
| 258 |
parseB.AUTOBranch.write(self,fort7_output) |
| 259 |
if fort8_output is not None: |
| 260 |
self().write(fort8_output) |
| 261 |
if fort9_output is not None: |
| 262 |
if hasattr(self,"diagnostics"): |
| 263 |
self.diagnostics.write(fort9_output) |
| 264 |
|
| 265 |
def writeFilename(self,fort7_filename,fort8_filename=None,fort9_filename=None,append=False): |
| 266 |
|
| 267 |
if fort8_filename is None: |
| 268 |
fort8_filename = fort7_filename |
| 269 |
elif len(self) > 0: |
| 270 |
parseB.AUTOBranch.writeFilename(self,fort7_filename,append) |
| 271 |
if fort8_filename != '': |
| 272 |
self().writeFilename(fort8_filename,append) |
| 273 |
if not fort9_filename is None: |
| 274 |
if hasattr(self,"diagnostics"): |
| 275 |
self.diagnostics.writeFilename(fort9_filename,append) |
| 276 |
append=True |
| 277 |
|
| 278 |
def deleteLabel(self,label=None,keepTY=0,keep=0,copy=0): |
| 279 |
|
| 280 |
|
| 281 |
if isinstance(label, types.FunctionType): |
| 282 |
label = [s["LAB"] for s in self(label)] |
| 283 |
new = parseB.AUTOBranch.deleteLabel(self,label,keepTY,keep,copy) |
| 284 |
if keepTY: |
| 285 |
if not copy: |
| 286 |
new = self |
| 287 |
for idx in new.labels.getIndices(): |
| 288 |
x = new._gettypelabel(idx)[1] |
| 289 |
if x["LAB"] ==0 and "solution" in x: |
| 290 |
del x["solution"] |
| 291 |
if copy: |
| 292 |
if hasattr(self,"diagnostics"): |
| 293 |
new.diagnostics = self.diagnostics |
| 294 |
return new |
| 295 |
|
| 296 |
def relabel(self,old_label=1,new_label=None): |
| 297 |
"""Relabels the first solution with the given label""" |
| 298 |
if new_label is None: |
| 299 |
new = self.__class__(parseB.AUTOBranch.relabel(self,old_label)) |
| 300 |
if hasattr(self,"diagnostics"): |
| 301 |
new.diagnostics = self.diagnostics |
| 302 |
return new |
| 303 |
parseB.AUTOBranch.relabel(self,old_label,new_label) |
| 304 |
|
| 305 |
def pointtest7(a,b): |
| 306 |
if "TY name" not in a: |
| 307 |
raise AUTOExceptions.AUTORegressionError("No TY name label") |
| 308 |
if "TY number" not in a: |
| 309 |
raise AUTOExceptions.AUTORegressionError("No TY number label") |
| 310 |
if "BR" not in a: |
| 311 |
raise AUTOExceptions.AUTORegressionError("No BR label") |
| 312 |
if "data" not in a: |
| 313 |
raise AUTOExceptions.AUTORegressionError("No data label") |
| 314 |
if "PT" not in a: |
| 315 |
raise AUTOExceptions.AUTORegressionError("No PT label") |
| 316 |
if "LAB" not in a: |
| 317 |
raise AUTOExceptions.AUTORegressionError("No LAB label") |
| 318 |
if len(a["data"]) != len(b["data"]): |
| 319 |
raise AUTOExceptions.AUTORegressionError("Data sections have different lengths") |
| 320 |
|
| 321 |
|
| 322 |
def pointtest8(a,b): |
| 323 |
keys = ['Type number', 'Type name', |
| 324 |
'Free Parameters', 'Branch number', 'Parameter NULL vector', |
| 325 |
'data', 'NCOL', 'Label', 'ISW', 'NTST', |
| 326 |
'Point number', 'Parameters'] |
| 327 |
|
| 328 |
scratch=a['Parameters'] |
| 329 |
scratch=b['Parameters'] |
| 330 |
for key in keys: |
| 331 |
if key not in a: |
| 332 |
raise AUTOExceptions.AUTORegressionError("No %s label"%(key,)) |
| 333 |
if len(a["data"]) != len(b["data"]): |
| 334 |
raise AUTOExceptions.AUTORegressionError("Data sections have different lengths") |
| 335 |
|
| 336 |
|
| 337 |
def test(): |
| 338 |
foo = bifDiag() |
| 339 |
foo.readFilename("test_data/fort.7","test_data/fort.8") |
| 340 |
if len(foo[0]) != 150: |
| 341 |
raise AUTOExceptions.AUTORegressionError("File length incorrect") |
| 342 |
pointtest7(foo[0].getIndex(0),foo[0].getIndex(57)) |
| 343 |
if len(foo()) != 5: |
| 344 |
raise AUTOExceptions.AUTORegressionError("File length incorrect") |
| 345 |
pointtest8(foo().getIndex(0),foo().getIndex(3)) |
| 346 |
|
| 347 |
if len(foo.getLabels()) != 5: |
| 348 |
raise AUTOExceptions.AUTORegressionError("Incorrect number of labels") |
| 349 |
|
| 350 |
print("Deleting labels") |
| 351 |
foo.deleteLabel(range(6,9)) |
| 352 |
|
| 353 |
if len(foo.getLabels()) != 2: |
| 354 |
raise AUTOExceptions.AUTORegressionError("Incorrect number of labels") |
| 355 |
|
| 356 |
print("Relabeling") |
| 357 |
foo.relabel(9,57) |
| 358 |
|
| 359 |
for i in range(len(foo[0])): |
| 360 |
if foo[0].getIndex(0)["TY number"] != 0: |
| 361 |
if foo[0].getIndex(0)["LAB"] != 57: |
| 362 |
raise AUTOExceptions.AUTORegressionError("Incorrect label") |
| 363 |
break |
| 364 |
if foo().getIndex(0)["Label"] != 57: |
| 365 |
raise AUTOExceptions.AUTORegressionError("Incorrect label") |
| 366 |
|
| 367 |
print("Making labels unique") |
| 368 |
foo.uniquelyLabel() |
| 369 |
|
| 370 |
for i in range(len(foo[0])): |
| 371 |
if foo[0].getIndex(0)["TY number"] != 0: |
| 372 |
if foo[0].getIndex(0)["LAB"] != 1: |
| 373 |
raise AUTOExceptions.AUTORegressionError("Incorrect label") |
| 374 |
break |
| 375 |
if foo().getIndex(0)["Label"] != 1: |
| 376 |
raise AUTOExceptions.AUTORegressionError("Incorrect label") |
| 377 |
|
| 378 |
print("bifDiag passed all tests") |
| 379 |
|
| 380 |
if __name__ == '__main__' : |
| 381 |
test() |