Skip to content

Commit

Permalink
lbboundaries: initialization logic for cpu.
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSzuttor committed Feb 18, 2020
1 parent 9aa17ad commit af2201c
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 51 deletions.
4 changes: 2 additions & 2 deletions doc/sphinx/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Available shapes are listed below.
- :class:`espressomd.shapes.SpheroCylinder`
- :class:`espressomd.shapes.Stomatocyte`
- :class:`espressomd.shapes.HollowConicalFrustum`
- :class:`espressomd.shapes.ShapeUnion`
- :class:`espressomd.shapes.Union`


.. _Adding shape-based constraints to the system:
Expand Down Expand Up @@ -438,7 +438,7 @@ Pictured is an example constraint with a ``SpheroCylinder`` shape created with :
:height: 6.00000cm


:class:`espressomd.shapes.ShapeUnion`
:class:`espressomd.shapes.Union`
A meta-shape which is the union of given shapes. Note that only the regions where
all shapes have a "positive distance" (see :ref:`Available options`) can be used for the
union. The distance to the union is defined as the minimum distance to any contained shape.
Expand Down
47 changes: 21 additions & 26 deletions src/core/grid_based_algorithms/lb_boundaries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*
*/


#include "grid_based_algorithms/lb_boundaries.hpp"

#include "communication.hpp"
Expand Down Expand Up @@ -116,8 +115,8 @@ void ek_init_boundaries() {
std::vector<std::shared_ptr<LBBoundary>> boundaries;
std::copy_if(lbboundaries.begin(), lbboundaries.end(),
std::back_inserter(boundaries), [&pos](auto const lbb) {
return not lbb->shape().is_inside(pos);
});
return not lbb->shape().is_inside(pos);
});
for (auto lbb : boundaries) {
if ((*lbb).charge_density() != 0.0f) {
node_charged = true;
Expand All @@ -126,7 +125,7 @@ void ek_init_boundaries() {
(*lbb).set_net_charge(
(*lbb).net_charge() +
(*lbb).charge_density() * ek_parameters.agrid *
ek_parameters.agrid * ek_parameters.agrid);
ek_parameters.agrid * ek_parameters.agrid);
}
}
if (not boundaries.empty()) {
Expand All @@ -137,7 +136,7 @@ void ek_init_boundaries() {
if (wallcharge_species != -1) {
if (node_charged)
host_wallcharge_species_density[ek_parameters.dim_y *
ek_parameters.dim_x * z +
ek_parameters.dim_x * z +
ek_parameters.dim_x * y + x] =
node_wallcharge / ek_parameters.valency[wallcharge_species];
}
Expand Down Expand Up @@ -184,7 +183,7 @@ void lb_init_boundaries() {
host_boundary_node_list[number_of_boundnodes] =
x + lbpar_gpu.dim_x * y + lbpar_gpu.dim_x * lbpar_gpu.dim_y * z;
auto const boundary_number =
std::distance(boundary, lbboundaries.rend());
std::distance(lbboundaries.begin(), boundary.base()) - 1;
host_boundary_index_list[number_of_boundnodes] =
boundary_number + 1;
number_of_boundnodes++;
Expand Down Expand Up @@ -215,14 +214,7 @@ void lb_init_boundaries() {
#endif /* defined ( CUDA) && defined (LB_BOUNDARIES_GPU) */
} else if (lattice_switch == ActiveLB::CPU) {
#if defined(LB_BOUNDARIES)
Utils::Vector3i offset;

auto const node_pos = calc_node_pos(comm_cart);

const auto lblattice = lb_lbfluid_get_lattice();
offset[0] = node_pos[0] * lblattice.grid[0];
offset[1] = node_pos[1] * lblattice.grid[1];
offset[2] = node_pos[2] * lblattice.grid[2];

for (int n = 0; n < lblattice.halo_grid_volume; n++) {
lbfields.at(n).boundary = 0;
Expand All @@ -231,6 +223,12 @@ void lb_init_boundaries() {
if (lblattice.halo_grid_volume == 0)
return;

auto const node_pos = calc_node_pos(comm_cart);
Utils::Vector3i offset;
offset[0] = node_pos[0] * lblattice.grid[0];
offset[1] = node_pos[1] * lblattice.grid[1];
offset[2] = node_pos[2] * lblattice.grid[2];

for (int z = 0; z < lblattice.grid[2] + 2; z++) {
for (int y = 0; y < lblattice.grid[1] + 2; y++) {
for (int x = 0; x < lblattice.grid[0] + 2; x++) {
Expand All @@ -239,22 +237,19 @@ void lb_init_boundaries() {
pos[1] = (offset[1] + (y - 0.5)) * lblattice.agrid;
pos[2] = (offset[2] + (z - 0.5)) * lblattice.agrid;

std::vector<bool> inside_boundaries;
for (auto it = lbboundaries.begin(); it != lbboundaries.end(); ++it) {
auto const inside_boundary = not(**it).shape().is_inside(pos);
inside_boundaries.push_back(inside_boundary);
}
auto const it =
std::find_if(inside_boundaries.begin(), inside_boundaries.end(),
[](bool b) { return b; });
if (it != inside_boundaries.end()) {
auto const boundary =
std::find_if(lbboundaries.rbegin(), lbboundaries.rend(),
[&pos](auto const lbb) {
return not lbb->shape().is_inside(pos);
});
if (boundary != lbboundaries.rend()) {
auto const index = get_linear_index(x, y, z, lblattice.halo_grid);
auto &node = lbfields[index];
auto const the_boundary =
std::distance(inside_boundaries.begin(), it);
node.boundary = the_boundary + 1;
auto const boundary_number =
std::distance(lbboundaries.begin(), boundary.base()) - 1;
node.boundary = boundary_number + 1;
node.slip_velocity =
LBBoundaries::lbboundaries[the_boundary]->velocity() *
(*boundary)->velocity() *
(lb_lbfluid_get_tau() / lb_lbfluid_get_agrid());
} else {
lbfields[get_linear_index(x, y, z, lblattice.halo_grid)].boundary =
Expand Down
2 changes: 1 addition & 1 deletion src/core/shapes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
set(Shapes_SRC
HollowConicalFrustum.cpp
HollowConicalFrustum.cpp
Cylinder.cpp
Ellipsoid.cpp
Rhomboid.cpp
Expand Down
13 changes: 6 additions & 7 deletions src/core/shapes/ShapeUnion.hpp → src/core/shapes/Union.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef SHAPES_SHAPE_UNION
#define SHAPES_SHAPE_UNION
#ifndef SHAPES_UNION
#define SHAPES_UNION

#include <memory>

#include "Shape.hpp"

namespace Shapes {

class ShapeUnion : public Shape {
class Union : public Shape {
public:
void add(std::shared_ptr<Shapes::Shape> const &s) {
m_shapes.emplace_back(s);
Expand All @@ -45,20 +45,19 @@ class ShapeUnion : public Shape {
* @param[out] vec Distance vector.
*/
void calculate_dist(Utils::Vector3d const &pos, double &dist,
Utils::Vector3d &vec) const {
Utils::Vector3d &vec) const override {
auto dist_compare = [&pos](std::pair<double, Utils::Vector3d> const &res,
std::shared_ptr<Shapes::Shape> const &s) {
double d;
Utils::Vector3d vec;
(*s).calculate_dist(pos, d, vec);
if (d < 0.0)
throw std::runtime_error(
"Distance to ShapeUnion not well-defined for given position!");
"Distance to Union not well-defined for given position!");
if (d < res.first) {
return std::make_pair(d, vec);
} else {
return res;
}
return res;
};
std::tie(dist, vec) =
std::accumulate(m_shapes.begin(), m_shapes.end(),
Expand Down
2 changes: 1 addition & 1 deletion src/core/shapes/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include(unit_test)
unit_test(NAME Wall_test SRC Wall_test.cpp DEPENDS Shapes)
unit_test(NAME ConicalFrustum_test SRC HollowConicalFrustum_test.cpp DEPENDS Shapes)
unit_test(NAME ShapeUnion_test SRC ShapeUnion_test.cpp DEPENDS Shapes)
unit_test(NAME Union_test SRC Union_test.cpp DEPENDS Shapes)
unit_test(NAME Ellipsoid_test SRC Ellipsoid_test.cpp DEPENDS Shapes)
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
#include <iostream>
#include <memory>

#define BOOST_TEST_MODULE ShapeUnion test
#define BOOST_TEST_MODULE Union test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "shapes/HollowConicalFrustum.hpp"
#include "shapes/ShapeUnion.hpp"
#include "shapes/Union.hpp"
#include "shapes/Wall.hpp"

BOOST_AUTO_TEST_CASE(dist_function) {
Expand All @@ -40,7 +40,7 @@ BOOST_AUTO_TEST_CASE(dist_function) {
wall2->set_normal(Utils::Vector3d{0., 0., -1.});
wall2->d() = -10.0;

Shapes::ShapeUnion uni;
Shapes::Union uni;
uni.add(wall1);
uni.add(wall2);

Expand Down
4 changes: 2 additions & 2 deletions src/python/espressomd/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ class HollowConicalFrustum(Shape, ScriptInterfaceHelper):


@script_interface_register
class ShapeUnion(Shape, ScriptObjectRegistry):
class Union(Shape, ScriptObjectRegistry):
"""A union of shapes.
This shape represents a union of shapes where the distance to the union
is defined by the smallest distance to any shape contained in the union.
"""
_so_name = "Shapes::ShapeUnion"
_so_name = "Shapes::Union"

def add(self, shape):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

#include "ScriptObjectRegistry.hpp"
#include "Shape.hpp"
#include "core/shapes/ShapeUnion.hpp"
#include "core/shapes/Union.hpp"

namespace ScriptInterface {
namespace Shapes {

class ShapeUnion : public Shape {
class Union : public Shape {
public:
ShapeUnion() : m_core_shape(new ::Shapes::ShapeUnion()) {}
Union() : m_core_shape(new ::Shapes::Union()) {}

Variant call_method(std::string const &name,
VariantMap const &params) override {
Expand Down Expand Up @@ -70,7 +70,7 @@ class ShapeUnion : public Shape {
}

private:
std::shared_ptr<::Shapes::ShapeUnion> m_core_shape;
std::shared_ptr<::Shapes::Union> m_core_shape;
std::vector<std::shared_ptr<Shapes::Shape>> m_shapes;
};

Expand Down
6 changes: 3 additions & 3 deletions src/script_interface/shapes/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
#include "NoWhere.hpp"
#include "Rhomboid.hpp"
#include "ScriptInterface.hpp"
#include "ShapeUnion.hpp"
#include "SimplePore.hpp"
#include "Slitpore.hpp"
#include "Sphere.hpp"
#include "SpheroCylinder.hpp"
#include "Stomatocyte.hpp"
#include "Torus.hpp"
#include "Union.hpp"
#include "Wall.hpp"

namespace ScriptInterface {
namespace Shapes {
void initialize() {
ScriptInterface::register_new<ScriptInterface::Shapes::HollowConicalFrustum>(
"Shapes::HollowConicalFrustum");
ScriptInterface::register_new<ScriptInterface::Shapes::ShapeUnion>(
"Shapes::ShapeUnion");
ScriptInterface::register_new<ScriptInterface::Shapes::Union>(
"Shapes::Union");
ScriptInterface::register_new<ScriptInterface::Shapes::NoWhere>(
"Shapes::NoWhere");
ScriptInterface::register_new<ScriptInterface::Shapes::Wall>("Shapes::Wall");
Expand Down
4 changes: 2 additions & 2 deletions testsuite/python/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


class ShapeTests(ut.TestCase):
def test_ShapeUnion(self):
union = espressomd.shapes.ShapeUnion()
def test_Union(self):
union = espressomd.shapes.Union()
wall1 = espressomd.shapes.Wall(normal=[0, 0, 1], dist=0)
wall2 = espressomd.shapes.Wall(normal=[0, 0, -1], dist=-10)
union.add([wall1, wall2])
Expand Down

0 comments on commit af2201c

Please sign in to comment.