def FracVap(k, z, Frac): """ Flash Calc ki, x, Oi=0.5 """ if Frac == None: Frac = 0.5 fk1 = k - 1 fk2 = fk1 * z ## print fk1 fk3 = power(fk1, 2) #print k j = 1 while j <= 30: fk4 = fk1 * Frac + 1 Fkz = sum(fk2 / fk4) dFkz = sum(-fk3 * z / power(fk4, 2)) ## print j, Frac, Fkz , dFkz if abs(Fkz) <= 1e-8: break Frac = Frac - Fkz / dFkz j += 1 ## if Frac > 1: ## Frac = 1 #### print "Vapor Saturado" ## if Frac < 0: ## Frac = 0 ## print "Liquido comprido" return Frac
def _logticks(self, lower, upper): #lower,upper = map(_Numeric.log10,[lower,upper]) #print 'logticks',lower,upper ticks = [] mag = _Numeric.power(10,_Numeric.floor(lower)) if upper-lower > 6: t = _Numeric.power(10,_Numeric.ceil(lower)) base = _Numeric.power(10,_Numeric.floor((upper-lower)/6)) def inc(t): return t*base-t else: t = _Numeric.ceil(_Numeric.power(10,lower)/mag)*mag def inc(t): return 10**int(_Numeric.floor(_Numeric.log10(t)+1e-16)) majortick = int(_Numeric.log10(mag)) while t <= pow(10,upper): if majortick != int(_Numeric.floor(_Numeric.log10(t)+1e-16)): majortick = int(_Numeric.floor(_Numeric.log10(t)+1e-16)) ticklabel = '1e%d'%majortick else: if upper-lower < 2: minortick = int(t/pow(10,majortick)+.5) ticklabel = '%de%d'%(minortick,majortick) else: ticklabel = '' ticks.append((_Numeric.log10(t), ticklabel)) t += inc(t) if len(ticks) == 0: ticks = [(0,'')] return ticks
def FracVap(k,z,Frac): """ Flash Calc ki, x, Oi=0.5 """ if Frac ==None: Frac= 0.5 fk1 = k-1 fk2 = fk1*z ## print fk1 fk3 = power(fk1,2) #print k j = 1 while j<=30: fk4 = fk1*Frac+1 Fkz = sum( fk2 / fk4) dFkz = sum( -fk3*z / power(fk4,2) ) ## print j, Frac, Fkz , dFkz if abs(Fkz)<=1e-8: break Frac = Frac - Fkz / dFkz j+=1 ## if Frac > 1: ## Frac = 1 #### print "Vapor Saturado" ## if Frac < 0: ## Frac = 0 ## print "Liquido comprido" return Frac
def GetXY(self, event): """Wrapper around _getXY, which handles log scales""" x,y = self._getXY(event) if self.getLogScale()[0]: x = _Numeric.power(10,x) if self.getLogScale()[1]: y = _Numeric.power(10,y) return x,y
def Solver(self, model, case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["RK_A"] b_i = model["RK_B"] case.Prop["MolWt"] = sum(xm * model["MoleWt"]) AlphaT = 1 / sqrt(T) a_i = Ac * AlphaT A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) Zl_i = self.EOS.ZL(A_i, B_i) Zv_i = self.EOS.ZG(A_i, B_i) CoeFugo_v = self.FugaP(Zv_i, A_i, B_i) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i) yf = xm xf = xm for i in range(3): B_v = MixingRules.Molar(yf, B_i) A_v = power(sum(yf * sqrt(A_i)), 2) B_l = MixingRules.Molar(xf, B_i) A_l = power(sum(xf * sqrt(A_i)), 2) Z_v = self.EOS.ZG(A_v, B_v) Z_l = self.EOS.ZL(A_l, B_l) CoeFugM_v = self.FugaM(Z_v, A_i, B_i, A_v, B_v) CoeFugM_l = self.FugaM(Z_l, A_i, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v FrVap, xf, yf = Flash(ki, xm) Z = FrVap * Z_v + (1 - FrVap) * Z_l case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["xf"] = xf case.Prop["yf"] = yf
def Solver(self,model,case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["RK_A"] b_i = model["RK_B"] case.Prop["MolWt"] = sum( xm* model["MoleWt"] ) AlphaT = 1/sqrt( T) a_i = Ac * AlphaT A_i = ( a_i * P)/ pow( R * T,2) B_i = ( b_i * P )/( R * T) Zl_i= self.EOS.ZL(A_i,B_i) Zv_i= self.EOS.ZG(A_i,B_i) CoeFugo_v = self.FugaP(Zv_i, A_i, B_i ) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i ) yf = xm xf = xm for i in range(3): B_v = MixingRules.Molar( yf, B_i) A_v = power( sum( yf* sqrt(A_i) ),2 ) B_l = MixingRules.Molar( xf, B_i) A_l = power( sum( xf* sqrt(A_i) ),2 ) Z_v = self.EOS.ZG(A_v,B_v) Z_l = self.EOS.ZL(A_l,B_l) CoeFugM_v = self.FugaM(Z_v, A_i,B_i,A_v,B_v) CoeFugM_l = self.FugaM(Z_l, A_i,B_i,A_l,B_l) fi = P*CoeFugM_v*yf ki = CoeFugM_l/CoeFugM_v FrVap, xf, yf = Flash(ki, xm) Z = FrVap*Z_v + (1- FrVap)* Z_l case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["xf"] = xf case.Prop["yf"] = yf
def projectOnSphere( xyz, radius=None, center=None ): """ Project the coordinates xyz on a sphere with a given radius around a given center. @param xyz: cartesian coordinates @type xyz: array N x 3 of float @param radius: radius of target sphere, if not provided the maximal distance to center will be used (default: None) @type radius: float @param center: center of the sphere, if not given the average of xyz will be assigned to the center (default: None) @type center: array 0 x 3 of float @return: array of cartesian coordinates (x, y, z) @rtype: array """ if center is None: center = N.average( xyz ) if radius is None: radius = max( N.sqrt( N.sum( N.power( xyz - center, 2 ), 1 ) ) ) rtp = cartesianToPolar( xyz - center ) rtp[ :, 0 ] = radius return polarToCartesian( rtp ) + center
def projectOnSphere(xyz, radius=None, center=None): """ Project the coordinates xyz on a sphere with a given radius around a given center. @param xyz: cartesian coordinates @type xyz: array N x 3 of float @param radius: radius of target sphere, if not provided the maximal distance to center will be used (default: None) @type radius: float @param center: center of the sphere, if not given the average of xyz will be assigned to the center (default: None) @type center: array 0 x 3 of float @return: array of cartesian coordinates (x, y, z) @rtype: array """ if center is None: center = N.average(xyz) if radius is None: radius = max(N.sqrt(N.sum(N.power(xyz - center, 2), 1))) rtp = cartesianToPolar(xyz - center) rtp[:, 0] = radius return polarToCartesian(rtp) + center
def logConfidence(x, R, clip=0): """ Estimate the probability of x NOT beeing a random observation from a lognormal distribution that is described by a set of random values. @param x: observed value @type x: float @param R: sample of random values @type R: [float] @param clip: clip zeros at this value 0->don't clip (default: 0) @type clip: float @return: confidence that x is not random, median of random distr. @rtype: (float, float) """ if clip and 0 in R: R = N.clip(R, clip, max(R)) if clip and x == 0: x = clip ## remove 0 instead of clipping R = N.compress(R, R) if x == 0: return 0, 0 ## get mean and stdv of log-transformed random sample alpha = N.average(N.log(R)) n = len(R) beta = N.sqrt(N.sum(N.power(N.log(R) - alpha, 2)) / (n - 1.)) return logArea(x, alpha, beta), logMedian(alpha)
def pairwiseRmsd( self, aMask=None, noFit=0 ): """ Calculate rmsd between each 2 coordinate frames. @param aMask: atom mask @type aMask: [1|0] @return: frames x frames array of float @rtype: array """ frames = self.frames if aMask != None: frames = N.compress( aMask, frames, 1 ) result = N.zeros( (len( frames ), len( frames )), N.Float32 ) for i in range(0, len( frames ) ): for j in range( i+1, len( frames ) ): if noFit: d = N.sqrt(N.sum(N.power(frames[i]-frames[j], 2), 1)) result[i,j] = result[j,i] = N.sqrt( N.average(d**2) ) else: rt, rmsdLst = rmsFit.match( frames[i], frames[j], 1 ) result[i,j] = result[j,i] = rmsdLst[0][1] return result
def pairwiseRmsd(self, aMask=None, noFit=0): """ Calculate rmsd between each 2 coordinate frames. @param aMask: atom mask @type aMask: [1|0] @return: frames x frames array of float @rtype: array """ frames = self.frames if aMask != None: frames = N.compress(aMask, frames, 1) result = N.zeros((len(frames), len(frames)), N.Float32) for i in range(0, len(frames)): for j in range(i + 1, len(frames)): if noFit: d = N.sqrt(N.sum(N.power(frames[i] - frames[j], 2), 1)) result[i, j] = result[j, i] = N.sqrt(N.average(d**2)) else: rt, rmsdLst = rmsFit.match(frames[i], frames[j], 1) result[i, j] = result[j, i] = rmsdLst[0][1] return result
def logConfidence( x, R, clip=0 ): """ Estimate the probability of x NOT beeing a random observation from a lognormal distribution that is described by a set of random values. @param x: observed value @type x: float @param R: sample of random values @type R: [float] @param clip: clip zeros at this value 0->don't clip (default: 0) @type clip: float @return: confidence that x is not random, median of random distr. @rtype: (float, float) """ if clip and 0 in R: R = N.clip( R, clip, max( R ) ) if clip and x == 0: x = clip ## remove 0 instead of clipping R = N.compress( R, R ) if x == 0: return 0, 0 ## get mean and stdv of log-transformed random sample alpha = N.average( N.log( R ) ) n = len( R ) beta = N.sqrt(N.sum(N.power(N.log( R ) - alpha, 2)) / (n - 1.)) return logArea( x, alpha, beta ), logMedian( alpha )
def __distances(self, point, xyz=None): """ point - 3 x 1 array of float; point of origin xyz - 3 x n array of float; coordinates, if None -- take model atoms -> distances of all atoms to given point """ if xyz is None: xyz = self.model.getXyz() return N.sqrt(N.sum(N.power(xyz - point, 2), 1))
def __init__(self, u, w): """ This help to develovep the server EOS(u,w)-> class""" self.u = u self.w = w self.delta = sqrt(power(u, 2) - 4 * w) self.Thermo = Thermo() self.name = str(self)
def hmmEmm2Prob( self, nullEmm, emmScore ): """ Convert HMM profile emmisiion scores into emmission probabilities @param nullEmm: null scores @type nullEmm: array @param emmScore: emmission scores @type emmScore: array @return: null and emmission probabilities, for each amino acid in each position @rtype: array( len_seq x 20 ), array( 1 x 20 ) """ ## Null probabilities: prob = 2 ^ (nullEmm / 1000) * 1/len(alphabet) nullProb = N.power( 2, N.array( nullEmm )/1000.0 )*(1./20) ## Emmission probabilities: prob = nullProb 2 ^ (nullEmm / 1000) emmProb = nullProb * N.power( 2, ( emmScore/1000.0) ) return emmProb, nullProb
def hmmEmm2Prob(self, nullEmm, emmScore): """ Convert HMM profile emmisiion scores into emmission probabilities @param nullEmm: null scores @type nullEmm: array @param emmScore: emmission scores @type emmScore: array @return: null and emmission probabilities, for each amino acid in each position @rtype: array( len_seq x 20 ), array( 1 x 20 ) """ ## Null probabilities: prob = 2 ^ (nullEmm / 1000) * 1/len(alphabet) nullProb = N.power(2, N.array(nullEmm) / 1000.0) * (1. / 20) ## Emmission probabilities: prob = nullProb 2 ^ (nullEmm / 1000) ## see http://www.ebc.ee/WWW/hmmer2-html/node26.html emmProb = nullProb * N.power(2, (emmScore / 1000.0)) return emmProb, nullProb
def rmsd_res(self, coord1, coord2): """ Calculate the rsmd on residue level for c-alpha between a model and its reference. @param coord1: first set of coordinates @type coord1: array @param coord2: second set of coordinates @type coord2: array @return: rmsd_res: rmsd per c-alpha @rtype: [float] """ rmsd_res = [] for i in range(len(coord1)): rmsd = N.sqrt( (N.power(coord1[i][0]-coord2[i][0],2) + \ N.power(coord1[i][1]-coord2[i][1],2 )+ \ N.power(coord1[i][2]-coord2[i][2],2 ))) rmsd_res.append(rmsd) return rmsd_res
def rmsd_res(self, coord1, coord2): """ Calculate the rsmd on residue level for c-alpha between a model and its reference. @param coord1: first set of coordinates @type coord1: array @param coord2: second set of coordinates @type coord2: array @return: rmsd_res: rmsd per c-alpha @rtype: [float] """ rmsd_res = [] for i in range( len(coord1) ): rmsd = N.sqrt( (N.power(coord1[i][0]-coord2[i][0],2) + \ N.power(coord1[i][1]-coord2[i][1],2 )+ \ N.power(coord1[i][2]-coord2[i][2],2 ))) rmsd_res.append(rmsd) return rmsd_res
def error(self, msm, d2): """ @param msm: membership matrix @type msm: array('f') @param d2: distance from data to the centers @type d2: array('f') @return: weighted error @rtype: float """ p = N.power(msm, self.w) product = N.dot(p, N.transpose(d2)) return N.trace(product)
def T(self,P,m): P= P/self.Factor T = [] T_j = array( m["HAR_B"] /( log(P)-m["HAR_A"] ) ) ## print m["HAR_D"] for j in range(len(m["HAR_A"])): T_r = T_j[j] i= 1 while i<=20: fP_i = log(P_i) - exp( m["HAR_A"][j]+m["HAR_B"][j] / ( T_r ) + m["HAR_C"][j]*log(T_r) + m["HAR_D"][j]*P/power(T_r,2) ) dP_i = m["HAR_C"][j]/T_r- m["HAR_B"][j]/power(T_r,2) - 2*m["HAR_D"][j]*P/power(T_r,3) i +=1 if abs(fP_i)<=1e-3: T.append(T_r) break T = T-fP_i/dPi return T
def variance(x, avg=None): """ Variance, S{sigma}^2 @param x: data @type x: array('f') or float @param avg: use this average, otherwise calculated from x @type avg: float OR None @return: float @rtype: float """ if avg is None: avg = N.average(x) if len(x) == 1: return 0.0 return N.sum(N.power(N.array(x) - avg, 2)) / (len(x) - 1.)
def variance(x, avg = None): """ Variance, S{sigma}^2 @param x: data @type x: array('f') or float @param avg: use this average, otherwise calculated from x @type avg: float OR None @return: float @rtype: float """ if avg is None: avg = N.average(x) if len(x) == 1: return 0.0 return N.sum(N.power(N.array(x) - avg, 2)) / (len(x) - 1.)
def _get(self): from numpy.oldnumeric import power from Isd.misc.mathutils import draw_dirichlet, rescale_uniform ## compute "probabilities" p = 1. - rescale_uniform(self.times) p = power(p, 2.) index = draw_dirichlet(p) val = self.queue[index] self.queue = self.queue[:index] + self.queue[index + 1:] if len(self.queue): self.sort() return val
def getFluct_global(self, mask=None): """ Get RMS of each atom from it's average position in trajectory. The frames should be superimposed (fit() ) to a reference. @param mask: N x 1 list/Numpy array of 0|1, (N=atoms), atoms to be considered. @type mask: [1|0] @return: Numpy array ( N_unmasked x 1 ) of float. @rtype: array """ frames = self.frames if mask is not None: frames = N.compress(mask, frames, 1) ## mean position of each atom in all frames avg = N.average(frames) return N.average(N.sqrt(N.sum(N.power(frames - avg, 2), 2)))
def getFluct_global( self, mask=None ): """ Get RMS of each atom from it's average position in trajectory. The frames should be superimposed (fit() ) to a reference. @param mask: N x 1 list/Numpy array of 0|1, (N=atoms), atoms to be considered. @type mask: [1|0] @return: Numpy array ( N_unmasked x 1 ) of float. @rtype: array """ frames = self.frames if mask is not None: frames = N.compress( mask, frames, 1 ) ## mean position of each atom in all frames avg = N.average( frames ) return N.average(N.sqrt(N.sum(N.power(frames - avg, 2), 2) ))
def _get(self): from numpy.oldnumeric import power from Isd.misc.mathutils import draw_dirichlet, rescale_uniform # compute "probabilities" p = 1. - rescale_uniform(self.times) p = power(p, 2.) index = draw_dirichlet(p) val = self.queue[index] self.queue = self.queue[:index] + self.queue[index + 1:] if len(self.queue): self.sort() return val
def rowDistances(x, y): """ Calculate the distances between the items of two arrays (of same shape) after least-squares superpositioning. @param x: first set of coordinates @type x: array('f') @param y: second set of coordinates @type y: array('f') @return: array( len(x), 'f' ), distance between x[i] and y[i] for all i @rtype: array """ ## find transformation for best match r, t = findTransformation(x, y) ## transform coordinates z = N.dot(y, N.transpose(r)) + t ## calculate row distances return N.sqrt(N.sum(N.power(x - z, 2), 1))
def rowDistances( x, y ): """ Calculate the distances between the items of two arrays (of same shape) after least-squares superpositioning. @param x: first set of coordinates @type x: array('f') @param y: second set of coordinates @type y: array('f') @return: array( len(x), 'f' ), distance between x[i] and y[i] for all i @rtype: array """ ## find transformation for best match r, t = findTransformation(x, y) ## transform coordinates z = N.dot(y, N.transpose(r)) + t ## calculate row distances return N.sqrt(N.sum(N.power(x - z, 2), 1))
def P(self,T,m): P_j= array( exp( m["HAR_A"]+m["HAR_B"] / T + m["HAR_C"]*log(T) ) ) P = [] T1 = log(T) T2 = power(T,2) ## print m["HAR_D"] for j in range(len(m["HAR_A"])): P_r = P_j[j] i= 1 ## print j,P_r,T2 while i<=20: P_i = m["HAR_A"][j]+m["HAR_B"][j]/ T + m["HAR_C"][j]*T1 + m["HAR_D"][j]*P_r/T2 ## print P_i P_i = exp(P_i) ## print P_i*0.13332236 i +=1 if abs(P_i-P_r)<=1: P.append(P_i) break P_r = P_i return array(P) * self.Factor
def pca( self, atomMask=None, frameMask=None, fit=1 ): """ Calculate principal components of trajectory frames. @param atomMask: 1 x N_atoms, [111001110..] atoms to consider (default: all) @type atomMask: [1|0] @param frameMask: 1 x N_frames, [001111..] frames to consider (default all ) @type frameMask: [1|0] @return: (N_frames x N_frames), (1 x N_frames), projection of each frame in PC space, eigenvalue of each PC @rtype: array, array, array """ if frameMask is None: frameMask = N.ones( len( self.frames ), N.int32 ) if atomMask is None: atomMask = N.ones(self.getRef().lenAtoms(), N.int32) if fit: self.fit( atomMask ) refxyz = N.average( self.frames, 0 ) data = N.compress( frameMask, self.frames, 0 ) data = data - refxyz data = N.compress( atomMask, data, 1 ) ## reduce to 2D array data = N.array( map( N.ravel, data ) ) V, L, U = LA.singular_value_decomposition( data ) return U, V * L, N.power(L, 2)
def pca(self, atomMask=None, frameMask=None, fit=1): """ Calculate principal components of trajectory frames. @param atomMask: 1 x N_atoms, [111001110..] atoms to consider (default: all) @type atomMask: [1|0] @param frameMask: 1 x N_frames, [001111..] frames to consider (default all ) @type frameMask: [1|0] @return: (N_frames x N_frames), (1 x N_frames), projection of each frame in PC space, eigenvalue of each PC @rtype: array, array, array """ if frameMask is None: frameMask = N.ones(len(self.frames), N.int32) if atomMask is None: atomMask = N.ones(self.getRef().lenAtoms(), N.int32) if fit: self.fit(atomMask) refxyz = N.average(self.frames, 0) data = N.compress(frameMask, self.frames, 0) data = data - refxyz data = N.compress(atomMask, data, 1) ## reduce to 2D array data = N.array(map(N.ravel, data)) V, L, U = LA.singular_value_decomposition(data) return U, V * L, N.power(L, 2)
def logConfidence(x, R, clip=1e-32): """ Estimate the probability of x NOT beeing a random observation from a lognormal distribution that is described by a set of random values. The exact solution to this problem is in L{Biskit.Statistics.lognormal}. @param x: observed value @type x: float @param R: sample of random values; 0 -> don't clip (default: 1e-32) @type R: [float] @param clip: clip zeros at this value @type clip: float @return: confidence that x is not random, mean of random distrib. @rtype: (float, float) """ if clip and 0 in R: R = N.clip(R, clip, max(R)) ## get mean and stdv of log-transformed random sample mean = N.average(N.log(R)) n = len(R) stdv = N.sqrt(N.sum(N.power(N.log(R) - mean, 2)) / (n - 1.)) ## create dense lognormal distribution representing the random sample stop = max(R) * 50.0 step = stop / 100000 start = step / 10.0 X = [(v, p_lognormal(v, mean, stdv)) for v in N.arange(start, stop, step)] ## analyse distribution d = Density(X) return d.findConfidenceInterval(x * 1.0)[0], d.average()
def logConfidence( x, R, clip=1e-32 ): """ Estimate the probability of x NOT beeing a random observation from a lognormal distribution that is described by a set of random values. The exact solution to this problem is in L{Biskit.Statistics.lognormal}. @param x: observed value @type x: float @param R: sample of random values; 0 -> don't clip (default: 1e-32) @type R: [float] @param clip: clip zeros at this value @type clip: float @return: confidence that x is not random, mean of random distrib. @rtype: (float, float) """ if clip and 0 in R: R = N.clip( R, clip, max( R ) ) ## get mean and stdv of log-transformed random sample mean = N.average( N.log( R ) ) n = len( R ) stdv = N.sqrt(N.sum(N.power(N.log( R ) - mean, 2)) / (n - 1.)) ## create dense lognormal distribution representing the random sample stop = max( R ) * 50.0 step = stop / 100000 start = step / 10.0 X = [(v, p_lognormal(v, mean, stdv) ) for v in N.arange(start, stop, step)] ## analyse distribution d = Density( X ) return d.findConfidenceInterval( x * 1.0 )[0], d.average()
def getFluct_local(self, mask=None, border_res=1, left_atoms=['C'], right_atoms=['N'], verbose=1): """ Get mean displacement of each atom from it's average position after fitting of each residue to the reference backbone coordinates of itself and selected atoms of neighboring residues to the right and left. @param mask: N_atoms x 1 array of 0||1, atoms for which fluctuation should be calculated @type mask: array @param border_res: number of neighboring residues to use for fitting @type border_res: int @param left_atoms: atoms (names) to use from these neighbore residues @type left_atoms: [str] @param right_atoms: atoms (names) to use from these neighbore residues @type right_atoms: [str] @return: Numpy array ( N_unmasked x 1 ) of float @rtype: array """ if mask is None: mask = N.ones(len(self.frames[0]), N.int32) if verbose: T.errWrite("rmsd fitting per residue...") residues = N.nonzero(self.ref.atom2resMask(mask)) ## backbone atoms used for fit fit_atoms_right = N.nonzero(self.ref.mask(right_atoms)) fit_atoms_left = N.nonzero(self.ref.mask(left_atoms)) ## chain index of each residue rchainMap = N.take(self.ref.chainMap(), self.ref.resIndex()) result = [] for res in residues: i_res, i_border = self.__resWindow(res, border_res, rchainMap, fit_atoms_left, fit_atoms_right) try: if not len(i_res): raise PDBError, 'empty residue' t_res = self.takeAtoms(i_res + i_border) i_center = range(len(i_res)) mask_BB = t_res.ref.maskBB() * t_res.ref.maskHeavy() ## fit with border atoms .. t_res.fit(ref=t_res.ref, mask=mask_BB, verbose=0) ## .. but calculate only with center residue atoms frames = N.take(t_res.frames, i_center, 1) avg = N.average(frames) rmsd = N.average(N.sqrt(N.sum(N.power(frames - avg, 2), 2))) result.extend(rmsd) if verbose: T.errWrite('#') except ZeroDivisionError: result.extend(N.zeros(len(i_res), N.Float32)) T.errWrite('?' + str(res)) if verbose: T.errWriteln("done") return result
def Solver(self, model, case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["PR_A"] b_i = model["PR_B"] W_i = model["OMEGA"] TC_i = model["TC"] case.Prop["MolWt"] = sum(xm * model["MoleWt"]) fwi = [] for Wii in W_i: if Wii < 0.5: fwi.append(0.37464 + 1.54226 * Wii - 0.26992 * power(Wii, 2)) else: fwi.append(0.3796 + 1.4850 * Wii - 0.1644 * power(Wii, 2) + 0.01666 * power(Wii, 3)) fwi = array(fwi) Tr_i = T / TC_i AlphaT = power((1 + fwi * (1 - sqrt(Tr_i))), 2) a_i = Ac * AlphaT A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) Zl_i = self.EOS.ZL(A_i, B_i) Zv_i = self.EOS.ZG(A_i, B_i) CoeFugo_v = self.FugaP(Zv_i, A_i, B_i) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i) yf = xm xf = xm for i in range(3): A_vi = MixingRules.MolarK2(yf, A_i, k=0.0) A_li = MixingRules.MolarK2(xf, A_i, k=0.0) B_v = MixingRules.Molar(yf, B_i) A_v = MixingRules.MolarK(yf, A_i, k=0.0) B_l = MixingRules.Molar(xf, B_i) A_l = MixingRules.MolarK(xf, A_i, k=0.0) Z_v = self.EOS.ZG(A_v, B_v) Z_l = self.EOS.ZL(A_l, B_l) CoeFugM_v = self.FugaM(Z_v, A_vi, B_i, A_v, B_v) CoeFugM_l = self.FugaM(Z_l, A_li, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v #print xm FrVap, xf, yf = Flash(ki, xm) Z = FrVap * Z_v + (1 - FrVap) * Z_l case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["xf"] = xf case.Prop["yf"] = yf
def variance(x, avg = None): if avg is None: avg = average(x) return Numeric.sum(Numeric.power(Numeric.array(x) - avg, 2)) / (len(x) - 1.)
def Solver(self, model, case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["SRK_A"] b_i = model["RK_B"] W_i = model["OMEGA"] TC_i = model["TC"] case.Prop["MolWt"] = sum(xm * model["MoleWt"]) fwi = 0.48 + 0.1574 * W_i - 0.176 * power(W_i, 2) Tr_i = T / TC_i AlphaT = power((1 + fwi * (1 - sqrt(Tr_i))), 2) a_i = Ac * AlphaT A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) Zl_i = self.EOS.ZL(A_i, B_i) Zv_i = self.EOS.ZG(A_i, B_i) CoeFugo_v = self.FugaP(Zv_i, A_i, B_i) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i) yf = xm xf = xm for i in range(3): A_vi = MixingRules.MolarK2(yf, A_i, k=0.0) A_li = MixingRules.MolarK2(xf, A_i, k=0.0) B_v = MixingRules.Molar(yf, B_i) A_v = MixingRules.MolarK(yf, A_i, k=0.0) B_l = MixingRules.Molar(xf, B_i) A_l = MixingRules.MolarK(xf, A_i, k=0.0) Z_v = self.EOS.ZG(A_v, B_v) Z_l = self.EOS.ZL(A_l, B_l) CoeFugM_v = self.FugaM(Z_v, A_vi, B_i, A_v, B_v) CoeFugM_l = self.FugaM(Z_l, A_li, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v #print xm FrVap, xf, yf = Flash(ki, xm) Z = FrVap * Z_v + (1 - FrVap) * Z_l case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["xf"] = xf case.Prop["yf"] = yf
def calc_membership_matrix(self, d2): ## remove 0s (if a cluster center is exactly on one item) d2 = N.clip(d2, N.power(1e200, 1 - self.w), 1e300) q = N.power(d2, 1. / (1. - self.w)) return q / N.sum(q)
def variance(x, avg=None): if avg is None: avg = average(x) return Numeric.sum(Numeric.power(Numeric.array(x) - avg, 2)) / (len(x) - 1.)
def Solver(self,model,case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["SRK_A"] b_i = model["RK_B"] W_i = model["OMEGA"] TC_i = model["TC"] case.Prop["MolWt"] = sum( xm* model["MoleWt"] ) fwi = 0.48+0.1574*W_i-0.176*power(W_i,2) Tr_i = T /TC_i AlphaT = power( ( 1+fwi*(1-sqrt(Tr_i) ) ),2) a_i = Ac * AlphaT A_i = ( a_i * P)/ pow( R * T,2) B_i = ( b_i * P )/( R * T) Zl_i= self.EOS.ZL(A_i,B_i) Zv_i= self.EOS.ZG(A_i,B_i) CoeFugo_v = self.FugaP(Zv_i, A_i, B_i ) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i ) yf = xm xf = xm for i in range(3): A_vi = MixingRules.MolarK2( yf, A_i, k=0.0) A_li = MixingRules.MolarK2( xf, A_i,k=0.0) B_v = MixingRules.Molar( yf, B_i) A_v = MixingRules.MolarK( yf,A_i ,k=0.0) B_l = MixingRules.Molar( xf, B_i) A_l = MixingRules.MolarK( xf,A_i,k=0.0) Z_v = self.EOS.ZG(A_v,B_v) Z_l = self.EOS.ZL(A_l,B_l) CoeFugM_v = self.FugaM(Z_v, A_vi,B_i,A_v,B_v) CoeFugM_l = self.FugaM(Z_l, A_li,B_i,A_l,B_l) fi = P*CoeFugM_v*yf ki = CoeFugM_l/CoeFugM_v #print xm FrVap, xf, yf = Flash(ki, xm) Z = FrVap*Z_v + (1- FrVap)* Z_l case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["xf"] = xf case.Prop["yf"] = yf
from ollin.Administrator.AdmOllin import Ollin from numpy.oldnumeric import array,power,pi PR=Ollin.AddModel("PR","PR") Ollin.Add(["HYDROGEN","METHANE","BENZENE","TOLUENE","DIPHENYL",],"PR") S1=Ollin.AddCase("S1")#Alimentacion al compresor S1.SetX([0.366021,0.548913,0.062618,0.021503,0.000945]) S1.T(38+273.15) ##S1.FracVap(0.9) S1.P(3206.062) Ollin.Solve("S1")#Caculamos S1 Ollin.Resumen("S1") L = (1-S1.Get("FracVap"))*1919.605 Gv = (L*S1.Get("MolWt_l"))/( S1.Get("LiqDen")*60 ) Vr = Gv*5 Lon = power((256*Vr/pi),0.333333) Dia = Lon/4 print "Longuitud", Lon print "Diametro",Dia
def GetYCurrentRange(self): yAxis = self._getYCurrentRange() if self.getLogScale()[1]: yAxis = _Numeric.power(10,yAxis) return yAxis
def match(x, y, n_iterations=1, z=2, eps_rmsd=0.5, eps_stdv=0.05): """ Matches two arrays onto each other, while iteratively removing outliers. Superimposed array y would be C{ N.dot(y, N.transpose(r)) + t }. @param n_iterations: number of calculations:: 1 .. no iteration 0 .. until convergence @type n_iterations: 1|0 @param z: number of standard deviations for outlier definition (default: 2) @type z: float @param eps_rmsd: tolerance in rmsd (default: 0.5) @type eps_rmsd: float @param eps_stdv: tolerance in standard deviations (default: 0.05) @type eps_stdv: float @return: (r,t), [ [percent_considered, rmsd_for_it, outliers] ] @rtype: (array, array), [float, float, int] """ iter_trace = [] rmsd_old = 0 stdv_old = 0 n = 0 converged = 0 mask = N.ones(len(y), N.int32) while not converged: ## find transformation for best match r, t = findTransformation(N.compress(mask, x, 0), N.compress(mask, y, 0)) ## transform coordinates xt = N.dot(y, N.transpose(r)) + t ## calculate row distances d = N.sqrt(N.sum(N.power(x - xt, 2), 1)) * mask ## calculate rmsd and stdv rmsd = N.sqrt(N.average(N.compress(mask, d)**2)) stdv = MU.SD(N.compress(mask, d)) ## check conditions for convergence d_rmsd = abs(rmsd - rmsd_old) d_stdv = abs(1 - stdv_old / stdv) if d_rmsd < eps_rmsd and d_stdv < eps_stdv: converged = 1 else: rmsd_old = rmsd stdv_old = stdv ## store result perc = round(float(N.sum(mask)) / float(len(mask)), 2) ## throw out non-matching rows mask = N.logical_and(mask, N.less(d, rmsd + z * stdv)) outliers = N.nonzero(N.logical_not(mask)) iter_trace.append([perc, round(rmsd, 3), outliers]) n += 1 if n_iterations and n >= n_iterations: break return (r, t), iter_trace
def match(x, y, n_iterations=1, z=2, eps_rmsd=0.5, eps_stdv=0.05): """ Matches two arrays onto each other, while iteratively removing outliers. Superimposed array y would be C{ N.dot(y, N.transpose(r)) + t }. @param n_iterations: number of calculations:: 1 .. no iteration 0 .. until convergence @type n_iterations: 1|0 @param z: number of standard deviations for outlier definition (default: 2) @type z: float @param eps_rmsd: tolerance in rmsd (default: 0.5) @type eps_rmsd: float @param eps_stdv: tolerance in standard deviations (default: 0.05) @type eps_stdv: float @return: (r,t), [ [percent_considered, rmsd_for_it, outliers] ] @rtype: (array, array), [float, float, int] """ iter_trace = [] rmsd_old = 0 stdv_old = 0 n = 0 converged = 0 mask = N.ones(len(y), N.int32 ) while not converged: ## find transformation for best match r, t = findTransformation(N.compress(mask, x, 0), N.compress(mask, y, 0)) ## transform coordinates xt = N.dot(y, N.transpose(r)) + t ## calculate row distances d = N.sqrt(N.sum(N.power(x - xt, 2), 1)) * mask ## calculate rmsd and stdv rmsd = N.sqrt(N.average(N.compress(mask, d)**2)) stdv = MU.SD(N.compress(mask, d)) ## check conditions for convergence d_rmsd = abs(rmsd - rmsd_old) d_stdv = abs(1 - stdv_old / stdv) if d_rmsd < eps_rmsd and d_stdv < eps_stdv: converged = 1 else: rmsd_old = rmsd stdv_old = stdv ## store result perc = round(float(N.sum(mask)) / float(len(mask)), 2) ## throw out non-matching rows mask = N.logical_and(mask, N.less(d, rmsd + z * stdv)) outliers = N.nonzero( N.logical_not( mask ) ) iter_trace.append([perc, round(rmsd, 3), outliers]) n += 1 if n_iterations and n >= n_iterations: break return (r, t), iter_trace
def Isotermic(self,model,case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["PR_A"] b_i = model["PR_B"] W_i = model["OMEGA"] TC_i = model["TC"] PC_i = model["PC"] VC_i = model["VC"] ## print xm TC = sum( xm* TC_i ) PC = sum( xm* PC_i ) VC = sum( xm* VC_i ) ## print "PC",PC_i ## print "TC",TC_i ## print TC,PC case.Prop["ZC"]=PC*VC/(R*TC) ## print case.Prop["ZC"] PreVap = self.PV.P(T,model) case.Prop["PreVap"]=PreVap fwi = [] for Wii in W_i: if Wii <0.5: fwi.append( 0.37464+1.54226*Wii-0.26992*power(Wii,2) ) else: fwi.append( 0.3796+1.4850*Wii-0.1644*power(Wii,2)+0.01666*power(Wii,3) ) fwi = array( fwi ) case.Prop["fw"]=fwi Tr_i = T /TC_i case.Prop["Tr"]=Tr_i AlphaT = power( ( 1+fwi*(1-sqrt(Tr_i) ) ),2) case.Prop["AlphaT"] = AlphaT case.Prop["dadT"]= self.dadT( Ac,fwi,Tr_i,TC_i) case.Prop["d2adT2"]= self.d2adT2( Ac,fwi,T,Tr_i,TC_i) case.Prop["MolWt"] = sum( xm* model["MoleWt"] ) a_i = Ac * AlphaT case.Prop["a"] = a_i A_i = ( a_i * P)/ pow( R * T,2) B_i = ( b_i * P )/( R * T) case.Prop["A"] = A_i case.Prop["B"] = B_i Zl_i= self.EOS.ZL(A_i,B_i) Zv_i= self.EOS.ZG(A_i,B_i) case.Prop["Zli"] = Zl_i case.Prop["Zvi"] = Zv_i case.Prop["Vli"] = Zl_i* R * T / P case.Prop["Vvi"] = Zv_i* R * T / P CoeFugo_v = self.FugaP(Zv_i, A_i, B_i ) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i ) k_i = 1 RFrac = 2 FVap, xf, yf = Flash(case.Prop["PreVap"]/P,xm) ## print sum(xf),sum(yf) ## print "Zli",sum(xf*Zl_i) ## print "Zvi",sum(yf*Zv_i) ## print "Frac",FVap,case.Prop["PreVap"]/P #Iteration to calculate the Fractio Vapor MinFuga = 1e10 while k_i <=20: ## print "a" A_vi = MixingRules.MolarK2( yf, A_i, k=0 ) A_li = MixingRules.MolarK2( xf, A_i,k=0) ## print "b" B_v = MixingRules.Molar( yf, B_i) A_v = MixingRules.MolarK( yf,A_i ,k=0 ) ## print "c" B_l = MixingRules.Molar( xf, B_i) A_l = MixingRules.MolarK( xf,A_i,k=0) Z_v = self.EOS.ZG(A_v,B_v) Z_l = self.EOS.ZL(A_l,B_l) ## print "Zv",Z_v ## print k_i,Z_v,Z_l CoeFugM_v = self.FugaM(Z_v, A_vi,B_i,A_v,B_v) CoeFugM_l = self.FugaM(Z_l, A_li,B_i,A_l,B_l) f_l = P*CoeFugM_l*xf f_v = P*CoeFugM_v*yf f_l = P*CoeFugM_l*xf f_v = P*CoeFugM_v*yf dFuga = sum(f_l-f_v) ## print "fuga",sum(f_l),sum(f_v),dFuga,FrVap if abs(dFuga) <MinFuga: ## print "min",dFuga MinFuga = abs(dFuga) ymin= yf xmin= xf ki = CoeFugM_l/CoeFugM_v ## print "ki",ki FrVap, xf, yf = Flash(ki, xm,FVap) ## print FrVap ## print "fuga",sum(f_l),sum(f_v) ## print xf,yf Z = FrVap*Z_v + (1- FrVap)* Z_l if abs(RFrac-FrVap)<= 1e-10: case.Solve = 1 break if abs(dFuga)<=1e-3: ## print "b" case.Solve = 1 break RFrac = FrVap k_i +=1 #End iteration to calculate the Fractio Vapor V_l = Z_l * R * T / P V_v = Z_v * R * T / P case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["ActivityVap"] = CoeFugM_v/CoeFugo_v case.Prop["ActivityLiq"] = CoeFugM_l/CoeFugo_l case.Prop["xf"] = xf case.Prop["yf"] = yf case.Prop["Vl"] = V_l case.Prop["Vv"] = V_v case.Prop["MolWt_l"] = sum( xf* model["MoleWt"] ) case.Prop["MolWt_v"] = sum( yf* model["MoleWt"] ) case.Prop["LiqDen"] = sum( xf* model["LIQDEN"] ) case.Prop["fl_i"] =f_l case.Prop["fv_i"] =f_v
def Isotermicx(self, model, case): yf = case.Prop["yf"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["RK_A"] b_i = model["RK_B"] case.Prop["dadT"] = self.dadT(Ac, T) case.Prop["d2adT2"] = self.d2adT2(Ac, T) ## case.Prop["MolWt"] = sum( xm* model["MoleWt"] ) PreVap = self.PV.P(T, model) case.Prop["PreVap"] = PreVap AlphaT = 1 / sqrt(T) case.Prop["AlphaT"] = AlphaT a_i = Ac * AlphaT case.Prop["a"] = a_i A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) case.Prop["A"] = A_i case.Prop["B"] = B_i Zl_i = self.EOS.ZL(A_i, B_i) Zv_i = self.EOS.ZG(A_i, B_i) case.Prop["Zli"] = Zl_i case.Prop["Zvi"] = Zv_i case.Prop["Vli"] = Zl_i * R * T / P case.Prop["Vvi"] = Zv_i * R * T / P CoeFugo_v = self.FugaP(Zv_i, A_i, B_i) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i) ## yf = xm xf = yf / (PreVap / P) sk = sum(PreVap / P) B_v = MixingRules.Molar(yf, B_i) A_v = power(sum(yf * sqrt(A_i)), 2) Z_v = self.EOS.ZG(A_v, B_v) CoeFugM_v = self.FugaM(Z_v, A_i, B_i, A_v, B_v) k_i = 1 RFrac = 2 ## FrVap, xf, yf = Flash(PreVap/P,xm) #Iteration to calculate the Fractio Vapor while k_i <= 10: B_l = MixingRules.Molar(xf, B_i) A_l = power(sum(xf * sqrt(A_i)), 2) Z_l = self.EOS.ZL(A_l, B_l) CoeFugM_l = self.FugaM(Z_l, A_i, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v ## Z = FrVap*Z_v + (1- FrVap)* Z_l if abs(sk - sum(ki)) <= 1e-8: print xf ## case.Solve = 1 break xf = yf / ki sk = sum(ki) k_i += 1 FrVap, xm = Flash(ki, x=xf, y=yf) print "Frac", FrVap Z = FrVap * Z_v + (1 - FrVap) * Z_l V_l = Z_l * R * T / P V_v = Z_v * R * T / P
def Isotermic(self, model, case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["SRK_A"] b_i = model["RK_B"] W_i = model["OMEGA"] TC_i = model["TC"] PreVap = self.PV.P(T, model) case.Prop["PreVap"] = PreVap ## print "PV", PreVap fwi = 0.48 + 0.1574 * W_i - 0.176 * power(W_i, 2) case.Prop["fw"] = fwi Tr_i = T / TC_i case.Prop["Tr"] = Tr_i AlphaT = power((1 + fwi * (1 - sqrt(Tr_i))), 2) case.Prop["AlphaT"] = AlphaT case.Prop["MolWt"] = sum(xm * model["MoleWt"]) case.Prop["dadT"] = self.dadT(Ac, fwi, Tr_i, TC_i) case.Prop["d2adT2"] = self.d2adT2(Ac, fwi, T, Tr_i, TC_i) a_i = Ac * AlphaT case.Prop["a"] = a_i A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) case.Prop["A"] = A_i case.Prop["B"] = B_i Zl_i = self.EOS.ZL(A_i, B_i) Zv_i = self.EOS.ZG(A_i, B_i) case.Prop["Zli"] = Zl_i case.Prop["Zvi"] = Zv_i case.Prop["Vli"] = Zl_i * R * T / P case.Prop["Vvi"] = Zv_i * R * T / P CoeFugo_v = self.FugaP(Zv_i, A_i, B_i) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i) ## yf = xm ## xf = xm/2 k_i = 1 RFrac = 2 FrVap, xf, yf = Flash(case.Prop["PreVap"] / P, xm) #Iteration to calculate the Fractio Vapor while k_i <= 20: ## print k_i A_vi = MixingRules.MolarK2(yf, A_i, k=0) A_li = MixingRules.MolarK2(xf, A_i, k=0) B_v = MixingRules.Molar(yf, B_i) A_v = MixingRules.MolarK(yf, A_i, k=0) B_l = MixingRules.Molar(xf, B_i) A_l = MixingRules.MolarK(xf, A_i, k=0) Z_v = self.EOS.ZG(A_v, B_v) Z_l = self.EOS.ZL(A_l, B_l) CoeFugM_v = self.FugaM(Z_v, A_vi, B_i, A_v, B_v) CoeFugM_l = self.FugaM(Z_l, A_li, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v FrVap, xf, yf = Flash(ki, xm) Z = FrVap * Z_v + (1 - FrVap) * Z_l ## if RFrac==FrVap: ## print "hola" if abs(RFrac - FrVap) <= 1e-10: case.Solve = 1 break RFrac = FrVap k_i += 1
def Solver(self,model,case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["PR_A"] b_i = model["PR_B"] W_i = model["OMEGA"] TC_i = model["TC"] case.Prop["MolWt"] = sum( xm* model["MoleWt"] ) fwi = [] for Wii in W_i: if Wii <0.5: fwi.append( 0.37464+1.54226*Wii-0.26992*power(Wii,2) ) else: fwi.append( 0.3796+1.4850*Wii-0.1644*power(Wii,2)+0.01666*power(Wii,3) ) fwi = array( fwi ) Tr_i = T /TC_i AlphaT = power( ( 1+fwi*(1-sqrt(Tr_i) ) ),2) a_i = Ac * AlphaT A_i = ( a_i * P)/ pow( R * T,2) B_i = ( b_i * P )/( R * T) Zl_i= self.EOS.ZL(A_i,B_i) Zv_i= self.EOS.ZG(A_i,B_i) CoeFugo_v = self.FugaP(Zv_i, A_i, B_i ) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i ) yf = xm xf = xm for i in range(3): A_vi = MixingRules.MolarK2( yf, A_i, k=0.0) A_li = MixingRules.MolarK2( xf, A_i,k=0.0) B_v = MixingRules.Molar( yf, B_i) A_v = MixingRules.MolarK( yf,A_i ,k=0.0) B_l = MixingRules.Molar( xf, B_i) A_l = MixingRules.MolarK( xf,A_i,k=0.0) Z_v = self.EOS.ZG(A_v,B_v) Z_l = self.EOS.ZL(A_l,B_l) CoeFugM_v = self.FugaM(Z_v, A_vi,B_i,A_v,B_v) CoeFugM_l = self.FugaM(Z_l, A_li,B_i,A_l,B_l) fi = P*CoeFugM_v*yf ki = CoeFugM_l/CoeFugM_v #print xm FrVap, xf, yf = Flash(ki, xm) Z = FrVap*Z_v + (1- FrVap)* Z_l case.Prop["Z"] = Z case.Prop["Zl"] = Z_l case.Prop["Zv"] = Z_v case.Prop["Ki"] = ki case.Prop["FracVap"] = FrVap case.Prop["CoefPureLiq"] = CoeFugo_l case.Prop["CoefPureVap"] = CoeFugo_v case.Prop["CoefMixVLiq"] = CoeFugM_l case.Prop["CoefMixVap"] = CoeFugM_v case.Prop["xf"] = xf case.Prop["yf"] = yf
def SRK(model, case): R = 0.08206 ## xm = case.ExtProp["x"] yf = case.ExtProp["yf"] xf = case.ExtProp["xf"] pv = case.ExtProp["pv"] FrVap = case.ExtProp["FracVap"] T = case.ExtProp["T"] P = case.ExtProp["P"] Ac = model.Const["SRK_A"] b_i = model.Const["RK_B"] W_i = model.Const["OMEGA"] TC_i = model.Const["TC"] ## print "Ac",Ac,b_i fwi = 0.48 + 0.1574 * W_i - 0.176 * power(W_i, 2) Tr_i = T / TC_i ## print Tr_i AlphaT = power((1 + fwi * (1 - sqrt(Tr_i))), 2) a_i = Ac * AlphaT A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) ## print "AiBi",A_i,B_i Zl_i = SRK.ZL(A_i, B_i) Zv_i = SRK.ZG(A_i, B_i) print "z", Zl_i, Zv_i CoeFugo_v = CoeFugo(Zv_i, A_i, B_i) CoeFugo_l = CoeFugo(Zl_i, A_i, B_i) #print CoeFugo_v/CoeFugo_l for i in range(3): A_vi = MixingRules.MolarK2(yf, A_i, k=0.5) B_vi = MixingRules.Molar(yf, B_i) A_li = MixingRules.MolarK2(xf, A_i, k=0.5) B_li = MixingRules.Molar(xf, B_i) B_v = MixingRules.Molar(yf, B_i) A_v = MixingRules.MolarK(yf, A_i, k=0.5) B_l = MixingRules.Molar(xf, B_i) A_l = MixingRules.MolarK(xf, A_i, k=0.5) Z_v = SRK.ZG(A_v, B_v) Z_l = SRK.ZL(A_l, B_l) CoeFugM_v = SRK.FugMix(Z_v, A_i, B_i, A_v, B_v) CoeFugM_l = SRK.FugMix(Z_l, A_i, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v #print xm FrVap, xf, yf = Flash(ki, xm) Z = FrVap * Z_v + (1 - FrVap) * Z_l print CoeFugM_v, "\n", ki, "\n", FrVap, "\n", xf, "\n", yf, "\n", CoeFugM_l, "\n", Z_v, Z_l
def fit( self, mask=None, ref=None, n_it=1, prof='rms', verbose=1, fit=1, **profInfos ): """ Superimpose all coordinate frames on reference coordinates. Put rms values in a profile. If n_it > 1, the fraction of atoms considered for the fit is put into a profile called |prof|_considered (i.e. by default 'rms_considered'). @param mask: atom mask, atoms to consider default: [all] @type mask: [1|0] @param ref: use as reference, default: None, average Structure @type ref: PDBModel @param n_it: number of fit iterations, kicking out outliers on the way 1 -> classic single fit, 0 -> until convergence (default: 1) @type n_it: int @param prof: save rms per frame in profile of this name, ['rms'] @type prof: str @param verbose: print progress info to STDERR (default: 1) @type verbose: 1|0 @param fit: transform frames after match, otherwise just calc rms (default: 1) @type fit: 1|0 @param profInfos: additional key=value pairs for rms profile info [] @type profInfos: key=value """ if ref == None: refxyz = N.average( self.frames, 0 ) else: refxyz = ref.getXyz() if mask is None: mask = N.ones( len( refxyz ), N.int32 ) refxyz = N.compress( mask, refxyz, 0 ) if verbose: T.errWrite( "rmsd fitting..." ) rms = [] ## rms value of each frame non_outliers = [] ## fraction of atoms considered for rms and fit iterations = [] ## number of iterations performed on each frame for i in range(0, len( self.frames) ): xyz = self.frames[i] if n_it != 1: (r, t), rmsdList = rmsFit.match( refxyz, N.compress( mask, xyz, 0), n_it) iterations.append( len( rmsdList ) ) non_outliers.append( rmsdList[-1][0] ) xyz_transformed = N.dot( xyz, N.transpose(r)) + t rms += [ rmsdList[-1][1] ] else: r, t = rmsFit.findTransformation( refxyz, N.compress( mask, xyz, 0)) xyz_transformed = N.dot( xyz, N.transpose(r)) + t d = N.sqrt(N.sum(N.power( N.compress(mask, xyz_transformed,0)\ - refxyz, 2), 1)) rms += [ N.sqrt( N.average(d**2) ) ] if fit: self.frames[i] = xyz_transformed.astype(N.float32) if verbose and i%100 == 0: T.errWrite( '#' ) self.setProfile( prof, rms, n_iterations=n_it, **profInfos ) if non_outliers: self.setProfile( prof+'_considered', non_outliers, n_iterations=n_it, comment='fraction of atoms considered for iterative fit' ) if verbose: T.errWrite( 'done\n' )
def calc_cluster_center(self, msm): p = N.power(msm, self.w) ccenter = N.transpose(N.dot(p, self.data)) return N.transpose(ccenter / N.sum(p, 1))
def getFluct_local( self, mask=None, border_res=1, left_atoms=['C'], right_atoms=['N'], verbose=1 ): """ Get mean displacement of each atom from it's average position after fitting of each residue to the reference backbone coordinates of itself and selected atoms of neighboring residues to the right and left. @param mask: N_atoms x 1 array of 0||1, atoms for which fluctuation should be calculated @type mask: array @param border_res: number of neighboring residues to use for fitting @type border_res: int @param left_atoms: atoms (names) to use from these neighbore residues @type left_atoms: [str] @param right_atoms: atoms (names) to use from these neighbore residues @type right_atoms: [str] @return: Numpy array ( N_unmasked x 1 ) of float @rtype: array """ if mask is None: mask = N.ones( len( self.frames[0] ), N.int32 ) if verbose: T.errWrite( "rmsd fitting per residue..." ) residues = N.nonzero( self.ref.atom2resMask( mask ) ) ## backbone atoms used for fit fit_atoms_right = N.nonzero( self.ref.mask( right_atoms ) ) fit_atoms_left = N.nonzero( self.ref.mask( left_atoms ) ) ## chain index of each residue rchainMap = N.take( self.ref.chainMap(), self.ref.resIndex() ) result = [] for res in residues: i_res, i_border = self.__resWindow(res, border_res, rchainMap, fit_atoms_left, fit_atoms_right) try: if not len( i_res ): raise PDBError, 'empty residue' t_res = self.takeAtoms( i_res + i_border ) i_center = range( len( i_res ) ) mask_BB = t_res.ref.maskBB() * t_res.ref.maskHeavy() ## fit with border atoms .. t_res.fit( ref=t_res.ref, mask=mask_BB, verbose=0 ) ## .. but calculate only with center residue atoms frames = N.take( t_res.frames, i_center, 1 ) avg = N.average( frames ) rmsd = N.average(N.sqrt(N.sum(N.power(frames - avg, 2), 2) )) result.extend( rmsd ) if verbose: T.errWrite('#') except ZeroDivisionError: result.extend( N.zeros( len(i_res), N.Float32 ) ) T.errWrite('?' + str( res )) if verbose: T.errWriteln( "done" ) return result
def SRK(model,case): R = 0.08206 ## xm = case.ExtProp["x"] yf = case.ExtProp["yf"] xf = case.ExtProp["xf"] pv = case.ExtProp["pv"] FrVap = case.ExtProp["FracVap"] T = case.ExtProp["T"] P = case.ExtProp["P"] Ac = model.Const["SRK_A"] b_i = model.Const["RK_B"] W_i = model.Const["OMEGA"] TC_i = model.Const["TC"] ## print "Ac",Ac,b_i fwi = 0.48+0.1574*W_i-0.176*power(W_i,2) Tr_i = T /TC_i ## print Tr_i AlphaT = power( ( 1+fwi*(1-sqrt(Tr_i) ) ),2) a_i = Ac * AlphaT A_i = ( a_i * P)/ pow( R * T,2) B_i = ( b_i * P )/( R * T) ## print "AiBi",A_i,B_i Zl_i = SRK.ZL(A_i,B_i) Zv_i = SRK.ZG(A_i,B_i) print "z", Zl_i, Zv_i CoeFugo_v = CoeFugo(Zv_i, A_i, B_i ) CoeFugo_l = CoeFugo(Zl_i, A_i, B_i ) #print CoeFugo_v/CoeFugo_l for i in range(3): A_vi = MixingRules.MolarK2( yf, A_i, k=0.5) B_vi = MixingRules.Molar( yf, B_i) A_li = MixingRules.MolarK2( xf, A_i,k=0.5) B_li = MixingRules.Molar( xf,B_i) B_v = MixingRules.Molar( yf, B_i) A_v = MixingRules.MolarK( yf, A_i, k=0.5) B_l = MixingRules.Molar( xf, B_i) A_l = MixingRules.MolarK( xf, A_i, k=0.5) Z_v = SRK.ZG(A_v,B_v) Z_l = SRK.ZL(A_l,B_l) CoeFugM_v = SRK.FugMix(Z_v, A_i,B_i,A_v,B_v) CoeFugM_l = SRK.FugMix(Z_l, A_i,B_i,A_l,B_l) fi = P*CoeFugM_v*yf ki = CoeFugM_l/CoeFugM_v #print xm FrVap, xf, yf = Flash(ki, xm) Z = FrVap*Z_v + (1- FrVap)* Z_l print CoeFugM_v,"\n",ki,"\n",FrVap,"\n",xf,"\n",yf,"\n",CoeFugM_l,"\n",Z_v,Z_l
def fit(self, mask=None, ref=None, n_it=1, prof='rms', verbose=1, fit=1, **profInfos): """ Superimpose all coordinate frames on reference coordinates. Put rms values in a profile. If n_it > 1, the fraction of atoms considered for the fit is put into a profile called |prof|_considered (i.e. by default 'rms_considered'). @param mask: atom mask, atoms to consider default: [all] @type mask: [1|0] @param ref: use as reference, default: None, average Structure @type ref: PDBModel @param n_it: number of fit iterations, kicking out outliers on the way 1 -> classic single fit, 0 -> until convergence (default: 1) @type n_it: int @param prof: save rms per frame in profile of this name, ['rms'] @type prof: str @param verbose: print progress info to STDERR (default: 1) @type verbose: 1|0 @param fit: transform frames after match, otherwise just calc rms (default: 1) @type fit: 1|0 @param profInfos: additional key=value pairs for rms profile info [] @type profInfos: key=value """ if ref == None: refxyz = N.average(self.frames, 0) else: refxyz = ref.getXyz() if mask is None: mask = N.ones(len(refxyz), N.int32) refxyz = N.compress(mask, refxyz, 0) if verbose: T.errWrite("rmsd fitting...") rms = [] ## rms value of each frame non_outliers = [] ## fraction of atoms considered for rms and fit iterations = [] ## number of iterations performed on each frame for i in range(0, len(self.frames)): xyz = self.frames[i] if n_it != 1: (r, t), rmsdList = rmsFit.match(refxyz, N.compress(mask, xyz, 0), n_it) iterations.append(len(rmsdList)) non_outliers.append(rmsdList[-1][0]) xyz_transformed = N.dot(xyz, N.transpose(r)) + t rms += [rmsdList[-1][1]] else: r, t = rmsFit.findTransformation(refxyz, N.compress(mask, xyz, 0)) xyz_transformed = N.dot(xyz, N.transpose(r)) + t d = N.sqrt(N.sum(N.power( N.compress(mask, xyz_transformed,0)\ - refxyz, 2), 1)) rms += [N.sqrt(N.average(d**2))] if fit: self.frames[i] = xyz_transformed.astype(N.float32) if verbose and i % 100 == 0: T.errWrite('#') self.setProfile(prof, rms, n_iterations=n_it, **profInfos) if non_outliers: self.setProfile( prof + '_considered', non_outliers, n_iterations=n_it, comment='fraction of atoms considered for iterative fit') if verbose: T.errWrite('done\n')
def GetXCurrentRange(self): xAxis = self._getXCurrentRange() if self.getLogScale()[0]: xAxis = _Numeric.power(10,xAxis) return xAxis
def Isotermic(self, model, case): xm = case.Prop["x"] T = case.Prop["T"] P = case.Prop["P"] Ac = model["RK_A"] b_i = model["RK_B"] case.Prop["dadT"] = self.dadT(Ac, T) case.Prop["d2adT2"] = self.d2adT2(Ac, T) case.Prop["MolWt"] = sum(xm * model["MoleWt"]) PreVap = self.PV.P(T, model) case.Prop["PreVap"] = PreVap AlphaT = 1 / sqrt(T) case.Prop["AlphaT"] = AlphaT a_i = Ac * AlphaT case.Prop["a"] = a_i A_i = (a_i * P) / pow(R * T, 2) B_i = (b_i * P) / (R * T) case.Prop["A"] = A_i case.Prop["B"] = B_i Zl_i = self.EOS.ZL(A_i, B_i) Zv_i = self.EOS.ZG(A_i, B_i) case.Prop["Zli"] = Zl_i case.Prop["Zvi"] = Zv_i case.Prop["Vli"] = Zl_i * R * T / P case.Prop["Vvi"] = Zv_i * R * T / P CoeFugo_v = self.FugaP(Zv_i, A_i, B_i) CoeFugo_l = self.FugaP(Zl_i, A_i, B_i) yf = xm xf = xm k_i = 1 RFrac = 2 FrVap, xf, yf = Flash(case.Prop["PreVap"] / P, xm) #Iteration to calculate the Fractio Vapor while k_i <= 10: B_v = MixingRules.Molar(yf, B_i) A_v = power(sum(yf * sqrt(A_i)), 2) B_l = MixingRules.Molar(xf, B_i) A_l = power(sum(xf * sqrt(A_i)), 2) Z_v = self.EOS.ZG(A_v, B_v) Z_l = self.EOS.ZL(A_l, B_l) CoeFugM_v = self.FugaM(Z_v, A_i, B_i, A_v, B_v) CoeFugM_l = self.FugaM(Z_l, A_i, B_i, A_l, B_l) fi = P * CoeFugM_v * yf ki = CoeFugM_l / CoeFugM_v FrVap, xf, yf = Flash(ki, xm) Z = FrVap * Z_v + (1 - FrVap) * Z_l if abs(RFrac - FrVap) <= 1e-10: case.Solve = 1 break RFrac = FrVap k_i += 1