void TestCaMonolayerWithDeath() throw (Exception)
    {
        EXIT_IF_PARALLEL;

        // Reset the maximum cell ID to zero (to account for previous tests)
        CellId::ResetMaxCellId();

        // Create a simple 2D PottsMesh
        PottsMeshGenerator<2> generator(10, 0, 0, 10, 0, 0);
        PottsMesh<2>* p_mesh = generator.GetMesh();

        // Create cells
        std::vector<CellPtr> cells;
        MAKE_PTR(DifferentiatedCellProliferativeType, p_diff_type);
        CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator;
        cells_generator.GenerateBasicRandom(cells, p_mesh->GetNumNodes(), p_diff_type);

        // Specify where cells lie
        std::vector<unsigned> location_indices;
        for (unsigned index=0; index<p_mesh->GetNumNodes(); index++)
        {
            location_indices.push_back(index);
        }
        TS_ASSERT_EQUALS(location_indices.size(),p_mesh->GetNumNodes());

        // Create cell population
        CaBasedCellPopulation<2> cell_population(*p_mesh, cells, location_indices);

        // Set up cell-based simulation
        OnLatticeSimulation<2> simulator(cell_population);
        simulator.SetOutputDirectory("TestCaMonolayerWithDeath");
        simulator.SetDt(0.1);
        simulator.SetEndTime(0.1); // only one step as we only care about cells being killed

        // No movement rule as only care about cell death

        // Add a cell killer that will kill all cells in the top half of the domain
        c_vector<double,2> point = zero_vector<double>(2);
        point[1] = 4.5;
        c_vector<double,2> normal = unit_vector<double>(2,1);
        MAKE_PTR_ARGS(PlaneBasedCellKiller<2>, p_killer, (&cell_population, point, normal)); // v>4.5
        simulator.AddCellKiller(p_killer);

        // Run simulation
        simulator.Solve();

        // Check the number of cells
        TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 50u);

        // Test no deaths and some births
        TS_ASSERT_EQUALS(simulator.GetNumBirths(), 0u);
        TS_ASSERT_EQUALS(simulator.GetNumDeaths(), 50u);

        // Check that cells above y=5.5 (i.e. above index 50) have been killed and removed
        for (unsigned i=0; i<simulator.rGetCellPopulation().GetNumNodes(); i++)
        {
            if (i < 50)
            {
                TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetCellUsingLocationIndex(i)->GetCellId(),i);
            }
            else
            {
                TS_ASSERT_THROWS_THIS(simulator.rGetCellPopulation().GetCellUsingLocationIndex(i),
                                      "Location index input argument does not correspond to a Cell");
            }
        }
    }
    void TestCaSingleCellRandomMovement() throw (Exception)
    {
        EXIT_IF_PARALLEL;

        // Specify the timestep and size of domain to let us calculate the probabilities of movement
        double delta_t = 1;
        double diffusion_parameter = 0.1;
        unsigned num_runs = 2000;
        unsigned location_of_cell[9] = {0,0,0,0,0,0,0,0,0};

        // Create a simple 2D PottsMesh
        PottsMeshGenerator<2> generator(3, 0, 0, 3, 0, 0);
        PottsMesh<2>* p_mesh = generator.GetMesh();

        // Create cells
        std::vector<CellPtr> cells;
        MAKE_PTR(DifferentiatedCellProliferativeType, p_diff_type);
        CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator;
        cells_generator.GenerateBasicRandom(cells, 1, p_diff_type);

        // Specify where cells lie
        std::vector<unsigned> location_indices;
        location_indices.push_back(4);

        // Create cell population
        CaBasedCellPopulation<2> cell_population(*p_mesh, cells, location_indices);

        // Set up cell-based simulation
        OnLatticeSimulation<2> simulator(cell_population);
        std::string output_directory = "TestCaSingleCellRandomMovement";
        simulator.SetOutputDirectory(output_directory);
        simulator.SetDt(delta_t);
        simulator.SetEndTime(delta_t);

        // Add update rule
        MAKE_PTR(DiffusionCaUpdateRule<2>, p_diffusion_update_rule);
        p_diffusion_update_rule->SetDiffusionParameter(diffusion_parameter);
        simulator.AddCaUpdateRule(p_diffusion_update_rule);

        for (unsigned i=1; i<=num_runs; i++)
        {
            simulator.SetEndTime(delta_t*i);

            // Run simulation
            simulator.Solve();

            TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 1u);
            AbstractCellPopulation<2>::Iterator cell_iter = simulator.rGetCellPopulation().Begin();

            unsigned cell_location = simulator.rGetCellPopulation().GetLocationIndexUsingCell(*cell_iter);
            TS_ASSERT_LESS_THAN(cell_location, 9u);

            location_of_cell[cell_location]++;

            // Reset the position of the cell
            simulator.rGetCellPopulation().MoveCellInLocationMap(*cell_iter, cell_location, 4u);

            TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetLocationIndexUsingCell(*cell_iter), 4u);
        }

        // Check that we still have only one cell
        TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 1u);
        TS_ASSERT_EQUALS(simulator.GetNumBirths(), 0u);
        TS_ASSERT_EQUALS(simulator.GetNumDeaths(), 0u);

        ///\todo Check that the cell is moving correctly
        double probability_of_occupation[9];
        for (unsigned i=0; i<9; i++)
        {
            probability_of_occupation[i] = (double) location_of_cell[i]/(double) num_runs;
        }

        // Note that these simulations are stochastic and so the tolerances are relatively loose
        TS_ASSERT_DELTA(probability_of_occupation[0], diffusion_parameter*delta_t/4.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[1], diffusion_parameter*delta_t/2.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[2], diffusion_parameter*delta_t/4.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[3], diffusion_parameter*delta_t/2.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[4], 1.0 - 3.0*diffusion_parameter*delta_t, 2e-2);
        TS_ASSERT_DELTA(probability_of_occupation[5], diffusion_parameter*delta_t/2.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[6], diffusion_parameter*delta_t/4.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[7], diffusion_parameter*delta_t/2.0, 1e-2);
        TS_ASSERT_DELTA(probability_of_occupation[8], diffusion_parameter*delta_t/4.0, 1e-2);

        // For coverage
        simulator.RemoveAllCaUpdateRules();
    }
    void TestCaMonolayerWithBirth() throw (Exception)
    {
        EXIT_IF_PARALLEL;

        // Create a simple 2D PottsMesh
        PottsMeshGenerator<2> generator(10, 0, 0, 10, 0, 0);
        PottsMesh<2>* p_mesh = generator.GetMesh();

        // Create cells
        std::vector<CellPtr> cells;
        MAKE_PTR(StemCellProliferativeType, p_stem_type);
        CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator;
        cells_generator.GenerateBasicRandom(cells, 2, p_stem_type);

        // Specify where the cells lie
        std::vector<unsigned> location_indices;
        location_indices.push_back(50);
        location_indices.push_back(51);

        // Create cell population
        CaBasedCellPopulation<2> cell_population(*p_mesh, cells, location_indices);

        // Set up cell-based simulation
        OnLatticeSimulation<2> simulator(cell_population);
        std::string output_directory = "TestCaMonolayerWithBirth";
        simulator.SetOutputDirectory(output_directory);
        simulator.SetDt(0.1);
        simulator.SetEndTime(40);

        // Adding update rule(s)
        MAKE_PTR(DiffusionCaUpdateRule<2u>, p_diffusion_update_rule);
        p_diffusion_update_rule->SetDiffusionParameter(0.5);
        simulator.AddCaUpdateRule(p_diffusion_update_rule);
        simulator.SetOutputDivisionLocations(true);

        // Run simulation
        simulator.Solve();

        // Check the number of cells
        TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 12u); ///\todo #2066 Check this!

        // Test no deaths and some births
        TS_ASSERT_EQUALS(simulator.GetNumBirths(), 10u); ///\todo #2066 Check this!
        TS_ASSERT_EQUALS(simulator.GetNumDeaths(), 0u);

        // Now remove the update rules and check that only birth happens when the simulator runs again
        simulator.RemoveAllPottsUpdateRules();
        simulator.SetEndTime(50);
        simulator.Solve();

        TS_ASSERT_EQUALS(simulator.GetNumDeaths(), 0u);
        TS_ASSERT_EQUALS(simulator.GetNumBirths(), 20u);
        TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 22u);

