def test_solve_qps(): for i0 in xrange(100): with numpy_seed(i0): qps = QPSolver(*get_random_problem()) for i1 in xrange(10): if i1 == 0: free = np.ones(qps.nx, dtype=bool) else: with numpy_seed(i0*100 + i1): free = get_random_free(qps) x = qps.solve(free) qps.check_feasible(x, free)
def test_esp_cost_solve(): for irep in xrange(nrep): with numpy_seed(irep): A = np.random.uniform(-1, 1, (11, 11)) B = np.random.uniform(-1, 1, 11) C = np.random.uniform(-1, 1, ()) A = np.dot(A, A.T) cost = ESPCost(A, B, C, 10) # test without constraint x = cost.solve() assert abs(cost.gradient(x)).max() < 1e-8 # test with constraint 0.0 x = cost.solve(qtot=0.0) charges = x[:10] assert abs(charges.sum()) < 1e-8 gradient = cost.gradient(x) assert abs(gradient[:10] - gradient[:10].mean()).max() < 1e-9 assert abs(gradient[10:]).max() < 1e-9 # test with constraint 1.0 x = cost.solve(qtot=1.0) charges = x[:10] assert abs(charges.sum()-1) < 1e-9 gradient = cost.gradient(x) assert abs(gradient[:10] - gradient[:10].mean()).max() < 1e-9 assert abs(gradient[10:]).max() < 1e-9
def translate_random_rvecs(coordinates, rvecs, seed): """Translate nuclei by a random integer linear combination of cell vectors.""" result = coordinates.copy() with numpy_seed(seed): for j in xrange(len(coordinates)): result[j] += np.dot(np.random.randint(-3, 4, 3), rvecs) return result
def test_esp_cost_solve_regularized(): for irep in xrange(nrep): with numpy_seed(irep): A = np.random.uniform(-1, 1, (11, 11)) B = np.random.uniform(-1, 1, 11) C = np.random.uniform(-1, 1, ()) A = np.dot(A, A.T) cost = ESPCost(A, B, C, 10) # test without constraint ridge = 1e-6 x = cost.solve(ridge=ridge) l = ridge*np.diag(cost._A)[:cost.natom].mean() ls = np.ones(cost._B.shape)*l ls[-1] = 0 assert abs(cost.gradient(x) + 2*ls*x).max() < 1e-9 # test with constraint 0.0 x = cost.solve(qtot=0.0, ridge=ridge) charges = x[:10] assert abs(charges.sum()) < 1e-9 gradient = cost.gradient(x) + 2*ls*x assert abs(gradient[:10] - gradient[:10].mean()).max() < 1e-10 assert abs(gradient[10:]).max() < 1e-10 # test with constraint 1.0 x = cost.solve(qtot=1.0, ridge=ridge) charges = x[:10] assert abs(charges.sum()-1) < 1e-9 gradient = cost.gradient(x) + 2*ls*x assert abs(gradient[:10] - gradient[:10].mean()).max() < 1e-10 assert abs(gradient[10:]).max() < 1e-10
def test_value_charges2(): for irep in xrange(nrep): cost = get_random_esp_cost_cube3d(irep) for ix in xrange(nrep): with numpy_seed(irep*10 + ix): x = np.random.normal(0, 1, len(cost._A)) x -= x.sum() assert cost.value(x) >= cost.value_charges(x[:-1])
def test_orbitals_from_fock(): with numpy_seed(1): a = np.random.normal(0, 1, (5, 5)) fock = a+a.T a = np.random.normal(0, 1, (5, 5)) olp = np.dot(a, a.T) orb = Orbitals(5) orb.from_fock(fock, olp) assert orb.error_eigen(fock, olp) < 1e-5
def get_random_esp_cost_cube0d_args(seed=1): """Generate random cube data for an aperiodic system.""" shape = np.array([5, 5, 5]) pbc = np.array([0, 0, 0]) with numpy_seed(seed): coordinates = np.random.normal(0, 1, (5, 3)) origin = np.random.uniform(-3, 3, 3) grid_rvecs = np.diag(np.random.uniform(2.0, 3.0, 3)) grid_rvecs += np.random.uniform(-0.1, 0.1, (3, 3)) vref = np.random.normal(0, 1, shape) weights = np.random.uniform(0, 1, shape) return coordinates, origin, grid_rvecs, shape, pbc, vref, weights
def test_orbitals_error_eigen(): with numpy_seed(1): orb = Orbitals(5) a = np.random.normal(0, 1, (5, 5)) fock = a+a.T evals, evecs = np.linalg.eigh(fock) orb.coeffs[:] = evecs orb.energies[:] = evals olp = np.identity(5) assert orb.error_eigen(fock, olp) < 1e-10 orb.coeffs[:] += np.random.normal(0, 1e-3, (5, 5)) assert orb.error_eigen(fock, olp) > 1e-10
def test_esp_cost_cube3d_gradient(): for irep in xrange(nrep): # Some parameters coordinates, origin, grid_rvecs, shape, pbc, vref, weights = \ get_random_esp_cost_cube3d_args(irep) grid = UniformGrid(origin, grid_rvecs, shape, pbc) cost = ESPCost.from_grid_data(coordinates, grid, vref, weights) with numpy_seed(irep): x0 = np.random.uniform(-0.5, 0.5, len(coordinates)+1) dxs = np.random.uniform(-1e-5, 1e-5, (100, len(coordinates)+1)) check_delta(cost.value, cost.gradient, x0, dxs)
def test_esp_cost_cube3d_invariance_rcut(): for irep in xrange(3): # Repeating this only 3 times because it is so slow. # Some parameters coordinates, origin, grid_rvecs, shape, pbc, vref, weights = \ get_random_esp_cost_cube3d_args(irep) grid = UniformGrid(origin, grid_rvecs, shape, pbc) # Generate costs with displaced origin costs = [] for icut in xrange(nrep): with numpy_seed(irep*10 + icut): rcut = np.random.uniform(10, 30) alpha = 4.5/rcut gcut = 1.5*alpha cost = ESPCost.from_grid_data(coordinates, grid, vref, weights, rcut=rcut, alpha=alpha, gcut=gcut) costs.append(cost) # Compare the cost functions check_costs(costs, eps1=1e-8)
def test_tridiagsym_solve(): N = 10 A = np.zeros((N, N), float) for irep in xrange(100): with numpy_seed(irep): # Make random numbers reproducible # randomize the diagonal A.ravel()[::N + 1] = np.random.uniform(1, 2, N) # randomize the upper diagonal A.ravel()[1::N + 1] = np.random.uniform(-1, 0, N - 1) # clone the lower diagonal A.ravel()[N::N + 1] = A.ravel()[1::N + 1] # test the inverse for all possible basis vectors Ainv = np.linalg.inv(A) for i in xrange(N): right = np.zeros(N, float) right[i] = 1.0 solution = np.zeros(N, float) tridiagsym_solve(A.ravel()[::N + 1].copy(), A.ravel()[N::N + 1].copy(), right, solution) error = abs(solution - Ainv[:, i]).max() assert (error < 1e-9)
def test_eval_spline_grid_add_random(): npoint = 10 cs = get_cosine_spline() for irep in xrange(10): with numpy_seed(irep): cell = get_random_cell(1.0, irep % 4) points = np.random.normal(-2, 3, (npoint, 3)) g = IntGrid(points, np.random.normal(0, 1.0, npoint)) center1 = np.random.uniform(-2, 2, 3) center2 = np.random.uniform(-2, 2, 3) output1 = np.zeros(npoint) g.eval_spline(cs, center1, output1, cell) output2 = np.zeros(npoint) g.eval_spline(cs, center2, output2, cell) output3 = np.zeros(npoint) g.eval_spline(cs, center1, output3, cell) g.eval_spline(cs, center2, output3, cell) assert abs(output1 + output2 - output3).max() < 1e-10
def test_eval_spline_grid_add_random(): npoint = 10 cs = get_cosine_spline() for irep in xrange(10): with numpy_seed(irep): cell = get_random_cell(1.0, irep % 4) points = np.random.normal(-2, 3, (npoint,3)) g = IntGrid(points, np.random.normal(0, 1.0, npoint)) center1 = np.random.uniform(-2, 2, 3) center2 = np.random.uniform(-2, 2, 3) output1 = np.zeros(npoint) g.eval_spline(cs, center1, output1, cell) output2 = np.zeros(npoint) g.eval_spline(cs, center2, output2, cell) output3 = np.zeros(npoint) g.eval_spline(cs, center1, output3, cell) g.eval_spline(cs, center2, output3, cell) assert abs(output1 + output2 - output3).max() < 1e-10
def test_consistent(): for irep in xrange(nrep): with numpy_seed(irep): # random system natom = 5 coordinates = np.random.uniform(0, 10, (natom, 3)) charges = np.random.normal(0, 1, natom) charges -= charges.mean() # random grid origin = np.random.uniform(-3, 3, 3) size = 10 grid_rvecs = np.diag(np.random.uniform(8.0, 10.0, 3)) / size grid_rvecs += np.random.uniform(-1.0, 1.0, (3, 3)) / size shape = np.array([size, size, size]) pbc = np.array([1, 1, 1]) ugrid = UniformGrid(origin, grid_rvecs, shape, pbc) # compute the ESP rcut = 20.0 alpha = 3.0 / 10.0 gcut = 1.1 * alpha esp = np.zeros(shape) compute_esp_grid_cube(ugrid, esp, coordinates, charges, rcut, alpha, gcut) # Set up weights weights = np.ones(shape) for i in xrange(natom): multiply_near_mask(coordinates[i], ugrid, 1.0, 0.5, weights) # Fit the charges and test cost = ESPCost.from_grid_data(coordinates, ugrid, esp, weights) x = cost.solve() assert cost.value_charges(charges) < 1e-7 assert cost.value(x) < 1e-7 assert abs(charges - x[:-1]).max() < 1e-4
def test_consistent(): for irep in xrange(nrep): with numpy_seed(irep): # random system natom = 5 coordinates = np.random.uniform(0, 10, (natom, 3)) charges = np.random.normal(0, 1, natom) charges -= charges.mean() # random grid origin = np.random.uniform(-3, 3, 3) size = 10 grid_rvecs = np.diag(np.random.uniform(8.0, 10.0, 3))/size grid_rvecs += np.random.uniform(-1.0, 1.0, (3, 3))/size shape = np.array([size, size, size]) pbc = np.array([1, 1, 1]) ugrid = UniformGrid(origin, grid_rvecs, shape, pbc) # compute the ESP rcut = 20.0 alpha = 3.0/10.0 gcut = 1.1*alpha esp = np.zeros(shape) compute_esp_grid_cube(ugrid, esp, coordinates, charges, rcut, alpha, gcut) # Set up weights weights = np.ones(shape) for i in xrange(natom): multiply_near_mask(coordinates[i], ugrid, 1.0, 0.5, weights) # Fit the charges and test cost = ESPCost.from_grid_data(coordinates, ugrid, esp, weights) x = cost.solve() assert cost.value_charges(charges) < 1e-7 assert cost.value(x) < 1e-7 assert abs(charges - x[:-1]).max() < 1e-4
def test_tridiagsym_solve(): N = 10 A = np.zeros((N,N), float) for irep in xrange(100): with numpy_seed(irep): # Make random numbers reproducible # randomize the diagonal A.ravel()[::N+1] = np.random.uniform(1,2,N) # randomize the upper diagonal A.ravel()[1::N+1] = np.random.uniform(-1,0,N-1) # clone the lower diagonal A.ravel()[N::N+1] = A.ravel()[1::N+1] # test the inverse for all possible basis vectors Ainv = np.linalg.inv(A) for i in xrange(N): right = np.zeros(N, float) right[i] = 1.0 solution = np.zeros(N, float) tridiagsym_solve( A.ravel()[::N+1].copy(), A.ravel()[N::N+1].copy(), right, solution ) error = abs(solution - Ainv[:,i]).max() assert(error < 1e-9)
def main(): """Main program.""" # Get command-line args example_paths, do_update, do_force, do_verbose = parse_args() # Silence the logger if not do_verbose: log.set_level(log.silent) # Exit status becomes 1 when some test results have changed beyond the thresholds, or # when previous or reference values are missing. exit_status = 0 # Run all examples. for example_path in example_paths: print example_path # Sanity checks if not example_path.endswith(".py"): raise RegressionTestError('Example path should end with ".py": {}.' .format(example_path)) with open(example_path) as f: example_lines = f.readlines() try: example_lines.index(comment_regtest) except ValueError: raise RegressionTestError('Missing regression-testing comment in example {}.' .format(example_path)) # Run example and collect results. example_globals = {} with numpy_seed(): print ' First random number:', np.random.rand() with open(example_path) as fh, numpy_seed(): exec fh in example_globals # pylint: disable=exec-used # Check that rt_results is defined in the example. if 'rt_results' not in example_globals: raise RegressionTestError('No rt_results defined in example {}.' .format(example_path)) # Extract relevant variables from example. rt_results = example_globals['rt_results'] rt_atols = example_globals.get('rt_atols', {}) rt_rtols = example_globals.get('rt_rtols', {}) rt_references = example_globals.get('rt_references', {}) rt_previous = example_globals.get('rt_previous', {}) known_keys = ['rt_results', 'rt_atols', 'rt_rtols', 'rt_references', 'rt_previous'] for key in example_globals: if key.startswith('rt_') and key not in known_keys: print 'Unrecognized variable starting with rt_: {}.' % key # Compare results with expected or previous values, if previous values are present. rt_status = {} for key, result in rt_results.iteritems(): target = rt_references.get(key, rt_previous.get(key)) if target is None: rt_status[key] = 'PREVIOUS OR REFERENCE MISSING' else: atol = rt_atols.get(key, default_atol) rtol = rt_rtols.get(key, default_rtol) if not isinstance(result, target.__class__): rt_status[key] = 'WRONG TYPE' elif isinstance(result, np.ndarray) and (result.shape != target.shape): rt_status[key] = 'WRONG SHAPE' elif np.allclose(result, target, atol=atol, rtol=rtol): rt_status[key] = 'OK' else: rt_status[key] = 'BOUNDS EXCEEDED' if do_update: update_example(example_path, example_lines, rt_previous, rt_results, rt_status, do_force) else: exit_status |= report_regressions(rt_results, rt_atols, rt_rtols, rt_references, rt_previous, rt_status) # Stop with proper exit code. sys.exit(exit_status)
def get_random_unitary(seed=1): """Return a random 3x3 unitary transformation.""" with numpy_seed(seed): A = np.random.normal(0, 1, (3, 3)) A = 0.5*(A+A.T) return np.linalg.eigh(A)[1]
def get_random_shift(seed=1): """Generate a random displacement in 3D Cartesian coordinates.""" with numpy_seed(seed): return np.random.uniform(-3, 3, 3)