In previous cell-based Chaste tutorials, we used existing force classes to define
how cells interact mechanically. In this tutorial we show
how to create a new force class, and how this can be used in a cell-based
simulation.
As in previous cell-based Chaste tutorials, we begin by including the necessary header files.
The next header defines a base class for forces, from which the new class will inherit.
The remaining header files define classes that will be used in the cell-based
simulation test. We have encountered each of these header files in previous cell-based
Chaste tutorials.
This header ensures that this test is only run on one process, since it doesn’t support parallel execution.
As an example, let us consider a force for a two-dimensional cell-based
simulation, that mimics gravity. To implement this we define a force
boundary condition class, MyForce, which inherits from
AbstractForce and overrides the methods AddForceContribution() and
OutputForceParameters().
Note that usually this code would be separated out into a separate declaration
in a .hpp file and definition in a .cpp file.
This force class includes a member variable, mStrength, which
defines the strength of the force. This member variable will be set
in the constructor.
We only need to include the next block of code if we wish to be able
to archive (save or load) the force model object in a cell-based simulation.
The code consists of a serialize() method, in which we first archive the force
using the serialization code defined in the base class AbstractForce,
then archive the member variable.
The first public method is a default constructor, which calls the base
constructor. There is a single input argument, which defines the strength
of the force. We provide a default value of 1.0 for this argument. Inside
the method, we add an assertion to make sure that the strength is strictly
positive.
The second public method overrides AddForceContribution().
This method takes in one arguments, a reference to the cell population itself.
Inside the method, we loop over nodes, and add a constant vector to
each node, in the negative y-direction and of magnitude mStrength.
We also add a get method for mStrength, to allow for testing.
Just as we encountered in Creating And Using A New Cell Killer, here we must override
a method that outputs any member variables to a specified results file rParamsFile.
In our case, we output the member variable mStrength, then call the method on the base class.
As mentioned in previous cell-based Chaste tutorials, we need to include the next block
of code to be able to archive the force object in a cell-based
simulation, and to obtain a unique identifier for our new force for writing
results to file.
This completes the code for MyForce. Note that usually this code
would be separated out into a separate declaration in a .hpp file and definition
in a .cpp file.
We now test that our new force is implemented correctly.
We first create a MeshBasedCellPopulation using the helper
classes HoneycombMeshGenerator and CellsGenerator,
as in previous cell-based Chaste tutorials.
Initialise all node forces to zero
We now create a force object of strength 5.0.
We test that the force calculation is correct.
The last block of code provides an archiving test for the force class,
in a similar way to previous cell-based Chaste tutorials:
Note that it is important to test archiving by using an abstract
pointer, so that you check that boost can identify and record which
concrete class it should be dealing with.
This tests the CHASTE_CLASS_EXPORT(MyForce) lines are implemented correctly.