Esempio n. 1
0
class EmbeddedReservoirDiscontinuousTest(UnitTest.TestCase):
    def testEmbeddedReservoirDiscontinuous3D(self):
        self.distance = 0.99
        self.slip_level_set = True
        self.work_folder = "EmbeddedReservoirDiscontinuousTest"
        self.reference_file = "reference_embedded_reservoir_discontinuous_3D"
        self.settings = "EmbeddedReservoirDiscontinuous3DTestParameters.json"
        self.ExecuteEmbeddedReservoirTest()

    def ExecuteEmbeddedReservoirTest(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            self.setUp()
            self.setUpProblem()
            self.runTest()
            self.tearDown()
            self.checkResults()

    def setUp(self):
        self.check_tolerance = 1.0e-8
        self.print_output = False
        self.print_reference_values = False

    def tearDown(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            KratosUtilities.DeleteFileIfExisting(
                self.ProjectParameters["solver_settings"]
                ["model_import_settings"]["input_filename"].GetString() +
                '.time')

    def setUpProblem(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            with open(self.settings, 'r') as parameter_file:
                self.ProjectParameters = KratosMultiphysics.Parameters(
                    parameter_file.read())

            self.Model = KratosMultiphysics.Model()
            self.simulation = FluidDynamicsAnalysis(self.Model,
                                                    self.ProjectParameters)

    def setUpDistanceField(self):
        # Get the model part containing the domain
        fluid_model_part = self.simulation._GetSolver().main_model_part

        # Set continuous distance field
        for node in fluid_model_part.Nodes:
            d = self.distance - node.Z
            node.SetSolutionStepValue(KratosMultiphysics.DISTANCE, d)

        # Set discontinuous distance field
        for element in fluid_model_part.Elements:
            i_node = 0
            elem_dist = KratosMultiphysics.Vector(4)
            for node in element.GetNodes():
                elem_dist[i_node] = node.GetSolutionStepValue(
                    KratosMultiphysics.DISTANCE)
                i_node += 1
            element.SetValue(KratosMultiphysics.ELEMENTAL_DISTANCES, elem_dist)

    def setUpInitialCondition(self):
        # Set exact initial solution
        v_zero = KratosMultiphysics.Vector(3, 0.0)
        for node in self.simulation._GetSolver().main_model_part.Nodes:
            node.SetSolutionStepValue(KratosMultiphysics.VELOCITY, 0, v_zero)
            node.SetSolutionStepValue(KratosMultiphysics.VELOCITY, 1, v_zero)
            node.SetSolutionStepValue(KratosMultiphysics.VELOCITY, 2, v_zero)
            if node.GetSolutionStepValue(KratosMultiphysics.DISTANCE) < 0.0:
                node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 0,
                                          1.0e+06)
                node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 1,
                                          1.0e+06)
                node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 2,
                                          1.0e+06)
            else:
                node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 0, 0.0)
                node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 1, 0.0)
                node.SetSolutionStepValue(KratosMultiphysics.PRESSURE, 2, 0.0)

    def runTest(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            # Set up the test
            self.simulation.Initialize()
            self.setUpDistanceField()
            self.setUpInitialCondition()

            # Run the test
            self.simulation.RunSolutionLoop()

            # Finalize the test
            self.simulation.Finalize()

    def checkResults(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            fluid_model_part = self.simulation._GetSolver().main_model_part
            if self.print_reference_values:
                with open(self.reference_file + '.csv', 'w') as ref_file:
                    ref_file.write(
                        "#ID, PRESSURE, VELOCITY_X, VELOCITY_Y, VELOCITY_Z\n")
                    for node in fluid_model_part.Nodes:
                        pres = node.GetSolutionStepValue(
                            KratosMultiphysics.PRESSURE)
                        v_x = node.GetSolutionStepValue(
                            KratosMultiphysics.VELOCITY_X)
                        v_y = node.GetSolutionStepValue(
                            KratosMultiphysics.VELOCITY_Y)
                        v_z = node.GetSolutionStepValue(
                            KratosMultiphysics.VELOCITY_Z)
                        ref_file.write("{0}, {1}, {2}, {3}, {4}\n".format(
                            node.Id, pres, v_x, v_y, v_z))
            else:
                with open(self.reference_file + '.csv', 'r') as reference_file:
                    reference_file.readline()  # skip header
                    line = reference_file.readline()

                    for node in fluid_model_part.Nodes:
                        values = [
                            float(i) for i in line.rstrip('\n ').split(',')
                        ]
                        reference_pres = values[1]
                        reference_v_x = values[2]
                        reference_v_y = values[3]
                        reference_v_z = values[4]

                        pres = node.GetSolutionStepValue(
                            KratosMultiphysics.PRESSURE)
                        v_x = node.GetSolutionStepValue(
                            KratosMultiphysics.VELOCITY_X)
                        v_y = node.GetSolutionStepValue(
                            KratosMultiphysics.VELOCITY_Y)
                        v_z = node.GetSolutionStepValue(
                            KratosMultiphysics.VELOCITY_Z)
                        self.assertAlmostEqual(reference_v_x,
                                               v_x,
                                               delta=self.check_tolerance)
                        self.assertAlmostEqual(reference_v_y,
                                               v_y,
                                               delta=self.check_tolerance)
                        self.assertAlmostEqual(reference_v_z,
                                               v_z,
                                               delta=self.check_tolerance)
                        self.assertAlmostEqual(reference_pres,
                                               pres,
                                               delta=self.check_tolerance)

                        line = reference_file.readline()
                    if line != '':  # If we did not reach the end of the reference file
                        self.fail(
                            "The number of nodes in the mdpa is smaller than the number of nodes in the output file"
                        )
Esempio n. 2
0
class EmbeddedVelocityInletEmulationTest(UnitTest.TestCase):
    def testEmbeddedVelocityInletEmulationSymbolic2D(self):
        self.print_output = False
        self.check_tolerance = 1.0e-10
        self.print_reference_values = False
        self.work_folder = "EmbeddedVelocityInletEmulationTest"
        self.reference_file = "reference_embedded_symbolic_navier_stokes"
        self.settings = "EmbeddedVelocityInletEmulationTest.json"
        self.formulation_settings = KratosMultiphysics.Parameters(r'''{
            "element_type"        : "embedded_symbolic_navier_stokes",
            "is_slip"             : false,
            "slip_length"         : 1.0e8,
            "penalty_coefficient" : 10.0,
            "dynamic_tau"         : 1.0
        }''')
        self.ExecuteTest()

    def testEmbeddedVelocityInletEmulationEmbedded2D(self):
        self.print_output = False
        self.check_tolerance = 1.0e-10
        self.print_reference_values = False
        self.work_folder = "EmbeddedVelocityInletEmulationTest"
        self.reference_file = "reference_embedded_navier_stokes"
        self.settings = "EmbeddedVelocityInletEmulationTest.json"
        self.formulation_settings = KratosMultiphysics.Parameters(r'''{
            "element_type": "embedded_navier_stokes",
            "is_slip": false,
            "slip_length": 1.0e8,
            "penalty_coefficient": 10.0,
            "dynamic_tau": 1.0
        }''')
        self.ExecuteTest()

    def ExecuteTest(self):
        self.buildSimulation()
        self.runTest()
        self.tearDown()
        self.checkResults()

    def buildSimulation(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            with open(self.settings, 'r') as parameter_file:
                self.ProjectParameters = KratosMultiphysics.Parameters(
                    parameter_file.read())
                self.ProjectParameters["solver_settings"][
                    "formulation"] = self.formulation_settings
                if self.print_output:
                    self.ProjectParameters[
                        "output_processes"] = self._get_output_process_settings(
                        )

        self.model = KratosMultiphysics.Model()
        self.simulation = FluidDynamicsAnalysis(self.model,
                                                self.ProjectParameters)

    def runTest(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            self.simulation.Initialize()
            self._set_inlet_emulation_simulation()
            self.simulation.RunSolutionLoop()
            self.simulation.Finalize()

    def checkResults(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            ## 2D results check
            main_model_part = self.simulation._GetSolver().main_model_part
            if (main_model_part.ProcessInfo[KratosMultiphysics.DOMAIN_SIZE] ==
                    2):
                if self.print_reference_values:
                    with open(self.reference_file + '.csv', 'w') as ref_file:
                        ref_file.write(
                            "#ID, VELOCITY_X, VELOCITY_Y, PRESSURE\n")
                        for node in main_model_part.Nodes:
                            vel = node.GetSolutionStepValue(
                                KratosMultiphysics.VELOCITY, 0)
                            pres = node.GetSolutionStepValue(
                                KratosMultiphysics.PRESSURE, 0)
                            ref_file.write("{0}, {1}, {2}, {3}\n".format(
                                node.Id, vel[0], vel[1], pres))
                else:
                    with open(self.reference_file + '.csv',
                              'r') as reference_file:
                        reference_file.readline()  # skip header
                        line = reference_file.readline()

                        for node in main_model_part.Nodes:
                            values = [
                                float(i) for i in line.rstrip('\n ').split(',')
                            ]
                            reference_vel_x = values[1]
                            reference_vel_y = values[2]
                            reference_pres = values[3]

                            velocity = node.GetSolutionStepValue(
                                KratosMultiphysics.VELOCITY)
                            pressure = node.GetSolutionStepValue(
                                KratosMultiphysics.PRESSURE)
                            self.assertAlmostEqual(reference_vel_x,
                                                   velocity[0],
                                                   delta=self.check_tolerance)
                            self.assertAlmostEqual(reference_vel_y,
                                                   velocity[1],
                                                   delta=self.check_tolerance)
                            self.assertAlmostEqual(reference_pres,
                                                   pressure,
                                                   delta=self.check_tolerance)

                            line = reference_file.readline()
                        if line != '':  # If we did not reach the end of the reference file
                            self.fail(
                                "The number of nodes in the mdpa is smaller than the number of nodes in the output file"
                            )

    def tearDown(self):
        with UnitTest.WorkFolderScope(self.work_folder, __file__):
            KratosUtilities.DeleteFileIfExisting(
                self.ProjectParameters["solver_settings"]
                ["model_import_settings"]["input_filename"].GetString() +
                '.time')

    def _set_inlet_emulation_simulation(self):
        main_model_part = self.simulation._GetSolver().main_model_part
        # Set distance field
        x_level_set = 2.5
        for node in main_model_part.Nodes:
            node.SetSolutionStepValue(KratosMultiphysics.DISTANCE,
                                      node.X - x_level_set)

        # Set embedded velocity in cut elements and deactivate negative distance elements
        embedded_velocity = KratosMultiphysics.Vector(3)
        embedded_velocity[0] = 1.0
        embedded_velocity[1] = 0.0
        embedded_velocity[2] = 0.0
        deactivate_negative_elems = True
        for elem in main_model_part.Elements:
            n_pos = 0
            n_neg = 0
            for node in elem.GetNodes():
                if node.GetSolutionStepValue(
                        KratosMultiphysics.DISTANCE) < 0.0:
                    n_neg += 1
                else:
                    n_pos += 1
            if (n_neg != 0 and n_pos != 0):
                for node in elem.GetNodes():
                    node.SetValue(KratosMultiphysics.EMBEDDED_VELOCITY,
                                  embedded_velocity)
            elif (n_neg == len(elem.GetNodes()) and deactivate_negative_elems):
                elem.Set(KratosMultiphysics.ACTIVE, False)

    def _get_output_process_settings(self):
        return KratosMultiphysics.Parameters(r'''{
            "gid_output" : [{
                "python_module" : "gid_output_process",
                "kratos_module" : "KratosMultiphysics",
                "process_name"  : "GiDOutputProcess",
                "help"          : "This process writes postprocessing files for GiD",
                "Parameters"    : {
                    "model_part_name"        : "FluidModelPart.fluid_computational_model_part",
                    "output_name"            : "embedded_velocity_inlet_emulation_test",
                    "postprocess_parameters" : {
                        "result_file_configuration" : {
                            "gidpost_flags"       : {
                                "GiDPostMode"           : "GiD_PostBinary",
                                "WriteDeformedMeshFlag" : "WriteDeformed",
                                "WriteConditionsFlag"   : "WriteConditions",
                                "MultiFileFlag"         : "SingleFile"
                            },
                            "file_label"          : "time",
                            "output_control_type" : "step",
                            "output_frequency"    : 1.0,
                            "body_output"         : true,
                            "node_output"         : false,
                            "skin_output"         : false,
                            "plane_output"        : [],
                            "nodal_results"       : ["VELOCITY","PRESSURE","DISTANCE"],
                            "gauss_point_results" : []
                        },
                        "point_data_configuration"  : []
                    }
                }
            }]
        }''')