#ifdef CHASTE_VTK
        // Test that the VTK writer has produced a file
        OutputFileHandler output_file_handler(output_directory, false);
        std::string results_dir = output_file_handler.GetOutputDirectoryFullPath();

        // Initial condition file
        FileFinder vtk_file(results_dir + "results_from_time_0/results_0.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file.Exists());

        // Final file
        FileFinder vtk_file2(results_dir + "results_from_time_0/results_400.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file2.Exists());
#endif //CHASTE_VTK
    }
Example #4
0
int main()
{
    int winWidth = 500;
    int winHeight = 500;

    // create the window
    sf::Window window(sf::VideoMode(winWidth, winHeight), "Fluid Simulation", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    // Limit to 60 fps
    window.setFramerateLimit(60);

    // Fluid sim init
    
#ifndef FLUID3D
    FluidSim2D simulator(192 / 2, 108 / 2, 0.1, 0.0, 0.0);
#else
    FluidSim3D simulator(24, 24, 24, 0.1, 0.0, 0.0, false);
#endif

    // OpenGL initialization
    glClearColor(0, 0, 0, 0);
    glEnable(GL_TEXTURE_3D);

    // run the main loop
    bool running = true;
    while (running)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
            }
            else if (event.type == sf::Event::MouseWheelMoved)
            {
#ifdef FLUID3D
                simulator.dist += event.mouseWheel.delta * -0.1;
#endif
            }
        }

        simulator.reinit();

        // Collect UI events
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
            simulator.setSource(sf::Mouse::getPosition(window), winWidth, winHeight);
        if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
            simulator.setVelocity(sf::Mouse::getPosition(window), winWidth, winHeight, 5.0f);

#ifdef FLUID3D
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
            simulator.yrot += 1;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            simulator.yrot -= 1;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            simulator.xrot += 1;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            simulator.xrot -= 1;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
            simulator.bounds = !simulator.bounds;
#endif

        simulator.simulate();

        simulator.draw(winWidth, winHeight);

        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return 0;
}
Example #5
0
 PointProcess CosinePoissonProcess::simulate(
     RNG &rng, const DateTime &t0, const DateTime &t1,
     std::function<Data *()> mark_generator) const {
   BoundedPoissonProcessSimulator simulator(this, 2 * lambda());
   return simulator.simulate(rng, t0, t1, mark_generator);
 }
    void TestPottsSpheroidCellSorting() throw (Exception)
    {
        EXIT_IF_PARALLEL;    // Potts simulations don't work in parallel because they depend on NodesOnlyMesh for writing.

        // Create a simple 3D PottsMesh
        unsigned domain_size = 10;
        unsigned element_number = 4;
        unsigned element_size = 2;

        PottsMeshGenerator<3> generator(domain_size, element_number, element_size, domain_size, element_number, element_size, domain_size, element_number, element_size);
        PottsMesh<3>* p_mesh = generator.GetMesh();

        // Create cells
        std::vector<CellPtr> cells;
        MAKE_PTR(DifferentiatedCellProliferativeType, p_diff_type);
        CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 3> cells_generator;
        cells_generator.GenerateBasicRandom(cells, p_mesh->GetNumElements(), p_diff_type);

        // Randomly label some cells
        boost::shared_ptr<AbstractCellProperty> p_label(CellPropertyRegistry::Instance()->Get<CellLabel>());
        RandomlyLabelCells(cells, p_label, 0.5);

        // Create cell population
        PottsBasedCellPopulation<3> cell_population(*p_mesh, cells);
        cell_population.AddCellPopulationCountWriter<CellMutationStatesCountWriter>(); // So outputs the labelled cells

        // Set up cell-based simulation
        OnLatticeSimulation<3> simulator(cell_population);
        simulator.SetOutputDirectory("TestPotts3DCellSorting");
        simulator.SetDt(0.1);
        simulator.SetEndTime(1.0);

        // Create update rules and pass to the simulation
        MAKE_PTR(VolumeConstraintPottsUpdateRule<3>, p_volume_constraint_update_rule);
        p_volume_constraint_update_rule->SetMatureCellTargetVolume(element_size*element_size*element_size);
        p_volume_constraint_update_rule->SetDeformationEnergyParameter(0.2);
        simulator.AddPottsUpdateRule(p_volume_constraint_update_rule);

        MAKE_PTR(DifferentialAdhesionPottsUpdateRule<3>, p_differential_adhesion_update_rule);
        p_differential_adhesion_update_rule->SetLabelledCellLabelledCellAdhesionEnergyParameter(0.16);
        p_differential_adhesion_update_rule->SetLabelledCellCellAdhesionEnergyParameter(0.11);
        p_differential_adhesion_update_rule->SetCellCellAdhesionEnergyParameter(0.02);
        p_differential_adhesion_update_rule->SetLabelledCellBoundaryAdhesionEnergyParameter(0.16);
        p_differential_adhesion_update_rule->SetCellBoundaryAdhesionEnergyParameter(0.16);
        simulator.AddPottsUpdateRule(p_differential_adhesion_update_rule);

        // Run simulation
        simulator.Solve();

        // Check that the same number of cells
        TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 64u);

        // Test no births or deaths
        TS_ASSERT_EQUALS(simulator.GetNumBirths(), 0u);
        TS_ASSERT_EQUALS(simulator.GetNumDeaths(), 0u);
