def test_dS_reshape(self): im = Immersion(50,50) U = np.zeros(im.mat_shape) dSv = im.calc_dS(U) dSm = np.reshape(dSv, im.mat_shape) for n in xrange(50): npt.assert_allclose(dSm[:,n],im.dS[n].vector().array(), rtol=1e-10, atol=1e-12)
def test_template_target_does_not_change(self): im = Immersion(100,10) qA = 1.0*im.qA.vector().array() qB = 1.0*im.qA.vector().array() U = 0.1*np.ones(im.mat_shape) im.calc_S(U) npt.assert_allclose(qA, im.qA.vector().array(), rtol=1e-10, atol=1e-12, err_msg="qA changes after calculating S!!") npt.assert_allclose(qB, im.qA.vector().array(), rtol=1e-10, atol=1e-12, err_msg="qA changes after calculating S!!")
def test_matrix_to_coeff_conversion(self): im = Immersion(50,50) U = np.random.rand(*im.mat_shape) Uc = im.matrix_to_coeffs(U) U2 = im.coeffs_to_matrix(Uc) for n in xrange(50): npt.assert_allclose(np.sum(U[:,n]), np.sum(Uc[n].vector().array()), rtol=1e-10, atol=1e-12) npt.assert_allclose(U, U2, rtol=1e-10, atol=1e-12)
def test_derivative(self): N = 100 M = 10 im = Immersion(M,N) u = dol.Expression(('cos(x[0])/10.0','cos(x[0])/10.0')) #u = dol.Expression(('0.1','0.1')) u = dol.interpolate(u, im.V) U = np.zeros(im.mat_shape) u_scaler = 0.0 for n in xrange(im.N): U[:,n] = u_scaler * u.vector().array() S = im.calc_S(U) dSarr = np.reshape(im.calc_dS(U),im.mat_shape) vdS = 0 v = dol.Expression(('cos(x[0])/2.0','cos(x[0])/2.0')) #v = dol.Expression(('pow(x[0],2)/3.0','pow(x[0],2)/3.0')) #v = dol.Expression(('x[0]','x[0]')) #v = dol.Expression(('x[0]*0.001','x[0]*0.001')) v = dol.interpolate(v, im.V) s_vdS = 0 s_v = 0 for dS in im.matrix_to_coeffs(dSarr): #print "--->", dS.vector().array(), v.vector().array() #print "--> ", np.sum(dS.vector().array()), np.sum(v.vector().array()) vdS += dol.assemble(dol.inner(v,dS)*dol.dx) #print s_vdS * im.dt vdS *= im.dt lims = [] Ss = [] Sps = [] eps = 10.**(-sp.arange(20)) for ep in eps: im = Immersion(M,N) Up = np.zeros(im.mat_shape) for n in xrange(im.N): Up[:,n] = u_scaler * u.vector().array() + ep*v.vector().array() Sp = im.calc_S(Up) Ss.append(S) Sps.append(Sp) lims.append((Sp - S)/ep) print "%s %15s %12s %12s %15s %15s" % ("Eps","LHS","RHS","S","Sp", "Sp - S") for n in xrange(len(eps)): print "%.0e %15.6f %12.6f %12.7f %12.7f %12.7f" % \ (eps[n], vdS, lims[n], Ss[n], Sps[n], Sps[n] - Ss[n])