def test_pressure_boundary_conditions(self):
     self.assertDictEqual(
         create_openfoam_wrapper(self.openfoam_model).BC[CUBA.PRESSURE], {
             'inlet': ('fixedValue', 0.008),
             'outlet': ('fixedValue', 0.0),
             'walls': 'zeroGradient',
             'frontAndBack': 'empty'
         })
 def test_velocity_boundary_conditions(self):
     self.assertDictEqual(
         create_openfoam_wrapper(self.openfoam_model).BC[CUBA.VELOCITY], {
             'inlet': 'zeroGradient',
             'outlet': 'zeroGradient',
             'walls': ('fixedValue', [0, 0, 0]),
             'frontAndBack': 'empty'
         })
Beispiel #3
0
def run_calc(global_settings,
             openfoam_settings,
             liggghts_settings,
             progress_callback,
             event_lock=None):
    """ Main routine which creates the wrappers and run the calculation

    Parameters
    ----------
    global_settings : GlobalParametersModel
        The trait model containing the global parameters of the calculation
    openfoam_settings : OpenfoamModel
        The trait model containing the Openfoam parameters
    liggghts_settings : LiggghtsModel
        The trait model containing the Liggghts parameters
    progress_callback
        A callback function which will return the progress of the computation
        which will be called with the progress state of the calculation as an
        integer in the range [0, 100]
    event_lock: threading.Event
        An event to trigger the continuation of the calculation so that
        the callback routine has a chance for copying the datasets.
        The callback routine must .set() the event, otherwise the
        calculation will be suspended and eventually timeout.
        The default None disables the check and let the evaluation continues
        to the end.

    Returns
    -------
    datasets:
        A tuple containing the datasets from openfoam and ligghts.
    """
    # Create Openfoam wrapper
    openfoam_wrapper = create_openfoam_wrapper(openfoam_settings)

    density = openfoam_wrapper.SP[CUBA.DENSITY]
    viscosity = openfoam_wrapper.SP[CUBA.DYNAMIC_VISCOSITY]

    channel_size = [
        openfoam_settings.channel_size_x, openfoam_settings.channel_size_y,
        openfoam_settings.channel_size_z
    ]
    num_grid = [
        openfoam_settings.num_grid_x, openfoam_settings.num_grid_y,
        openfoam_settings.num_grid_z
    ]

    openfoam_mesh = create_openfoam_mesh(openfoam_wrapper, openfoam_settings)

    # Create Liggghts wrapper
    liggghts_wrapper = create_liggghts_wrapper(liggghts_settings)

    flow_dataset, wall_dataset = create_liggghts_datasets(liggghts_settings)

    liggghts_wrapper.add_dataset(flow_dataset)
    liggghts_wrapper.add_dataset(wall_dataset)

    flow_dataset = liggghts_wrapper.get_dataset(flow_dataset.name)

    # Generate cell list
    cellmat = {}
    index = np.zeros(3, dtype=np.int)
    gridsize = [
        channel_size[0] / num_grid[0], channel_size[1] / num_grid[1],
        channel_size[2] / num_grid[2]
    ]

    for cell in openfoam_mesh.iter_cells():
        lln = [channel_size[0] * 2, channel_size[1] * 2, channel_size[2] * 2]
        for k in range(8):
            for i in range(3):
                if openfoam_mesh.get_point(
                        cell.points[k]).coordinates[i] < lln[i]:
                    lln[i] = openfoam_mesh.get_point(
                        cell.points[k]).coordinates[i]

        for i in range(0, 3):
            index[i] = round(lln[i] / gridsize[i])

        cellmat[index[0], index[1], index[2]] = cell.uid

    # Main loop

    datasets = None
    # Repeating OF calculation several times with modified pressure drop last
    # result from previous iteration as input for new iteration
    for numrun in xrange(global_settings.num_iterations):
        # Perform Openfoam calculations
        openfoam_wrapper.run()

        # Compute relative velocity & drag force
        for particle in flow_dataset.iter_particles():
            testpoint = particle.coordinates
            index = np.zeros(3, dtype=np.int)

            for i in range(3):
                index[i] = int(
                    math.floor(testpoint[i] /
                               (channel_size[i] / float(num_grid[i]))))

                if index[i] == num_grid[i]:
                    index[i] = 0
                elif index[i] == -1:
                    index[i] = num_grid[i] - 1

            cell_id = cellmat[index[0], index[1], index[2]]
            cell = openfoam_mesh.get_cell(cell_id)

            rel_velo = np.zeros(3, dtype=np.int)
            for i in range(3):
                rel_velo[i] = cell.data[CUBA.VELOCITY][i] - \
                    list(particle.data[CUBA.VELOCITY])[i]

            dragforce = compute_drag_force(global_settings.force_type,
                                           particle.data[CUBA.RADIUS],
                                           rel_velo, viscosity, density)

            particle.data[CUBA.EXTERNAL_APPLIED_FORCE] = tuple(dragforce)
            flow_dataset.update_particles([particle])

        # Perform Liggghts calculations
        liggghts_wrapper.run()

        datasets = (openfoam_wrapper.get_dataset('mesh'),
                    liggghts_wrapper.get_dataset('flow_particles'),
                    liggghts_wrapper.get_dataset('wall_particles'))

        if numrun % global_settings.update_frequency == 0:
            progress_callback(datasets, numrun, global_settings.num_iterations)
            if event_lock is not None:
                # We wait 10 seconds for the UI to give us the chance
                # to continue. The operation should actually be fast
                # (we just copy the datasets). If we timeout, it's likely
                # that the UI is stuck, so we want to quit.
                if not event_lock.wait(10.0):
                    return None
                event_lock.clear()

    return datasets
 def test_viscosity(self):
     self.openfoam_model.viscosity = 1.5e-3
     self.assertEqual(
         create_openfoam_wrapper(
             self.openfoam_model).SP[CUBA.DYNAMIC_VISCOSITY], 1.5e-3)
 def test_num_iterations(self):
     self.openfoam_model.num_iterations = 56
     self.assertEqual(
         create_openfoam_wrapper(
             self.openfoam_model).SP[CUBA.NUMBER_OF_TIME_STEPS], 56)
 def test_density(self):
     self.openfoam_model.density = 1005.0
     self.assertEqual(
         create_openfoam_wrapper(self.openfoam_model).SP[CUBA.DENSITY],
         1005.0)
 def test_timestep(self):
     self.openfoam_model.timestep = 1.02e-5
     self.assertEqual(
         create_openfoam_wrapper(self.openfoam_model).SP[CUBA.TIME_STEP],
         1.02e-5)
 def test_is_wrapper(self):
     self.assertIsInstance(create_openfoam_wrapper(self.openfoam_model),
                           openfoam_internal.Wrapper)
     self.openfoam_model.mode = 'io'
     self.assertIsInstance(create_openfoam_wrapper(self.openfoam_model),
                           openfoam_file_io.Wrapper)
 def test_unknown_mesh_type(self):
     self.openfoam_model.mesh_type = 'coucou'
     openfoam_wrapper = create_openfoam_wrapper(self.openfoam_model)
     with self.assertRaises(ValueError):
         create_openfoam_mesh(openfoam_wrapper, self.openfoam_model)
 def test_quad_mesh_creation(self):
     self.openfoam_model.mesh_type = 'quad'
     openfoam_wrapper = create_openfoam_wrapper(self.openfoam_model)
     create_openfoam_mesh(openfoam_wrapper, self.openfoam_model)
 def test_block_mesh_creation(self):
     openfoam_wrapper = create_openfoam_wrapper(self.openfoam_model)
     create_openfoam_mesh(openfoam_wrapper, self.openfoam_model)