diff --git a/examples/boundary_conditions/CMakeLists.txt b/examples/boundary_conditions/CMakeLists.txt index 65c03b0af..05bdc3d42 100644 --- a/examples/boundary_conditions/CMakeLists.txt +++ b/examples/boundary_conditions/CMakeLists.txt @@ -1,32 +1,36 @@ #=============================================================================== # @file CMakeLists.txt # # @author Seyedeh Mohadeseh Taheri Mousavi # # @date creation: Wed Dec 16 2015 # @date last modification: Mon Jan 18 2016 # # @brief CMakeLists for the cohesive examples # # @section LICENSE # # Copyright (©) 2015 EPFL (Ecole Polytechnique Fédérale de Lausanne) Laboratory # (LSMS - Laboratoire de Simulation en Mécanique des Solides) # # Akantu is free software: you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. # # Akantu is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with Akantu. If not, see . # #=============================================================================== -add_example(predefined_bc "Example of predefined boundary condition" PACKAGE core) -add_example(user_defined_bc "Example of user defined boundary condition" PACKAGE core) +add_example(predefined_bc + "Example of predefined boundary condition" PACKAGE core) +add_example(user_defined_bc + "Example of user defined boundary condition" PACKAGE core) +add_example(python_user_defined_bc + "Example of python user defined boundary condition" PACKAGE pybind11) diff --git a/examples/boundary_conditions/python_user_defined_bc/CMakeLists.txt b/examples/boundary_conditions/python_user_defined_bc/CMakeLists.txt new file mode 100644 index 000000000..d621bad9e --- /dev/null +++ b/examples/boundary_conditions/python_user_defined_bc/CMakeLists.txt @@ -0,0 +1,11 @@ +add_mesh(python_user_defined_bc_mesh fine_mesh.geo 2 1) + +register_example(python_user_defined_bc + SOURCES python_user_defined_bc.cc + DEPENDS python_user_defined_bc_mesh + PYTHON + FILES_TO_COPY boundary_condition.py material.dat + ) + +target_include_directories(python_user_defined_bc PRIVATE + ${PROJECT_SOURCE_DIR}/python) diff --git a/examples/boundary_conditions/python_user_defined_bc/boundary_condition.py b/examples/boundary_conditions/python_user_defined_bc/boundary_condition.py new file mode 100644 index 000000000..da316c990 --- /dev/null +++ b/examples/boundary_conditions/python_user_defined_bc/boundary_condition.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 +import math + + +class SinBoundary: + + def __init__(self, amplitude, phase): + self.amplitude = amplitude + self.phase = phase + + def compute(self, disp, coord, flags): + disp[1] = - self.amplitude * math.sin(self.phase * coord[1]) + flags[1] = True diff --git a/examples/boundary_conditions/python_user_defined_bc/fine_mesh.geo b/examples/boundary_conditions/python_user_defined_bc/fine_mesh.geo new file mode 100644 index 000000000..f8165adb5 --- /dev/null +++ b/examples/boundary_conditions/python_user_defined_bc/fine_mesh.geo @@ -0,0 +1,29 @@ +// Mesh size +h = 0.1; + +// Dimensions of the square +Lx = 2; +Ly = 2; + +// ------------------------------------------ +// Geometry +// ------------------------------------------ +Point(1) = { 0.0, 0.0, 0.0, h}; +Point(2) = { Lx, 0.0, 0.0, h}; +Point(3) = { Lx, Ly, 0.0, h}; +Point(4) = { 0.0, Ly, 0.0, h}; + +Line(1) = {1, 2}; +Line(2) = {2, 3}; +Line(3) = {3, 4}; +Line(4) = {4, 1}; + +Line Loop(1) = {1:4}; + +Plane Surface(1) = {1}; + +Physical Surface("steel") = {1}; +Physical Line("Fixed_y") = {1}; +Physical Line("Fixed_x") = {4}; +Physical Line("Traction") = {2}; +Physical Line("Free") = {3}; diff --git a/examples/boundary_conditions/python_user_defined_bc/material.dat b/examples/boundary_conditions/python_user_defined_bc/material.dat new file mode 100644 index 000000000..e648fe1da --- /dev/null +++ b/examples/boundary_conditions/python_user_defined_bc/material.dat @@ -0,0 +1,7 @@ +material elastic [ + name = steel + rho = 7800 # density + E = 2.1e11 # young's modulus + nu = 0.3 # poisson's ratio +] + diff --git a/examples/boundary_conditions/python_user_defined_bc/python_user_defined_bc.cc b/examples/boundary_conditions/python_user_defined_bc/python_user_defined_bc.cc new file mode 100644 index 000000000..d288dc0f3 --- /dev/null +++ b/examples/boundary_conditions/python_user_defined_bc/python_user_defined_bc.cc @@ -0,0 +1,91 @@ +/** + * @file python_user_defined_bc.cc + * + * @brief python-user-defined boundary condition example + * + * @section LICENSE + * + * Copyright (©) 2015 EPFL (Ecole Polytechnique Fédérale de Lausanne) Laboratory + * (LSMS - Laboratoire de Simulation en Mécanique des Solides) + * + * Akantu is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * Akantu is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Akantu. If not, see . + * + */ + +/* -------------------------------------------------------------------------- */ +#include "solid_mechanics_model.hh" +#include "py_aka_array.hh" +/* -------------------------------------------------------------------------- */ +#include +#include +#include +/* -------------------------------------------------------------------------- */ +namespace py = pybind11; +/* -------------------------------------------------------------------------- */ + +using namespace akantu; + +class SineBoundary : public BC::Dirichlet::DirichletFunctor { +public: + SineBoundary(Real amplitude, Real phase) { + py_module = py::module::import("boundary_condition"); + py_sin_boundary = py_module.attr("SinBoundary")(amplitude, phase); + } + +public: + inline void operator()(__attribute__((unused)) UInt node, + Vector & flags, Vector & primal, + const Vector & coord) const { + py_sin_boundary.attr("compute")(primal, coord, flags); + } + +protected: + py::object py_sin_boundary; + py::module py_module; +}; + +/* -------------------------------------------------------------------------- */ +int main(int argc, char * argv[]) { + initialize("material.dat", argc, argv); + + py::scoped_interpreter guard{}; + + UInt spatial_dimension = 2; + + Mesh mesh(spatial_dimension); + mesh.read("fine_mesh.msh"); + + SolidMechanicsModel model(mesh); + + /// model initialization + model.initFull(); + + /// boundary conditions + Vector traction(2, 0.2); + SineBoundary sin_boundary(.2, 10.); + + model.applyBC(sin_boundary, "Fixed_x"); + model.applyBC(BC::Dirichlet::FixedValue(0., _y), "Fixed_y"); + model.applyBC(BC::Neumann::FromTraction(traction), "Traction"); + + // output a paraview file with the boundary conditions + model.setBaseName("plate"); + model.addDumpFieldVector("displacement"); + model.addDumpFieldVector("external_force"); + model.addDumpField("blocked_dofs"); + model.dump(); + + finalize(); + return EXIT_SUCCESS; +}