class FactoryTester(unittest.TestCase): def setUp(self): self.factory = Factory() ## Tests the createParametersObject() function without a type parameter def testCreateParametersObjectWithNoTypeParam(self): params = {"gamma": "1.4", "R": "200"} self.factory.createParametersObject("IdealGasEoS", params) ## Tests the createParametersObject() function with a type parameter def testCreateParametersObjectWithTypeParam(self): params = {"type": "IdealGasEoS", "gamma": "1.4", "R": "200"} object_class = params["type"] self.factory.createParametersObject(object_class, params) ## Tests the createObject() function def testCreateObject(self): params = {"type": "IdealGasEoS", "gamma": "1.4", "R": "200"} object_class = params["type"] self.factory.createObject(object_class, params) ## Tests the createObjectFromParametersObject() function def testCreateObjectFromParametersObject(self): object_class = "IdealGasEoS" parameters_object = self.factory.createParametersObject(object_class) parameters_object.set("gamma", 1.4) parameters_object.set("R", 200) self.factory.createObjectFromParametersObject(object_class, parameters_object)
def outputPrintDoFVectorToFile(path): # create the DoF handler factory = Factory() n_cell = 5 mesh_name = "mesh" mesh = factory.createObject("UniformMesh", {"name": mesh_name, "n_cell": n_cell}) ics = [ factory.createObject( "InitialConditions1Phase", { "mesh_name": mesh_name, "A": "1", "rho": "1", "u": "1", "p": "1" }) ] dof_handler = factory.createObject("DoFHandler1Phase", {"meshes": [mesh], "ics": ics}) # solution vector U = list(range((n_cell + 1) * 3)) # capture the output captor = OutputCaptor() printDoFVector(U, dof_handler) out = captor.getCapturedOutput() # write the output file text_file = open(path + "print_dof_vector.txt", "w") text_file.write(out) text_file.close()
def testJunctionConstraintDoFIndices(self): # create the factory factory = Factory() # create the meshes n_cells_list = [3, 5, 4, 1] meshes = list() for i, n_cells in enumerate(n_cells_list): name = "mesh" + str(i + 1) params = {"name": name, "n_cell": n_cells} meshes.append(factory.createObject("UniformMesh", params)) # ICs ics = [ factory.createObject( "InitialConditions1Phase", { "mesh_name": meshes[0].name, "A": "1", "rho": "1", "u": "1", "p": "1" }) ] # create the DoF handler params = {"meshes": meshes, "ics": ics} dof_handler = factory.createObject("DoFHandler1Phase", params) # create an EoS eos_list = [factory.createObject("TestEoS")] # create the junctions n_constraints_list = [2, 6, 3, 1] n_junctions = len(n_constraints_list) meshes_list = [ ["mesh1", "mesh2"], ["mesh2", "mesh3"], ["mesh1", "mesh2"], ["mesh3", "mesh4"] ] sides_list = [["right", "left"]] * n_junctions junctions = list() for i in range(n_junctions): params = { "mesh_names": meshes_list[i], "mesh_sides": sides_list[i], "dof_handler": dof_handler, "eos_list": eos_list, "n_constraints": n_constraints_list[i] } junctions.append(factory.createObject("TestJunction", params)) # update the DoF handler with the junction constraints dof_handler.updateWithJunctionConstraints(junctions) # check all of the constraint DoF indices are the expected expected_constraint_dof_indices = [[12, 13], [35, 36, 37, 38, 39, 40], [14, 15, 16], [56]] for i, junction in enumerate(junctions): self.assertEqual(junction.i_constraint, expected_constraint_dof_indices[i])
def setUp(self): factory = Factory() params = dict() params["gamma"] = 1.4 params["R"] = 290.0 self.eos = factory.createObject("IdealGasEoS", params) self.derivative_tester = FunctionDerivativesTester()
def setUp(self): factory = Factory() params = dict() params["gamma"] = 1.4 params["cv"] = 2.0 params["q"] = -5.0 params["p_inf"] = 0.75 params["q_prime"] = 6.0 self.eos = factory.createObject("StiffenedGasEoS", params) self.derivative_tester = FunctionDerivativesTester()
def run(mods=list()): # parse the input file input_file_parser = InputFileParser() input_file_parser.parse("print_fluid_properties.in") # apply modifications to input parameters, if any for mod in mods: input_file_parser.applyModification(mod) # create the factory factory = Factory() # equation of state eos_param_data = input_file_parser.getBlockData("EoS") eos_class = eos_param_data["type"] eos = factory.createObject(eos_class, eos_param_data) # thermodynamic state state_data = input_file_parser.getBlockData("ThermodynamicState") state = factory.createObject("ThermodynamicState", state_data) state.computeRemainingProperties(eos) print(state)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # specific volume aux test_aux = factory.createObject("SpecificVolume", {"phase": 0}) # density aux params = dict() params["var"] = "rho1" params["other_vars"] = ["aA1", "arhoA1"] params["coefs"] = [2.0, 3.0] rho_aux = factory.createObject("TestAux", params) other_aux = [rho_aux] root_vars = ["aA1", "arhoA1"] class SpecificVolumeDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives( test_aux, other_aux, root_vars) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # test aux test_aux = factory.createObject("BerryInterfacePressureBar") # phase-1 pressure params = dict() params["var"] = "p1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [1.1, 1.2, 1.3, 1.4] p1_aux = factory.createObject("TestAux", params) # phase-2 pressure params = dict() params["var"] = "p2" params["other_vars"] = ["aA1", "arhoA2", "arhouA2", "arhoEA2"] params["coefs"] = [1.7, 1.9, 2.3, 1.6] p2_aux = factory.createObject("TestAux", params) # phase-1 acoustic impedance aux params = dict() params["var"] = "z1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [2.1, 2.3, 3.4, 4.5] z1_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # Lax-Friedrichs coefficient aux test_aux = factory.createObject("LaxFriedrichsCoefficientVolumeFraction") # constant data constant_data = {"dx": 0.5} # positive interfacial velocity aux params = dict() params["var"] = "uI" params["other_vars"] = [ "aA1", "arhoA1", "arhouA1", "arhoEA1", "arhoA2", "arhouA2", "arhoEA2" ] params["coefs"] = [1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5] uI_positive_aux = factory.createObject("TestAux", params) # negative interfacial velocity aux params = dict() params["var"] = "uI" params["other_vars"] = [ "aA1", "arhoA1", "arhouA1", "arhoEA1", "arhoA2", "arhouA2", "arhoEA2" ] params["coefs"] = [-1.3, -1.5, -1.7, -1.9, -2.1, -2.3, -2.5] uI_negative_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # test aux test_aux = factory.createObject("AcousticImpedance", {"phase": 0}) # density aux params = dict() params["var"] = "rho1" params["other_vars"] = ["aA1", "arhoA1"] params["coefs"] = [1.4, 2.5] rho_aux = factory.createObject("TestAux", params) # sound speed aux params = dict() params["var"] = "c1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [1.2, 2.2, 3.2, 4.2] c_aux = factory.createObject("TestAux", params) other_aux = [rho_aux, c_aux] root_vars = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] class AcousticImpedanceDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester()
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() test_aux = factory.createObject("MassFlux", {"phase": 0}) other_aux = list() root_vars = ["arhouA1"] class MassFluxDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives( test_aux, other_aux, root_vars, constant_data={ "A": 0.3 }) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # gradient of test aux params = dict() params["aux"] = "testaux" params["variable_names"] = ["var1", "var2"] test_aux = factory.createObject("AuxGradient", params) # test aux params = dict() params["var"] = "testaux" params["other_vars"] = ["var1", "var2"] params["coefs"] = [1.5, 2.5] aux = factory.createObject("TestAux", params) other_aux = [aux] root_vars = ["grad_var1", "grad_var2"] constant_data = {"var1": 1.2, "var2": 1.4} class AuxGradientDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives(
def computeSoundSpeed(v, e): v_slope = 2.0 e_slope = 3.0 c = v_slope * v + e_slope * e return (c, v_slope, e_slope) factory = Factory() # sound speed aux params = dict() params["phase"] = 0 params["c_function"] = computeSoundSpeed test_aux = factory.createObject("SoundSpeed", params) # specific volume aux params = dict() params["var"] = "v1" params["other_vars"] = ["aA1", "arhoA1"] params["coefs"] = [2.0, 3.0] v_aux = factory.createObject("TestAux", params) # pressure aux params = dict() params["var"] = "p1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [1.5, 2.5, 3.5, 4.5] p_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # interface velocity aux test_aux = factory.createObject("AmbrosoInterfaceVelocity") # phase-1 velocity aux params = dict() params["var"] = "u1" params["other_vars"] = ["arhoA1", "arhouA1"] params["coefs"] = [1.2, 2.2] u1_aux = factory.createObject("TestAux", params) # phase-2 velocity aux params = dict() params["var"] = "u2" params["other_vars"] = ["arhoA2", "arhouA2"] params["coefs"] = [1.5, 2.5] u2_aux = factory.createObject("TestAux", params) # beta aux params = dict() params["var"] = "beta" params["other_vars"] = ["arhoA1", "arhoA2"] params["coefs"] = [1.7, 3.2] beta_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() test_aux = factory.createObject("Velocity", {"phase": 0}) other_aux = list() root_vars = ["arhoA1", "arhouA1"] class VelocityDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives( test_aux, other_aux, root_vars) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
def checkDerivatives(self, kernel_name, model_type, phase, aux_dependencies, \ aux_gradients=list(), kernel_params=dict(), fd_eps=1e-8): self.model_type = model_type self.phase = phase # factory factory = Factory() # mesh params = {"n_cell": 1} meshes = [factory.createObject("UniformMesh", params)] # DoF handler dof_handler_params = {"meshes": meshes} if self.model_type == ModelType.OnePhase: ic_params = { "mesh_name": meshes[0].name, "A": "2", "rho": "1", "u": "1", "p": "1" } ics = [factory.createObject("InitialConditions1Phase", ic_params)] dof_handler_class = "DoFHandler1Phase" else: ic_params = { "mesh_name": meshes[0].name, "A": "2", "vf1": "0.3", "rho1": "1", "u1": "1", "p1": "1", "rho2": "1", "u2": "1", "p2": "1" } ics = [factory.createObject("InitialConditions2Phase", ic_params)] if self.model_type == ModelType.TwoPhaseNonInteracting: dof_handler_class = "DoFHandler2PhaseNonInteracting" elif self.model_type == ModelType.TwoPhase: dof_handler_class = "DoFHandler2Phase" dof_handler_params["ics"] = ics dof_handler = factory.createObject(dof_handler_class, dof_handler_params) # quadrature n_q_points = 2 quadrature_params = {"n_q_points": n_q_points} quadrature = factory.createObject("Quadrature", quadrature_params) # FE values fe_values_params = { "quadrature": quadrature, "dof_handler": dof_handler, "meshes": meshes } self.fe_values = factory.createObject("FEValues", fe_values_params) # kernel kernel_params["phase"] = phase kernel_params["dof_handler"] = dof_handler kernel = factory.createObject(kernel_name, kernel_params) # aux aux_list = list() for a, aux_name in enumerate(sorted(aux_dependencies.keys())): # "vf1" is a special case of aux because its name is also the name of # a solution variable (in 2-phase); therefore one needs to make sure that # it uses its own "identity" aux instead of the generic test aux if aux_name == "vf1": params = {"phase": 0, "size": n_q_points} aux_list.append( factory.createObject("VolumeFractionPhase1", params)) else: params = TestAuxParameters(factory) params.set("var", aux_name) params.set("other_vars", aux_dependencies[aux_name]) coefs = list() for d, _ in enumerate(aux_dependencies[aux_name]): coefs.append(a + 2.0 + d * 0.5) params.set("coefs", coefs) params.set("b", 1.0) params.set("size", n_q_points) aux_list.append(TestAux(params)) # add the aux derivatives for aux_name in aux_gradients: params = { "aux": aux_name, "variable_names": aux_dependencies[aux_name], "size": n_q_points } aux_list.append(factory.createObject("AuxGradient", params)) # data data = dict() aux_names = [aux.name for aux in aux_list] der = initializeDerivativeData(aux_names, n_q_points) for aux_name in aux_dependencies: for dep in aux_dependencies[aux_name]: der[aux_name][dep] = np.zeros(n_q_points) self.elem = 0 i = 0 j = 1 q = 0 data["htc_wall"] = 0.2 data["T_wall"] = 1.3 data["P_heat"] = 0.6 data["grad_A"] = 0.3 data["phi"] = self.fe_values.get_phi() data["grad_phi"] = self.fe_values.get_grad_phi(self.elem) data["JxW"] = self.fe_values.get_JxW(self.elem) # compute base solution U = np.zeros(dof_handler.n_dof) for k in range(dof_handler.n_dof): U[k] = k + 1.0 self.computeSolutionDependentData(U, data) for aux in aux_list: aux.compute(data, der) # base calculation r = kernel.computeResidual(data, i)[q] J_hand_coded = dict() for var_index in kernel.var_indices: J_hand_coded[var_index] = kernel.computeJacobian( data, der, var_index, i, j)[q] # finite difference Jacobians rel_diffs = dict() J_fd = dict() for var_index in kernel.var_indices: # perturb solution and recompute aux U_perturbed = deepcopy(U) j_global = dof_handler.i(j, var_index) U_perturbed[j_global] += fd_eps self.computeSolutionDependentData(U_perturbed, data) for aux in aux_list: aux.compute(data, der) # compute finite difference Jacobian r_perturbed = kernel.computeResidual(data, i)[q] J_fd[var_index] = (r_perturbed - r) / fd_eps rel_diffs[var_index] = computeRelativeDifference( J_hand_coded[var_index], J_fd[var_index]) # print results for var_index in kernel.var_indices: var = dof_handler.variable_names[var_index] print("\nDerivative variable:", var) print(" Hand-coded =", J_hand_coded[var_index]) print(" Finite difference =", J_fd[var_index]) print(" Rel. difference =", rel_diffs[var_index]) # take the absolute value of the relative differences for x in rel_diffs: rel_diffs[x] = abs(rel_diffs[x]) return rel_diffs
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # test aux test_aux = factory.createObject("BerryInterfaceVelocityBar") # phase-1 velocity params = dict() params["var"] = "u1" params["other_vars"] = ["arhoA1", "arhouA1"] params["coefs"] = [1.1, 1.2] u1_aux = factory.createObject("TestAux", params) # phase-2 velocity params = dict() params["var"] = "u2" params["other_vars"] = ["arhoA2", "arhouA2"] params["coefs"] = [1.7, 1.9] u2_aux = factory.createObject("TestAux", params) # phase-1 acoustic impedance aux params = dict() params["var"] = "z1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [2.1, 2.3, 3.4, 4.5] z1_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # test aux test_aux = factory.createObject("EnergyFlux", {"phase": 0}) # volume fraction aux vf_aux = factory.createObject("VolumeFractionPhase1", {"phase": 0}) # velocity aux params = dict() params["var"] = "u1" params["other_vars"] = ["arhoA1", "arhouA1"] params["coefs"] = [1.3, 2.2] u_aux = factory.createObject("TestAux", params) # pressure aux params = dict() params["var"] = "p1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [1.4, 2.3, 2.1, 1.2] p_aux = factory.createObject("TestAux", params) other_aux = [vf_aux, u_aux, p_aux] root_vars = ["aA1", "arhoA1", "arhouA1", "arhoEA1"]
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # beta aux test_aux = factory.createObject("AmbrosoBeta", {"chi": 0.5}) other_aux = [] root_vars = ["arhoA1", "arhoA2"] class AmbrosoBetaDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives( test_aux, other_aux, root_vars) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() test_aux = factory.createObject("SpecificTotalEnergy", {"phase": 0}) other_aux = list() root_vars = ["arhoA1", "arhoEA1"] class SpecificTotalEnergyDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives( test_aux, other_aux, root_vars) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() params = dict() params["var"] = "test" params["other_vars"] = ["A", "B", "C"] params["coefs"] = [2.0, 3.0, 4.0] test_aux = factory.createObject("TestAux", params) other_aux = list() root_vars = ["A", "B", "C"] class TestAuxDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives(test_aux, other_aux, root_vars) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
def computeTemperature(v, e): v_slope = 2.0 e_slope = 3.0 T = v_slope * v + e_slope * e return (T, v_slope, e_slope) factory = Factory() # temperature aux params = dict() params["phase"] = 0 params["T_function"] = computeTemperature test_aux = factory.createObject("Temperature", params) # specific volume aux params = dict() params["var"] = "v1" params["other_vars"] = ["aA1", "arhoA1"] params["coefs"] = [2.0, 3.0] v_aux = factory.createObject("TestAux", params) # specific internal energy aux params = dict() params["var"] = "e1" params["other_vars"] = ["arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [2.5, 3.5, 4.5] e_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # specific internal energy aux test_aux = factory.createObject("SpecificInternalEnergy", {"phase": 0}) # velocity aux params = dict() params["var"] = "u1" params["other_vars"] = ["arhoA1", "arhouA1"] params["coefs"] = [1.1, 1.2] u_aux = factory.createObject("TestAux", params) # specific total energy aux params = dict() params["var"] = "E1" params["other_vars"] = ["arhoA1", "arhoEA1"] params["coefs"] = [3.3, 3.9] E_aux = factory.createObject("TestAux", params) other_aux = [u_aux, E_aux] root_vars = ["arhoA1", "arhouA1", "arhoEA1"] class SpecificInternalEnergyDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester()
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # test aux test_aux = factory.createObject("BerryVelocityRelaxationCoef") # pressure relaxation aux params = dict() params["var"] = "p_relax" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1", "arhoA2", "arhouA2", "arhoEA2"] params["coefs"] = [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7] p_relax_aux = factory.createObject("TestAux", params) # phase-1 acoustic impedance aux params = dict() params["var"] = "z1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [2.1, 2.3, 3.4, 4.5] z1_aux = factory.createObject("TestAux", params) # phase-2 acoustic impedance aux params = dict() params["var"] = "z2" params["other_vars"] = ["aA1", "arhoA2", "arhouA2", "arhoEA2"] params["coefs"] = [2.4, 2.2, 3.3, 4.4] z2_aux = factory.createObject("TestAux", params)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # interface pressure aux test_aux = factory.createObject("AmbrosoInterfacePressure") # phase-1 pressure aux params = dict() params["var"] = "p1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [1.2, 2.2, 3.2, 4.2] p1_aux = factory.createObject("TestAux", params) # phase-2 pressure aux params = dict() params["var"] = "p2" params["other_vars"] = ["aA1", "arhoA2", "arhouA2", "arhoEA2"] params["coefs"] = [1.5, 2.5, 3.5, 4.5] p2_aux = factory.createObject("TestAux", params) # velocity relaxation coefficient aux params = dict() params["var"] = "u_relax" params["other_vars"] = [ "aA1", "arhoA1", "arhouA1", "arhoEA1", "arhoA2", "arhouA2", "arhoEA2" ] params["coefs"] = [1.2, 1.5, 1.7, 2.2, 2.5, 2.7, 3.2]
def checkJacobian(self, bc_name, model_type=ModelType.OnePhase, boundary="right", bc_params=dict(), fd_eps=1e-8): self.model_type = model_type # factory factory = Factory() # mesh params = {"n_cell": 1} meshes = [factory.createObject("UniformMesh", params)] # test node index if boundary == "left": k_test = 0 else: k_test = 1 # DoF handler dof_handler_params = {"meshes": meshes} if self.model_type == ModelType.OnePhase: ic_params = { "mesh_name": meshes[0].name, "A": "0.2", "rho": "1", "u": "1", "p": "1" } ics = [factory.createObject("InitialConditions1Phase", ic_params)] dof_handler_class = "DoFHandler1Phase" else: ic_params = { "mesh_name": meshes[0].name, "A": "0.2", "vf1": "0.3", "rho1": "1", "u1": "1", "p1": "1", "rho2": "1", "u2": "1", "p2": "1" } ics = [factory.createObject("InitialConditions2Phase", ic_params)] if self.model_type == ModelType.TwoPhaseNonInteracting: dof_handler_class = "DoFHandler2PhaseNonInteracting" elif self.model_type == ModelType.TwoPhase: dof_handler_class = "DoFHandler2Phase" dof_handler_params["ics"] = ics dof_handler = factory.createObject(dof_handler_class, dof_handler_params) n_dof = dof_handler.n_dof n_var = dof_handler.n_var # equation of state eos_params = dict() eos_params["gamma"] = 1.4 eos_params["R"] = 2.0 eos_list = [factory.createObject("IdealGasEoS", eos_params)] # BC bc_params["mesh_name"] = meshes[0].name bc_params["boundary"] = boundary bc_params["dof_handler"] = dof_handler bc_params["eos_list"] = eos_list bc = factory.createObject(bc_name, bc_params) # compute base solution U = np.zeros(n_dof) for i in range(n_dof): U[i] = i + 1.0 # base calculation r = np.zeros(n_dof) J_hand_coded = np.zeros(shape=(n_dof, n_dof)) bc.applyWeakBC(U, r, J_hand_coded) # finite difference Jacobians rel_diffs = np.zeros(shape=(n_var, n_var)) J_fd = np.zeros(shape=(n_var, n_var)) for var_index_j in range(dof_handler.n_var): # solution index to perturb j = dof_handler.i(k_test, var_index_j) # perturb solution U_perturbed = deepcopy(U) U_perturbed[j] += fd_eps # compute finite difference Jacobian r_perturbed = np.zeros(n_dof) J_perturbed = np.zeros(shape=(n_dof, n_dof)) bc.applyWeakBC(U_perturbed, r_perturbed, J_perturbed) for var_index_i in range(n_var): # residual index tested i = dof_handler.i(k_test, var_index_i) J_fd[var_index_i][var_index_j] = (r_perturbed[i] - r[i]) / fd_eps rel_diffs[var_index_i][ var_index_j] = computeRelativeDifference( J_hand_coded[i][j], J_fd[var_index_i][var_index_j]) # print results for var_index_i in range(n_var): i = dof_handler.i(k_test, var_index_i) var_i = dof_handler.variable_names[var_index_i] print("\nEquation variable:", var_i) for var_index_j in range(n_var): j = dof_handler.i(k_test, var_index_j) var_j = dof_handler.variable_names[var_index_j] print("\n Derivative variable:", var_j) print(" Hand-coded =", J_hand_coded[i][j]) print(" Finite difference =", J_fd[var_index_i][var_index_j]) print(" Rel. difference =", rel_diffs[var_index_i][var_index_j]) # take the absolute value of the relative differences return abs(rel_diffs)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # internal energy density aux test_aux = factory.createObject("InternalEnergyDensity", {"phase": 0}) # density aux params = dict() params["var"] = "rho1" params["other_vars"] = ["aA1", "arhoA1"] params["coefs"] = [2.0, 3.0] rho_aux = factory.createObject("TestAux", params) # specific internal energy aux params = dict() params["var"] = "e1" params["other_vars"] = ["arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [2.5, 3.5, 4.5] e_aux = factory.createObject("TestAux", params) other_aux = [rho_aux, e_aux] root_vars = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] class InternalEnergyDensityDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester()
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() test_aux = factory.createObject("Density", {"phase": 0}) vf_aux = factory.createObject("VolumeFractionPhase1", {"phase": 0}) other_aux = [vf_aux] root_vars = ["aA1", "arhoA1"] class DensityDerivativesTester(unittest.TestCase): def setUp(self): self.derivatives_tester = AuxDerivativesTester() def test(self): rel_diffs = self.derivatives_tester.checkDerivatives( test_aux, other_aux, root_vars, constant_data={"A": 0.3}) for key in rel_diffs: self.assertLessEqual(rel_diffs[key], 1e-6)
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # viscous flux aux test_aux = factory.createObject("EntropyMinimumMassFlux", {"phase": 0}) # volume fraction aux vf_aux = factory.createObject("VolumeFractionPhase1", {"phase": 0}) # viscous coefficient aux params = dict() params["var"] = "visccoef_arhoA1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1", "arhoA2", "arhouA2", "arhoEA2"] params["coefs"] = [1.5, 2.5, 3.5, 4.5, 1.1, 1.7, 2.1] visccoef_arho_aux = factory.createObject("TestAux", params) # density gradient aux params = dict() params["var"] = "grad_rho1" params["other_vars"] = ["grad_aA1", "grad_arhoA1"] params["coefs"] = [1.2, 2.4] grad_rho_aux = factory.createObject("TestAux", params) # density aux params = dict() params["var"] = "rho1" params["other_vars"] = ["aA1", "arhoA1"]
import unittest from sem_python.base.Factory import Factory from ....src.testers.AuxDerivativesTester import AuxDerivativesTester factory = Factory() # test aux test_aux = factory.createObject("BerryInterfaceVelocity") # bar interface velocity aux params = dict() params["var"] = "uI_bar" params["other_vars"] = [ "aA1", "arhoA1", "arhouA1", "arhoEA1", "arhoA2", "arhouA2", "arhoEA2" ] params["coefs"] = [1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7] uI_bar_aux = factory.createObject("TestAux", params) # phase-1 pressure aux params = dict() params["var"] = "p1" params["other_vars"] = ["aA1", "arhoA1", "arhouA1", "arhoEA1"] params["coefs"] = [2.1, 2.3, 3.4, 4.5] p1_aux = factory.createObject("TestAux", params) # phase-2 pressure aux params = dict() params["var"] = "p2" params["other_vars"] = ["aA1", "arhoA2", "arhouA2", "arhoEA2"] params["coefs"] = [2.4, 2.2, 3.3, 4.4]