Skip to content

Commit

Permalink
OffLattice support (#453)
Browse files Browse the repository at this point in the history
* 🔥 Remove alias functions used for Cython

* 🚧 Add python apis for offlattice

* ✅ Add a test for offlattice

* 💥 Add SpatiocyteWorld.add_offlattice()

Add add_offlattice() to SpatiocyteWorld instead of add_space()

* ✨ Add a class for generating OffLatticeSpace

Add spatiocyte::OffLattice, which stores arguments for creating an instance of OffLatticeSpace

* ✨ Add an argument to add_offlattice()

* ♻️ Refactor remove_voxel()

* ♻️ Remove some functions from VoxelSpaceBase

Remove some functions for Particle from VoxelSpaceBase

* 🔥 Remove other functions from VoxelSpaceBase

Remove other functions for Particle from VoxelSpaceBase

* 🔥 Remove VoxelSpaceBase::particle_at()

* ♻️ Impl find_voxel() instead of find_particle()

Implement VoxelSpaceBase::find_voxel() instead of VoxelSpaceBase::find_particle()

* 🎨 Format VoxelSpaceBase.[ch]pp

* 🎨 Format spatiocyte codes

* ♻️ Move get_value() and get_value_exact()

* 🎨 Format LatticeSpaces

* ♻️ Use ranged-for-loop instead of iterator

* ♻️ Use auto for the return value of list_voxels()

* ♻️ Isolate push_voxels() from VoxelSpaceBase

* 🐛 Make list_voxels() to show the real species

* 💥 Modify SpatiocyteWorld::list_voxels()

Change the return type of SpatiocyteWorld::list_voxels()

* 💥 Do not use radius, D and loc from ParticleVoxel

* 💥 Modify VoxelSpaceBase::list_voxels()

Introduce VoxelView and change the return type of VoxelSpaceBase::list_voxels()

* 💥 Change the return type of find_voxel()

* ♻️ Modify VoxelSpaceBase::get_voxel_at()

* 💥 Remove radius and D from VoxelPool

* ♻️ Remove VoxelSpaceBase::get_voxel_pool()

* ♻️ Modify SpatiocyteWorld::update_voxel()

Change the argument types of SpatiocyteWorld::update_voxel()

* ♻️ Modify VoxelSpaceBase::update_voxel()

Change the argument types of VoxelSpaceBase::update_voxel()

* 🔥 Remove ParticleVoxel completely!

* ♻️ Add Voxel::get_neighbor_randomly()

* ♻️ Add another Voxel::get_neighbor_randomly()

For StepEvent2D

* ♻️ StepEvent2D uses Voxel::get_neighbor_randomly()

* ♻️ Remove rng from args of get_neighbor_randomly()

* ♻️ Move functions for neighbor to SpatiocyteWorld

* ♻️ Change the types of intefaces_ and neighbors_

* ♻️ Refactor add_structure() and remove is_inside()

* ♻️ Change the arguments of gen_particle_from()

* ♻️ Add find_space_and_voxel_pool()

* ♻️ Add find_space_and_molecule_pool()

* 🔥 Remove coordinate2voxel()

* 🐛 Fix bug around locating molecules

* 🔥 Remove SpatiocyteWorld::coordinate_type

* 🐛 Fix num_voxels* and num_molecules*

* 🐛 Fix the bug of SpatiocyteWorld::add_molecules()

* ♻️ Refactor get_molecule_info()

* 🐛 Get information from a model before add a space

* ✅ Add test for get_molecule_info()

* 🐛 Fix get_location() with a vacant voxel

* 🐛 Fix SpatiocyteWorld::check_neighbor()

* 🐛 Fix reactions with OffLatticeSpace

* ♻️ Add a virtual function StepEvent::dimension()

Add StepEvent::dimension() and lift up StepEvent::walk() to a common
function()

* ⚡ Use template for StepEvent and some functions

* ♻️ Add a model to a python test

Add a model to a python unittest test_constructor

* ✅ Add a test for OffLattice

* 🔀 Cherry pick a commit for CircleCI

* 🔀 Cherry pick another commit for CircleCI

Co-authored-by: Kozo Nishida <[email protected]>
  • Loading branch information
0ncorhynchus and kozo2 authored Feb 9, 2020
1 parent cebd2a1 commit fd1e52b
Show file tree
Hide file tree
Showing 40 changed files with 2,707 additions and 3,136 deletions.
32 changes: 15 additions & 17 deletions ecell4/core/HCPLatticeSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,49 @@
#include <boost/numeric/interval/detail/msvc_rounding_control.hpp>
#endif

namespace ecell4 {
namespace ecell4
{

#ifdef WIN32_MSC
double rint(const double x)
{
return boost::numeric::interval_lib::detail::rint(x);
}

double round(const double x)
{
return floor(x + 0.5);
}
double round(const double x) { return floor(x + 0.5); }
#endif

void HCPLatticeSpace::set_lattice_properties(const Real3& edge_lengths, const bool is_periodic)
void HCPLatticeSpace::set_lattice_properties(const Real3 &edge_lengths,
const bool is_periodic)
{
//XXX: derived from SpatiocyteStepper::setLatticeProperties()
// XXX: derived from SpatiocyteStepper::setLatticeProperties()
HCP_L = voxel_radius_ / sqrt(3.0);
HCP_X = voxel_radius_ * sqrt(8.0 / 3.0); // Lx
HCP_Y = voxel_radius_ * sqrt(3.0); // Ly
HCP_Y = voxel_radius_ * sqrt(3.0); // Ly

const Real& lengthX = edge_lengths[0];
const Real& lengthY = edge_lengths[1];
const Real& lengthZ = edge_lengths[2];
const Real &lengthX = edge_lengths[0];
const Real &lengthY = edge_lengths[1];
const Real &lengthZ = edge_lengths[2];

col_size_ = (Integer)rint(lengthX / HCP_X);
layer_size_ = (Integer)rint(lengthY / HCP_Y);
row_size_ = (Integer)rint((lengthZ / 2) / voxel_radius_);

if (is_periodic)
{
// The number of voxels in each axis must be even for a periodic boundary.
// The number of voxels in each axis must be even for a periodic
// boundary.
col_size_ = (col_size_ % 2 == 0 ? col_size_ : col_size_ + 1);
layer_size_ = (layer_size_ % 2 == 0 ? layer_size_ : layer_size_ + 1);
row_size_ = (row_size_ % 2 == 0 ? row_size_ : row_size_ + 1);
}

edge_lengths_ = Real3(
col_size_ * HCP_X,
layer_size_ * HCP_Y,
row_size_ * voxel_radius_ * 2);
edge_lengths_ = Real3(col_size_ * HCP_X, layer_size_ * HCP_Y,
row_size_ * voxel_radius_ * 2);

row_size_ += 2;
layer_size_ += 2;
col_size_ += 2;
}

} // ecell4
} // namespace ecell4
94 changes: 37 additions & 57 deletions ecell4/core/HCPLatticeSpace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
namespace ecell4
{

class HCPLatticeSpace
: public VoxelSpaceBase
class HCPLatticeSpace : public VoxelSpaceBase
{
public:

typedef VoxelSpaceBase base_type;

public:

HCPLatticeSpace(const Real3& edge_lengths, const Real& voxel_radius, const bool is_periodic)
HCPLatticeSpace(const Real3 &edge_lengths, const Real &voxel_radius,
const bool is_periodic)
: base_type(voxel_radius)
{
set_lattice_properties(edge_lengths, is_periodic);
Expand All @@ -26,99 +24,86 @@ class HCPLatticeSpace
; // do nothing
}

virtual void reset(const Real3& edge_lengths, const Real& voxel_radius, const bool is_periodic)
virtual void reset(const Real3 &edge_lengths, const Real &voxel_radius,
const bool is_periodic)
{
voxel_radius_ = voxel_radius;

set_lattice_properties(edge_lengths, is_periodic);
}

void set_lattice_properties(const Real3& edge_lengths, const bool is_periodic);
void set_lattice_properties(const Real3 &edge_lengths,
const bool is_periodic);

/**
* Primitives
*/

const Real3& edge_lengths() const
{
return edge_lengths_;
}
const Real3 &edge_lengths() const { return edge_lengths_; }

virtual const Integer col_size() const
{
return col_size_ - 2;
}
virtual const Integer col_size() const { return col_size_ - 2; }

virtual const Integer row_size() const
{
return row_size_ - 2;
}
virtual const Integer row_size() const { return row_size_ - 2; }

virtual const Integer layer_size() const
{
return layer_size_ - 2;
}
virtual const Integer layer_size() const { return layer_size_ - 2; }

/**
Coordinate transformations
*/
coordinate_type global2coordinate(const Integer3& global) const
coordinate_type global2coordinate(const Integer3 &global) const
{
const Integer3 g(global.col + 1, global.row + 1, global.layer + 1);
return g.row + row_size_ * (g.col + col_size_ * g.layer);
}

Integer3 coordinate2global(const coordinate_type& coord) const
Integer3 coordinate2global(const coordinate_type &coord) const
{
const Integer NUM_COLROW(row_size_ * col_size_);
const Integer LAYER(coord / NUM_COLROW);
const Integer SURPLUS(coord - LAYER * NUM_COLROW);
const Integer COL(SURPLUS / row_size_);
const Integer3 global(COL, SURPLUS - COL * row_size_, LAYER);
const Integer3 retval(
global.col - 1, global.row - 1, global.layer - 1);
const Integer3 retval(global.col - 1, global.row - 1, global.layer - 1);
return retval;
}

Real3 coordinate2position(const coordinate_type& coord) const
Real3 coordinate2position(const coordinate_type &coord) const
{
return global2position(coordinate2global(coord));
}

coordinate_type position2coordinate(const Real3& pos) const
coordinate_type position2coordinate(const Real3 &pos) const
{
return global2coordinate(position2global(pos));
}

Real3 global2position(const Integer3& global) const
Real3 global2position(const Integer3 &global) const
{
// the center point of a voxel
const Real3 pos(
global.col * HCP_X,
(global.col % 2) * HCP_L + HCP_Y * global.layer,
(global.row * 2 + (global.layer + global.col) % 2)
* voxel_radius_);
global.col * HCP_X, (global.col % 2) * HCP_L + HCP_Y * global.layer,
(global.row * 2 + (global.layer + global.col) % 2) * voxel_radius_);
return pos;
}

Integer3 position2global(const Real3& pos) const
Integer3 position2global(const Real3 &pos) const
{
const Integer col(round(pos[0] / HCP_X));
const Integer layer(round((pos[1] - (col % 2) * HCP_L) / HCP_Y));
const Integer row(round(
(pos[2] / voxel_radius_ - ((layer + col) % 2)) / 2));
const Integer row(
round((pos[2] / voxel_radius_ - ((layer + col) % 2)) / 2));
const Integer3 global(col, row, layer);
return global;
}

Integer num_neighbors(const coordinate_type& coord) const
Integer num_neighbors(const coordinate_type &coord) const
{
if (!is_inside(coord)) return 0;
if (!is_inside(coord))
return 0;
return 12;
}

coordinate_type periodic_transpose(
const coordinate_type& coord) const
coordinate_type periodic_transpose(const coordinate_type &coord) const
{
Integer3 global(coordinate2global(coord));

Expand All @@ -128,15 +113,15 @@ class HCPLatticeSpace

global.col = global.col < 0 ? global.col + col_size() : global.col;
global.row = global.row < 0 ? global.row + row_size() : global.row;
global.layer = global.layer < 0 ? global.layer + layer_size() : global.layer;
global.layer =
global.layer < 0 ? global.layer + layer_size() : global.layer;

return global2coordinate(global);
}

protected:

coordinate_type get_neighbor_(
const coordinate_type& coord, const Integer& nrand) const
coordinate_type get_neighbor_(const coordinate_type &coord,
const Integer &nrand) const
{
const Integer NUM_COLROW(col_size_ * row_size_);
const Integer NUM_ROW(row_size_);
Expand Down Expand Up @@ -177,24 +162,20 @@ class HCPLatticeSpace
}

public:

bool is_in_range(const coordinate_type& coord) const
bool is_in_range(const coordinate_type &coord) const
{
return coord >= 0 && coord < row_size_ * col_size_ * layer_size_;
}

virtual bool is_inside(const coordinate_type& coord) const
virtual bool is_inside(const coordinate_type &coord) const
{
const Integer3 global(coordinate2global(coord));
return global.col >= 0 && global.col < col_size()
&& global.row >= 0 && global.row < row_size()
&& global.layer >= 0 && global.layer < layer_size();
return global.col >= 0 && global.col < col_size() && global.row >= 0 &&
global.row < row_size() && global.layer >= 0 &&
global.layer < layer_size();
}

virtual Integer size() const
{
return row_size_ * col_size_ * layer_size_;
}
virtual Integer size() const { return row_size_ * col_size_ * layer_size_; }

virtual Integer3 shape() const
{
Expand All @@ -207,12 +188,11 @@ class HCPLatticeSpace
}

protected:

Real3 edge_lengths_;
Real HCP_L, HCP_X, HCP_Y;
Integer row_size_, layer_size_, col_size_;
};

} // ecell4
} // namespace ecell4

#endif /* ECELL4_LATTICE_SPACE_BASE_HPP */
Loading

0 comments on commit fd1e52b

Please sign in to comment.