#ifdef CHASTE_VTK
        // Test that VTK writer has produced some files
        OutputFileHandler handler("TestPotts3DCellSorting", false);
        std::string results_dir = handler.GetOutputDirectoryFullPath();

        // Initial condition file
        FileFinder vtk_file(results_dir + "results_from_time_0/results_0.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file.Exists());

        // Final file
        FileFinder vtk_file2(results_dir + "results_from_time_0/results_10.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file2.Exists());

        // Check that the second VTK file for Potts specific data (element IDs)
        VtkMeshReader<3,3> vtk_reader(results_dir + "results_from_time_0/results_10.vtu");
        std::vector<double> cell_types;
        vtk_reader.GetPointData("Cell types", cell_types);
        TS_ASSERT_EQUALS(cell_types.size(), 1000u);

        // The cell types are between -1 and 5. Check the maximum
        TS_ASSERT_DELTA(*max_element(cell_types.begin(), cell_types.end()), 5.0, 1e-12);

        std::vector<double> cell_ids;
        vtk_reader.GetPointData("Cell IDs", cell_ids);
        // Prior to release 3.2 this was called "Element index".
        // It is changed to cell_id as this is preferable for VTK output.
        TS_ASSERT_EQUALS(cell_ids.size(), 1000u);
        TS_ASSERT_DELTA(*max_element(cell_ids.begin(), cell_ids.end()), 63.0, 1e-12);

 #endif //CHASTE_VTK
    }
    void TestPottsMonolayerCellSortingPeriodic() throw (Exception)
    {
        EXIT_IF_PARALLEL;    // Potts simulations don't work in parallel because they depend on NodesOnlyMesh for writing.

        // Create a simple 2D PottsMesh
        PottsMeshGenerator<2> generator(20, 5, 4, 20, 5, 4, 1, 1, 1, false, true, true); // Periodic in x and y
        PottsMesh<2>* p_mesh = generator.GetMesh();

        // Create cells
        std::vector<CellPtr> cells;
        MAKE_PTR(DifferentiatedCellProliferativeType, p_diff_type);
        CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator;
        cells_generator.GenerateBasicRandom(cells, p_mesh->GetNumElements(), p_diff_type);

        // Randomly label some cells
        boost::shared_ptr<AbstractCellProperty> p_label(CellPropertyRegistry::Instance()->Get<CellLabel>());
        RandomlyLabelCells(cells, p_label, 0.5);

        // Create cell population
        PottsBasedCellPopulation<2> cell_population(*p_mesh, cells);
        cell_population.AddCellPopulationCountWriter<CellMutationStatesCountWriter>(); // So outputs the labelled cells

        // Set up cell-based simulation
        OnLatticeSimulation<2> simulator(cell_population);
        simulator.SetOutputDirectory("TestPottsCellSortingPeriodic");
        simulator.SetDt(0.1);
        simulator.SetEndTime(1.0);

        // Create update rules and pass to the simulation
        MAKE_PTR(VolumeConstraintPottsUpdateRule<2>, p_volume_constraint_update_rule);
        p_volume_constraint_update_rule->SetMatureCellTargetVolume(16);
        p_volume_constraint_update_rule->SetDeformationEnergyParameter(0.2);
        simulator.AddPottsUpdateRule(p_volume_constraint_update_rule);

        MAKE_PTR(DifferentialAdhesionPottsUpdateRule<2>, p_differential_adhesion_update_rule);
        p_differential_adhesion_update_rule->SetLabelledCellLabelledCellAdhesionEnergyParameter(0.16);
        p_differential_adhesion_update_rule->SetLabelledCellCellAdhesionEnergyParameter(0.11);
        p_differential_adhesion_update_rule->SetCellCellAdhesionEnergyParameter(0.02);
        p_differential_adhesion_update_rule->SetLabelledCellBoundaryAdhesionEnergyParameter(0.16);
        p_differential_adhesion_update_rule->SetCellBoundaryAdhesionEnergyParameter(0.16);
        simulator.AddPottsUpdateRule(p_differential_adhesion_update_rule);

        // Run simulation
        simulator.Solve();

        // Check that the same number of cells
        TS_ASSERT_EQUALS(simulator.rGetCellPopulation().GetNumRealCells(), 25u);

        // Test no births or deaths
        TS_ASSERT_EQUALS(simulator.GetNumBirths(), 0u);
        TS_ASSERT_EQUALS(simulator.GetNumDeaths(), 0u);
#ifdef CHASTE_VTK
        //Test that VTK writer has produced some files
        OutputFileHandler handler("TestPottsCellSortingPeriodic", false);
        std::string results_dir = handler.GetOutputDirectoryFullPath();
        // Initial condition file
        FileFinder vtk_file_1(results_dir + "results_from_time_0/results_0.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file_1.Exists());
        FileFinder vtk_file_2(results_dir + "results_from_time_0/outlines_0.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file_2.Exists());

        // Final file
        FileFinder vtk_file_3(results_dir + "results_from_time_0/results_10.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file_3.Exists());
        FileFinder vtk_file_4(results_dir + "results_from_time_0/outlines_10.vtu", RelativeTo::Absolute);
        TS_ASSERT(vtk_file_4.Exists());
 #endif //CHASTE_VTK
    }
    //______________________________________________________________
    void TabDemoWidget::benchmark( void )
    {
        if( !isVisible() ) return;

        if( true )
        {
            simulator().selectComboBoxItem( ui.tabPositionComboBox, 1 );
            simulator().selectComboBoxItem( ui.tabPositionComboBox, 2 );
            simulator().selectComboBoxItem( ui.tabPositionComboBox, 3 );
            simulator().selectComboBoxItem( ui.tabPositionComboBox, 0 );

            simulator().selectComboBoxItem( ui.textPositionComboBox, 0 );
            simulator().selectComboBoxItem( ui.textPositionComboBox, 2 );
            simulator().selectComboBoxItem( ui.textPositionComboBox, 1 );
        }

        if( true )
        {

            simulator().click( ui.documentModeCheckBox );
            simulator().click( ui.documentModeCheckBox );

            simulator().click( ui.cornerWidgetsCheckBox );
            simulator().click( ui.cornerWidgetsCheckBox );

            simulator().click( ui.tabBarVisibilityCheckBox );
            simulator().click( ui.tabBarVisibilityCheckBox );
        }

        if( true )
        {
            simulator().selectTab( ui.tabWidget, 1 );
            simulator().selectTab( ui.tabWidget, 2 );
            simulator().selectTab( ui.tabWidget, 3 );
            simulator().selectTab( ui.tabWidget, 0 );
        }

        // run
        simulator().run();

    }