def test_DimAndShape2eq(self): dim=self.domain.getDim() u = Symbol('u',(2,), dim=dim) nlpde = NonlinearPDE(self.domain, u, debug=0) args=dict(X=grad(u), Y=5*u[0]) self.assertRaises(IllegalCoefficientValue, nlpde.setValue,**args) args=dict(X=grad(u[0]), Y=5*u) self.assertRaises(IllegalCoefficientValue, nlpde.setValue,**args)
def test_DimAndShape1eq(self): dim=self.domain.getDim() if dim==3: u = Symbol('u', dim=2) else: u = Symbol('u', dim=3) nlpde = NonlinearPDE(self.domain, u, debug=0) args=dict(X=grad(u), Y=5*u) self.assertRaises(IllegalCoefficientValue, nlpde.setValue,**args) u = Symbol('u', dim=dim) args=dict(X=u, Y=5*u) self.assertRaises(IllegalCoefficientValue, nlpde.setValue,**args) args=dict(X=grad(u), Y=5*grad(u)) self.assertRaises(IllegalCoefficientValue, nlpde.setValue,**args)
def test_yDirection(self): dim=self.domain.getDim() if dim==3: return u = Symbol('u',(2,), dim=dim) q = Symbol('q', (2,2)) theta = Symbol('theta') theta=3.141/6 q[0,0]=cos(theta) q[0,1]=-sin(theta) q[1,0]=sin(theta) q[1,1]=cos(theta) sigma = Symbol('sigma',(2,2)) p = NonlinearPDE(self.domain, u, debug=0) epsilon = symmetric(grad(u)) # epsilon = matrixmult(matrixmult(q,epsilon0),q.transpose(1)) c00=10;c01=8;c05=0 c01=8;c11=10;c15=0 c05=0;c15=0;c55=1 sigma[0,0]=c00*epsilon[0,0]+c01*epsilon[1,1]+c05*2*epsilon[1,0] sigma[1,1]=c01*epsilon[0,0]+c11*epsilon[1,1]+c15*2*epsilon[1,0] sigma[0,1]=c05*epsilon[0,0]+c15*epsilon[1,1]+c55*2*epsilon[1,0] sigma[1,0]=sigma[0,1] # sigma0=matrixmult(matrixmult(q.transpose(1),epsilon),q) x = self.domain.getX() gammaD=whereZero(x[1])*[1,1]#+whereZero(x[0])*[1,0]+whereZero(x[0]-1)*[1,0] yconstraint = FunctionOnBoundary(self.domain).getX()[1] p.setValue(X=sigma,q=gammaD,y=[-50,0]*whereZero(yconstraint-1),r=[1,1]) v = p.getSolution(u=[0,0]) x=np.ndarray((2,)) x[0]=0.5 x[1]=0.5 loc=Locator(v.getFunctionSpace(),x) valAtX=loc(v) self.assertTrue(valAtX[0]>10*valAtX[1])
def test_system(self): pde, u_ex, g_ex = self.getPDE(True) g = grad(u_ex) self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex)) u = pde.getSolution() error = Lsup(u-u_ex) self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
def getArguments(self, m): """ """ raise RuntimeError("Please use the setPoint interface") self.__pre_args = grad(m) self.__pre_input = m return (self.__pre_args,)
def getArguments(self, rho): """ Returns precomputed values shared by `getDefect()` and `getGradient()`. :param rho: resistivity :type rho: ``Data`` of shape (2,) :return: Hx, grad(Hx) :rtype: ``tuple`` of ``Data`` """ DIM = self.getDomain().getDim() pde = self.setUpPDE() D = pde.getCoefficient('D') f = self._omega_mu D[0, 1] = -f D[1, 0] = f A = pde.getCoefficient('A') for i in range(DIM): A[0, i, 0, i] = rho A[1, i, 1, i] = rho pde.setValue(A=A, D=D, r=self._r) u = pde.getSolution() return u, escript.grad(u)
def getArguments(self, m): """ """ raise RuntimeError("Please use the setPoint interface") self.__pre_args = escript.grad(m) self.__pre_input = m return self.__pre_args,
def _getAcceleration(self, t, u): """ returns the acceleraton for time `t` and solution `u` at time `t` """ dQ = escript.grad(u[0])[0] dP = escript.grad(u[1])[1:] sigma = self.__mypde.getCoefficient('X') sigma[0, 0] = self.v2_n * dQ sigma[0, 1:] = self.v2_t * dP sigma[1, 0] = self.v2_t * dQ sigma[1, 1:] = self.v2_p * dP self.__mypde.setValue(X=-sigma, y_dirac=self.__r * self.__wavelet.getValue(t)) return self.__mypde.getSolution()
def test_run(self): #test just to confirm nlpde works u=Symbol('u', dim=self.domain.getDim()) nlpde = NonlinearPDE(self.domain, u) x=self.domain.getX() gammaD=whereZero(x[0])+whereZero(x[1]) nlpde.setValue(X=grad(u), Y=5*u, q=gammaD, r=1) v=nlpde.getSolution(u=1)
def test_system(self): pde, u_ex, g_ex = self.getPDE(True) g = grad(u_ex) self.assertLess(Lsup(g_ex - g), self.REL_TOL * Lsup(g_ex)) u = pde.getSolution() error = Lsup(u - u_ex) self.assertLess(error, self.REL_TOL * Lsup(u_ex), "solution error %s is too big." % error)
def calculate_q(k_vector, viscosity, pressure, density, g_vector): """ Calculate flux vector (q). """ q = -(k_vector / viscosity * (es.grad(pressure) - density * g_vector)) return q
def getPotentialNumeric(self): """ Returns 3 list each made up of a number of list containing primary, secondary and total potentials diferences. Each of the lists contain a list for each value of n. """ primCon=self.primaryConductivity coords=self.domain.getX() tol=1e-8 pde=LinearPDE(self.domain, numEquations=1) pde.getSolverOptions().setTolerance(tol) pde.setSymmetryOn() primaryPde=LinearPDE(self.domain, numEquations=1) primaryPde.getSolverOptions().setTolerance(tol) primaryPde.setSymmetryOn() DIM=self.domain.getDim() x=self.domain.getX() q=whereZero(x[DIM-1]-inf(x[DIM-1])) for i in xrange(DIM-1): xi=x[i] q+=whereZero(xi-inf(xi))+whereZero(xi-sup(xi)) A = self.secondaryConductivity * kronecker(self.domain) APrimary = self.primaryConductivity * kronecker(self.domain) pde.setValue(A=A,q=q) primaryPde.setValue(A=APrimary,q=q) delPhiSecondaryList = [] delPhiPrimaryList = [] delPhiTotalList = [] for i in range(1,self.n+1): # 1 to n maxR = self.numElectrodes - 1 - (2*i) #max amount of readings that will fit in the survey delPhiSecondary = [] delPhiPrimary = [] delPhiTotal = [] for j in range(maxR): y_dirac=Scalar(0,DiracDeltaFunctions(self.domain)) y_dirac.setTaggedValue(self.electrodeTags[j],self.current) y_dirac.setTaggedValue(self.electrodeTags[j + ((2*i) + 1)],-self.current) self.sources.append([self.electrodeTags[j], self.electrodeTags[j + ((2*i) + 1)]]) primaryPde.setValue(y_dirac=y_dirac) numericPrimaryPot = primaryPde.getSolution() X=(primCon-self.secondaryConductivity) * grad(numericPrimaryPot) pde.setValue(X=X) u=pde.getSolution() loc=Locator(self.domain,[self.electrodes[j+i],self.electrodes[j+i+1]]) self.samples.append([self.electrodeTags[j+i],self.electrodeTags[j+i+1]]) valPrimary=loc.getValue(numericPrimaryPot) valSecondary=loc.getValue(u) delPhiPrimary.append(valPrimary[1]-valPrimary[0]) delPhiSecondary.append(valSecondary[1]-valSecondary[0]) delPhiTotal.append(delPhiPrimary[j]+delPhiSecondary[j]) delPhiPrimaryList.append(delPhiPrimary) delPhiSecondaryList.append(delPhiSecondary) delPhiTotalList.append(delPhiTotal) self.delPhiPrimaryList=delPhiPrimaryList self.delPhiSecondaryList=delPhiSecondaryList self.delPhiTotalList = delPhiTotalList return [delPhiPrimaryList, delPhiSecondaryList, delPhiTotalList]
def getPotentialNumeric(self): """ Returns 3 list each made up of a number of list containing primary, secondary and total potentials diferences. Each of the lists contain a list for each value of n. """ primCon=self.primaryConductivity coords=self.domain.getX() tol=1e-8 pde=LinearPDE(self.domain, numEquations=1) pde.getSolverOptions().setTolerance(tol) pde.setSymmetryOn() primaryPde=LinearPDE(self.domain, numEquations=1) primaryPde.getSolverOptions().setTolerance(tol) primaryPde.setSymmetryOn() DIM=self.domain.getDim() x=self.domain.getX() q=es.whereZero(x[DIM-1]-es.inf(x[DIM-1])) for i in xrange(DIM-1): xi=x[i] q+=es.whereZero(xi-es.inf(xi))+es.whereZero(xi-es.sup(xi)) A = self.secondaryConductivity * es.kronecker(self.domain) APrimary = self.primaryConductivity * es.kronecker(self.domain) pde.setValue(A=A,q=q) primaryPde.setValue(A=APrimary,q=q) delPhiSecondaryList = [] delPhiPrimaryList = [] delPhiTotalList = [] for i in range(1,self.n+1): # 1 to n maxR = self.numElectrodes - 1 - (2*i) #max amount of readings that will fit in the survey delPhiSecondary = [] delPhiPrimary = [] delPhiTotal = [] for j in range(maxR): y_dirac=es.Scalar(0,es.DiracDeltaFunctions(self.domain)) y_dirac.setTaggedValue(self.electrodeTags[j],self.current) y_dirac.setTaggedValue(self.electrodeTags[j + ((2*i) + 1)],-self.current) self.sources.append([self.electrodeTags[j], self.electrodeTags[j + ((2*i) + 1)]]) primaryPde.setValue(y_dirac=y_dirac) numericPrimaryPot = primaryPde.getSolution() X=(primCon-self.secondaryConductivity) * es.grad(numericPrimaryPot) pde.setValue(X=X) u=pde.getSolution() loc=Locator(self.domain,[self.electrodes[j+i],self.electrodes[j+i+1]]) self.samples.append([self.electrodeTags[j+i],self.electrodeTags[j+i+1]]) valPrimary=loc.getValue(numericPrimaryPot) valSecondary=loc.getValue(u) delPhiPrimary.append(valPrimary[1]-valPrimary[0]) delPhiSecondary.append(valSecondary[1]-valSecondary[0]) delPhiTotal.append(delPhiPrimary[j]+delPhiSecondary[j]) delPhiPrimaryList.append(delPhiPrimary) delPhiSecondaryList.append(delPhiSecondary) delPhiTotalList.append(delPhiTotal) self.delPhiPrimaryList=delPhiPrimaryList self.delPhiSecondaryList=delPhiSecondaryList self.delPhiTotalList = delPhiTotalList return [delPhiPrimaryList, delPhiSecondaryList, delPhiTotalList]
def _getAcceleration(self, t, u): """ returns the acceleraton for time t and solution u at time t """ self.__r.setTaggedValue(self.__source_tag, self.__wavelet.getValue(t)) self.__mypde.setValue( X=-escript.grad(u, escript.Function(self.__mypde.getDomain())), y_dirac=self.__r) return self.__mypde.getSolution()
def setPoint(self, m): """ sets the point which this function will work with :param m: level set function :type m: `Data` """ self.__pre_input = m self.__pre_args = grad(m)
def test_systemcomplex(self): pde, u_ex, g_ex = self.getPDE(True, iscomplex=True) g = grad(u_ex) self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex)) u = pde.getSolution() error = Lsup(u-u_ex) self.assertTrue(u.isComplex()) self.assertEqual(u.getShape(), (pde.getDim(), )) self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
def setPoint(self, m): """ sets the point which this function will work with :param m: level set function :type m: `Data` """ self.__pre_input = m self.__pre_args = escript.grad(m)
def test_single(self): pde, u_ex, g_ex = self.getPDE(False) g=grad(u_ex) self.assertLess(Lsup(g_ex-g), self.REL_TOL*Lsup(g_ex)) u = pde.getSolution() self.assertFalse(u.isComplex()) self.assertEqual(u.getShape(), ( )) error = Lsup(u-u_ex) self.assertLess(error, self.REL_TOL*Lsup(u_ex), "solution error %s is too big."%error)
def G(self,T,alpha): """ tangential operator for taylor galerikin """ g=grad(T) self.__pde.setValue(X=-self.thermal_permabilty*g, \ Y=self.thermal_source-self.__rhocp*inner(self.velocity,g), \ r=(self.__fixed_T-self.temperature)*alpha,\ q=self.location_fixed_temperature) return self.__pde.getSolution()
def G(self, T, alpha): """ tangential operator for taylor galerikin """ g = grad(T) self.__pde.setValue(X=-self.thermal_permabilty*g, \ Y=self.thermal_source-self.__rhocp*inner(self.velocity,g), \ r=(self.__fixed_T-self.temperature)*alpha,\ q=self.location_fixed_temperature) return self.__pde.getSolution()
def getGradient(self, u): """ returns the gradient of a scalar function in direction of the coordinate axis. :rtype: `esys.escript.Vector` """ g = esc.grad(u) if not self.isCartesian(): d = self.getScalingFactors() g *= d return g
def getGradient(self, u): """ returns the gradient of a scalar function in direction of the coordinate axis. :rtype: `esys.escript.Vector """ g=esc.grad(u) if not self.isCartesian(): d=self.getScalingFactors() g*=d return g
def __getGradField(self, proj, mt2d_field, wm): """ DESCRIPTION: ----------- Calculates the complementary fields via Faraday's Law (TE-mode) or via Ampere's Law (TM-mode). Partial derivative w.r.t. the vertical coordinate are taken at the surface for which an Escript 'Projector' object is used to calculate the gradient. ARGUMENTS: ---------- proj :: escript Projection object mt2d_field :: calculated magnetotelluric field wm :: number with actual angular sounding frequency * mu RETURNS: -------- mt2d_grad :: dictionary with computed gradient fields """ # Define the derived gradient fields: if self.mode.upper() == 'TE': # H = -(dE/dz) / iwm grad_real = -proj.getValue(escript.grad(mt2d_field["imag"]) / wm) grad_imag = proj.getValue(escript.grad(mt2d_field["real"]) / wm) #<Note the coupled dependency on real/imaginary part>: else: # E = (dH/dz) / sigma grad_real = proj.getValue( escript.grad(mt2d_field["real"]) / self.sigma) grad_imag = proj.getValue( escript.grad(mt2d_field["imag"]) / self.sigma) #<'sigma' is an Escript data-object and as such the division # will use the tagged sigma values of the associated domains> # And return as dictionary for real and imaginary parts: mt2d_grad = {"real": grad_real[1], "imag": grad_imag[1]} #<Note>: the derivative w.r.t. 'z' is used (i.e. '[1]'). return mt2d_grad
def getGradientAtPoint(self): """ returns the gradient of the cost function J with respect to m. :note: This implementation returns Y_k=dPsi/dm_k and X_kj=dPsi/dm_kj """ # Using cached values m = self.__pre_input grad_m = self.__pre_args mu = self.__mu mu_c = self.__mu_c DIM = self.getDomain().getDim() numLS = self.getNumLevelSets() grad_m = escript.grad(m, escript.Function(m.getDomain())) if self.__w0 is not None: Y = m * self.__w0 * mu else: if numLS == 1: Y = escript.Scalar(0, grad_m.getFunctionSpace()) else: Y = escript.Data(0, (numLS, ), grad_m.getFunctionSpace()) if self.__w1 is not None: if numLS == 1: X = grad_m * self.__w1 * mu else: X = grad_m * self.__w1 for k in range(numLS): X[k, :] *= mu[k] else: X = escript.Data(0, grad_m.getShape(), grad_m.getFunctionSpace()) # cross gradient terms: if numLS > 1: for k in range(numLS): grad_m_k = grad_m[k, :] l2_grad_m_k = escript.length(grad_m_k)**2 for l in range(k): grad_m_l = grad_m[l, :] l2_grad_m_l = escript.length(grad_m_l)**2 grad_m_lk = inner(grad_m_l, grad_m_k) f = mu_c[l, k] * self.__wc[l, k] X[l, :] += f * (l2_grad_m_k * grad_m_l - grad_m_lk * grad_m_k) X[k, :] += f * (l2_grad_m_l * grad_m_k - grad_m_lk * grad_m_l) return pdetools.ArithmeticTuple(Y, X)
def getGradientAtPoint(self): """ returns the gradient of the cost function J with respect to m. :note: This implementation returns Y_k=dPsi/dm_k and X_kj=dPsi/dm_kj """ # Using cached values m = self.__pre_input grad_m = self.__pre_args mu = self.__mu mu_c = self.__mu_c DIM = self.getDomain().getDim() numLS = self.getNumLevelSets() grad_m = grad(m, Function(m.getDomain())) if self.__w0 is not None: Y = m * self.__w0 * mu else: if numLS == 1: Y = Scalar(0, grad_m.getFunctionSpace()) else: Y = Data(0, (numLS,), grad_m.getFunctionSpace()) if self.__w1 is not None: if numLS == 1: X = grad_m * self.__w1 * mu else: X = grad_m * self.__w1 for k in range(numLS): X[k, :] *= mu[k] else: X = Data(0, grad_m.getShape(), grad_m.getFunctionSpace()) # cross gradient terms: if numLS > 1: for k in range(numLS): grad_m_k = grad_m[k, :] l2_grad_m_k = length(grad_m_k) ** 2 for l in range(k): grad_m_l = grad_m[l, :] l2_grad_m_l = length(grad_m_l) ** 2 grad_m_lk = inner(grad_m_l, grad_m_k) f = mu_c[l, k] * self.__wc[l, k] X[l, :] += f * (l2_grad_m_k * grad_m_l - grad_m_lk * grad_m_k) X[k, :] += f * (l2_grad_m_l * grad_m_k - grad_m_lk * grad_m_l) return ArithmeticTuple(Y, X)
def test_setVals2eq(self): #test setting Coefficients with 2 coeficients dim = self.domain.getDim() u = Symbol('u', (2, ), dim=dim) q = Symbol('q', (2, 2)) nlpde = NonlinearPDE(self.domain, u, debug=0) x = self.domain.getX() gammaD = whereZero( x[1]) * [1, 1] #+whereZero(x[0])*[1,0]+whereZero(x[0]-1)*[1,0] yconstraint = FunctionOnBoundary(self.domain).getX()[1] nlpde.setValue(X=grad(u), q=gammaD, Y=[-50, -50] * u) A = nlpde.getCoefficient("A") B = nlpde.getCoefficient("B") C = nlpde.getCoefficient("C") D = nlpde.getCoefficient("D") if dim == 2: ATest = numpy.empty((2, 2, 2, 2), dtype=object) ATest[0] = (((1, 0), (0, 0)), ((0, 1), (0, 0))) ATest[1] = (((0, 0), (1, 0)), ((0, 0), (0, 1))) BTest = numpy.empty((2, 2, 2), dtype=object) BTest[0] = 0 BTest[1] = 0 CTest = BTest DTest = numpy.empty((2, 2), dtype=object) DTest[0] = (-50, 0) DTest[1] = (0, -50) self.assertTrue(numpy.ndarray.__eq__(ATest, A).all()) self.assertTrue(numpy.ndarray.__eq__(BTest, B).all()) self.assertTrue(numpy.ndarray.__eq__(CTest, C).all()) self.assertTrue(numpy.ndarray.__eq__(DTest, D).all()) else: ATest = numpy.empty((2, 3, 2, 3), dtype=object) ATest[0] = (((1, 0, 0), (0, 0, 0)), ((0, 1, 0), (0, 0, 0)), ((0, 0, 1), (0, 0, 0))) ATest[1] = (((0, 0, 0), (1, 0, 0)), ((0, 0, 0), (0, 1, 0)), ((0, 0, 0), (0, 0, 1))) #ATest[1]=(((0,0,1),(1,0,0)),((0,0),(0,1))) BTest = numpy.empty((2, 3, 2), dtype=object) BTest[0] = 0 BTest[1] = 0 CTest = numpy.empty((2, 2, 3), dtype=object) CTest[0] = 0 CTest[1] = 0 DTest = numpy.empty((2, 2), dtype=object) DTest[0] = (-50, 0) DTest[1] = (0, -50) self.assertTrue(numpy.ndarray.__eq__(BTest, B).all()) self.assertTrue(numpy.ndarray.__eq__(CTest, C).all()) self.assertTrue(numpy.ndarray.__eq__(DTest, D).all())
def getDualProduct(self, m, r): """ returns the dual product of a gradient represented by X=r[1] and Y=r[0] with a level set function m: *Y_i*m_i + X_ij*m_{i,j}* :type m: `Data` :type r: `ArithmeticTuple` :rtype: ``float`` """ A = 0 if not r[0].isEmpty(): A += integrate(inner(r[0], m)) if not r[1].isEmpty(): A += integrate(inner(r[1], grad(m))) return A
def getDualProduct(self, m, r): """ returns the dual product of a gradient represented by X=r[1] and Y=r[0] with a level set function m: *Y_i*m_i + X_ij*m_{i,j}* :type m: `Data` :type r: `ArithmeticTuple` :rtype: ``float`` """ A=0 if not r[0].isEmpty(): A+=integrate(inner(r[0], m)) if not r[1].isEmpty(): A+=integrate(inner(r[1], grad(m))) return A
def _getAcceleration(self, t, u): """ returns the acceleraton for time `t` and solution `u` at time `t` """ du = escript.grad(u) if self.fastAssembler: self.__mypde.setValue(du=du, y_dirac=self.__r * self.__wavelet.getValue(t)) else: sigma = self.__mypde.getCoefficient('X') if self.__mypde.getDim() == 3: e11 = du[0, 0] e22 = du[1, 1] e33 = du[2, 2] sigma[0, 0] = self.c11 * e11 + self.c13 * (e22 + e33) sigma[1, 1] = self.c13 * e11 + self.c33 * e22 + self.c23 * e33 sigma[2, 2] = self.c13 * e11 + self.c23 * e22 + self.c33 * e33 s = self.c44 * (du[2, 1] + du[1, 2]) sigma[1, 2] = s sigma[2, 1] = s s = self.c66 * (du[2, 0] + du[0, 2]) sigma[0, 2] = s sigma[2, 0] = s s = self.c66 * (du[0, 1] + du[1, 0]) sigma[0, 1] = s sigma[1, 0] = s else: e11 = du[0, 0] e22 = du[1, 1] sigma[0, 0] = self.c11 * e11 + self.c13 * e22 sigma[1, 1] = self.c13 * e11 + self.c33 * e22 s = self.c66 * (du[1, 0] + du[0, 1]) sigma[0, 1] = s sigma[1, 0] = s self.__mypde.setValue(X=-sigma, y_dirac=self.__r * self.__wavelet.getValue(t)) return self.__mypde.getSolution()
def test_setVals2eq(self): #test setting Coefficients with 2 coeficients dim=self.domain.getDim() u = Symbol('u',(2,), dim=dim) q = Symbol('q', (2,2)) nlpde = NonlinearPDE(self.domain, u, debug=0) x = self.domain.getX() gammaD=whereZero(x[1])*[1,1]#+whereZero(x[0])*[1,0]+whereZero(x[0]-1)*[1,0] yconstraint = FunctionOnBoundary(self.domain).getX()[1] nlpde.setValue(X=grad(u),q=gammaD,Y=[-50,-50]*u) A=nlpde.getCoefficient("A") B=nlpde.getCoefficient("B") C=nlpde.getCoefficient("C") D=nlpde.getCoefficient("D") if dim==2: ATest=numpy.empty((2,2,2,2),dtype=object) ATest[0]=(((1,0),(0,0)),((0,1),(0,0))) ATest[1]=(((0,0),(1,0)),((0,0),(0,1))) BTest=numpy.empty((2,2,2),dtype=object) BTest[0]=0 BTest[1]=0 CTest=BTest DTest=numpy.empty((2,2),dtype=object) DTest[0]=(-50,0) DTest[1]=(0,-50) self.assertTrue(numpy.ndarray.__eq__(ATest, A).all()) self.assertTrue(numpy.ndarray.__eq__(BTest, B).all()) self.assertTrue(numpy.ndarray.__eq__(CTest, C).all()) self.assertTrue(numpy.ndarray.__eq__(DTest, D).all()) else: ATest=numpy.empty((2,3,2,3),dtype=object) ATest[0]=(((1,0,0),(0,0,0)),((0,1,0),(0,0,0)),((0,0,1),(0,0,0))) ATest[1]=(((0,0,0),(1,0,0)),((0,0,0),(0,1,0)),((0,0,0),(0,0,1))) #ATest[1]=(((0,0,1),(1,0,0)),((0,0),(0,1))) BTest=numpy.empty((2,3,2),dtype=object) BTest[0]=0 BTest[1]=0 CTest=numpy.empty((2,2,3),dtype=object) CTest[0]=0 CTest[1]=0 DTest=numpy.empty((2,2),dtype=object) DTest[0]=(-50,0) DTest[1]=(0,-50) self.assertTrue(numpy.ndarray.__eq__(BTest, B).all()) self.assertTrue(numpy.ndarray.__eq__(CTest, C).all()) self.assertTrue(numpy.ndarray.__eq__(DTest, D).all())
def _getAcceleration(self, t, u): """ returns the acceleraton for time `t` and solution `u` at time `t` """ du = escript.grad(u) sigma = self.__mypde.getCoefficient('X') e_xx = du[0, 0] e_zz = du[1, 1] e_xz = du[0, 1] + du[1, 0] sigma[0, 0] = self.c11 * e_xx + self.c13 * e_zz + self.c16 * e_xz sigma[1, 1] = self.c13 * e_xx + self.c33 * e_zz + self.c36 * e_xz sigma_xz = self.c16 * e_xx + self.c36 * e_zz + self.c66 * e_xz sigma[0, 1] = sigma_xz sigma[1, 0] = sigma_xz self.__mypde.setValue(X=-sigma, y_dirac=self.__r * self.__wavelet.getValue(t)) return self.__mypde.getSolution()
def test_setVals1eq(self): #test setting Coefficients with 1 equation dim=self.domain.getDim() u=Symbol('u', dim=dim) nlpde = NonlinearPDE(self.domain, u) x=self.domain.getX() gammaD=whereZero(x[0])+whereZero(x[1]) nlpde.setValue(X=grad(u), Y=5*u, q=gammaD, r=1) A=nlpde.getCoefficient("A") B=nlpde.getCoefficient("B") C=nlpde.getCoefficient("C") D=nlpde.getCoefficient("D") if dim==2: ATest=numpy.empty((2,2), dtype=object) ATest[0]=1,0 ATest[1]=0,1 BTest=numpy.empty((2,), dtype=object) BTest[0]=0 BTest[1]=0 CTest=BTest self.assertTrue(A-ATest==Symbol(numpy.zeros((2,2)))) self.assertTrue(B-BTest==Symbol(numpy.zeros((2,)))) self.assertTrue(C-CTest==Symbol(numpy.zeros((2,)))) temp=Symbol('temp') self.assertTrue(D-temp.subs(temp,5)==temp.subs(temp,0)) else: ATest=numpy.empty((3,3), dtype=object) ATest[0]=1,0,0 ATest[1]=0,1,0 ATest[2]=0,0,1 BTest=numpy.empty((3,), dtype=object) BTest[0]=0 BTest[1]=0 BTest[2]=0 CTest=BTest self.assertTrue(A-ATest==Symbol(numpy.zeros((3,3)))) self.assertTrue(B-BTest==Symbol(numpy.zeros((3,)))) self.assertTrue(C-CTest==Symbol(numpy.zeros((3,)))) temp=Symbol('temp') self.assertTrue(D-temp.subs(temp,5)==temp.subs(temp,0))
def getArguments(self, sigma): """ Returns precomputed values shared by `getDefect()` and `getGradient()`. :param sigma: conductivity :type sigma: ``Data`` of shape (2,) :return: E_x, E_z :rtype: ``Data`` of shape (2,) """ DIM = self.getDomain().getDim() pde = self.setUpPDE() D = pde.getCoefficient('D') f = self._omega_mu * sigma D[0, 1] = -f D[1, 0] = f A = pde.getCoefficient('A') A[0, :, 0, :] = escript.kronecker(DIM) A[1, :, 1, :] = escript.kronecker(DIM) pde.setValue(A=A, D=D, r=self._r) u = pde.getSolution() return u, escript.grad(u)[:, 1]
def getArguments(self, m): """ """ return grad(m),
def getGradient(self, rho, Hx, g_Hx): """ Returns the gradient of the defect with respect to resistivity. :param rho: a suggestion for resistivity :type rho: ``Data`` of shape () :param Hx: magnetic field :type Hx: ``Data`` of shape (2,) :param g_Hx: gradient of magnetic field :type g_Hx: ``Data`` of shape (2,2) """ pde = self.setUpPDE() DIM = self.getDomain().getDim() x = g_Hx.getFunctionSpace().getX() Hx = escript.interpolate(Hx, x.getFunctionSpace()) u0 = Hx[0] u1 = Hx[1] u00 = g_Hx[0, 0] u10 = g_Hx[1, 0] u01 = g_Hx[0, 1] u11 = g_Hx[1, 1] A = pde.getCoefficient('A') D = pde.getCoefficient('D') Y = pde.getCoefficient('Y') X = pde.getCoefficient('X') for i in range(DIM): A[0, i, 0, i] = rho A[1, i, 1, i] = rho f = self._omega_mu D[0, 1] = f D[1, 0] = -f Z = self._Z scale = 1. / (u0**2 + u1**2) scale2 = scale**2 scale *= self._weight scale2 *= rho * self._weight rho_scale = rho * scale gscale = u01**2 + u11**2 Y[0] = scale2 * ((Z[0] * u01 + Z[1] * u11) * (u0**2 - u1**2) + 2 * u0 * u1 * (Z[0] * u11 - Z[1] * u01) - rho * u0 * gscale) Y[1] = scale2 * ((Z[0] * u11 - Z[1] * u01) * (u1**2 - u0**2) + 2 * u0 * u1 * (Z[0] * u01 + Z[1] * u11) - rho * u1 * gscale) X[0, 1] = rho_scale * (-Z[0] * u0 + Z[1] * u1 + rho * u01) X[1, 1] = rho_scale * (-Z[0] * u1 - Z[1] * u0 + rho * u11) pde.setValue(A=A, D=D, X=X, Y=Y) g = escript.grad(pde.getSolution()) Hstarr_x = g[0, 0] Hstari_x = g[1, 0] Hstarr_z = g[0, 1] Hstari_z = g[1, 1] return -scale*(u0*(Z[0]*u01+Z[1]*u11)+u1*(Z[0]*u11-Z[1]*u01)-rho*gscale)\ - Hstarr_x*u00 - Hstarr_z*u01 - Hstari_x*u10 - Hstari_z*u11