Cellular Automata CHEM274B Group Project
Contributor(s): Chongye Feng, Emmanuel Cortes, Trevor Oldham
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | List of all members
CellularAutomata< T > Class Template Reference

A CellularAutomata class for simulating cellular automata models. More...

#include <CAdatatypes.h>

Inheritance diagram for CellularAutomata< T >:
BaseCellularAutomata

Public Member Functions

T * get_vector ()
 Get the vector cell grid. More...
 
T * get_next_vector ()
 Get the next state vector cell grid. More...
 
T ** get_matrix ()
 Get the matrix cell grid. More...
 
T ** get_next_matrix ()
 Get the next state matrix cell grid. More...
 
T *** get_tensor ()
 Get the tensor cell grid. More...
 
T *** get_next_tensor ()
 Get the next state tensor cell grid. More...
 
int setup_boundary (CAEnums::Boundary bound_type, int radius)
 Setup boundary with enum values from boundary and set the boundary radius. More...
 
 CellularAutomata ()
 Construct a new Cellular Automata object. More...
 
 ~CellularAutomata ()
 Destroy the Cellular Automata object. More...
 
int setup_dimensions_1d (int axis1_dim, int fill_value=0)
 Set up 1d array of cell states. More...
 
int setup_dimensions_2d (int axis1_dim, int axis2_dim, int fill_value=0)
 Set up 2d matrix of cell states. More...
 
int setup_dimensions_3d (int axis1_dim, int axis2_dim, int axis3_dim, int fill_value=0)
 Set up 3d tensor of cell states. More...
 
int init_condition (int x_state, double prob)
 Initializes the first state of the grid using random numbers. More...
 
int step (void(custom_rule)(int *, int, T *, int, T &))
 Simulates a cellular automata step. More...
 
int step ()
 Simulates a cellular automata step. More...
 
int print_grid ()
 Print the current state of the grid. More...
 
int setup_dimensions_1d (int axis1_dim, int fill_value)
 Set up 1d array of cell states. More...
 
int setup_dimensions_2d (int axis1_dim, int axis2_dim, int fill_value)
 Set up 2d matrix of cell states. More...
 
int setup_dimensions_3d (int axis1_dim, int axis2_dim, int axis3_dim, int fill_value)
 Set up 3d tensor of cell states. More...
 
int init_condition (int x_state, double prob)
 Initializes the first state of the grid using random numbers. More...
 
int print_grid ()
 Print the current state of the grid. More...
 
int step (void(custom_rule)(int *, int, int *, int, int &))
 Simulates a cellular automata step. More...
 
- Public Member Functions inherited from BaseCellularAutomata
 BaseCellularAutomata ()
 Construct a new Cellular Automata:: Cellular Automata object. More...
 
virtual ~BaseCellularAutomata ()
 Destroy the Cellular Automata:: Cellular Automata object. More...
 
int setup_neighborhood (CAEnums::Neighborhood neighborhood_type)
 Setup neighborhood with values from enum neighborhood. More...
 
int setup_cell_states (int num_states)
 Defines the range of cell states to be used in the CA object. More...
 
int setup_rule (CAEnums::Rule rule_type)
 Setup the rule choice to specify the rule type to be used in CA object. More...
 
void print_error_status (CAEnums::ErrorCode error)
 Prints an error message for the given error code. More...
 

Static Public Member Functions

static int get_neighborhood_size (int rank, int radius, CAEnums::Neighborhood neighborhood_type)
 calculates the neighborhood size using the rank, radius and neighborhood type More...
 
static void initialize_majority_rule_counter (MajorityCounter &counter, int num_states)
 initializes a MajorityCounter instance utilized by Majority rule More...
 

Additional Inherited Members

- Public Attributes inherited from BaseCellularAutomata
CAEnums::Boundary boundary_type
 enum code to hold boundary
 
int boundary_radius
 declare a radius for the boundary
 
CAEnums::Neighborhood neighborhood_type
 enum code to hold neighborhood type
 
int num_states
 integer code for number of states
 
CAEnums::Rule rule_type
 enum code for rule type
 
int axis1_dim
 count of cells in first dimension
 
int axis2_dim
 count of cells in second dimension
 
int axis3_dim
 count of cells in third dimension
 

Detailed Description

template<typename T>
class CellularAutomata< T >

A CellularAutomata class for simulating cellular automata models.

This templated class supports structs/class objects or integer states.

