def hFit(a,T): """ a = [a1 ... a6] T is numpy array Returns h and dh/dT (where by h, H/RT is meant) """ h = a[0] + a[1]*T/2 + a[2]*T**2/3 + a[3]*T**3/4 + a[4]*T**4/5 + a[5]/T dhdT = a[1]/2 + a[2]*2*T/3 + a[3]*3*T**2/4 + a[4]*4*T**3/5 - a[5]/T**2 return h,dhdT def sFit(a,T): """ a = [a1 ... a7] T is numpy array Returns s and ds/dT (where by s, S/R is meant) """ s = a[0]*np.log(T) + a[1]*T + a[2]*T**2/2 + a[3]*T**3/3 + a[4]*T**4/4 + a[6] dsdT = a[0]/T + a[1] + a[2]*T + a[3]*T**2 + a[4]*T**3 return s,dsdT def coeffImport(species): """ Takes a Cantera species object """ midT = species.thermo.coeffs[0] minT = species.thermo.min_temp maxT = species.thermo.max_temp highCoeffs = species.thermo.coeffs[1:8] lowCoeffs = species.thermo.coeffs[8:15] return minT,midT,maxT,lowCoeffs,highCoeffs def continuity(mech,absTol=1e-5): """ Takes a string representing Cantera mechanism, e.g. 'gri30.cti' absTol is the absolute tolerance used when checking for C0 and C1 continuity. Can vary the default value if desired. Absolute rather than relative tolerance used to avoid division by zero since some species (e.g. Ar) have zero slope for all T. """ print('Mechanism file '+mech) gas = ct.Solution(mech) ns = gas.n_species ne= gas.n_elements nr = gas.n_reactions """ Compute gas standard state properties for each species """ T0 = 298.15 P0 = 101325. gas.TP = T0, P0 R = ct.gas_constant # universal gas constant w = np.zeros(ns) hs = np.zeros(ns) cps = np.zeros(ns) ss = np.zeros(ns) deltahf = np.zeros(ns) for k in range(gas.n_species): w[k]=gas.molecular_weights[k] # molecular weight of the ith species hs[k] = gas.standard_enthalpies_RT[k] #specific enthalpy of the ith species deltahf[k] = 1E-6*hs[k]*R*gas.T # heat of formation (kJ/mol) cps[k] = gas.standard_cp_R[k] # heat capacity of the ith species ss[k] = gas.standard_entropies_R[k] # entropy of the ith species print('Number of elements = '+str(ne)) print('Number of species = '+str(ns)) print('Number of reactions = '+str(nr)) S = ct.Species.listFromFile(mech) # list of species objects nCoeffs = int((S[0].thermo.n_coeffs-1)/2) # number of coefficients per polynomial, i.e. identifies NASA-7 vs. NASA-9s globalMinT = gas.min_temp globalMaxT = gas.max_temp print('NASA-'+str(nCoeffs)+' polynomials') print('Range of valid temperatures for all fits:') print('Max(Min. T) for all species = '+str(globalMinT)+' K') print('Min(Max. T) for all species = '+str(globalMaxT)+' K') print("Absolute error tolerance for C0 and C1 {:.2e}".format(absTol)) print('\n') f = open("BadSpecies.txt","w+") print('Species\tCp C0\tCp C1\th C0\th C1\ts C0\ts C1\tMin T\tMid T\tMax T\tW\tCp/R\tS/R\tH/RT\tDeltaHf') cpjumpmax = 0. ibad = 0 for i,species in enumerate(S): minT,midT,maxT,lowCoeffs,highCoeffs = coeffImport(species) # Polynomial class takes coefficients in ascending powers of x lowPolyCp = P(lowCoeffs[0:5]) highPolyCp = P(highCoeffs[0:5]) # Check for continuity cpjump = abs(lowPolyCp(midT)-highPolyCp(midT)) if cpjump > cpjumpmax: cpjumpmax = cpjump spmax = species cpc0 = cpjump < absTol if not cpc0: f.write('{0:18} {1:10.2f} {2:10.2f} {3:10.2f} \n'.format(species.name,minT,midT,maxT)) ibad = ibad + 1 cpc1 = abs(lowPolyCp.deriv()(midT)-highPolyCp.deriv()(midT)) < absTol hc0 = abs(hFit(lowCoeffs[0:6],midT)[0]-hFit(highCoeffs[0:6],midT)[0]) < absTol hc1 = abs(hFit(lowCoeffs[0:6],midT)[1]-hFit(highCoeffs[0:6],midT)[1]) < absTol sc0 = abs(sFit(lowCoeffs[0:7],midT)[0]-sFit(highCoeffs[0:7],midT)[0]) < absTol sc1 = abs(sFit(lowCoeffs[0:7],midT)[1]-sFit(highCoeffs[0:7],midT)[1]) < absTol print(species.name+'\t'+str(cpc0)+'\t'+str(cpc1)+'\t'+str(hc0)+'\t'+str(hc1)+'\t'+str(sc0)+'\t'+ \ str(sc1)+'\t'+str(minT)+'\t'+str(midT)+'\t'+str(maxT)+'\t{:.2f}'.format(w[i])+'\t{:.2f}'.format(cps[i])+ \ '\t{:.2f}'.format(ss[i])+'\t{:7.2f}'.format(hs[i])+'\t{:7.2f}'.format(deltahf[i])) print('\n'"Wrote {:d} species to BadSpecies.txt file".format(ibad)) print('\n'+spmax.name+" has the maximum Cp/R jump of {:.2e}".format(cpjumpmax)) jumps(spmax) plotter(spmax) plt.show() f.close() return def plotter(species): """ Takes a Cantera species object """ minT,midT,maxT,lowCoeffs,highCoeffs = coeffImport(species) lowPolyCp = P(lowCoeffs[0:5]) highPolyCp = P(highCoeffs[0:5]) lowTrange = np.linspace(minT,midT) highTrange = np.linspace(midT,maxT) plt.figure() plt.plot(lowTrange,lowPolyCp(lowTrange),label='Low') plt.plot(highTrange,highPolyCp(highTrange),label='High') plt.legend() plt.title(r'$C_p$ fits for '+species.name) plt.xlabel('T [K]') plt.ylabel(r'$C_p$/R') plt.figure() plt.plot(lowTrange,hFit(lowCoeffs[0:6],lowTrange)[0],label='Low') plt.plot(highTrange,hFit(highCoeffs[0:6],highTrange)[0],label='High') plt.legend() plt.title(r'h fits for '+species.name) plt.xlabel('T [K]') plt.ylabel('h/RT') plt.figure() plt.plot(lowTrange,sFit(lowCoeffs[0:7],lowTrange)[0],label='Low') plt.plot(highTrange,sFit(highCoeffs[0:7],highTrange)[0],label='High') plt.legend() plt.title(r's fits for '+species.name) plt.xlabel('T [K]') plt.ylabel('s/R') return def jumps(species): """ Takes a cantera species object """ minT,midT,maxT,lowCoeffs,highCoeffs = coeffImport(species) lowPolyCp = P(lowCoeffs[0:5]) highPolyCp = P(highCoeffs[0:5]) lowTrange = np.linspace(minT,midT) highTrange = np.linspace(midT,maxT) CpJump = lowPolyCp(midT)-highPolyCp(midT) CpTJump = lowPolyCp.deriv()(midT)-highPolyCp.deriv()(midT) HJump = hFit(lowCoeffs[0:6],midT)[0]-hFit(highCoeffs[0:6],midT)[0] HTJump = hFit(lowCoeffs[0:6],midT)[1]-hFit(highCoeffs[0:6],midT)[1] SJump = sFit(lowCoeffs[0:7],midT)[0]-sFit(highCoeffs[0:7],midT)[0] STJump = sFit(lowCoeffs[0:7],midT)[1]-sFit(highCoeffs[0:7],midT)[1] print('\n') print(species.name+" discontinuities at {:.2f} K".format(midT)) print("Cp/R {:.2e}".format(CpJump)) print("Cp'/R {:.2e}".format(CpTJump)) print("H/RT {:.2e}".format(HJump)) print("H/RT' {:.2e}".format(HTJump)) print("S/R {:.2e}".format(SJump)) print("S'/R {:.2e}".format(STJump)) Tzero = 298.15 R = ct.gas_constant Cp = R*lowPolyCp(Tzero)/1000. H = R*Tzero*hFit(lowCoeffs[0:6],Tzero)[0]/1.E06 S = R*sFit(lowCoeffs[0:7],Tzero)[0]/1000. print('\n') print("Minimum temperature {:.2f} K".format(minT)) print("Midpoint temperature {:.2f} K".format(midT)) print("Maximum temperature {:.2f} K".format(maxT)) print('\n'+species.name+" properties at {:.2f} K".format(Tzero)) print("Cp {:.3f} J/mol-K {:.3f} cal/mol-K".format(Cp, Cp/4.184)) print("H {:.3f} kJ/mol {:.3f} cal/K".format(H,H/4.184)) print("S {:.3f} J/mol-K {:.3f} cal/mol-K".format(S,S/4.184)) return
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |