Esempio n. 1
0
    /*
     * We are checking that the CellPtrs work with the Wnt
     * cell-cycle models here. This just tests the set-up and checks that
     * the functions can all be called (not what they return).
     *
     * For more in depth tests see TestNightlyCellPtr.hpp
     * (these test that the cell cycle times are correct for the
     * various mutant cells)
     */
    void TestWntMutantVariantsAndLabelling() throw(Exception)
    {
        SimulationTime* p_simulation_time = SimulationTime::Instance();

        unsigned num_steps = 10;
        p_simulation_time->SetEndTimeAndNumberOfTimeSteps(1.0, num_steps+1);

        double wnt_stimulus = 1.0;
        WntConcentration<2>::Instance()->SetConstantWntValueForTesting(wnt_stimulus);

        boost::shared_ptr<AbstractCellProperty> p_wt_state(CellPropertyRegistry::Instance()->Get<WildTypeCellMutationState>());
        boost::shared_ptr<AbstractCellProperty> p_apc_one_hit_state(CellPropertyRegistry::Instance()->Get<ApcOneHitCellMutationState>());
        boost::shared_ptr<AbstractCellProperty> p_apc_two_hit_state(CellPropertyRegistry::Instance()->Get<ApcTwoHitCellMutationState>());
        boost::shared_ptr<AbstractCellProperty> p_bcat_one_hit_state(CellPropertyRegistry::Instance()->Get<BetaCateninOneHitCellMutationState>());
        boost::shared_ptr<AbstractCellProperty> p_label(CellPropertyRegistry::Instance()->Get<CellLabel>());
        boost::shared_ptr<AbstractCellProperty> p_transit_type(CellPropertyRegistry::Instance()->Get<TransitCellProliferativeType>());

        WntCellCycleModel* p_cell_cycle_model1 = new WntCellCycleModel();
        p_cell_cycle_model1->SetDimension(2);
        CellPtr p_wnt_cell(new Cell(p_apc_one_hit_state, p_cell_cycle_model1));
        p_wnt_cell->SetCellProliferativeType(p_transit_type);
        p_wnt_cell->InitialiseCellCycleModel();

        WntCellCycleModel* p_cell_cycle_model2 = new WntCellCycleModel();
        p_cell_cycle_model2->SetDimension(2);
        CellPtr p_wnt_cell2(new Cell(p_bcat_one_hit_state, p_cell_cycle_model2));
        p_wnt_cell2->SetCellProliferativeType(p_transit_type);
        p_wnt_cell2->InitialiseCellCycleModel();

        WntCellCycleModel* p_cell_cycle_model3 = new WntCellCycleModel();
        p_cell_cycle_model3->SetDimension(2);
        CellPtr p_wnt_cell3(new Cell(p_apc_two_hit_state, p_cell_cycle_model3));
        p_wnt_cell3->SetCellProliferativeType(p_transit_type);
        p_wnt_cell3->InitialiseCellCycleModel();

        WntCellCycleModel* p_cell_cycle_model4 = new WntCellCycleModel();
        p_cell_cycle_model4->SetDimension(2);
        CellPropertyCollection collection;
        collection.AddProperty(p_label);
        CellPtr p_wnt_cell4(new Cell(p_wt_state, p_cell_cycle_model4, false, collection));
        p_wnt_cell4->SetCellProliferativeType(p_transit_type);
        p_wnt_cell4->InitialiseCellCycleModel();

        TS_ASSERT_EQUALS(p_wnt_cell->ReadyToDivide(), false);
        TS_ASSERT_EQUALS(p_wnt_cell2->ReadyToDivide(), false);
        TS_ASSERT_EQUALS(p_wnt_cell3->ReadyToDivide(), false);
        TS_ASSERT_EQUALS(p_wnt_cell4->ReadyToDivide(), false);

        //Tidy up
        WntConcentration<2>::Destroy();
    }
    /*
     * EMPTYLINE
     *
     * In this test, we demonstrate how to simulate a heterotypic monolayer that incorporates
     * differential adhesion, using a vertex-based approach. This may be compared with the
     * second test in the TestRunningPottsBasedSimulationsTutorial, which implements a similar
     * simulation using a cellular Potts model.
     */
    void TestVertexBasedDifferentialAdhesionSimulation() throw (Exception)
    {
        /* First we create a regular vertex mesh. Here we choose to set the value of the cell rearrangement threshold. */
        HoneycombVertexMeshGenerator generator(5, 5);
        MutableVertexMesh<2,2>* p_mesh = generator.GetMesh();
        p_mesh->SetCellRearrangementThreshold(0.1);

        /* We then create some cells using the helper class {{{CellsGenerator}}}. Note that in this simulation
         * the cells are all differentiated, and thus no cell division occurs; if we wished, we could modify
         * the three lines below in a straightforward manner to incorporate cell proliferation and investigate
         * the effect of this on the cell sorting process. */
        std::vector<CellPtr> cells;
        MAKE_PTR(DifferentiatedCellProliferativeType, p_diff_type);
        CellsGenerator<FixedDurationGenerationBasedCellCycleModel, 2> cells_generator;
        cells_generator.GenerateBasic(cells, p_mesh->GetNumElements(), std::vector<unsigned>(), p_diff_type);

        /* Using the vertex mesh and cells, we create a cell-based population object, and specify which results to
         * output to file. */
        VertexBasedCellPopulation<2> cell_population(*p_mesh, cells);
        cell_population.AddCellPopulationCountWriter<CellMutationStatesCountWriter>();
        cell_population.AddCellPopulationCountWriter<CellProliferativeTypesCountWriter>();
        cell_population.AddCellPopulationCountWriter<CellProliferativePhasesCountWriter>();
        cell_population.AddCellWriter<CellProliferativePhasesWriter>();
        cell_population.AddCellWriter<CellAgesWriter>();
        cell_population.AddCellWriter<CellVolumesWriter>();

        /* We randomly label some cells using the cell property {{{CellLabel}}}. We begin by creating a shared pointer to
         * this cell property using the helper singleton {{{CellPropertyRegistry}}}. We then loop over the cells and label
         * each cell independently with probability 0.5. Note that since the cells have been passed to the
         * {{{VertexBasedCellPopulation}}} object, the vector {{{cells}}} above is now empty, so we must use the
         * {{{Iterator}}} to loop over cells. */
         boost::shared_ptr<AbstractCellProperty> p_label(CellPropertyRegistry::Instance()->Get<CellLabel>());
        for (AbstractCellPopulation<2>::Iterator cell_iter = cell_population.Begin();
             cell_iter != cell_population.End();
             ++cell_iter)
        {
            if (RandomNumberGenerator::Instance()->ranf() < 0.5)
            {
                cell_iter->AddCellProperty(p_label);
            }
        }

        /* We are now in a position to create and configure the cell-based simulation object.
         * We can make the simulation run for longer to see more cell sorting by increasing the end time. */
        OffLatticeSimulation<2> simulator(cell_population);
        simulator.SetOutputDirectory("TestVertexBasedDifferentialAdhesionSimulation");
        simulator.SetSamplingTimestepMultiple(10);
        simulator.SetEndTime(1.0);

        /* Next we create the differential adhesion force law. This builds upon the model of Nagai, Honda and co-workers
         * encounted in the TestRunningVertexBasedSimulationsTutorial by allowing different values of the adhesion
         * energy parameters depending on the types of two neighbouring cells. Here we interpret the 'type' of a cell
         * as whether or not it has the cell property {{{CellLabel}}}; it would be straightforward to create a similar
         * force law that took account of a cell's mutation state, for example. Having created the force law, we set the
         * values of the parameters. If the adhesion energy for two neighbouring homotypic cells is less than that of two
         * heterotypic cells, then we may expect cell sorting to occur, in which the cells of each type will tend to locally
         * aggregate over time. */
        MAKE_PTR(NagaiHondaDifferentialAdhesionForce<2>, p_force);
        p_force->SetNagaiHondaDeformationEnergyParameter(55.0);
        p_force->SetNagaiHondaMembraneSurfaceEnergyParameter(0.0);
        p_force->SetNagaiHondaCellCellAdhesionEnergyParameter(1.0);
        p_force->SetNagaiHondaLabelledCellCellAdhesionEnergyParameter(6.0);
        p_force->SetNagaiHondaLabelledCellLabelledCellAdhesionEnergyParameter(3.0);
        p_force->SetNagaiHondaCellBoundaryAdhesionEnergyParameter(12.0);
        p_force->SetNagaiHondaLabelledCellBoundaryAdhesionEnergyParameter(40.0);
        simulator.AddForce(p_force);

        /* A {{{NagaiHondaForceDifferentialAdhesionForce}}} assumes that each cell has been assigned a target area.
         * The {{{SimpleTargetAreaModifier}}} will assign and update the target areas of all cells.
         */
        MAKE_PTR(SimpleTargetAreaModifier<2>, p_growth_modifier);
        simulator.AddSimulationModifier(p_growth_modifier);

        /* Finally, we run the simulation. */
        simulator.Solve();
    }
