def P(self, zval): # choose between identity and fancy projection if zval[self.si] > 0: return zval else: expr = self._P vals = zval params = self.z return tn.eval(expr, params, vals)
def _delf(self, t, xval, uval): # calculates the jump term assuming the field switches # between fplus and fminus at (t, x) params = np.concatenate(([t], xval, uval)) fp = self._fplus.func(*params) fm = self._fmins.func(*params) dphi = self.dphi(xval) # this assumes x = [z, zdot] M = tn.eval(self.Mz, self.z, xval[:self.dim]) M = scipy.linalg.block_diag(M, np.eye(self.dim)) #dphi = matmult(M, dphi) out = -np.outer(fp-fm, dphi)/np.abs(np.inner(fp, dphi)) #Tracer()() #out = np.zeros((2*self.dim, 2*self.dim)) #for i in range(self.dim): # out[self.si, i] = -M[self.si, i] return out
def dP(self, zval): # for debug purposes return tn.eval(self._dP, self.z, zval)
def __init__(self, si=0, **kwargs): # this is the special index: z[si] = phi(z) self.si = si self.t = S('t') n = self.dim self.qtoz = zip(self.q, self._Ohm) self.alltoz = self.qtoz + zip(self.x, self.z) self.ztoq = zip(self.z, self._Psi) self.alltoq = self.ztoq + zip(self.x, self._Psi) self.ztox = zip(self.z, self.x) # dOhm/dz, dPhi/dq, assuming the pieces are already defined self._dOhm = tn.diff(self._Ohm, self.z) self._dPsi = tn.diff(self._Psi, self.q) self.Mz = matmult(self._dOhm.T, self.Mq, self._dOhm) self.Mzi = self._Mzi() self.dMq = tn.diff(self.Mq, self.q) self.dMz = tn.diff(self.Mz, self.z) self.delta = self.Mzi[:, self.si] / self.Mzi[self.si, self.si] # self.ddelta = tn.diff(self.delta, self.z) self.Vz = self.Vq.subs(self.alltoz, simultaneous=True) self.dVz = tn.diff(self.Vz, self.z) self._P = self._makeP(self.k) self._dP = tn.diff(self._P, self.z) self._dPi = np.array(sym.Matrix(self._dP).inv()) self.ztozz = {self.z[i]: self._P[i] for i in range(self.dim)} self.Mzzi = tn.subs(self.Mzi, self.ztozz) self.dMzz = tn.subs(self.dMz, self.ztozz) self.dVzz = tn.subs(self.dVz, self.ztozz) self.dPzz = tn.subs(self._dP, self.ztozz) params = [self.t, self.x, self.u] self._fplus = self._makefp(params) self._fmins = self._makefm(params) self._dfxp = tn.SymExpr(self._fplus.diff(self.x)) self._dfxp.callable(*params) self._dfxm = tn.SymExpr(self._fmins.diff(self.x)) self._dfxm.callable(*params) self._dfup = tn.SymExpr(self._fplus.diff(self.u)) self._dfup.callable(*params) self._dfum = tn.SymExpr(self._fmins.diff(self.u)) self._dfum.callable(*params) self._ohm = tn.lambdify(self.z, self._Ohm) self._psi = tn.lambdify(self.q, self._Psi) # make the jump term generator callable # and a bunch of other stuff as well self.delf = lambda t, x, u: self._delf(t, x, u) self.Ohm = lambda z: tn.eval(self._Ohm, self.z, z) self.dOhm = lambda z: tn.eval(self._dOhm, self.z, z) self.Psi = lambda q: tn.eval(self._Psi, self.q, q) self.dPsi = lambda q: tn.eval(self._dPsi, self.q, q)