def test_gravity_vector_calculation(): """ Tests that the function correctly calculates the direction of the gravity vector from two angles. """ print "\n" print "Testing Cantilever Simulation gravity_vector_calculation function:" print "\n" # Create the simulation sim = CantileverSimulation() passCounter = 0 # Run the tests if (sim.gravity_vector_calculation(90 * math.pi / 180, 0 * math.pi / 180).all() == np.array( [-9.81, 0.0, 0.0]).all()): passCounter += 1 if (sim.gravity_vector_calculation(90 * math.pi / 180, 0 * math.pi / 180).all() == np.array( [-9.81, 0.0, 0.0]).all()): passCounter += 1 # Can easily add more test cases! print "Calculation passed {0} of 2 tests.".format(passCounter) print "\n"
def test_node_moving_calculation(): """ Tests that the function which moves the nodes of the FE model does so in the correct manner. """ print "\n" print "Testing Cantilever Simulation node relocation function:" print "\n" # Create the simulation sim = CantileverSimulation() # Set up the counter passCounter = 0 # Run the tests sim.set_cantilever_dimensions(np.array([30, 12, 12])) if (np.isclose(sim.calculate_new_x_value(30.0), 30)): passCounter += 1 if (np.isclose(sim.calculate_new_x_value(0.0), 0)): passCounter += 1 if (np.isclose(sim.calculate_new_x_value(15.0), 7.5)): passCounter += 1 if (np.isclose(sim.calculate_new_x_value(20.0), 120.0 / 9.0)): passCounter += 1 print "Calculation passed {0} of 4 tests.".format(passCounter) print "\n"
def test_node_moving_function(): """ Tests whether the node moving function successfully moves the nodes closer to the model's fixed end. """ print "\n" print "Testing Cantilever Simulation node relocation function:" print "\n" # Set up the simulation, making it small so the location of nodes can be easily determined. sim = CantileverSimulation() sim.set_cantilever_dimensions(np.array([30, 12, 12])) sim.set_cantilever_elements(np.array([1, 1, 1])) sim.setup_cantilever_simulation() # Since the simulation uses Cubic Lagrange interpolation, there are 64 nodes in the one element. sim.move_one_layer_nodes() # We know that the nodes should've been at 0, 10, 20 and 30 mm along the x-axis. passCounter = 0 if np.isclose( sim.dependentField.ParameterSetGetNodeDP( iron.FieldVariableTypes.U, iron.FieldParameterSetTypes.VALUES, 1, 1, 1, 1), 0): passCounter += 1 if np.isclose( sim.dependentField.ParameterSetGetNodeDP( iron.FieldVariableTypes.U, iron.FieldParameterSetTypes.VALUES, 1, 1, 2, 1), 30.0 / 9.0): passCounter += 1 if np.isclose( sim.dependentField.ParameterSetGetNodeDP( iron.FieldVariableTypes.U, iron.FieldParameterSetTypes.VALUES, 1, 1, 3, 1), 120.0 / 9.0): passCounter += 1 if np.isclose( sim.dependentField.ParameterSetGetNodeDP( iron.FieldVariableTypes.U, iron.FieldParameterSetTypes.VALUES, 1, 1, 4, 1), 30): passCounter += 1 destroy_routine(sim) print "Calculation passed {0} of 4 tests.".format(passCounter) print "\n"
def test_set_diagnostic_level(): """ Test if the set_diagnostic_level method sets the simulation's diagnostic level as expected. """ print "\n" print "Testing Cantilever Simulation set_diagnostics_level function:" print "\n" # Create the simulation sim = CantileverSimulation() # Check its initial diagnostic level if (sim.diagnostics == None): print "Diagnostics Initiation Successful." else: print "Diagnostics Initiation Failed." # Call the set_diagnostic_level function and check if it changed the level correctly. sim.set_diagnostic_level(1) if (sim.diagnostics == 1): print "set_diagnostic_level function Successful." else: print "set_diagnostic_level function Failed." print "\n"
simulation.region.Destroy() simulation.basis.Destroy() simulation.problem.Destroy() dimensions = np.array([30, 12, 12]) elements = np.array([2, 2, 2]) initial_parameter = np.array([8.4378]) theta1 = 30*math.pi/180 phi1 = 0*math.pi/180 theta2 = 30*math.pi/180 phi2 = 90*math.pi/180 ps = ParameterEstimation() ps.simulation = CantileverSimulation() ps.simulation.set_cantilever_dimensions(dimensions) ps.simulation.set_cantilever_elements(elements) ps.simulation.set_gravity_vector(ps.simulation.gravity_vector_calculation(theta1, phi1)) ps.simulation.set_diagnostic_level(0) ps.simulation.setup_cantilever_simulation() ps.simulation.set_Neo_Hookean_single_layer(initial_parameter) ps.simulation.solve_simulation() #ps.simulation.export_results("output_3/Cantilever") ps.simulation.set_projection_data() ps.set_objective_function(single_layer_objective_function) [H, detH, condH, detH0] = ps.new_evaluate_hessian_method(initial_parameter, 1e-7) print "For angles Theta = {0}, Phi = {1}".format(theta1, phi1) print "Gravity Vector = {0}".format(ps.simulation.gravity_vector)
def generate_one_layer_cantilever_output(): """ Generates expected output data set for test_one_layer_cantilever_output unit test. Stiffness value is 5.432 """ # Set up the simulation sim = CantileverSimulation() sim.set_cantilever_dimensions(np.array([30, 12, 12])) sim.set_cantilever_elements(np.array([4, 4, 4])) sim.setup_cantilever_simulation() sim.set_Neo_Hookean_single_layer(np.array([5.432])) # Solve the simulation sim.solve_simulation() # Export the results sim.export_results("Expected Output/generate_one_layer_cantilever_output")
else: self.elementRecord = np.append( self.elementRecord, [self.elements[0] * self.elements[1] * self.elements[2]]) # Create instance of ConvergenceTest class conTest = ConvergenceTest() # Define some useful variables. dimensions = np.array([30, 12, 12]) parameterValue = np.array([7.589]) conTest.tolerance = 1e-3 # Add a simulation to the convergence conTest.sim = CantileverSimulation() # Set up the chosen simulation. conTest.elements = np.array([2, 2, 2]) conTest.sim.set_cantilever_elements(conTest.elements) conTest.sim.set_cantilever_dimensions(dimensions) conTest.sim.set_diagnostic_level(1) conTest.sim.setup_cantilever_simulation() conTest.sim.set_Neo_Hookean_single_layer(parameterValue) # Now solve the simulation and generate a data set conTest.sim.solve_simulation() conTest.currentDataSet = conTest.sim.generate_data(0) conTest.store_data() conTest.store_elements()
def new_evaluate_hessian_method(self, x, stepsize): """ :param x: :param stepsize: :return: """ dimensions = self.simulation.cantilever_dimensions elements = self.simulation.cantilever_elements gravity_vector = self.simulation.gravity_vector diagnostic_level = self.simulation.diagnostics data = self.simulation.data ps = ParameterEstimation() ps.simulation = CantileverSimulation() ps.simulation.set_cantilever_dimensions(dimensions) ps.simulation.set_cantilever_elements(elements) ps.simulation.set_gravity_vector(gravity_vector) ps.simulation.set_diagnostic_level(diagnostic_level) ps.simulation.data = data destroy_routine(self.simulation) ps.simulation.setup_cantilever_simulation() objfun = self.objective_function n = len(x) A = np.zeros(n) B = np.zeros(n) ee = stepsize * np.eye(n) # First-order derivatives: 2n function calls needed for i in range(n): A[i] = objfun(x + ee[:, i], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) B[i] = objfun(x - ee[:, i], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) # Second-order derivatives based on function calls only (Abramowitz and Stegun 1972, p.884): for dense Hessian, 2n+4n^2/2 function calls needed. H = np.zeros((n, n)) for i in range(n): C = objfun(x + 2 * ee[:, i], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) E = objfun(x, ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) F = objfun(x - 2 * ee[:, i], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) H[i, i] = (-C + 16 * A[i] - 30 * E + 16 * B[i] - F) / (12 * (ee[i, i]**2)) for j in range(i + 1, n): G = objfun(x + ee[:, i] + ee[:, j], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) I = objfun(x + ee[:, i] - ee[:, j], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) J = objfun(x - ee[:, i] + ee[:, j], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) K = objfun(x - ee[:, i] - ee[:, j], ps.simulation) reset_simulation(ps.simulation, dimensions, elements, gravity_vector, diagnostic_level, data) H[i, j] = (G - I - J + K) / (4 * ee[i, i] * ee[j, j]) H[j, i] = H[i, j] destroy_routine(ps.simulation) import cmath n = len(H) detH = np.linalg.det(H) condH = 1.0 / np.linalg.cond(H) H0 = np.zeros((n, n)) for j in range(n): for k in range(n): H0[j, k] = H[j, k] / np.abs((cmath.sqrt(H[j, j] * H[k, k]))) detH0 = np.linalg.det(H0) return H, detH, condH, detH0
def create_routine(simulation, dimensions, elements, gravity_vector, diagnostic_level, data): """ :param dimensions: :param elements: :param gravity_vector: :param diagnostic_level: :param data: :return: """ destroy_routine(simulation) simulation = None simulation = CantileverSimulation() simulation.set_cantilever_dimensions(dimensions) simulation.set_cantilever_elements(elements) simulation.set_gravity_vector(gravity_vector) simulation.set_diagnostic_level(diagnostic_level) simulation.setup_cantilever_simulation() simulation.data = data return simulation