The object must contain a .state property that the CellularAutomata class updates.
The object must have a default constructor or default initialzed values.
The object must meet the requirements of CopyConstructible and CopyAssignable (until C++11)MoveConstructible and MoveAssignable (since C++11)
The object must have a defined != operator for the comparison of cell states.

If these requirements are not satisfied then the class will produce undefined behavior.

Template Parameters
T: struct/class with a .state property, move operator and assignment operator.
int: when cell states are represented by an integer

Constructor & Destructor Documentation

◆ CellularAutomata()

template<typename T >
CellularAutomata< T >::CellularAutomata ( )
inline

Construct a new Cellular Automata object.

Sets the default value to all class attributes.

◆ ~CellularAutomata()

template<typename T >
CellularAutomata< T >::~CellularAutomata ( )
inline

Destroy the Cellular Automata object.

Deallocates memory reserved for vector/matrix/tensor.

Member Function Documentation

◆ get_matrix()

template<typename T >
T ** CellularAutomata< T >::get_matrix ( )
inline

Get the matrix cell grid.

Returns
T*

◆ get_neighborhood_size()

template<typename T >
static int CellularAutomata< T >::get_neighborhood_size ( int  rank,
int  radius,
CAEnums::Neighborhood  neighborhood_type 
)
inlinestatic

calculates the neighborhood size using the rank, radius and neighborhood type

Parameters
rankrank of the cell's tensor
radiusradius of the neighborhood
neighborhood_typethe type of neighborhood
Returns
int

◆ get_next_matrix()

template<typename T >
T ** CellularAutomata< T >::get_next_matrix ( )
inline

Get the next state matrix cell grid.

Returns
T*

◆ get_next_tensor()

template<typename T >
T *** CellularAutomata< T >::get_next_tensor ( )
inline

Get the next state tensor cell grid.

Returns
T*

◆ get_next_vector()

template<typename T >
T * CellularAutomata< T >::get_next_vector ( )
inline

Get the next state vector cell grid.

Returns
T*

◆ get_tensor()

template<typename T >
T *** CellularAutomata< T >::get_tensor ( )
inline

Get the tensor cell grid.

Returns
T*

◆ get_vector()

template<typename T >
T * CellularAutomata< T >::get_vector ( )
inline

Get the vector cell grid.

Returns
T*

◆ init_condition() [1/2]

template<typename T >
int CellularAutomata< T >::init_condition ( int  x_state,
double  prob 
)
inline

Initializes the first state of the grid using random numbers.

Parameters
x_statechoose the cell state to initialize the grid with.
probthe probability of a cell to turn to state given from x_state
Returns
int - error code
CellsAreNull: tensor not initialized
InvalidCellStateCondition: x_state must be less than num_states
0: no error

◆ init_condition() [2/2]

int CellularAutomata< int >::init_condition ( int  x_state,
double  prob 
)

Initializes the first state of the grid using random numbers.

Parameters
x_statechoose the cell state to initialize the grid with.
probthe probability of a cell to turn to state given from x_state
Returns
int - error code
CellsAreNull: tensor not initialized
InvalidCellStateCondition: x_state must be less than num_states
0: no error

◆ initialize_majority_rule_counter()

template<typename T >
static void CellularAutomata< T >::initialize_majority_rule_counter ( MajorityCounter counter,
int  num_states 
)
inlinestatic

initializes a MajorityCounter instance utilized by Majority rule

Parameters
counterobject to keep number of votes for each particular cell state
num_statesnumber of different cell states

◆ print_grid() [1/2]

template<typename T >
int CellularAutomata< T >::print_grid ( )
inline

Print the current state of the grid.

Returns
int - error code
CellsAreNull: neither vector, matrix, nor tensor are initialized
0: no error

◆ print_grid() [2/2]

int CellularAutomata< int >::print_grid ( )

Print the current state of the grid.

Returns
int - error code
CellsAreNull: neither vector, matrix, nor tensor are initialized
0: no error

◆ setup_boundary()

template<typename T >
int CellularAutomata< T >::setup_boundary ( CAEnums::Boundary  bound_type,
int  radius 
)
inline

Setup boundary with enum values from boundary and set the boundary radius.

Parameters
bound_typeenum value for boundary (None, Periodic, Walled, CutOff)
radiusradius for the boundary
Returns
int - error code
InvalidRadius: radius can't be less than equal to 0
RadiusLargerThanDimensions: radius must be smaller than half of the dimensions' size. 0: no error

◆ setup_dimensions_1d() [1/2]

int CellularAutomata< int >::setup_dimensions_1d ( int  axis1_dim,
int  fill_value 
)

