Skip to content

Commit

Permalink
#20 Add cells example
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Oct 16, 2024
1 parent 6e4bd48 commit 64d6158
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 0 deletions.
80 changes: 80 additions & 0 deletions examples/cells/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
cmake_minimum_required(VERSION 3.16...3.22)
project(shapes LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)

# Find Python
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)

# Fetch pybind11
include(FetchContent)
FetchContent_Declare(
pybind11
GIT_REPOSITORY https://github.com/pybind/pybind11
GIT_TAG v2.13.5
GIT_SHALLOW 1
)
FetchContent_MakeAvailable(pybind11)

# Add external library
add_subdirectory(extern/meshgen meshgen_build)

# Add a shared library for the main C++ source
file(GLOB_RECURSE SHAPES_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/*.cpp)
add_library(shapes SHARED ${SHAPES_SOURCES})
target_include_directories(
shapes
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/geometry
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/math_funcs
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/mesh
${CMAKE_CURRENT_SOURCE_DIR}/src/cpp/primitives
)
target_link_libraries(shapes PUBLIC meshgen::meshgen)

# Copy the Python source and test trees to the build location
file(
COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/pyshapes
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/
)
file(
COPY ${CMAKE_CURRENT_SOURCE_DIR}/src/py/test/
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/
)

# Create a shared library for each wrapper module
foreach(MODULE geometry math_funcs mesh primitives)
# Add the autogenerated wrappers to the module target
file(GLOB MODULE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/wrapper/${MODULE}/*.cpp)

set(MODULE_LIB _pyshapes_${MODULE})
add_library(${MODULE_LIB} SHARED ${MODULE_SOURCES})
target_link_libraries(
${MODULE_LIB}
PRIVATE
Python3::Python
pybind11::module
shapes
)
target_include_directories(
${MODULE_LIB}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/wrapper
${CMAKE_CURRENT_SOURCE_DIR}/wrapper/geometry
${CMAKE_CURRENT_SOURCE_DIR}/wrapper/math_funcs
${CMAKE_CURRENT_SOURCE_DIR}/wrapper/mesh
${CMAKE_CURRENT_SOURCE_DIR}/wrapper/primitives
)

# Set suitable extensions for the module and place the compiled
# shared library in a suitable location inside the python package
set_target_properties(
${MODULE_LIB}
PROPERTIES
PREFIX "${PYTHON_MODULE_PREFIX}"
SUFFIX "${PYTHON_MODULE_EXTENSION}"
LIBRARY_OUTPUT_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}/pyshapes/${MODULE}/
)
endforeach()
37 changes: 37 additions & 0 deletions examples/cells/dynamic/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# For docs on config options, see examples/shapes/wrapper/package_info.yaml
name: pycells

smart_ptr_type: std::shared_ptr
pointer_call_policy: reference
reference_call_policy: reference_internal

common_include_file: False
source_includes:
- <memory>

exclude_default_args: False

template_substitutions:
- signature: <unsigned DIM>
replacement: [[2], [3]]
- signature: <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
replacement: [[2, 2], [3, 3]]
- signature: <unsigned ELEMENT_DIM, unsigned SPACE_DIM=ELEMENT_DIM>
replacement: [[2, 2], [3, 3]]

modules:
- name: lib
source_locations:
- cycle
- mesh
- ode
- pde
- population
- visualization

classes:
- name: AbstractCellPopulation
- name: MeshBasedCellPopulation

prefix_text: |
// This file is auto-generated by cppwg; manual changes will be overwritten.
6 changes: 6 additions & 0 deletions examples/cells/extern/meshgen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.8)
project(meshgen)

add_library(meshgen MeshGen.cpp MeshGen.hpp)
target_include_directories(meshgen PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_library(meshgen::meshgen ALIAS meshgen)
16 changes: 16 additions & 0 deletions examples/cells/extern/meshgen/MeshGen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "MeshGen.hpp"

MeshGen::MeshGen()
{
// Constructor
}

MeshGen::~MeshGen()
{
// Destructor
}

void MeshGen::generateMesh()
{
// Generate a mesh
}
24 changes: 24 additions & 0 deletions examples/cells/extern/meshgen/MeshGen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _MESHGEN_HPP
#define _MESHGEN_HPP

class MeshGen
{

public:
/**
* Default Constructor
*/
MeshGen();

/**
* Destructor
*/
~MeshGen();

/**
* Generate a mesh
*/
void generateMesh();
};

#endif // _MESHGEN_HPP
31 changes: 31 additions & 0 deletions examples/cells/src/cpp/mesh/AbstractMesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "AbstractMesh.hpp"

template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
AbstractMesh<ELEMENT_DIM, SPACE_DIM>::AbstractMesh() : mIndex(0)
{
}

template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
AbstractMesh<ELEMENT_DIM, SPACE_DIM>::~AbstractMesh()
{
}

template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
unsigned AbstractMesh<ELEMENT_DIM, SPACE_DIM>::GetIndex() const
{
return mIndex;
}

