# Field Secretion¶

PDE solvers in the CC3D allow users to specify secretion properties
individually for each cell type. However, there are situations where you
want only a single cell to secrete the chemical. In this case you have
to use `Secretor`

objects. In Twedit++, go to `CC3D Python->Secretion`

menu
to see what options are available. Let us look at the example code to
understand what kind of capabilities CC3D offers in this regard (see
`Demos/SteppableDemos/Secretion`

):

```
class SecretionSteppable(SecretionBasePy):
def __init__(self,_simulator,_frequency=1):
SecretionBasePy.__init__(self,_simulator, _frequency)
def step(self,mcs):
attrSecretor=self.getFieldSecretor("ATTR")
for cell in self.cellList:
if cell.type==3:
attrSecretor.secreteInsideCell(cell,300)
attrSecretor.secreteInsideCellAtBoundary(cell,300)
attrSecretor.secreteOutsideCellAtBoundary(cell,500)
attrSecretor.secreteInsideCellAtCOM(cell,300)
```

In the step function we obtain a handle to field secretor object that
operates on diffusing field `ATTR`

. In the for loop where we go over all
cells in the simulation we pick cells which are of type 3 (notice we use
numeric value here instead of an alias). Inside the loop we use
secreteInsideCell, secreteInsideCellAtBoundary,
secreteOutsideCellAtBoundary, and secreteInsideCellAtCOM member
functions of the secretor object to carry out secretion in the region
occupied by a given cell. `secreteInsideCell`

increases concentration by a
given amount (here `300`

) in every pixel occupied by a cell.
`secreteInsideCellAtBoundary`

and `secreteOutsideCellAtBoundary`

increase
concentration but only in pixels which at the boundary but are inside
cell or outside pixels touching cell boundary. Finally,
`secreteInsideCellAtCOM`

increases concentration in a single pixel that is
closest to cell center of mass of a cell.

Notice that `SecretionSteppable`

inherits from `SecretionBasePy`

. We do this
to ensure that Python-based secretion plays nicely with PDE solvers.
This requires that such steppable must be called before MCS, or rather
before the PDE solvers start evolving the field. If we look at the
definition of `SecretionBasePy`

we will see that it inherits from
`SteppableBasePy`

and in the `__init__`

function it sets
`self.runBeforeMCS`

flag to `1`

:

```
class SecretionBasePy(SteppableBasePy):
def __init__(self,_simulator,_frequency=1):
SteppableBasePy.__init__(self,_simulator,_frequency)
self.runBeforeMCS=1
```

## Direct (but somewhat naive) Implementation¶

Now, for the sake of completeness, let us implement cell secretion at the COM using alternative code:

```
self.field = self.getConcentrationField('ATTR')
lmfLength = 1.0;
xScale = 1.0
yScale = 1.0
zScale = 1.0
# FOR HEX LATTICE IN 2D
# lmfLength=sqrt(2.0/(3.0*sqrt(3.0)))*sqrt(3.0)
# xScale=1.0
# yScale=sqrt(3.0)/2.0
# zScale=sqrt(6.0)/3.0
for cell in self.cellList:
# converting from real coordinates to pixels
xCM = int(cell.xCOM / (lmfLength * xScale))
yCM = int(cell.yCOM / (lmfLength * yScale))
if cell.type == 3:
self.field[xCM, yCM, 0] = self.field[xCM, yCM, 0] + 10.0
```

As you can tell, it is significantly more work than our original solution.

## Lattice Conversion Factors¶