Set up 1d array of cell states.

Parameters
axis1_dimthe size of a one dimensional vector
fill_valuethe value to set every cell state to
Returns
int - error code
CellsAlreadyInitialized: vector was already allocated
CellsMalloc: couldn't allocate memory for the specified vector size
0: no error

◆ setup_dimensions_1d() [2/2]

template<typename T >
int CellularAutomata< T >::setup_dimensions_1d ( int  axis1_dim,
int  fill_value = 0 
)
inline

Set up 1d array of cell states.

Parameters
axis1_dimthe size of a one dimensional vector
fill_valuethe value to set every cell state to
Returns
int - error code
CellsAlreadyInitialized: vector was already allocated
CellsMalloc: couldn't allocate memory for the specified vector size
0: no error

◆ setup_dimensions_2d() [1/2]

int CellularAutomata< int >::setup_dimensions_2d ( int  axis1_dim,
int  axis2_dim,
int  fill_value 
)

Set up 2d matrix of cell states.

Parameters
axis1_dimThe size of the first dimension
axis2_dimThe size of the second dimension
fill_valuethe value to set every cell state to
Returns
int - error code
CellsAlreadyInitialized: matrix was already allocated
CellsMalloc: couldn't allocate memory for the specified matrix size
0: no error

◆ setup_dimensions_2d() [2/2]

template<typename T >
int CellularAutomata< T >::setup_dimensions_2d ( int  axis1_dim,
int  axis2_dim,
int  fill_value = 0 
)
inline

Set up 2d matrix of cell states.

Parameters
axis1_dimThe size of the first dimension
axis2_dimThe size of the second dimension
fill_valuethe value to set every cell state to
Returns
int - error code
CellsAlreadyInitialized: matrix was already allocated
CellsMalloc: couldn't allocate memory for the specified matrix size
0: no error

◆ setup_dimensions_3d() [1/2]

int CellularAutomata< int >::setup_dimensions_3d ( int  axis1_dim,
int  axis2_dim,
int  axis3_dim,
int  fill_value 
)

Set up 3d tensor of cell states.

Parameters
axis1_dimThe size of the first dimension
axis2_dimThe size of the second dimension
axis3_dimThe size of the third dimension
fill_valuethe value to set every cell state to
Returns
int - error code
CellsAlreadyInitialized: tensor was already allocated
CellsMalloc: couldn't allocate memory for the specified tensor size
0: no error

◆ setup_dimensions_3d() [2/2]

template<typename T >
int CellularAutomata< T >::setup_dimensions_3d ( int  axis1_dim,
int  axis2_dim,
int  axis3_dim,
int  fill_value = 0 
)
inline

Set up 3d tensor of cell states.

Parameters
axis1_dimThe size of the first dimension
axis2_dimThe size of the second dimension
axis3_dimThe size of the third dimension
fill_valuethe value to set every cell state to
Returns
int - error code
CellsAlreadyInitialized: tensor was already allocated
CellsMalloc: couldn't allocate memory for the specified tensor size
0: no error

◆ step() [1/3]

template<typename T >
int CellularAutomata< T >::step ( )
inline

Simulates a cellular automata step.

A new state is generated and stored as the new state for subsequent calls to step method.

Returns
int - error code
Error codes returned by get_state_from_neighborhood_Xd methods
0: no error

◆ step() [2/3]

int CellularAutomata< int >::step ( void(custom_rule)(int *, int, int *, int, int &)  )

Simulates a cellular automata step.

A new state is generated and stored stored as the new state for subsequent calls to step method.

This method supports the use of a custom rule type.

If cell states move on the grid, the user is responsible for handling clashes. If two cells move to the same cell position, the grid will retain the most recent cell assignment (new will replace the old).

Parameters
custom_rulefunction that is called when a Custom rule type is specified
Returns
int - error code
Error codes returned by get_state_from_neighborhood_Xd methods
0: no error

◆ step() [3/3]

template<typename T >
int CellularAutomata< T >::step ( void(custom_rule)(int *, int, T *, int, T &)  )
inline

Simulates a cellular automata step.

A new state is generated and stored stored as the new state for subsequent calls to step method.

This method supports the use of a custom rule type.

If cell states move on the grid, the user is responsible for handling clashes. If two cells move to the same cell position, the grid will retain the most recent cell assignment (new will replace the old).

Parameters
custom_rulefunction that is called when a Custom rule type is specified
Returns
int - error code
Error codes returned by get_state_from_neighborhood_Xd methods
0: no error

The documentation for this class was generated from the following file: