def checkForSampledData(data, debug): """ Check to see if data has been sampled and update ndata Args: data: shared alamo data options debug: Additional options may be specified and will be applied to the .alm """ lst_name = data["stropts"]["almname"].split(".")[0] + ".lst" with open(lst_name) as infile, open("awkres", "w") as outfile: copy = False for line in infile: if "Errors on observed data points" in line.strip(): copy = True elif "Maximum absolute errors" in line.strip(): copy = False elif copy: outfile.write(line) f = open("awkres") lf = f.read() f.close() lf2 = lf.split("\n") lf2 = lf2[1:-1] sys.stdout.write( "Updating number of training points from " + str(data["opts"]["ndata"]) + " to " + str(len(lf2)) + "\n" ) data["opts"]["ndata"] = len(lf2) xdata = np.zeros([data["opts"]["ndata"], data["opts"]["ninputs"]]) zdata = np.zeros([data["opts"]["ndata"], data["opts"]["noutputs"]]) for i in range(len(lf2)): lf3 = lf2[i].split(" ") while "" in lf3: lf3.remove("") for j in range(data["opts"]["ninputs"]): xdata[i][j] = float(lf3[j]) for j in range(data["opts"]["noutputs"]): zdata[i][j] = float(lf3[data["opts"]["ninputs"] + j]) deletefile("awkres") return xdata, zdata
def get_alamo_version(): x = [0, 1] z = [0, 1] res = alamo(x, z, saveopt=False) # xval=xival, zval=zival, mock=True) try: deletefile("logscratch") deletefile("almopt.txt") deletefile("z1.py") except Exception: pass return res["version"]
def doalamo(data, time, xlo, xup, rpc, rspec, sharedata): # This subroutine calls alamo to generate targets for dynamic problems # this call could be replaced by a call to alamopy.doalamo() # Inputs: # data - process data from ripemodel() # time - time from the pc dictionary in ripemodel() # xlo/xup - bounds for time in dynamic problems # rpc - list containing range of process conditions # rspec - list of species # Outputs: # profiles - dictionary of prfiles from recursive piece-wise alamo # r2ret - r2 of submodels # import sympy from idaes.surrogate.alamopy import multos as mos from idaes.surrogate.ripe import debug # from sympy import symbols if len(np.shape(data)) == 2: data = np.expand_dims(data, axis=-1) # Convert data to python index xlo = xlo - 1 xup = xup - 1 r2ret = {} profiles = {} # st = symbols("st") # Build alamo models for each process condition for p in rpc: r2ret[p] = {} profiles[p] = {} with open("temp.alm", "w") as a: a.write("linfcns 1\n") a.write("funform 1\n") a.write("trace 1\n") a.write("tracefname temptrace.trc\n") a.write("solvemip 1\n") a.write("ninputs 1\n") a.write("noutputs " + str(len(rspec)) + "\n") a.write("xlabels st\n") a.write("modeler 1\n") a.write("monomialpower 0.5 1 1.5 2 \n") a.write("logfcns 1\n") a.write("expfcns 1\n") a.write("constant 0\n") a.write("xmin 0\n") a.write("xmax " + str(time[-1]) + "\n") a.write("cvxbic 1\n") a.write("initialpoints " + str(len(range(xlo, xup))) + "\n") a.write("ndata " + str(len(range(xlo, xup))) + "\n") a.write("begin_data\n") for i in range(xlo, xup): # tstr = str(time[i]) + " " for j in rspec: tstr = tstr + str(data[i, j, p]) + " " a.write(tstr + "\n") a.write("end_data\n") os.system(sharedata["alamo_path"] + " temp.alm > tmpscratch") f = open("temptrace.trc") lf = f.read() f.close() lf2 = lf.split("\n") z = 0 for i in rpc: for j in rspec: if "#filename" in lf2[z]: z = z + 1 profiles[i][j] = lf2[z].split(",")[-1].split(" = ")[-1] r2ret[i][j] = lf2[z].split(",")[18] z = z + 1 mos.copyfile("sv.alm", "temp.alm") if not debug["savescratch"]: mos.deletefile("sv.alm", "tmpscratch", "temp.alm", "temptrace.trc", "temp.lst") return [profiles, r2ret, sharedata]
def cleanFiles(data, debug, pywrite=False, **kwargs): """ Removes intermediate files Args: data/debug: shared default options for .alm file vargs: Validation data """ # Delete files if debug["mock"]: try: deletefile("temp.alm") if pywrite: for z in alamopy.data["zlabels"]: deletefile("%s.py" % z) return except Exception: pass if not debug["savepyfcn"]: for z in alamopy.data["results"]["zlabels"]: deletefile("%s.py" % z) if not debug["savescratch"]: deletefile( str(data["stropts"]["almname"]) + " " + str(data["stropts"]["almname"]).split(".")[0] + ".lst") if not debug["savetrace"]: deletefile(data["stropts"]["tracefname"]) if not debug["saveopt"]: global custom_fxn_list, surface_constraint_list, extrapxmin, extrapxmax custom_fxn_list = [] surface_constraint_list = [] extrapxmax = None extrapxmin = None if "almopt" in kwargs.keys(): deletefile(kwargs["almopt"]) if debug["simwrap"]: deletefile("simwrapper.py")