def test04c_Grad_many_pt(self): print('========== TEST 4c ==========') N, M = 2, 2 # create random vectors r = np.random.rand(M, 3) m = np.random.rand(N, 3) dp_pos = np.random.rand(N, 3) s = np.random.rand(3) n = np.random.rand(3) # use the code for several dipoles but single point G1 = np.array([f.gradient_single_pt(ri, dp_pos, m, s, n, verbose=True) for ri in r]) # use the code for several points but single dipole G2 = np.array([f.gradient_single_dipole(r, dp_posi, mi, s, n, verbose=True) for mi, dp_posi in zip(m, dp_pos)]) err = np.mean(np.abs(G1 - np.sum(G2, 0))) if self.verbose: print(('shapes', np.shape(G1), np.shape(G2))) print('err') print(err) print('G1') print(G1) print('G2') print((np.sum(G2, 0))) if err > 1e-6: raise ValueError
def test04b_Grad_many_pt(self): print('========== TEST 4b ==========') N, M = 2, 4 # create random vectors r = np.random.rand(M, 3) m = np.random.rand(3) dp_pos = np.random.rand(3) s = np.random.rand(3) n = np.random.rand(3) if self.verbose: print(('r', r)) print(('m', m)) print(('dp_pos', dp_pos)) G_simple = np.array( [f.gradient_single_pt(ri, dp_pos, m, s, n) for ri in r]) G = f.gradient(r, dp_pos, m, s, n) if self.verbose: print(('G', np.array(G['G']))) print(('G_simple', G_simple)) print(('G diff', G_simple - np.array(G['G']))) err = np.mean(np.abs(G_simple - np.array(G['G']))) if err > 1e-6: raise ValueError
def test04b_Grad_many_pt(self): print('========== TEST 4b ==========') N, M= 2, 4 # create random vectors r = np.random.rand(M, 3) m = np.random.rand(3) dp_pos = np.random.rand(3) s = np.random.rand(3) n = np.random.rand(3) if self.verbose: print(('r', r)) print(('m', m)) print(('dp_pos', dp_pos)) G_simple = np.array([f.gradient_single_pt(ri, dp_pos, m, s, n) for ri in r]) G = f.gradient(r, dp_pos, m, s, n) if self.verbose: print(('G', np.array(G['G']))) print(('G_simple', G_simple)) print(('G diff', G_simple - np.array(G['G']))) err = np.mean(np.abs(G_simple - np.array(G['G']))) if err > 1e-6: raise ValueError
def test03b_Grad_single_pt(self): """ test the function b_field_single_pt with random values :return: """ N = 2 # create random vectors r = np.random.rand(3) m = np.random.rand(N, 3) dp_pos = np.random.rand(N, 3) s = np.random.rand(3) n = np.random.rand(3) # r = np.array([ 0.61272976, 0.93453872, 0.13334545]) # m = np.array([ 0.53571495, 0.45998269, 0.65775688]) # dp_pos =np.array([ 0.01321936, 0.6526304, 0.50160241]) if self.verbose: print(('r', r)) print(('m', m)) print(('dp_pos', dp_pos)) print(('s', s)) print(('n', n)) # calculate for each dipole with the simple formula G_simple = np.array([self.my_grad_simple(r, p, mi, s, n) for mi, p in zip(m, dp_pos)]) # now sum up to get total field gradient G_simple = np.sum(G_simple, 0) G = f.gradient_single_pt(r, dp_pos, m, s, n, verbose=True) err = np.mean(np.abs(G_simple - G)) if self.verbose: print(('err', err)) print(('G_simple', G_simple)) print(('G', G)) if err > 1e-8: raise ValueError
def test03b_Grad_single_pt(self): """ test the function b_field_single_pt with random values :return: """ N = 2 # create random vectors r = np.random.rand(3) m = np.random.rand(N, 3) dp_pos = np.random.rand(N, 3) s = np.random.rand(3) n = np.random.rand(3) # r = np.array([ 0.61272976, 0.93453872, 0.13334545]) # m = np.array([ 0.53571495, 0.45998269, 0.65775688]) # dp_pos =np.array([ 0.01321936, 0.6526304, 0.50160241]) if self.verbose: print(('r', r)) print(('m', m)) print(('dp_pos', dp_pos)) print(('s', s)) print(('n', n)) # calculate for each dipole with the simple formula G_simple = np.array( [self.my_grad_simple(r, p, mi, s, n) for mi, p in zip(m, dp_pos)]) # now sum up to get total field gradient G_simple = np.sum(G_simple, 0) G = f.gradient_single_pt(r, dp_pos, m, s, n, verbose=True) err = np.mean(np.abs(G_simple - G)) if self.verbose: print(('err', err)) print(('G_simple', G_simple)) print(('G', G)) if err > 1e-8: raise ValueError
def test03a_Grad_single_pt(self): """ test the function b_field_single_pt repeating the same calculatation :return: """ # create random vectors r = np.random.rand(3) m = np.random.rand(3) dp_pos = np.random.rand(3) s = np.random.rand(3) n = np.random.rand(3) if self.verbose: print(('r', r)) print(('m', m)) print(('dp_pos', dp_pos)) print(('s', s)) print(('n', n)) # calculate for each dipole with the simple formula G_simple = self.my_grad_simple(r, dp_pos, m, s, n) # now sum up to get total field gradient # calculate the gradient at the same position twice but using the vector code G = f.gradient_single_pt(r, np.array([dp_pos, dp_pos]), np.array([m, m]), s, n, verbose=True) err = np.mean(np.abs(G_simple - G / 2)) if self.verbose: print(('err', err)) print(('G_simple', G_simple)) if err > 1e-8: raise ValueError
def test03a_Grad_single_pt(self): """ test the function b_field_single_pt repeating the same calculatation :return: """ # create random vectors r = np.random.rand(3) m = np.random.rand(3) dp_pos = np.random.rand(3) s = np.random.rand(3) n = np.random.rand(3) if self.verbose: print(('r', r)) print(('m', m)) print(('dp_pos', dp_pos)) print(('s', s)) print(('n', n)) # calculate for each dipole with the simple formula G_simple = self.my_grad_simple(r, dp_pos, m, s, n) # now sum up to get total field gradient # calculate the gradient at the same position twice but using the vector code G = f.gradient_single_pt(r, np.array([dp_pos, dp_pos]), np.array([m, m]), s, n, verbose=True) err = np.mean(np.abs(G_simple - G/2)) if self.verbose: print(('err', err)) print(('G_simple', G_simple)) if err > 1e-8: raise ValueError
def test04c_Grad_many_pt(self): print('========== TEST 4c ==========') N, M = 2, 2 # create random vectors r = np.random.rand(M, 3) m = np.random.rand(N, 3) dp_pos = np.random.rand(N, 3) s = np.random.rand(3) n = np.random.rand(3) # use the code for several dipoles but single point G1 = np.array([ f.gradient_single_pt(ri, dp_pos, m, s, n, verbose=True) for ri in r ]) # use the code for several points but single dipole G2 = np.array([ f.gradient_single_dipole(r, dp_posi, mi, s, n, verbose=True) for mi, dp_posi in zip(m, dp_pos) ]) err = np.mean(np.abs(G1 - np.sum(G2, 0))) if self.verbose: print(('shapes', np.shape(G1), np.shape(G2))) print('err') print(err) print('G1') print(G1) print('G2') print((np.sum(G2, 0))) if err > 1e-6: raise ValueError