template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
void AbstractMesh<ELEMENT_DIM, SPACE_DIM>::SetIndex(unsigned index)
{
mIndex = index;
}

template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
void AbstractMesh<ELEMENT_DIM, SPACE_DIM>::AddVertex(Point<SPACE_DIM> vertex)
{
}

template class AbstractMesh<2, 2>;
template class AbstractMesh<3, 3>;
50 changes: 50 additions & 0 deletions examples/cells/src/cpp/mesh/AbstractMesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef _ABSTRACT_MESH_HPP
#define _ABSTRACT_MESH_HPP

#include "Point.hpp"

/**
* A mesh in SPACE_DIM space with ELEMENT_DIM dimensional elements
*/
template <unsigned ELEMENT_DIM, unsigned SPACE_DIM = ELEMENT_DIM>
class AbstractMesh
{
private:
/**
* AbstractMesh index
*/
unsigned mIndex;

public:
/**
* Default Constructor
*/
AbstractMesh();

/**
* Destructor
*/
~AbstractMesh();

/**
* Return the index
*/
unsigned GetIndex() const;

/**
* Set the index
*/
void SetIndex(unsigned index);

/**
* Add a vertex to the mesh
*/
void AddVertex(Point<SPACE_DIM> vertex);

/**
* Scale the mesh by a factor
*/
virtual void Scale(const double factor) = 0;
};

#endif // _ABSTRACT_MESH_HPP
19 changes: 19 additions & 0 deletions examples/cells/src/cpp/mesh/ConcreteMesh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "AbstractMesh.hpp"
#include "ConcreteMesh.hpp"

template <unsigned DIM>
ConcreteMesh<DIM>::ConcreteMesh() : AbstractMesh<DIM, DIM>()
{
}

template <unsigned DIM>
ConcreteMesh<DIM>::~ConcreteMesh()
{
}
template <unsigned DIM>
void ConcreteMesh<DIM>::Scale(const double factor){
// Scale the mesh
};

template class ConcreteMesh<2>;
template class ConcreteMesh<3>;
29 changes: 29 additions & 0 deletions examples/cells/src/cpp/mesh/ConcreteMesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef _CONCRETE_MESH_HPP
#define _CONCRETE_MESH_HPP

#include "AbstractMesh.hpp"

/**
* A concrete mesh implementation
*/
template <unsigned DIM>
class ConcreteMesh : public AbstractMesh<DIM, DIM>
{
public:
/**
* Default Constructor
*/
ConcreteMesh();

/**
* Destructor
*/
~ConcreteMesh();

/**
* Scale the mesh by a factor
*/
void Scale(const double factor) override;
};

#endif // _CONCRETE_MESH_HPP
16 changes: 16 additions & 0 deletions examples/cells/src/cpp/mesh/MeshFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "ConcreteMesh.hpp"

#include "MeshFactory.hpp"

template <class MESH>
MeshFactory<MESH>::MeshFactory(): mMeshGen()
{
}

template <class MESH>
MeshFactory<MESH>::~MeshFactory()
{
}

template class MeshFactory<ConcreteMesh<2> >;
template class MeshFactory<ConcreteMesh<3> >;
27 changes: 27 additions & 0 deletions examples/cells/src/cpp/mesh/MeshFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef _MESH_FACTORY_HPP
#define _MESH_FACTORY_HPP

#include "MeshGen.hpp"

/**
* A concrete mesh implementation
*/
template <class MESH>
class MeshFactory
{
private:
MeshGen mMeshGen;

public:
/**
* Default Constructor
*/
MeshFactory();

/**
* Destructor
*/
~MeshFactory();
};

#endif // _MESH_FACTORY_HPP
Empty file.
25 changes: 25 additions & 0 deletions examples/cells/src/py/cells/_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import inspect
from collections.abc import Iterable


class TemplateClassDict:

def __init__(self, template_dict):
self._dict = {}
for arg_tuple, cls in template_dict.items():
if not inspect.isclass(cls):
raise TypeError("Expected class, got {}".format(type(cls)))
if not isinstance(arg_tuple, Iterable):
arg_tuple = (arg_tuple,)
key = tuple(
arg.__name__ if inspect.isclass(arg) else str(arg) for arg in arg_tuple
)
self._dict[key] = cls

def __getitem__(self, arg_tuple):
if not isinstance(arg_tuple, Iterable):
arg_tuple = (arg_tuple,)
key = tuple(
arg.__name__ if inspect.isclass(arg) else str(arg) for arg in arg_tuple
)
return self._dict[key]
1 change: 1 addition & 0 deletions examples/cells/src/py/cells/mesh/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pycells._pycells_lib import ConcreteMesh_2
10 changes: 10 additions & 0 deletions examples/cells/src/py/test/test_cells.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import unittest


class TestCells(unittest.TestCase):

def testCells(self):
pass

if __name__ == "__main__":
unittest.main()

0 comments on commit 64d6158

Please sign in to comment.