In Creating And Using A New Cell Property we showed how to create a new cell
property class, MotileCellProperty, and how this can be used in a cell-based simulation.
In this tutorial, we show how to create a new cell writer class, which can be used to output
different data from a cell-based simulation. Our example will be a writer class for outputting
information about those cells in a simulation that have the MotileCellProperty.
As in previous cell-based Chaste tutorials, we begin by including the necessary header file and
archiving headers. For simplicity, we duplicate the code presented in Creating And Using A New Cell Property
that defines the MotileCellProperty class. As before, note that usually this code would be
separated out into a declaration in a .hpp file and a definition in a .cpp file.
We also include some header files defining classes to be used in the cell-based
simulation test. We have encountered each of these header files in previous cell-based
Chaste tutorials.
We next define a class that writes information about each cell in the population, using
the public methods of the population class.
Note that usually this code would be separated out into a declaration in a .hpp file and
definition in a .cpp file.
We inherit from the base class, AbstractCellWriter, whose header we must include.
This abstract class encapsulates how files are written.
To specify how this writer should act on each cell, we implement the VisitCell() method.
The serialize() method defines how a cell writer object itself can be written to file.
In almost all cases it should just call the base class serializer, using the code below.
If the new cell writer class has any data members, they should be serialized in this method
after calling the base class serializer.
The constructor method calls the base class constructor, with the name of the output file as
a parameter. In this case the filename written to will be “cellmotilityresults.dat”.
The next method provides functionality that will be used in future for outputting data to VTK.
The implementation of the VisitCell() method defines the data this writer commits to the file.
Data can be streamed into the member variable mpOutputStream using the << operator.
These data are then written to file.
In this example, for each cell pCell in pCellPopulation, we write its
location index, followed by a space, followed by its location coordinates (space separated)
followed by a 0 or 1 indicating whether the cell has the motile property.
The base class handles writing the timestamp and newline for each simulation timestep, so we
don’t need to worry about that.
As mentioned in previous cell-based Chaste tutorials, we need to include the next block
of code to be able to archive the cell property and writer objects in a cell-based simulation,
and to obtain a unique identifier for our new classes for when writing results to file.
Identifiers for both classes are defined together here, since we can only have each #include once
in this source file. Normally the first #include and export would go in each respective class’s header file, and the second
include and export in its source file.
This completes the code for MotileCellProperty and CellMotilityWriter.
We now define the test class, which inherits from AbstractCellBasedTestSuite and
demonstrates how MotileCellProperty and CellMotilityWriter can be used in
a cell-based simulation.
We begin by creating a NodeBasedCellPopulation, just as in Creating And Using A New Cell Property.
We add the MotileCellProperty to a random selection of cells.
We also add the CellLabel to these cells so that we can easily visualize the different cell types.
In order to write cell motility data using our writer, we must add it to the list of writers
used by the population. This is achieved using the AddCellWriter() method,
which is templated.
We then pass in the cell population into an OffLatticeSimulation,
and set the output directory, output multiple, and end time.
Next we create a force law and pass it to the OffLatticeSimulation, and call Solve() to run the simulation.
you should see a collection of cells with the MotileCellProperty (labelled dark blue) moving towards the origin.
Upon running this test, the output file cellmotilityresults.dat should be created in the folder
tmp/$USER/testoutput/TestOffLatticeSimulationWithMotileCellPropertyAndWriters/results_from_time_0.