Esempio n. 3
0
Parser::stmt_t Parser::p_stmt_toks(Lexer &lex,const Context &ctx,std::vector<Lexer::Token> *toks){
  Lexer::Token tok,tok1,tok2;
  lex >> tok;

  stmt_t res_stmt = stmt_t::nop();
  std::vector<Lexer::Token> mytoks;

  switch(tok.type){
  case Lexer::NOP:
    ppush(&mytoks,tok);
    res_stmt = stmt_t::nop(tok.pos,mytoks);
    break;
  case Lexer::READ:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      lex >> tok1;
      if(tok1.type == Lexer::REG){
        ppush(&mytoks,tok1);
        force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
        memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        res_stmt = resolve_pointer(ml,[&tok1,&tok](const memloc_t &ml){
            return stmt_t::read_assign(tok1.value,ml,tok.pos);
          },ctx,mytoks);
      }else{
        lex.putback(tok1);
        Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        force_toks(lex,Lexer::EQ,&mytoks);
        expr_t e = p_expr_toks(lex,&mytoks);
        res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
            return stmt_t::read_assert(ml,e,tok.pos);
          },ctx,mytoks);
      }
      break;
    }
  case Lexer::SYNCRD:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      lex >> tok1;
      if(tok1.type == Lexer::REG){
        ppush(&mytoks,tok1);
        force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
        memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        res_stmt = resolve_pointer(ml,[&tok1,&tok](const memloc_t &ml){
            return stmt_t::syncrd_assign(tok1.value,ml,tok.pos);
          },ctx,mytoks);
      }else{
        lex.putback(tok1);
        Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        force_toks(lex,Lexer::EQ,&mytoks);
        expr_t e = p_expr_toks(lex,&mytoks);
        res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
            return stmt_t::syncrd_assert(ml,e,tok.pos);
          },ctx,mytoks);
      }
      break;
    }
  case Lexer::WRITE:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
      force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
      expr_t e = p_expr_toks(lex,&mytoks);
      res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
          return stmt_t::write(ml,e,tok.pos);
        },ctx,mytoks);
      break;
    }
  case Lexer::SYNCWR:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
      force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
      expr_t e = p_expr_toks(lex,&mytoks);
      res_stmt = resolve_pointer(ml,[&e,&tok](const memloc_t &ml){
          return stmt_t::syncwr(ml,e,tok.pos);
        },ctx,mytoks);
      break;
    }
  case Lexer::FENCE:
    {
      ppush(&mytoks,tok);
      res_stmt = stmt_t::full_fence(tok.pos,mytoks);
      break;
    }
  case Lexer::SSFENCE:
    {
      ppush(&mytoks,tok);
      res_stmt = stmt_t::ss_fence(tok.pos,mytoks);
      break;
    }
  case Lexer::LLFENCE:
    {
      ppush(&mytoks,tok);
      res_stmt = stmt_t::ll_fence(tok.pos,mytoks);
      break;
    }
  case Lexer::CAS:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::LPAREN,&mytoks);
      Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
      force_toks(lex,Lexer::COMMA,&mytoks);
      expr_t v0(p_expr_toks(lex,&mytoks));
      force_toks(lex,Lexer::COMMA,&mytoks);
      expr_t v1(p_expr_toks(lex,&mytoks));
      force_toks(lex,Lexer::RPAREN,&mytoks);
      res_stmt = resolve_pointer(ml,[&v0,&v1,&tok](const memloc_t &ml){
          return stmt_t::cas(ml,v0,v1,tok.pos);
        },ctx,mytoks);
      break;
    }
  case Lexer::REG:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
      expr_t e = p_expr_toks(lex,&mytoks);
      res_stmt = stmt_t::assignment(tok.value,e,tok.pos,mytoks);
      break;
    }
  case Lexer::IF:
    {
      ppush(&mytoks,tok);
      bexpr_t b(p_bexpr_toks(lex,&mytoks));

      force_toks(lex,Lexer::THEN,&mytoks);
      stmt_t::labeled_stmt_t s0 = p_lstmt(lex,ctx,&mytoks);
      lex >> tok1;
      if(tok1.type == Lexer::ELSE){
        ppush(&mytoks,tok1);
        stmt_t::labeled_stmt_t s1 = p_lstmt(lex,ctx,&mytoks);
        res_stmt = stmt_t::if_stmt(b,s0,s1,tok.pos,mytoks);
      }else{
        lex.putback(tok1);
        res_stmt = stmt_t::if_stmt(b,s0,tok.pos,mytoks);
      }
      break;
    }
  case Lexer::WHILE:
    {
      ppush(&mytoks,tok);
      bexpr_t b(p_bexpr_toks(lex,&mytoks));
      force_toks(lex,Lexer::DO,&mytoks);
      stmt_t::labeled_stmt_t s = p_lstmt(lex,ctx,&mytoks);
      res_stmt = stmt_t::while_stmt(b,s,tok.pos,mytoks);
      break;
    }
  case Lexer::GOTO:
    {
      ppush(&mytoks,tok);
      Lang::label_t lbl = p_label(lex,&mytoks);
      res_stmt = stmt_t::goto_stmt(lbl,tok.pos,mytoks);
      break;
    }
  case Lexer::EITHER:
    {
      ppush(&mytoks,tok);
      Lexer::TokenPos pos0 = tok.pos;
      std::vector<stmt_t> seq;
      Lexer::Token lctok;
      Lexer::Token tok;
      lex >> lctok;
      lex.putback(lctok);
      force_toks(lex,Lexer::LCURL,&mytoks);

      seq.push_back(p_stmt_list(lex,ctx,&mytoks));
      lex >> tok;
      while(tok.type == Lexer::EITHER_OR){
        ppush(&mytoks,tok);
        seq.push_back(p_stmt_list(lex,ctx,&mytoks));
        lex >> tok;
      }
      lex.putback(tok);
      force_toks(lex,Lexer::RCURL,&mytoks,"Expected '}' at "," to match '{' at "+
                 lctok.pos.to_long_string()+".");

      res_stmt = stmt_t::either(seq,pos0);
      break;
    }
  case Lexer::LOCKED:
    {
      ppush(&mytoks,tok);
      Lexer::TokenPos pos0 = tok.pos;
      std::vector<stmt_t> seq;
      Lexer::Token lctok;
      Lexer::Token tok;
      lex >> lctok;
      if(lctok.type == Lexer::WRITE){
        ppush(&mytoks,lctok);
        force_toks(lex,Lexer::COLON,&mytoks);
        Parser::memloc_or_pointer_t ml = p_memloc_toks(lex,ctx,&mytoks);
        force_toks(lex,Lexer::ASSIGNMENT,&mytoks);
        expr_t e = p_expr_toks(lex,&mytoks);
        res_stmt = resolve_pointer(ml,[&e,&pos0](const memloc_t &ml){
            return stmt_t::locked_write(ml,e,pos0);
          },ctx,mytoks);
      }else{
        lex.putback(lctok);
        force_toks(lex,Lexer::LCURL,&mytoks);

        stmt_t s = p_stmt_list(lex,ctx,&mytoks);
        std::string cmt;
        if(!stmt_t::check_locked_invariant(s,&cmt)){
          throw new SyntaxError("Error in locked block at "+lctok.pos.to_long_string()+": "+cmt,lctok.pos);
        }
        seq.push_back(s);
        lex >> tok;
        while(tok.type == Lexer::EITHER_OR){
          ppush(&mytoks,tok);
          s = p_stmt_list(lex,ctx,&mytoks);
          if(!stmt_t::check_locked_invariant(s,&cmt)){
            throw new SyntaxError("Error in locked block at "+tok.pos.to_long_string()+": "+cmt,tok.pos);
          }
          seq.push_back(s);
          lex >> tok;
        }
        lex.putback(tok);
        force_toks(lex,Lexer::RCURL,&mytoks,"Expected '}' at "," to match '{' at "+
                   lctok.pos.to_long_string()+".");

        res_stmt = stmt_t::locked_block(seq,pos0);
      }
      break;
    }
 case Lexer::SLOCKED:
    {
      Lexer::TokenPos pos0 = tok.pos;
      force(lex,Lexer::WRITE);
      force(lex,Lexer::COLON);
      Parser::memloc_or_pointer_t ml = p_memloc(lex,ctx);
      force(lex,Lexer::ASSIGNMENT);
      expr_t e = p_expr(lex);
      return resolve_pointer(ml,[&e,&pos0](const memloc_t &ml){
          return stmt_t::slocked_write(ml,e,pos0);
        },ctx,mytoks);
    }
  case Lexer::LCURL:
    {
      ppush(&mytoks,tok);
      stmt_t sl = p_stmt_list(lex,ctx,&mytoks);
      force_toks(lex,Lexer::RCURL,&mytoks,"Expected '}' at "," to match '{' at "+
                 tok.pos.to_long_string()+".");
      res_stmt = sl;
      break;
    }
  case Lexer::ASSUME:
    {
      ppush(&mytoks,tok);
      force_toks(lex,Lexer::COLON,&mytoks);
      bexpr_t cnd = p_bexpr_toks(lex,&mytoks);
      res_stmt = stmt_t::assume(cnd,tok.pos,mytoks);
      break;
    }
  default:
    throw new SyntaxError("Expected statement at "+tok.pos.to_long_string()+".",tok.pos);
  }
  if(toks) toks->insert(toks->end(),mytoks.begin(),mytoks.end());
  return res_stmt;
}
    void TestWriteResultsToFileAndOutputCellPopulationParameters()
    {
        EXIT_IF_PARALLEL;    // Copying in parallel uses parallel NodesOnlyMesh and will therefore cause unexpected errors.

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

        std::string output_directory = "TestPottsBasedCellPopulationWriters";
        OutputFileHandler output_file_handler(output_directory, false);

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

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

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

        // For coverage, label one cell
        boost::shared_ptr<AbstractCellProperty> p_label(cell_population.GetCellPropertyRegistry()->Get<CellLabel>());
        cell_population.GetCellUsingLocationIndex(0)->AddCellProperty(p_label);

        TS_ASSERT_EQUALS(cell_population.GetIdentifier(), "PottsBasedCellPopulation-2");

        cell_population.SetCellAncestorsToLocationIndices();
        cell_population.AddCellPopulationCountWriter<CellMutationStatesCountWriter>();
        cell_population.AddCellPopulationCountWriter<CellProliferativeTypesCountWriter>();
        cell_population.AddCellPopulationCountWriter<CellProliferativePhasesCountWriter>();
        cell_population.AddCellWriter<CellProliferativePhasesWriter>();
        cell_population.AddCellWriter<CellAncestorWriter>();
        cell_population.AddCellWriter<CellAgesWriter>();
        cell_population.AddCellWriter<CellVolumesWriter>();
        cell_population.SetNumSweepsPerTimestep(5);

        TS_ASSERT_EQUALS(cell_population.GetNumSweepsPerTimestep(), 5u);

        // VTK writing needs a simulation time
        SimulationTime::Instance()->SetEndTimeAndNumberOfTimeSteps(1.0, 1);

        cell_population.OpenWritersFiles(output_file_handler);
        cell_population.WriteResultsToFiles(output_directory);
        cell_population.CloseWritersFiles();

        // Compare output with saved files of what they should look like
        std::string results_dir = output_file_handler.GetOutputDirectoryFullPath();

        FileComparison(results_dir + "results.viznodes", "cell_based/test/data/TestPottsBasedCellPopulationWriters/results.viznodes").CompareFiles();
        FileComparison(results_dir + "results.vizcelltypes", "cell_based/test/data/TestPottsBasedCellPopulationWriters/results.vizcelltypes").CompareFiles();
        FileComparison(results_dir + "results.vizancestors", "cell_based/test/data/TestPottsBasedCellPopulationWriters/results.vizancestors").CompareFiles();
        FileComparison(results_dir + "cellmutationstates.dat", "cell_based/test/data/TestPottsBasedCellPopulationWriters/cellmutationstates.dat").CompareFiles();
        FileComparison(results_dir + "cellages.dat", "cell_based/test/data/TestPottsBasedCellPopulationWriters/cellages.dat").CompareFiles();
        FileComparison(results_dir + "cellareas.dat", "cell_based/test/data/TestPottsBasedCellPopulationWriters/cellareas.dat").CompareFiles();

        // Test that the cell population parameters are output correctly
        out_stream parameter_file = output_file_handler.OpenOutputFile("results.parameters");

        // Write cell population parameters to file
        cell_population.OutputCellPopulationParameters(parameter_file);
        parameter_file->close();

        // Compare output with saved files of what they should look like
        FileComparison( results_dir + "results.parameters", "cell_based/test/data/TestPottsBasedCellPopulationWriters/results.parameters").CompareFiles();
    }
    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
    }