In the code where we manually implement secretion at the cell’sCOM we use
strange looking variables lmfLength, xScale and yScale. CC3D allows
users to run simulations on square (Cartesian) or hexagonal lattices.
Under the hood these two lattices rely on the Cartesian lattice. However
distances between neighboring pixels are different on Cartesian and hex
lattice. This is what those 3 variables accomplish. The take home
message is that to convert COM coordinates on hex lattice to Cartesian
lattice coordinates we need to use converting factors. Please see
writeup **“Hexagonal Lattices in CompuCell3D”**
(http://www.compucell3d.org/BinDoc/cc3d_binaries/Manuals/HexagonalLattice.pdf)
for more information. To convert between hex and Cartesian lattice
coordinates we can use PySteppableBase built-in functions
(`self.cartesian2Hex`

, `and self.hex2Cartesian`

) – see also Twedit++ CC3D
Python menu Distances, Vectors, Transformations:

```
hex_coords = self.cartesian2Hex(_in=[10, 20, 11])
pt = self.hex2Cartesian(_in=[11.2, 13.1, 21.123])
```

## Tracking Amount of Secreted (Uptaken) Chemical¶

While the ability to have fine control over how the chemicals get secreted/uptaken
is a useful feature, quite often we would like to know the total amount of the chemical that was added
to the simulation as a result of the call to one of the `secrete`

or `uptake`

functions from he secretor object.

Let us rewrite previous example using the API ythat facilitates tracking of the amount of chemical that was added:

```
class SecretionSteppable(SecretionBasePy):
def __init__(self,_simulator,_frequency=1):
SecretionBasePy.__init__(self,_simulator, _frequency)
def step(self,mcs):
attrSecretor=self.getFieldSecretor("ATTR")
for cell in self.cellList:
if cell.type==3:
res = attrSecretor.secreteInsideCellTotalCount(cell,300)
print 'secreted ', res.tot_amount, ' inside cell'
res = attrSecretor.secreteInsideCellAtBoundaryTotalCount(cell,300)
print 'secreted ', res.tot_amount, ' inside cell at the boundary'
res = attrSecretor.secreteOutsideCellAtBoundaryTotalCount(cell,500)
print 'secreted ', res.tot_amount, ' outside the cell at the boundary'
res = attrSecretor.secreteInsideCellAtCOMTotalCount(cell,300)
print 'secreted ', res.tot_amount, ' inside the cell at the COM'
```

As you can see the calls to that return the total amount of chemical added/uptaked are the same calls as we
used in our previous example except we add `TotalCount`

to the name of the function. The new function e.g.
`secreteInsideCellTotalCount`

returns object `res`

that is an instance of `FieldSecretorResult`

class
that contains the summary of the secreion/uptake operation. Most importantly when we access `total_amount`

member of the `res`

object we get the total amount that was added/uptaken from the chemical field e.g. :

```
res = attrSecretor.secreteInsideCellTotalCount(cell,300)
print 'secreted ', res.tot_amount, ' inside cell'
```

For cmpleteness we present a complete list of C++ signatures of all the functions that can be used to fine-control
how uptake/secretion happens in CC3D. All those functions are members of the `secretor`

object and are
accessible from Python

```
bool _secreteInsideCellConstantConcentration(CellG * _cell, float _amount);
FieldSecretorResult _secreteInsideCellConstantConcentrationTotalCount(CellG * _cell, float _amount);
bool _secreteInsideCell(CellG * _cell, float _amount);
FieldSecretorResult _secreteInsideCellTotalCount(CellG * _cell, float _amount);
bool _secreteInsideCellAtBoundary(CellG * _cell, float _amount);
FieldSecretorResult _secreteInsideCellAtBoundaryTotalCount(CellG * _cell, float _amount);
bool _secreteInsideCellAtBoundaryOnContactWith(CellG * _cell, float _amount,
const std::vector<unsigned char> & _onContactVec);
FieldSecretorResult _secreteInsideCellAtBoundaryOnContactWithTotalCount(CellG * _cell,
float _amount, const std::vector<unsigned char> & _onContactVec);
bool _secreteOutsideCellAtBoundary(CellG * _cell, float _amount);
FieldSecretorResult _secreteOutsideCellAtBoundaryTotalCount(CellG * _cell, float _amount);
bool _secreteOutsideCellAtBoundaryOnContactWith(CellG * _cell, float _amount,
const std::vector<unsigned char> & _onContactVec);
FieldSecretorResult _secreteOutsideCellAtBoundaryOnContactWithTotalCount(CellG * _cell,
float _amount, const std::vector<unsigned char> & _onContactVec);
bool secreteInsideCellAtCOM(CellG * _cell, float _amount);
FieldSecretorResult secreteInsideCellAtCOMTotalCount(CellG * _cell, float _amount);
bool _uptakeInsideCell(CellG * _cell, float _maxUptake, float _relativeUptake);
FieldSecretorResult _uptakeInsideCellTotalCount(CellG * _cell, float _maxUptake, float _relativeUptake);
bool _uptakeInsideCellAtBoundary(CellG * _cell, float _maxUptake, float _relativeUptake);
FieldSecretorResult _uptakeInsideCellAtBoundaryTotalCount(CellG * _cell, float _maxUptake, float _relativeUptake);
bool _uptakeInsideCellAtBoundaryOnContactWith(CellG * _cell, float _maxUptake,
float _relativeUptake,const std::vector<unsigned char> & _onContactVec);
FieldSecretorResult _uptakeInsideCellAtBoundaryOnContactWithTotalCount(CellG * _cell,
float _maxUptake, float _relativeUptake, const std::vector<unsigned char> & _onContactVec);
bool _uptakeOutsideCellAtBoundary(CellG * _cell, float _maxUptake, float _relativeUptake);
FieldSecretorResult _uptakeOutsideCellAtBoundaryTotalCount(CellG * _cell, float _maxUptake, float _relativeUptake);
bool _uptakeOutsideCellAtBoundaryOnContactWith(CellG * _cell, float _maxUptake,
float _relativeUptake,const std::vector<unsigned char> & _onContactVec);
FieldSecretorResult _uptakeOutsideCellAtBoundaryOnContactWithTotalCount(CellG * _cell,
float _maxUptake, float _relativeUptake, const std::vector<unsigned char> & _onContactVec);
bool uptakeInsideCellAtCOM(CellG * _cell, float _maxUptake, float _relativeUptake);
FieldSecretorResult uptakeInsideCellAtCOMTotalCount(CellG * _cell, float _maxUptake, float _relativeUptake);
```

For example if we want to use `uptakeInsideCellAtCOMTotalCount(CellG * _cell, float _maxUptake, float _relativeUptake);`

from python we would use the following code:

In this case `_cell`

is a `cell`

object that we normally deal with in Python, `_maxUptake`

has value of `3`

and `_relativeUptake`

is set to `0.1`

In similar fashion we could use remaining functions listed above