def test_even(self): print('\nChecking Tensors with even grid points...') for dim, n, fft_form in itertools.product([2, 3], [4, 5], fft_forms): msg = 'Tensors with: dim={}, n={}, fft_form={}'.format( dim, n, fft_form) N = dim * (n, ) M = tuple(2 * np.array(N)) u = Tensor(name='test', shape=(), N=N, Fourier=False, fft_form=fft_form) u.randomize() Fu = u.fourier(copy=True) FuM = Fu.project(M) uM = FuM.fourier() if n % 2 == 0: self.assertGreaterEqual(u.norm(), FuM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertGreaterEqual(u.norm(), uM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) else: self.assertAlmostEqual(u.norm(), FuM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(u.norm(), uM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(0, u.mean() - FuM.mean(), msg=msg) self.assertAlmostEqual(u.mean(), uM.mean(), msg=msg) # testing that that interpolation on double grid have exactly the same values slc = tuple(u.order * [ slice(None), ] + [slice(0, M[i], 2) for i in range(dim)]) self.assertAlmostEqual(0, np.linalg.norm(u.val - uM.val[slc]), msg=msg) print('...ok')
def test_even(self): print('\nChecking Tensors with even grid points...') for dim, n, fft_form in itertools.product([2,3], [4,5], fft_forms): msg='Tensors with: dim={}, n={}, fft_form={}'.format(dim, n, fft_form) N=dim*(n,) M=tuple(2*np.array(N)) u=Tensor(name='test', shape=(), N=N, Fourier=False, fft_form=fft_form) u.randomize() Fu=u.fourier(copy=True) FuM=Fu.project(M) uM=FuM.fourier() if n%2 == 0: self.assertGreaterEqual(u.norm(), FuM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertGreaterEqual(u.norm(), uM.norm(), msg=msg) self.assertGreaterEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) else: self.assertAlmostEqual(u.norm(), FuM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), FuM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(u.norm(), uM.norm(), msg=msg) self.assertAlmostEqual(u.norm(componentwise=True), uM.norm(componentwise=True), msg=msg) self.assertAlmostEqual(0, u.mean()-FuM.mean(), msg=msg) self.assertAlmostEqual(u.mean(), uM.mean(), msg=msg) # testing that that interpolation on double grid have exactly the same values slc=tuple(u.order*[slice(None),]+[slice(0,M[i],2) for i in range(dim)]) self.assertAlmostEqual(0, np.linalg.norm(u.val-uM.val[slc]), msg=msg) print('...ok')
def test_operators(self): print('\nChecking operators...') for dim, fft_form in itertools.product([2, 3], fft_forms): N = 5 * np.ones(dim, dtype=np.int) F = DFT(N=N, inverse=False, fft_form=fft_form) iF = DFT(N=N, inverse=True, fft_form=fft_form) # Fourier transform prt.disable() print(F) # checking representation prt.enable() u = Tensor(name='u', shape=(), N=N, Fourier=False, fft_form=fft_form).randomize() Fu = F(u) u2 = iF(Fu) self.assertAlmostEqual(0, (u == u2)[1], delta=1e-13, msg='Fourier transform') fft_formsC = copy(fft_forms) fft_formsC.remove(fft_form) for fft_formc in fft_formsC: FuC = Fu.set_fft_form(fft_formc, copy=True) Fu2 = FuC.set_fft_form(fft_form, copy=True) msg = 'Tensor.set_fft_form()' self.assertAlmostEqual(0, Fu.norm() - FuC.norm(), delta=1e-13, msg=msg) self.assertAlmostEqual(0, norm(Fu.mean() - FuC.mean()), delta=1e-13, msg=msg) self.assertAlmostEqual(0, (Fu == Fu2)[1], delta=1e-13, msg=msg) # scalar problem u = Tensor(name='u', shape=(1, ), N=N, Fourier=False, fft_form=fft_form).randomize() u.val -= np.mean(u.val) Fu = F(u) Fu2 = potential(grad(Fu)) self.assertAlmostEqual(0, (Fu == Fu2)[1], delta=1e-13, msg='scalar problem, Fourier=True') u2 = potential(grad(u)) self.assertAlmostEqual(0, (u == u2)[1], delta=1e-13, msg='scalar problem, Fourier=False') hG, hD = grad_div_tensor(N, fft_form=fft_form) self.assertAlmostEqual(0, (hD(hG(Fu)) == div(grad(Fu)))[1], delta=1e-13, msg='scalar problem, Fourier=True') # vectorial problem u = Tensor(name='u', shape=(dim, ), N=N, Fourier=False, fft_form=fft_form) u.randomize() u.add_mean(-u.mean()) Fu = F(u) Fu2 = potential(grad(Fu)) self.assertAlmostEqual(0, (Fu == Fu2)[1], delta=1e-13, msg='vectorial problem, Fourier=True') u2 = potential(grad(u)) self.assertAlmostEqual(0, (u == u2)[1], delta=1e-13, msg='vectorial problem, Fourier=False') # 'vectorial problem - symetric gradient Fu2 = potential(symgrad(Fu), small_strain=True) self.assertAlmostEqual(0, (Fu == Fu2)[1], delta=1e-13, msg='vectorial - sym, Fourier=True') u2 = potential(symgrad(u), small_strain=True) self.assertAlmostEqual(0, (u == u2)[1], delta=1e-13, msg='vectorial - sym, Fourier=False') # matrix version of DFT u = Tensor(name='u', shape=(1, ), N=N, Fourier=False, fft_form='c').randomize() F = DFT(N=N, inverse=False, fft_form='c') Fu = F(u) dft = F.matrix(shape=u.shape) Fu2 = dft.dot(u.val.ravel()) self.assertAlmostEqual(0, norm(Fu.val.ravel() - Fu2), delta=1e-13) print('...ok')
def test_compatibility(self): print('\nChecking compatibility...') for dim, fft_form in itertools.product([3], fft_forms): N = 5 * np.ones(dim, dtype=np.int) F = DFT(inverse=False, N=N, fft_form=fft_form) iF = DFT(inverse=True, N=N, fft_form=fft_form) # scalar problem _, G1l, G2l = scalar(N, Y=np.ones(dim), fft_form=fft_form) P1 = Operator(name='P1', mat=[[iF, G1l, F]]) P2 = Operator(name='P2', mat=[[iF, G2l, F]]) u = Tensor(name='u', shape=(1, ), N=N, Fourier=False, fft_form=fft_form) u.randomize() grad_u = grad(u) self.assertAlmostEqual(0, (P1(grad_u) - grad_u).norm(), delta=1e-13) self.assertAlmostEqual(0, P2(grad_u).norm(), delta=1e-13) e = P1( Tensor(name='u', shape=(dim, ), N=N, Fourier=False, fft_form=fft_form).randomize()) e2 = grad(potential(e)) self.assertAlmostEqual(0, (e - e2).norm(), delta=1e-13) # vectorial problem hG = elasticity_large_deformation(N=N, Y=np.ones(dim), fft_form=fft_form) P1 = Operator(name='P', mat=[[iF, hG, F]]) u = Tensor(name='u', shape=(dim, ), N=N, Fourier=False, fft_form=fft_form) u.randomize() grad_u = grad(u) val = (P1(grad_u) - grad_u).norm() self.assertAlmostEqual(0, val, delta=1e-13) e = Tensor(name='F', shape=(dim, dim), N=N, Fourier=False, fft_form=fft_form) e = P1(e.randomize()) e2 = grad(potential(e)) self.assertAlmostEqual(0, (e - e2).norm(), delta=1e-13) # transpose P1TT = P1.transpose().transpose() self.assertTrue(P1(grad_u) == P1TT(grad_u)) self.assertTrue(hG == (hG.transpose_left().transpose_left())) self.assertTrue(hG == (hG.transpose_right().transpose_right())) # vectorial problem - symetric gradient hG = elasticity_small_strain(N=N, Y=np.ones(dim), fft_form=fft_form) P1 = Operator(name='P', mat=[[iF, hG, F]]) u = Tensor(name='u', shape=(dim, ), N=N, Fourier=False, fft_form=fft_form) u.randomize() grad_u = symgrad(u) val = (P1(grad_u) - grad_u).norm() self.assertAlmostEqual(0, val, delta=1e-13) e = Tensor(name='strain', shape=(dim, dim), N=N, Fourier=False, fft_form=fft_form) e = P1(e.randomize()) e2 = symgrad(potential(e, small_strain=True)) self.assertAlmostEqual(0, (e - e2).norm(), delta=1e-13) # means Fu = F(u) E = np.random.random(u.shape) u.set_mean(E) self.assertAlmostEqual(0, norm(u.mean() - E), delta=1e-13) Fu.set_mean(E) self.assertAlmostEqual(0, norm(Fu.mean() - E), delta=1e-13) # __repr__ prt.disable() print(P1) print(u) prt.enable() self.assertAlmostEqual(0, (P1 == P1.transpose()), delta=1e-13) print('...ok')
= (np.sum(xN.val*xN.val)/np.prod(xN.N))**0.5 = """) print(xN.norm()) print("""FxN.norm() = np.linalg.norm(FxN.val) = = np.sum(FxN.val*np.conj(FxN.val)).real**0.5 =""") print(FxN.norm()) print(""" The trigonometric polynomials can be also multiplied. The standard multiplication with '*' operations corresponds to scalar product leading to a square of norm, i.e. FxN.norm() = xN.norm() = (xN*xN)**0.5 = (FxN*FxN)**0.5 =""") print((xN*xN)**0.5) print((FxN*FxN)**0.5) print(""" The mean value of trigonometric polynomial is calculated independently for each component of vector-field of trigonometric polynomial. In the real space, it can be calculated as a mean of trigonometric polynomial at grid points, while in Fourier space, it corresponds to zero frequency placed in the center of grid, i.e. xN.mean()[0] = xN[0].mean() = xN.val[0].mean() = FxN[0, 2, 2].real =""") print(xN.mean()[0]) print(xN[0].mean()) print(xN.val[0].mean()) print(FxN[0, 2, 2].real) print("""======================== Finally, we will plot the fundamental trigonometric polynomial, which satisfies dirac-delta property at grid points and which
def test_operators(self): print('\nChecking operators...') for dim, fft_form in itertools.product([2, 3], fft_forms): N=5*np.ones(dim, dtype=np.int) F=DFT(N=N, inverse=False, fft_form=fft_form) iF=DFT(N=N, inverse=True, fft_form=fft_form) # Fourier transform prt.disable() print(F) # checking representation prt.enable() u=Tensor(name='u', shape=(), N=N, Fourier=False, fft_form=fft_form).randomize() Fu=F(u) u2=iF(Fu) self.assertAlmostEqual(0, (u==u2)[1], delta=1e-13, msg='Fourier transform') fft_formsC=copy(fft_forms) fft_formsC.remove(fft_form) for fft_formc in fft_formsC: FuC=Fu.set_fft_form(fft_formc, copy=True) Fu2=FuC.set_fft_form(fft_form, copy=True) msg='Tensor.set_fft_form()' self.assertAlmostEqual(0, Fu.norm()-FuC.norm(), delta=1e-13, msg=msg) self.assertAlmostEqual(0, norm(Fu.mean()-FuC.mean()), delta=1e-13, msg=msg) self.assertAlmostEqual(0, (Fu==Fu2)[1], delta=1e-13, msg=msg) # scalar problem u=Tensor(name='u', shape=(1,), N=N, Fourier=False, fft_form=fft_form).randomize() u.val-=np.mean(u.val) Fu=F(u) Fu2=potential(grad(Fu)) self.assertAlmostEqual(0, (Fu==Fu2)[1], delta=1e-13, msg='scalar problem, Fourier=True') u2=potential(grad(u)) self.assertAlmostEqual(0, (u==u2)[1], delta=1e-13, msg='scalar problem, Fourier=False') hG, hD=grad_div_tensor(N, fft_form=fft_form) self.assertAlmostEqual(0, (hD(hG(Fu))==div(grad(Fu)))[1], delta=1e-13, msg='scalar problem, Fourier=True') # vectorial problem u=Tensor(name='u', shape=(dim,), N=N, Fourier=False, fft_form=fft_form) u.randomize() u.add_mean(-u.mean()) Fu=F(u) Fu2=potential(grad(Fu)) self.assertAlmostEqual(0, (Fu==Fu2)[1], delta=1e-13, msg='vectorial problem, Fourier=True') u2=potential(grad(u)) self.assertAlmostEqual(0, (u==u2)[1], delta=1e-13, msg='vectorial problem, Fourier=False') # 'vectorial problem - symetric gradient Fu2=potential(symgrad(Fu), small_strain=True) self.assertAlmostEqual(0, (Fu==Fu2)[1], delta=1e-13, msg='vectorial - sym, Fourier=True') u2=potential(symgrad(u), small_strain=True) self.assertAlmostEqual(0, (u==u2)[1], delta=1e-13, msg='vectorial - sym, Fourier=False') # matrix version of DFT u=Tensor(name='u', shape=(1,), N=N, Fourier=False, fft_form='c').randomize() F=DFT(N=N, inverse=False, fft_form='c') Fu=F(u) dft=F.matrix(shape=u.shape) Fu2=dft.dot(u.val.ravel()) self.assertAlmostEqual(0, norm(Fu.val.ravel()-Fu2), delta=1e-13) print('...ok')
def test_compatibility(self): print('\nChecking compatibility...') for dim, fft_form in itertools.product([3], fft_forms): N=5*np.ones(dim, dtype=np.int) F=DFT(inverse=False, N=N, fft_form=fft_form) iF=DFT(inverse=True, N=N, fft_form=fft_form) # scalar problem _, G1l, G2l=scalar(N, Y=np.ones(dim), fft_form=fft_form) P1=Operator(name='P1', mat=[[iF, G1l, F]]) P2=Operator(name='P2', mat=[[iF, G2l, F]]) u=Tensor(name='u', shape=(1,), N=N, Fourier=False, fft_form=fft_form) u.randomize() grad_u=grad(u) self.assertAlmostEqual(0, (P1(grad_u)-grad_u).norm(), delta=1e-13) self.assertAlmostEqual(0, P2(grad_u).norm(), delta=1e-13) e=P1(Tensor(name='u', shape=(dim,), N=N, Fourier=False, fft_form=fft_form).randomize()) e2=grad(potential(e)) self.assertAlmostEqual(0, (e-e2).norm(), delta=1e-13) # vectorial problem hG=elasticity_large_deformation(N=N, Y=np.ones(dim), fft_form=fft_form) P1=Operator(name='P', mat=[[iF, hG, F]]) u=Tensor(name='u', shape=(dim,), N=N, Fourier=False, fft_form=fft_form) u.randomize() grad_u=grad(u) val=(P1(grad_u)-grad_u).norm() self.assertAlmostEqual(0, val, delta=1e-13) e=Tensor(name='F', shape=(dim, dim), N=N, Fourier=False, fft_form=fft_form) e=P1(e.randomize()) e2=grad(potential(e)) self.assertAlmostEqual(0, (e-e2).norm(), delta=1e-13) # transpose P1TT=P1.transpose().transpose() self.assertTrue(P1(grad_u)==P1TT(grad_u)) self.assertTrue(hG==(hG.transpose_left().transpose_left())) self.assertTrue(hG==(hG.transpose_right().transpose_right())) # vectorial problem - symetric gradient hG=elasticity_small_strain(N=N, Y=np.ones(dim), fft_form=fft_form) P1=Operator(name='P', mat=[[iF, hG, F]]) u=Tensor(name='u', shape=(dim,), N=N, Fourier=False, fft_form=fft_form) u.randomize() grad_u=symgrad(u) val=(P1(grad_u)-grad_u).norm() self.assertAlmostEqual(0, val, delta=1e-13) e=Tensor(name='strain', shape=(dim, dim), N=N, Fourier=False, fft_form=fft_form) e=P1(e.randomize()) e2=symgrad(potential(e, small_strain=True)) self.assertAlmostEqual(0, (e-e2).norm(), delta=1e-13) # means Fu=F(u) E=np.random.random(u.shape) u.set_mean(E) self.assertAlmostEqual(0, norm(u.mean()-E), delta=1e-13) Fu.set_mean(E) self.assertAlmostEqual(0, norm(Fu.mean()-E), delta=1e-13) # __repr__ prt.disable() print(P1) print(u) prt.enable() self.assertAlmostEqual(0, (P1==P1.transpose()), delta=1e-13) print('...ok')
xN.norm() = np.linalg.norm(xN.val)/np.prod(xN.N)**0.5 = = (np.sum(xN.val*xN.val)/np.prod(xN.N))**0.5 = """) print(xN.norm()) print("""FxN.norm() = np.linalg.norm(FxN.val) = = np.sum(FxN.val*np.conj(FxN.val)).real**0.5 =""") print(FxN.norm()) print(""" The trigonometric polynomials can be also multiplied. The standard multiplication with '*' operations corresponds to scalar product leading to a square of norm, i.e. FxN.norm() = xN.norm() = (xN*xN)**0.5 = (FxN*FxN)**0.5 =""") print((xN * xN)**0.5) print((FxN * FxN)**0.5) print(""" The mean value of trigonometric polynomial is calculated independently for each component of vector-field of trigonometric polynomial. In the real space, it can be calculated as a mean of trigonometric polynomial at grid points, while in Fourier space, it corresponds to zero frequency placed in the center of grid, i.e. xN.mean()[0] = xN[0].mean() = xN.val[0].mean() = FxN[0, 2, 2].real =""") print(xN.mean()[0]) print(xN[0].mean()) print(xN.val[0].mean()) print(FxN[0, 2, 2].real) print("""======================== Finally, we will plot the fundamental trigonometric polynomial, which satisfies dirac-delta property at grid points and which plays a major way in a theory of FFT-based homogenization.