def calculate_internal_force_span_Q_1(self, span_Q: int) -> sp.lambdify: """ Compute the shear lamdified function for a "span_Q", which is the span where the distribuited load Q is applied and the others Q is zero """ # TODO togliere i commentati nCampate = self.nCampate # lenghts = self.beam.spans_lenght() cum_lenghts = self.beam.spans_cum_lenght() total_lenght = self.beam.spans_total_lenght() x = self.x # List of matrixes r = self.r # List of matrixes I = np.identity(nCampate) # span_i = 1 # campata vera # n_span = 0 # ---- With Sympy: s = sp.Symbol("s") v_i = [(r[span_Q][n_span] - (I[span_Q, n_span] * (s - cum_lenghts[span_Q]))) * (sp.Heaviside(s - cum_lenghts[n_span]) - sp.Heaviside(s - cum_lenghts[n_span + 1])) for n_span in range(nCampate)] v_i_lambdify = sp.lambdify(s, np.sum(v_i, axis=0)) # ---- With numpy: # Not tried. See the BendingMoment Class instead return v_i_lambdify # TODO m_span_Q_1
def laplace_term(expr, t, s): const, expr = factor_const(expr, t) tsym = sym.sympify(str(t)) expr = expr.replace(tsym, t) if expr.has(sym.Integral): return laplace_integral(expr, t, s) * const if expr.has(sym.function.AppliedUndef): rest = sym.S.One for factor in expr.as_ordered_factors(): if isinstance(factor, sym.function.AppliedUndef): result = laplace_func(factor, t, s) else: if factor.has(t): raise ValueError('TODO: need derivative of undefined' ' function for %s' % factor) rest *= factor return result * rest * const if expr.has(sym.Heaviside(t)): return laplace_0(expr.replace(sym.Heaviside(t), 1), t, s) * const if expr.has(sym.DiracDelta) or expr.has(sym.Heaviside): try: return laplace_0minus(expr, t, s) * const except ValueError: pass return laplace_0(expr, t, s) * const
def batman_equations_heaviside(): # From : http://mathworld.wolfram.com/BatmanCurve.html x = sm.symbols('x', real=True) h_ = sm.symbols('h_') w = 3 * sm.sqrt(1 - (x / 7)**2) l = ((x + 3) / 2 - sm.S(3) / 7 * sm.sqrt(10) * sm.sqrt(4 - (x + 1)**2) + sm.S(6) / 7 * sm.sqrt(10)) r = ((3 - x) / 2 - sm.S(3) / 7 * sm.sqrt(10) * sm.sqrt(4 - (x - 1)**2) + sm.S(6) / 7 * sm.sqrt(10)) f = ((h_ - l) * sm.Heaviside(x + 1, 0) + (r - h_) * sm.Heaviside(x - 1, 0) + (l - w) * sm.Heaviside(x + 3, 0) + (w - r) * sm.Heaviside(x - 3, 0) + w) f_of = f.xreplace( {x: sm.Abs(x + sm.S(1) / 2) + sm.Abs(x - sm.S(1) / 2) + 6}) h = sm.S(1) / 2 * (f_of - 11 * (x + sm.S(3) / 4) + sm.Abs(x - sm.S(3) / 4)) f = f.xreplace({h_: h}) g = (sm.S(1) / 2 * (sm.Abs(x / 2) + sm.sqrt(1 - (sm.Abs(sm.Abs(x) - 2) - 1)**2) - sm.S(1) / 112 * (3 * sm.sqrt(33) - 7) * x**2 + 3 * sm.sqrt(1 - (sm.S(1) / 7 * x)**2) - 3) * ((x + 4) / sm.Abs(x + 4) - (x - 4) / sm.Abs(x - 4)) - 3 * sm.sqrt(1 - (x / 7)**2)) return f, g
def laplace_term(expr, t, s): var = sym.Symbol(str(t)) expr = expr.replace(var, t) if expr.has(sym.function.AppliedUndef) and expr.args[0] == t: # TODO, handle things like 3 * v(t), a * v(t), 3 * t * v(t), v(t-T), # v(4 * a * t), etc. if not isinstance(expr, sym.function.AppliedUndef): raise ValueError('Could not compute Laplace transform for ' + str(expr)) # Convert v(t) to V(s), etc. name = expr.func.__name__ name = name[0].upper() + name[1:] + '(s)' return sym.sympify(name) if expr.has(sym.Heaviside(t)): return laplace_0(expr.replace(sym.Heaviside(t), 1), t, s) if expr.has(sym.DiracDelta) or expr.has(sym.Heaviside): try: return laplace_0minus(expr, t, s) except ValueError: pass return laplace_0(expr, t, s)
def laplace_term(expr, t, s): tsym = sym.sympify(str(t)) expr = expr.replace(tsym, t) if expr.has(sym.function.AppliedUndef): rest = sym.sympify(1) for factor in expr.as_ordered_factors(): if isinstance(factor, sym.function.AppliedUndef): if factor.args[0] != t: raise ValueError('Weird function %s not of t' % factor) # Convert v(t) to V(s), etc. name = factor.func.__name__ ssym = sym.sympify(str(s)) func = name[0].upper() + name[1:] + '(%s)' % str(ssym) result = sym.sympify(func).subs(ssym, s) else: if factor.has(t): raise ValueError('TODO: need derivative of undefined' ' function for %s' % factor) rest *= factor return result * rest if expr.has(sym.Heaviside(t)): return laplace_0(expr.replace(sym.Heaviside(t), 1), t, s) if expr.has(sym.DiracDelta) or expr.has(sym.Heaviside): try: return laplace_0minus(expr, t, s) except ValueError: pass return laplace_0(expr, t, s)
def to_symf(self): d = sym.symbols("d") Ld = self.axd1 - self.axd0 chr_pls = (sym.Heaviside(d - self.axd0, 1) - sym.Heaviside(d - self.axd1, 1)) px = self.xc0 + (d - self.axd0) / Ld * (self.xc1 - self.xc0) py = self.yc0 + (d - self.axd0) / Ld * (self.yc1 - self.yc0) return (px * chr_pls, py * chr_pls)
def laplace_term(expr, t, s): # Unilateral LT ignores expr for t < 0 so remove Piecewise. if expr.is_Piecewise and expr.args[0].args[1].has(t >= 0): expr = expr.args[0].args[0] const, expr = factor_const(expr, t) terms = expr.as_ordered_terms() if len(terms) > 1: result = 0 for term in terms: result += laplace_term(term, t, s) return const * result tsym = sympify(str(t)) expr = expr.replace(tsym, t) if expr.has(sym.Integral): return laplace_integral(expr, t, s) * const if expr.is_Function and expr.func in (sym.sin, sym.cos) and expr.args[0].has(t): return laplace_sin_cos(expr, t, s) * const if expr.has(AppliedUndef): if expr.has(sym.Derivative): return laplace_derivative_undef(expr, t, s) * const factors = expr.as_ordered_factors() if len(factors) == 1: return const * laplace_func(factors[0], t, s) elif len(factors) > 2: raise ValueError('TODO: cannot handle product %s' % expr) foo = factors[1] if foo.is_Function and foo.func == sym.exp and foo.args[0].has(t): scale, shift = scale_shift(foo.args[0], t) if shift == 0: result = laplace_func(factors[0], t, s) return const * result.subs(s, s - scale) raise ValueError('TODO: cannot handle product %s' % expr) if expr.has(sym.Heaviside(t)): return laplace_0(expr.replace(sym.Heaviside(t), 1), t, s) * const if expr.has(sym.DiracDelta) or expr.has(sym.Heaviside): try: return laplace_0minus(expr, t, s) * const except ValueError: pass return laplace_0(expr, t, s) * const
def test_issue_13167_21411(): if not numpy: skip("numpy not installed") f1 = lambdify(x, sympy.Heaviside(x)) f2 = lambdify(x, sympy.Heaviside(x, 1)) res1 = f1([-1, 0, 1]) res2 = f2([-1, 0, 1]) assert Abs(res1[0]).n() < 1e-15 # First functionality: only one argument passed assert Abs(res1[1] - 1/2).n() < 1e-15 assert Abs(res1[2] - 1).n() < 1e-15 assert Abs(res2[0]).n() < 1e-15 # Second functionality: two arguments passed assert Abs(res2[1] - 1).n() < 1e-15 assert Abs(res2[2] - 1).n() < 1e-15
def int_legendre_bath_kernel(n): ''' Generates an analytical expression for the integration over the product between Legendre polynomial Pn(x) and the time Green's function expression, for use in generating Legendre bath orbitals. Parameters ---------- n : int order of Legendre polynomial Returns ------- expr : sympy.Expr sympy expression ''' t = sp.symbols('t', real=True) a = sp.symbols('e', nonzero=True, real=True) b = sp.symbols('beta', positive=True, nonzero=True, real=True) pn = sp.legendre(n, 2.0 * t / b + 1.0) gf = sp.exp(-a * (t + sp.Heaviside(a) * b)) expr = sp.integrate(pn * gf, t) return expr
def sp_derive(): import sympy as sp vars = 'G_s, s_n, s_p_n, w_n, dw_n, ds_n, G_s, G_w, c, phi' syms = sp.symbols(vars) for var, sym in zip(vars.split(','), syms): globals()[var.strip()] = sym s_n1 = s_n + ds_n w_n1 = w_n + dw_n tau_trial = G_s * (s_n1 - s_p_n) print('diff', sp.diff(tau_trial, ds_n)) print(tau_trial) sig_n1 = G_w * w_n1 print(sig_n1) tau_fr = (c + sig_n1 * sp.tan(phi)) * \ sp.Heaviside(sig_n1 - c / sp.tan(phi)) print(tau_fr) d_tau_fr = sp.diff(tau_fr, dw_n) print(d_tau_fr) f_trial = sp.abs(tau_trial) - tau_fr print(f_trial) d_gamma = f_trial / G_s print('d_gamma') sp.pretty_print(d_gamma) print('d_gamma_s') sp.pretty_print(sp.diff(d_gamma, ds_n)) print('tau_n1') tau_n1 = sp.simplify(tau_trial - d_gamma * G_s * sp.sign(tau_trial)) sp.pretty_print(tau_n1) print('dtau_n1_w') dtau_n1_w = sp.diff(tau_n1, dw_n) sp.pretty_print(dtau_n1_w) print('dtau_n1_s') dtau_n1_s = sp.diff(d_gamma * sp.sign(tau_trial), ds_n) print(dtau_n1_s) s_p_n1 = s_p_n + d_gamma * sp.sign(tau_trial) print(s_p_n1)
def heaviside_pattern(symbol): m = sympy.Wild('m', exclude=[symbol]) b = sympy.Wild('b', exclude=[symbol]) g = sympy.Wild('g') pattern = sympy.Heaviside(m * symbol + b) * g return pattern, m, b, g
def l_fcx(expr): import sympy fi = sympy.im(expr) fr = sympy.re(expr) return str( sympy.simplify( sympy.atan(sim(fi / fr)) + sympy.Heaviside(-fr) * sympy.pi))
def step_z3(x): y = x.copy() original_shape = y.shape y = y.reshape(max(y.shape[0], y.shape[1]), 1) for idx in range(y.shape[0]): y[idx, 0] = sp.Heaviside(y[idx, 0]) # y[idx, 0] = z3.If(y[idx, 0] > 0.0, 1.0, 0.0) # using 0.0 and 1.0 avoids int/float issues return y # .reshape(original_shape)
def codegen_legendre_bath_kernel(n): ''' Generates the Python code to compute the Legendre bath orbital for a given order. The code is somewhat optimised with common subexpression elimination, but sympy isn't great at identifying CSEs with the high-order exponents. Parameters ---------- n : int order of Legendre polynomial Returns ------- func : str string containing the code. The first line is given as `def legendre_bath_kernel_n(e, beta, chempot=0.0):`, where `n` is replaced with the input variable. ''' t = sp.symbols('t', real=True) a = sp.symbols('e', nonzero=True, real=True) b = sp.symbols('beta', positive=True, nonzero=True, real=True) exp = sp.symbols('exp') exp_hi = sp.symbols('exp_hi') exp_lo = sp.symbols('exp_lo') exponent = sp.exp(-a * (t + b * sp.Heaviside(a))) expr = int_legendre_bath_kernel(n) expr = expr.subs({exponent: exp}) hi = sp.simplify(expr.subs({t: 0, exp: exp_hi})) lo = sp.simplify(expr.subs({t: -b, exp: exp_lo})) expr = hi - lo expr = sp.cse(expr, optimizations='basic') numpy_printer()._kf['Heaviside'] = 'numpy.heaviside' func = 'def legendre_bath_kernel_%d(e, beta, chempot=0.0):\n' % n func += ' e = np.asarray(e)\n' for subexpr in expr[0]: func += ' %s\n' % numpy_printer().doprint(subexpr[1], subexpr[0]) func += ' exp_hi = np.ones_like(e)\n' func += ' exp_lo = np.ones_like(e)\n' func += ' exp_hi[e > chempot] = np.exp(-e[e > chempot]*beta)\n' func += ' exp_lo[e < chempot] = np.exp(e[e < chempot]*beta)\n' func += ' %s\n' % numpy_printer().doprint(expr[-1][0], 'val') func += ' return val\n' func = func.replace('numpy.heaviside(a)', 'numpy.heaviside(a-chempot, 0.0)') func = func.replace('numpy', 'np') return func
def zoh_discretization(t, k, s, z, T, P): pfe = sp.apart(P / s) terms = pfe.args if isinstance(pfe, sp.Add) else [pfe] terms = [ sp.inverse_laplace_transform(term, s, t).subs(t, k * T) for term in terms ] terms = [term.subs(sp.Heaviside(T * k), 1) for term in terms] return (z - 1) / z * z_transform(k, z, sum(terms))
def sym_L(self, x, u, t): return x[0]**2 +\ x[1]**2 +\ x[2]**2 +\ x[3]**2 +\ x[4]**2 +\ x[5]**2 +\ u[0]*sp.Heaviside(u[0]) * 1e-5 +\ u[1]**2 * 100
def evaluate_at_times(expression, t, ts): """Evaluate a sympy expression over time Arguments: expression: a sympy expression containing references to a time variable `t` t : a `sympy.Symbol` which is in `expression` and will be subsitituted from `ts` ts: an iterable containing times for evaluation. Should be only ints or floats versionadded: 0.1.3 """ return [ expression.subs(t, ti).subs({ sympy.Heaviside(float(0)): 1, sympy.Heaviside(0): 1 }) for ti in ts ]
def list_eval(f, x, l): """ retunrs a list. It evaluates the expression f for an array list. The function handles the heaviside(0) as 1 f: expression: the expression to be evaluated x: symbol: the symbol in the expression list: the list which is to be substituted """ ans = np.array([]) for i in l: ans = np.append( ans, float( sp.N((f.subs(x, i)).replace(sp.Heaviside(0), sp.Heaviside(0, 1))))) return ans
def sym_f(self, x, u, t): _, _, theta, xdot, ydot, thetadot = x thrust, gimbal = u p_thrust = thrust * sp.Heaviside(thrust) xddot = 1 / self.mass * (-sp.sin(theta + gimbal) * p_thrust) yddot = 1 / self.mass * (sp.cos(theta + gimbal) * p_thrust) - self.g thetaddot = 1 / self.I * (sp.sin(gimbal) * p_thrust * self.l) return sp.Matrix([xdot, ydot, thetadot, xddot, yddot, thetaddot])
def calculate_internal_force_span_Q_1( self, span_Q: int) -> sp.lambdify: # TODO _1 _func """ Compute the bending moment lamdified function for a "span_Q", which is the span where the distribuited load Q is applied and the others Q is zero """ # TODO togliere i commentati nCampate = self.nCampate # lenghts = self.beam.spans_lenght() cum_lenghts = self.beam.spans_cum_lenght() total_lenght = self.beam.spans_total_lenght() x = self.x # List of matrixes r = self.r # List of matrixes I = np.identity(nCampate) # span_i = 1 # campata vera # n_span = 0 # ---- With Sympy: s = sp.Symbol("s") m_i = [((x[span_Q][n_span] + r[span_Q][n_span] * (s - cum_lenghts[n_span])) - ((I[span_Q, n_span] * (s - cum_lenghts[span_Q])**2) / 2)) * (sp.Heaviside(s - cum_lenghts[n_span]) - sp.Heaviside(s - cum_lenghts[n_span + 1])) for n_span in range(nCampate)] m_i_lambdify = sp.lambdify(s, np.sum(m_i, axis=0)) # s_lambdify = np.linspace(0, total_lenght, 1000) # ---- With numpy: TODO maybe. doesnt work as expected the heaviside func # s = np.linspace(0, total_lenght, 1000) # m_i = [ # ((x[span_Q][n_span] + r[span_Q][n_span] * (s-cum_lenghts[n_span])) - ((I[span_Q,n_span]*(s-cum_lenghts[span_Q])**2)/2)) \ # * (np.heaviside(s-cum_lenghts[n_span],0) - np.heaviside(s-cum_lenghts[n_span+1],0)) \ # for n_span in range(nCampate) # ] # m_i is a list of list. We want to sum each list inside, not the total of everything: so we need "axis=0" # Example from numpy documentation: np.sum([[0, 1], [0, 5]], axis=0) >>> array([0, 6]) # return np.sum(m_i,axis=0) return m_i_lambdify # TODO m_span_Q_1
def test(): t, f, a = sym.symbols('t f a', real=True) a = sym.symbols('a', positive=True) print(fourier_transform(a, t, f)) print(fourier_transform(sym.exp(-sym.I * 2 * sym.pi * a * t), t, f)) print(fourier_transform(sym.cos(2 * sym.pi * a * t), t, f)) print(fourier_transform(sym.sin(2 * sym.pi * a * t), t, f)) print(fourier_transform(a * t, t, f)) print(fourier_transform(sym.exp(-a * t) * sym.Heaviside(t), t, f)) print(inverse_fourier_transform(a, f, t)) print(inverse_fourier_transform(1 / (sym.I * 2 * sym.pi * f + a), f, t))
def laplace_term(expr, t, s): const, expr = factor_const(expr, t) tsym = sympify(str(t)) expr = expr.replace(tsym, t) if expr.has(sym.Integral): return laplace_integral(expr, t, s) * const if expr.has(AppliedUndef): if expr.has(sym.Derivative): return laplace_derivative_undef(expr, t, s) * const factors = expr.as_ordered_factors() if len(factors) == 1: return const * laplace_func(factors[0], t, s) elif len(factors) > 2: raise ValueError('TODO: cannot handle product %s' % expr) foo = factors[1] if foo.is_Function and foo.func == sym.exp and foo.args[0].has(t): scale, shift = scale_shift(foo.args[0], t) if shift == 0: result = laplace_func(factors[0], t, s) return const * result.subs(s, s - scale) raise ValueError('TODO: cannot handle product %s' % expr) if expr.has(sym.Heaviside(t)): return laplace_0(expr.replace(sym.Heaviside(t), 1), t, s) * const if expr.has(sym.DiracDelta) or expr.has(sym.Heaviside): try: return laplace_0minus(expr, t, s) * const except ValueError: pass return laplace_0(expr, t, s) * const
def axial_loads_sym(self): d = sym.symbols('d') uv_ax = self.uv_axis() (qx, qy) = self.sum_my_dl() P_ax = -( sym.integrate(qx*uv_ax[0], d) + sym.integrate(qy*uv_ax[1], d) ) PR = self.my_P_and_R() if isinstance(PR, str): return PR for p in PR: #The distributed loads were all counted already #Moments have no influence on P_ax if not isinstance(p, Distr_Load) and not isinstance(p, Moment): p_ax = np.dot(uv_ax, p.get_comp()) P_ax -= p_ax*sym.Heaviside(d-p.ax_dist, 1) #, .5) return P_ax.rewrite(sym.Piecewise).doit()
def inverse_laplace_term(expr, s, t, **assumptions): expr, delay = delay_factor(sym.simplify(expr), s) result1, result2 = inverse_laplace_term1(expr, s, t, **assumptions) if delay != 0: result1 = result1.subs(t, t - delay) result2 = result2.subs(t, t - delay) # TODO, should check for delay < 0. If so the causal # part is no longer causal. if assumptions.get('causal', False): result2 = result2 * sym.Heaviside(t - delay) return result1, result2
def inverse_laplace_term(expr, s, t, **assumptions): expr, delay = delay_factor(expr, s) cresult, uresult = inverse_laplace_term1(expr, s, t, **assumptions) if delay != 0: cresult = cresult.subs(t, t - delay) uresult = uresult.subs(t, t - delay) # TODO, should check for delay < 0. If so the causal # part is no longer causal. if assumptions.get('causal', False): uresult = uresult * sym.Heaviside(t - delay) return cresult, uresult
def moment_symf(self): PR = self.my_P_and_R() if isinstance(PR, str): return PR V = self.shear_symf() if isinstance(V, str): return V d = sym.symbols('d') M = sym.integrate(V, d) #Zero out s0 - constant of integration M -= M.subs(d, 0) for p in PR: if isinstance(p, Moment): M -= p.mz*sym.Heaviside(d-p.ax_dist, 1) return M.rewrite(sym.Piecewise).doit()
def shear_symf(self): (qx, qy) = self.sum_my_dl() PR = self.my_P_and_R() if isinstance(PR, str): return PR V = sym.Integer(0) d = sym.symbols('d') uvax = self.uv_axis() uvprp = np.array((-uvax[1], uvax[0])) #Dot product spelled out here (b/c it's all sym) V += uvprp[0]*sym.integrate(qx, d) + uvprp[1]*sym.integrate(qy, d) for p in PR: #The distributed loads were all counted already #Moments have no influence on V if not isinstance(p, Distr_Load) and not isinstance(p, Moment): Vp = np.dot(uvprp, p.get_comp()) V += Vp*sym.Heaviside(d-p.ax_dist, 1) #, .5) #Rewriting as piecewise helps it to integrate the Heaviside return V.rewrite(sym.Piecewise).doit()
def eval_heaviside(harg, ibnd, substep, integrand, symbol): # If we are integrating over x and the integrand has the form # Heaviside(m*x+b)*g(x) == Heaviside(harg)*g(symbol) # then there needs to be continuity at -b/m == ibnd, # so we subtract the appropriate term. return sympy.Heaviside(harg) * (substep - substep.subs(symbol, ibnd))
""" import datetime import sympy from sympy.abc import k, A, rho, R, Q, C, T, alpha, omega from scipy import integrate, inf omega_cutoff = 1e10 print(datetime.datetime.now()) theta = omega * rho * C alpha = sympy.acos((1 - theta**2) / (1 + theta**2)) F2 = (1 / (2 * (A + 1))**2 * (A**2 + (A + 2)**2 - A * (A + 2) * sympy.cos(2 * omega * T + alpha))) H2 = sympy.Heaviside(omega_cutoff - omega) symbolic_integrand = F2 * H2 inputs = {'A': 39, 'rho': 50, 'T': 4e-9, 'C': 6e-12} numeric_integrand = sympy.lambdify(omega, symbolic_integrand.subs(inputs), modules=('numpy', 'sympy')) noise_V2 = integrate.quad(numeric_integrand, 0, omega_cutoff) print(noise_V2) print(datetime.datetime.now())
def process(self, p, conn, param, alpha): mu, Ex, Kp, Kg1, Kg2, Kvb, Gco, Gcf = const = sp.symbols( "mu,Ex,Kp,Kg1,Kg2,Kvb,Gco,Gcf") Ug1k, Ug2k, Uak = v = sp.symbols("Ug1k,Ug2k,Uak") t = Kp * (1 / mu + Ug1k / Ug2k) E1 = Ug2k / Kp * sp.log(1 + sp.exp(t)) E1_ = Ug2k / Kp * t #sign = sp.sign sign = lambda x: 2 * sp.Heaviside(x) - 1 calc_Ia = sp.Piecewise( (0, Ug2k <= 0), (0, t < -500), (-pow(E1_, Ex) / Kg1 * (1 + sign(E1_)) * sp.atan(Uak / Kvb), t > 500), (-pow(E1, Ex) / Kg1 * (1 + sign(E1)) * sp.atan(Uak / Kvb), True)) calc_Ia = calc_Ia.subs(dict([(k, param[str(k)]) for k in const])) calc_Ig = sp.Piecewise((0, Ug1k < Gco), (-Gcf * pow(Ug1k - Gco, 1.5), True)) calc_Ig = calc_Ig.subs(dict([(k, param[str(k)]) for k in const])) t = Ug2k / mu + Ug1k calc_Is = sp.Piecewise((0, t <= 0), (-sp.exp(Ex * sp.log(t)) / Kg2, True)) calc_Is = calc_Is.subs(dict([(k, param[str(k)]) for k in const])) # def calc_Ia(v): # Ug1k = float(v[0]) # Ug2k = float(v[1]) # Uak = float(v[2]) # if Ug2k <= 0.0: # return 0 # t = Kp * (1 / mu + Ug1k / Ug2k) # if t > 500: # E1 = Ug2k / Kp * t # elif t < -500: # return 0 # else: # E1 = Ug2k / Kp * math.log(1 + math.exp(t)) # r = pow(E1,Ex)/Kg1 * 2*(E1 > 0.0) * math.atan(Uak/Kvb); # #print Ug1k, Ug2k, Uak, r # return -r # def calc_Ig(v): # Ugk = float(v[0]) # if Ugk < Gco: # return 0 # r = Gcf*pow(Ugk-Gco, 1.5) # return -r # def calc_Is(v): # Ug1k = float(v[0]) # Ug2k = float(v[1]) # t = Ug2k / mu + Ug1k # if t <= 0: # return 0 # r = math.exp(Ex*math.log(t)) / Kg2 # return -r idx1 = p.new_row("N", self, "Ig") p.add_2conn("Nl", idx1, (conn[0], conn[3])) p.add_2conn("Nr", idx1, (conn[0], conn[3])) p.set_function(idx1, calc_Ig, v[:1], idx1) idx2 = p.new_row("N", self, "Is") p.add_2conn("Nl", idx2, (conn[1], conn[3])) p.add_2conn("Nr", idx2, (conn[1], conn[3])) p.set_function(idx2, calc_Is, v, idx1) idx3 = p.new_row("N", self, "Ip") p.add_2conn("Nl", idx3, (conn[2], conn[3])) p.add_2conn("Nr", idx3, (conn[2], conn[3])) p.set_function(idx3, calc_Ia, v, idx1)