import extpy def solve(self): """ run the active solver on the current model (reporting to the status bar only) """ assert self.__class__.__name__=="Instance" assert self.isModel() print "SELF IS A MODEL" browser = extpy.getbrowser() if browser: print "Using browser.solver" solver = browser.solver else: print "Using hardwired default solver, QRSlv" solver = ascpy.Solver("QRSlv") if browser and have_solverreporter: reporter = SimpleSolverReporter(browser) else: print "Using console solver reporter" reporter = ascpy.SolverReporter() # the 'sim' object is registered in simulation.cpp each time a method is to be run # (an exception is thrown if not available (eg if C++ not being used) sim = ascpy.Registry().getSimulation("sim") sim.solve(solver,reporter) extpy.registermethod(solve)
from pylab import * except: pass def leastsq_plot(self): """Plot a least-squares fit, no added intermediate points though.""" import loading loading.load_matplotlib(throw=True) ioff() figure() n = self.n.getIntValue() x = [] y = [] ye = [] for i in range(n): x.append(float(self.x[i+1])) y.append(float(self.y[i+1])) ye.append(float(self.f[i+1].y)) plot(x,y,'bo') plot(x,ye,'b-') extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() extpy.registermethod(leastsq_plot)
browser = extpy.getbrowser() browser.do_solve() ioff() figure() gca().set_aspect('equal', adjustable='datalim') hold(True) for alpha in range(10,74,4): self.alpha.setRealValueWithUnits(alpha,"deg") try: browser.sim.solve(browser.solver,SimpleSolverReporter(browser)) except: browser.reporter.reportError('Failed to solve for alpha = %d' % alpha) continue x = [float(x) for x in [self.x_A, self.x_B, self.x_C, self.x_D]] y = [float(y) for y in [self.y_A, self.y_B, self.y_C, self.y_D]] plot(x,y,"y-") plot(x[0:2],y[0:2],"ro") plot(x[2:4],y[2:4],"bo") extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() extpy.registermethod(fourbarplot) #the above method can be called using "EXTERNAL fourbarplot(SELF)" in ASCEND.
## plot the data plot(XX1, YY1, label=("%d K" % T)) hold(1) legend() xlabel("Molar volume") ylabel("Presure (Pa)") title("p-V curves at varying temperatures") ion() show() extpy.registermethod(pvplot) def zplot(self): browser = extpy.getbrowser() ioff() figure() axes([0.1, 0.1, 0.71, 0.8]) # # I have chosen three temperatures # TT = [190, 210, 230, 250, 270, 290, 310, 330, 350, 370] for T in TT: self.T.setRealValue(T) #
loading.load_matplotlib(throw=True) ioff() figure() hold(1) FH = fprops.fluid(str(self.hxd.component_hot.getSymbolValue()), str(self.hxd.type_hot.getSymbolValue())) FC = fprops.fluid(str(self.hxd.component.getSymbolValue()), str(self.hxd.type.getSymbolValue())) extpy.getbrowser().reporter.reportNote( "Hot fluid is %s, cold fluid is %s" % (FH.name, FC.name)) plot_TH(pconsth(self.inlet_hot, self.outlet_hot, 50),'r-', Href = (float(self.outlet_hot.h)*float(self.outlet_hot.mdot))\ ) plot_TH(pconsth(self.inlet, self.outlet, 50),'b-', Href = (float(self.inlet.h)*float(self.inlet.mdot))\ ) title(unicode(r"%s-%s heat exchanger" % (FH.name, FC.name))) ylabel(unicode(r"T / [°C]")) xlabel("H / [MW]") extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() extpy.registermethod(regenerator_plot_fprops)
Href = (float(self.outlet.h)*float(self.outlet.mdot))\ ) plot_TH(pconsth(self.inlet_cold, self.outlet_cold, 50),'b-', Href = (float(self.inlet_cold.h)*float(self.inlet_cold.mdot))\ ) title(unicode(r"Combined-cycle air-%s heat exchanger" % D.name)) ylabel(unicode(r"T / [°C]")) xlabel("H / [MW]") extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() #savefig(os.path.expanduser("~/Desktop/air_stream_heatex.eps")) extpy.registermethod(cycle_plot_rankine) extpy.registermethod(cycle_plot_rankine_reheat) extpy.registermethod(cycle_plot_rankine_regen1) extpy.registermethod(cycle_plot_rankine_regen2) extpy.registermethod(cycle_plot_brayton_regen) extpy.registermethod(cycle_plot_brayton_reheat_regen_intercool) extpy.registermethod(cycle_plot_brayton_split) extpy.registermethod(cycle_plot_ccgt) extpy.registermethod(heater_closed_plot) extpy.registermethod(air_stream_heat_exchanger_plot) #the above method can be called using "EXTERNAL fourbarplot(SELF)" in ASCEND.
except: pass def leastsq_plot(self): """Plot a least-squares fit, no added intermediate points though.""" import loading loading.load_matplotlib(throw=True) ioff() figure() n = self.n.getIntValue() x = [] y = [] ye = [] for i in range(n): x.append(float(self.x[i + 1])) y.append(float(self.y[i + 1])) ye.append(float(self.f[i + 1].y)) plot(x, y, 'bo') plot(x, ye, 'b-') extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() extpy.registermethod(leastsq_plot)
TT1.append(float(self.TdegC)) XX2.append(float(self.y1)) TT2.append(float(self.TdegC)) except: browser.reporter.reportError('Failed to solve for x1 = %f' % x1) continue xlabel("Mole Fraction (x or y)") ylabel("Temperature in degrees C") title("Txy plot") plot(XX1,TT1) plot(XX2,TT2) hold(1) ion() show() extpy.registermethod(txyplot) def pxyplot(self): browser = extpy.getbrowser() ioff() figure() for T in [340]: self.T.setRealValue(T) XX1 = [] PP1 = [] XX2 = [] PP2 = [] for x1 in [0.01,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.40,0.45,0.50,0.55,0.60,0.65,0.7,0.75,0.8,0.85,0.9,0.95,0.99]: self.x1.setRealValue(x1) try: browser.sim.solve(browser.solver,SimpleSolverReporter(browser,message="T = %f, x1 = %f" % (T,x1)))
note = notes[i] if note.getId()==None: browser.reporter.reportNote("Empty note ID...") continue n = note.getId() param = None for p in params: if p.getName()==n: param = p if param: if param.isInt(): v = int( note.getText() ) param.setIntValue(v) elif param.isReal(): v = float( note.getText() ) param.setRealValue(v) elif param.isStr(): v = note.getText() param.setStrValue(v) elif param.isBool(): v = bool( note.getText() ) param.setBoolValue(v) else: raise Exception("unknown parameter type") reporter.reportNote("Set %s = %s" % (param.getName(),v)) else: reporter.reportWarning("Ignoring unrecognised parameter '%s' for solver '%s' (from solver notes)\n%s:%d" % (n,solvername,note.getFilename(),note.getLineNumber())) extpy.registermethod(solvernotes)
def solve(self): """ run the active solver on the current model (reporting to the status bar only) """ assert self.__class__.__name__ == "Instance" assert self.isModel() print "SELF IS A MODEL" browser = extpy.getbrowser() if browser: print "Using browser.solver" solver = browser.solver else: print "Using hardwired default solver, QRSlv" solver = ascpy.Solver("QRSlv") if browser and have_solverreporter: reporter = SimpleSolverReporter(browser) else: print "Using console solver reporter" reporter = ascpy.SolverReporter() # the 'sim' object is registered in simulation.cpp each time a method is to be run # (an exception is thrown if not available (eg if C++ not being used) sim = ascpy.Registry().getSimulation("sim") sim.solve(solver, reporter) extpy.registermethod(solve)
hold(True) leg = [] Re_vals = array([1000,1500,2000,2100,2300,2400,2500,2600,2700,2900,3000,4000,5000,10000,20000,50000,100000,200000,500000,1e6]) #browser.reporter.reportNote(str(len(Re_vals))) for eps_on_D in [1e-5,2e-5,5e-5,1e-4,2e-4,5e-4,1e-3,5e-3,0.01,0.02]: self.eps_on_D.setRealValue(eps_on_D) f_vals = zeros(size(Re_vals),'f') for i in range(0,len(Re_vals)): self.Re.setRealValue(Re_vals[i]) browser.sim.solve(ascpy.Solver("QRSlv"),SimpleSolverReporter(browser)) f_vals[i] = self.f.getRealValue() loglog(Re_vals,f_vals) leg += ["e/D = %f" % eps_on_D] legend(leg) ion() show() def checksomevalue(self): """ a silly listing testing routine """ self = ascpy.Registry().getInstance('context') s = "Value of f = %f" % self.f.getRealValue() print s browser.reporter.reportNote(s) extpy.registermethod(moodyplot) extpy.registermethod(checksomevalue) extpy.registermethod(setsomevalue)
for eps_on_D in [ 1e-5, 2e-5, 5e-5, 1e-4, 2e-4, 5e-4, 1e-3, 5e-3, 0.01, 0.02 ]: self.eps_on_D.setRealValue(eps_on_D) f_vals = zeros(size(Re_vals), 'f') for i in range(0, len(Re_vals)): self.Re.setRealValue(Re_vals[i]) browser.sim.solve(ascpy.Solver("QRSlv"), SimpleSolverReporter(browser)) f_vals[i] = self.f.getRealValue() loglog(Re_vals, f_vals) leg += ["e/D = %f" % eps_on_D] legend(leg) ion() show() def checksomevalue(self): """ a silly listing testing routine """ self = ascpy.Registry().getInstance('context') s = "Value of f = %f" % self.f.getRealValue() print s browser.reporter.reportNote(s) extpy.registermethod(moodyplot) extpy.registermethod(checksomevalue) extpy.registermethod(setsomevalue)
browser.do_solve() ioff() figure() gca().set_aspect('equal', adjustable='datalim') hold(True) for alpha in range(10, 74, 4): self.alpha.setRealValueWithUnits(alpha, "deg") try: browser.sim.solve(browser.solver, SimpleSolverReporter(browser)) except: browser.reporter.reportError('Failed to solve for alpha = %d' % alpha) continue x = [float(x) for x in [self.x_A, self.x_B, self.x_C, self.x_D]] y = [float(y) for y in [self.y_A, self.y_B, self.y_C, self.y_D]] plot(x, y, "y-") plot(x[0:2], y[0:2], "ro") plot(x[2:4], y[2:4], "bo") extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() extpy.registermethod(fourbarplot) #the above method can be called using "EXTERNAL fourbarplot(SELF)" in ASCEND.
# finding a specific child instance print "TEST OF ABILITY TO GET SPECIFIC CHILD VALUES:" print self.a, "=", self.a.getValue() self.x_1.setFixed(not self.x_1.isFixed()) self.x_2.setFixed(not self.x_2.isFixed()) self.y_1.setFixed(not self.x_1.isFixed()) self.y_2.setFixed(not self.x_2.isFixed()) print "SETTING VALUE OF X_2" self.x_2.setRealValueWithUnits(2.0, "m") print "X_2 = %f" % self.x_2 if browser: browser.reporter.reportNote( "No, now go away or I shall taunt you a second time!") else: print "No, now go away or I shall taunt you a second time!" def adjust_a(self): sel extpy.registermethod(mypythonmethod) #the above method can be called using "EXTERNAL mypythonmethod(SELF)" in ASCEND. extpy.registermethod(adjust_a)
xx.append(((float(S.h)*float(S.mdot)) - Href)/1.e6) plot(xx,yy,style) def regenerator_plot_fprops(self): """Plot T-H diagram of regenerator""" import loading; loading.load_matplotlib(throw=True) ioff(); figure(); hold(1) FH = fprops.fluid(str(self.hxd.component_hot.getSymbolValue()),str(self.hxd.type_hot.getSymbolValue())) FC = fprops.fluid(str(self.hxd.component.getSymbolValue()),str(self.hxd.type.getSymbolValue())) extpy.getbrowser().reporter.reportNote("Hot fluid is %s, cold fluid is %s" % (FH.name, FC.name)) plot_TH(pconsth(self.inlet_hot, self.outlet_hot, 50),'r-', Href = (float(self.outlet_hot.h)*float(self.outlet_hot.mdot))\ ) plot_TH(pconsth(self.inlet, self.outlet, 50),'b-', Href = (float(self.inlet.h)*float(self.inlet.mdot))\ ) title(unicode(r"%s-%s heat exchanger" % (FH.name, FC.name))) ylabel(unicode(r"T / [°C]")) xlabel("H / [MW]") extpy.getbrowser().reporter.reportNote("Plotting completed") ion() show() extpy.registermethod(regenerator_plot_fprops)
import ascpy import extpy browser = extpy.getbrowser() def listnotes(self): """ make a list of NOTES for the present model """ self = ascpy.Registry().getInstance('context') db = browser.library.getAnnotationDatabase() notes = db.getNotes(self.getType(),ascpy.SymChar("solver")) for i in range(1,len(notes)): mm = notes[i].getMethod() ll = notes[i].getLanguage() ii = notes[i].getId() tt = notes[i].getText() s = "type = %s, method = %s, lang = %s, id = %s, text = %s" % (notes[i].getType(), mm, ll, ii, tt) print "NOTES:",s browser.reporter.reportNote(s) # note: 'setup_solver' moved to 'solvernotes.py' (and renamed) extpy.registermethod(listnotes)
ydata[3].append(self.T("K") - 273.15) ydata[4].append(self.v_wind("m/s")) subplot(311) plot(xdata, ydata[0]) title("Weather data vs Time") ylabel(unicode("Radiation / [W/m²]")) hold(1) plot(xdata, ydata[1]) plot(xdata, ydata[2]) legend(series[0:3]) grid(1) subplot(312) plot(xdata, ydata[3]) ylabel('Temperature [K]') grid(1) subplot(313) plot(xdata, ydata[4]) ylabel('Wind speed [m/s]') grid(1) xlabel("Time / [h]") ion() show() extpy.registermethod(timestudy) #the above method can be called using "EXTERNAL timestudy(SELF)" in ASCEND.
for k,v in fff.iteritems(): F = file(v,'w') I.writeMatrix(F,k) print "WROTE MATRICES TO FILE. NOW PROCESSING..." # we can't import scipy here due to a crash. so we must use a subprocess... script = os.path.expanduser('~/ascend/models/johnpye/roots_subproc.py') if os.path.exists(script): P = subprocess.Popen(['python',script]+[fff[d] for d in derivs],stdout=subprocess.PIPE,close_fds=True) ret = P.wait() if ret: print "GOT ERROR CODE FROM roots_subproc.py" browser.reporter.reportError(P.stdout.read()) deletefiles(fff) return 1 print "OK" else: browser.reporter.reportError("Couldn't find script '%s'" % script) deletefiles(fff) return 1 deletefiles(fff) return 0 extpy.registermethod(roots) #the above method can be called using "EXTERNAL roots(SELF)" in ASCEND.
browser.reporter.reportError('Failed to solve for P = %f' % P) continue ## plot the data plot(XX1,YY1,label=("%d K" % T)) hold(1) legend() xlabel("Molar volume") ylabel("Presure (Pa)") title("p-V curves at varying temperatures") ion() show() extpy.registermethod(pvplot) def zplot(self): browser = extpy.getbrowser() ioff() figure() axes([0.1,0.1,0.71,0.8]) # # I have chosen three temperatures # TT = [190,210,230,250,270,290,310,330,350,370] for T in TT: self.T.setRealValue(T) # # collect the data for plotting in two sets of arrays (one for X, one for Y)
def run(self): self.show() gtk.main() except: gtkgl_deps = 0 #--------------------------------------------------------------------- def framevis(self): """Visualise the frame using OpenGL and GtkGlExt""" browser = extpy.getbrowser() if not browser: print "no 'browser'" return 1 if not gtkgl_deps: browser.reporter.reportError("Unable to load PyGtkGlExt") return 1 REP = browser.reporter.reportNote frame = AscFrame(self) win = ModelWindow(frame) win.run() extpy.registermethod(framevis)
print "CHILDREN OF INSTANCE '%s':" % self.getName() for i in self.getChildren(): print i.getName()," = ",i.getValue() # finding a specific child instance print "TEST OF ABILITY TO GET SPECIFIC CHILD VALUES:" print self.a,"=",self.a.getValue() self.x_1.setFixed(not self.x_1.isFixed()) self.x_2.setFixed(not self.x_2.isFixed()) self.y_1.setFixed(not self.x_1.isFixed()) self.y_2.setFixed(not self.x_2.isFixed()) print "SETTING VALUE OF X_2" self.x_2.setRealValueWithUnits(2.0,"m") print "X_2 = %f" % self.x_2 if browser: browser.reporter.reportNote("No, now go away or I shall taunt you a second time!") else: print "No, now go away or I shall taunt you a second time!" def adjust_a(self): sel extpy.registermethod(mypythonmethod) #the above method can be called using "EXTERNAL mypythonmethod(SELF)" in ASCEND. extpy.registermethod(adjust_a)
ydata[4].append(self.v_wind("m/s")) subplot(311) plot(xdata,ydata[0]) title("Weather data vs Time") ylabel(unicode("Radiation / [W/m²]")) hold(1) plot(xdata,ydata[1]) plot(xdata,ydata[2]) legend(series[0:3]) grid(1) subplot(312) plot(xdata,ydata[3]) ylabel('Temperature [K]') grid(1) subplot(313) plot(xdata,ydata[4]) ylabel('Wind speed [m/s]') grid(1) xlabel("Time / [h]") ion() show() extpy.registermethod(timestudy) #the above method can be called using "EXTERNAL timestudy(SELF)" in ASCEND.
TT2.append(float(self.TdegC)) except: browser.reporter.reportError('Failed to solve for x1 = %f' % x1) continue xlabel("Mole Fraction (x or y)") ylabel("Temperature in degrees C") title("Txy plot") plot(XX1, TT1) plot(XX2, TT2) hold(1) ion() show() extpy.registermethod(txyplot) def pxyplot(self): browser = extpy.getbrowser() ioff() figure() for T in [340]: self.T.setRealValue(T) XX1 = [] PP1 = [] XX2 = [] PP2 = [] for x1 in [ 0.01, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 0.99