diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index c04cec9af..f8ad9811e 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -1,101 +1,100 @@ #=============================================================================== # @file CMakeLists.txt # # @author Nicolas Richart # # @date creation: Fri Dec 12 2014 # @date last modification: Mon Jan 18 2016 # # @brief CMake file for the python wrapping of akantu # # @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 . # #=============================================================================== set(PYAKANTU_SRCS py_aka_common.cc py_aka_error.cc py_akantu.cc py_boundary_conditions.cc py_fe_engine.cc py_group_manager.cc py_mesh.cc py_model.cc py_parser.cc ) package_is_activated(iohelper _is_activated) if (_is_activated) list(APPEND PYAKANTU_SRCS py_dumpable.cc ) endif() package_is_activated(solid_mechanics _is_activated) if (_is_activated) list(APPEND PYAKANTU_SRCS py_solid_mechanics_model.cc py_material.cc ) endif() package_is_activated(cohesive_element _is_activated) if (_is_activated) list(APPEND PYAKANTU_SRCS py_solid_mechanics_model_cohesive.cc ) endif() package_is_activated(heat_transfer _is_activated) if (_is_activated) list(APPEND PYAKANTU_SRCS py_heat_transfer_model.cc ) endif() package_is_activated(contact_mechanics _is_activated) if(_is_activated) list(APPEND PYAKANTU_SRCS py_contact_mechanics_model.cc ) endif() package_is_activated(model_couplers _is_activated) if(_is_activated) list(APPEND PYAKANTU_SRCS py_model_couplers.cc ) endif() - pybind11_add_module(py11_akantu ${PYAKANTU_SRCS}) target_link_libraries(py11_akantu PUBLIC akantu) set_target_properties(py11_akantu PROPERTIES DEBUG_POSTFIX "") set(_python_install_dir ${CMAKE_INSTALL_LIBDIR}/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages) install(TARGETS py11_akantu LIBRARY DESTINATION ${_python_install_dir}) install(DIRECTORY akantu DESTINATION ${_python_install_dir} FILES_MATCHING PATTERN "*.py") diff --git a/python/py_material.cc b/python/py_material.cc index e7418c522..a6f163006 100644 --- a/python/py_material.cc +++ b/python/py_material.cc @@ -1,257 +1,246 @@ /* -------------------------------------------------------------------------- */ #include "py_aka_array.hh" /* -------------------------------------------------------------------------- */ #include #include #if defined(AKANTU_COHESIVE_ELEMENT) #include #endif /* -------------------------------------------------------------------------- */ #include #include #include /* -------------------------------------------------------------------------- */ namespace py = pybind11; /* -------------------------------------------------------------------------- */ #if not defined(PYBIND11_OVERRIDE) #define PYBIND11_OVERRIDE PYBIND11_OVERLOAD #define PYBIND11_OVERRIDE_PURE PYBIND11_OVERLOAD_PURE #endif namespace akantu { template class PyMaterial : public _Material { public: /* Inherit the constructors */ using _Material::_Material; ~PyMaterial() override = default; void initMaterial() override { - PYBIND11_OVERRIDE(void, _Material, initMaterial, ); // NOLINT + PYBIND11_OVERRIDE(void, _Material, initMaterial, ); // NOLINT }; void computeStress(ElementType el_type, GhostType ghost_type = _not_ghost) override { PYBIND11_OVERRIDE_PURE(void, _Material, computeStress, el_type, ghost_type); } - void computeTangentModuli(ElementType el_type, - Array & tangent_matrix, + void computeTangentModuli(ElementType el_type, Array & tangent_matrix, GhostType ghost_type = _not_ghost) override { PYBIND11_OVERRIDE(void, _Material, computeTangentModuli, el_type, tangent_matrix, ghost_type); } void computePotentialEnergy(ElementType el_type) override { PYBIND11_OVERRIDE(void, _Material, computePotentialEnergy, el_type); } Real getPushWaveSpeed(const Element & element) const override { PYBIND11_OVERRIDE(Real, _Material, getPushWaveSpeed, element); } Real getShearWaveSpeed(const Element & element) const override { PYBIND11_OVERRIDE(Real, _Material, getShearWaveSpeed, element); } void registerInternal(const std::string & name, UInt nb_component) { this->internals[name] = std::make_shared>(name, *this); AKANTU_DEBUG_INFO("alloc internal " << name << " " << &this->internals[name]); this->internals[name]->initialize(nb_component); } auto & getInternals() { return this->internals; } protected: std::map>> internals; }; /* -------------------------------------------------------------------------- */ template void register_element_type_map_array(py::module & mod, const std::string & name) { py::class_, std::shared_ptr>>( mod, ("ElementTypeMapArray" + name).c_str()) .def( "__call__", [](ElementTypeMapArray & self, ElementType type, GhostType ghost_type) -> decltype(auto) { return self(type, ghost_type); }, py::arg("type"), py::arg("ghost_type") = _not_ghost, py::return_value_policy::reference) .def( "elementTypes", [](ElementTypeMapArray & self, UInt _dim, GhostType _ghost_type, ElementKind _kind) -> decltype(auto) { auto types = self.elementTypes(_dim, _ghost_type, _kind); std::vector _types; for (auto && t : types) { _types.push_back(t); } return _types; }, py::arg("dim") = _all_dimensions, py::arg("ghost_type") = _not_ghost, py::arg("kind") = _ek_regular); py::class_, ElementTypeMapArray, std::shared_ptr>>( mod, ("InternalField" + name).c_str()); } /* -------------------------------------------------------------------------- */ template void define_material(py::module & mod, const std::string & name) { py::class_<_Material, PyMaterial<_Material>, Parsable>( mod, name.c_str(), py::multiple_inheritance()) .def(py::init()) .def( "getGradU", [](Material & self, ElementType el_type, GhostType ghost_type = _not_ghost) -> decltype(auto) { return self.getGradU(el_type, ghost_type); }, py::arg("el_type"), py::arg("ghost_type") = _not_ghost, py::return_value_policy::reference) .def( "getStress", [](Material & self, ElementType el_type, GhostType ghost_type = _not_ghost) -> decltype(auto) { return self.getStress(el_type, ghost_type); }, py::arg("el_type"), py::arg("ghost_type") = _not_ghost, py::return_value_policy::reference) .def( "getPotentialEnergy", [](Material & self, ElementType el_type) -> decltype(auto) { return self.getPotentialEnergy(el_type); }, py::return_value_policy::reference) .def("initMaterial", &Material::initMaterial) .def("getModel", &Material::getModel) .def("registerInternal", [](Material & self, const std::string & name, UInt nb_component) { return dynamic_cast &>(self).registerInternal( name, nb_component); }) .def( "getInternalFieldReal", [](Material & self, const ID & id, const ElementType & type, const GhostType & ghost_type) -> Array & { return self.getArray(id, type, ghost_type); }, py::arg("id"), py::arg("type"), py::arg("ghost_type") = _not_ghost) .def( "getInternalFieldUInt", [](Material & self, const ID & id, const ElementType & type, const GhostType & ghost_type) -> Array & { return self.getArray(id, type, ghost_type); }, py::arg("id"), py::arg("type"), py::arg("ghost_type") = _not_ghost) .def( "getElementFilter", [](Material & self, const ElementType & type, const GhostType & ghost_type) -> const Array & { return self.getElementFilter()(type, ghost_type); }, py::arg("type"), py::arg("ghost_type") = _not_ghost); } /* -------------------------------------------------------------------------- */ void register_material(py::module & mod) { py::class_(mod, "MaterialFactory") .def_static( "getInstance", []() -> MaterialFactory & { return Material::getFactory(); }, py::return_value_policy::reference) .def("registerAllocator", [](MaterialFactory & self, const std::string id, py::function func) { self.registerAllocator( id, [func, id](UInt dim, const ID & /*unused*/, SolidMechanicsModel & model, const ID & option) -> std::unique_ptr { py::object obj = func(dim, id, model, option); auto & ptr = py::cast(obj); obj.release(); return std::unique_ptr(&ptr); }); }); register_element_type_map_array(mod, "Real"); register_element_type_map_array(mod, "UInt"); define_material(mod, "Material"); +} + +/* -------------------------------------------------------------------------- */ +template +void register_data_material_selector(py::module & mod, + const std::string & name) { + py::class_, MaterialSelector, + std::shared_ptr>>( + mod, ("ElementDataMaterialSelector" + name).c_str()); + + py::class_, ElementDataMaterialSelector, + std::shared_ptr>>( + mod, ("MeshDataMaterialSelector" + name).c_str()) + .def(py::init(), + py::arg("name"), py::arg("model"), py::arg("first_index") = 1); +} +/* -------------------------------------------------------------------------- */ +void register_material_selector(py::module & mod) { py::class_>( mod, "MaterialSelector") .def("setFallback", [](MaterialSelector & self, UInt f) { self.setFallback(f); }) .def("setFallback", [](MaterialSelector & self, const std::shared_ptr & fallback_selector) { self.setFallback(fallback_selector); }) .def("setFallback", [](MaterialSelector & self, MaterialSelector & fallback_selector) { self.setFallback(fallback_selector); }); - py::class_, MaterialSelector, - std::shared_ptr>>( - mod, "MeshDataMaterialSelectorString") - .def(py::init(), - py::arg("name"), py::arg("model"), py::arg("first_index") = 1); - #if defined(AKANTU_COHESIVE_ELEMENT) py::class_>( mod, "DefaultMaterialCohesiveSelector") .def(py::init()); py::class_>( mod, "MeshDataMaterialCohesiveSelector") .def(py::init()); py::class_>( mod, "MaterialCohesiveRulesSelector") .def(py::init(), py::arg("model"), py::arg("rules"), py::arg("mesh_data_id") = "physical_names"); #endif -} - -/* -------------------------------------------------------------------------- */ -template -void register_data_material_selector(py::module & mod, - const std::string & name) { - py::class_, MaterialSelector, - std::shared_ptr>>( - mod, ("ElementDataMaterialSelector" + name).c_str()); - - py::class_, ElementDataMaterialSelector, - std::shared_ptr>>( - mod, ("MeshDataMaterialSelector" + name).c_str()) - .def(py::init(), - py::arg("name"), py::arg("model"), py::arg("first_index") = 1); -} - -/* -------------------------------------------------------------------------- */ -void register_material_selector(py::module & mod) { - py::class_>( - mod, "MaterialSelector"); register_data_material_selector(mod, "String"); } /* -------------------------------------------------------------------------- */ } // namespace akantu - diff --git a/src/common/aka_array_tmpl.hh b/src/common/aka_array_tmpl.hh index 468464f1e..bdbb0a45d 100644 --- a/src/common/aka_array_tmpl.hh +++ b/src/common/aka_array_tmpl.hh @@ -1,1362 +1,1364 @@ /** * @file aka_array_tmpl.hh * * @author Guillaume Anciaux * @author Nicolas Richart * * @date creation: Thu Jul 15 2010 * @date last modification: Tue Feb 20 2018 * * @brief Inline functions of the classes Array and ArrayBase * * * Copyright (©) 2010-2018 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 . * */ /* -------------------------------------------------------------------------- */ /* Inline Functions Array */ /* -------------------------------------------------------------------------- */ #include "aka_array.hh" // NOLINT #include "aka_static_memory.hh" /* -------------------------------------------------------------------------- */ #include /* -------------------------------------------------------------------------- */ #ifndef AKANTU_AKA_ARRAY_TMPL_HH_ #define AKANTU_AKA_ARRAY_TMPL_HH_ namespace akantu { namespace debug { struct ArrayException : public Exception {}; } // namespace debug /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ template ArrayDataLayer::ArrayDataLayer(UInt size, UInt nb_component, const ID & id) : ArrayBase(id) { allocate(size, nb_component); } /* -------------------------------------------------------------------------- */ template ArrayDataLayer::ArrayDataLayer(UInt size, UInt nb_component, const_reference value, const ID & id) : ArrayBase(id) { allocate(size, nb_component, value); } /* -------------------------------------------------------------------------- */ template ArrayDataLayer::ArrayDataLayer(const ArrayDataLayer & vect, const ID & id) : ArrayBase(vect, id) { this->data_storage = vect.data_storage; this->size_ = vect.size_; this->nb_component = vect.nb_component; this->values = this->data_storage.data(); } /* -------------------------------------------------------------------------- */ template ArrayDataLayer::ArrayDataLayer( const std::vector & vect) { this->data_storage = vect; this->size_ = vect.size(); this->nb_component = 1; this->values = this->data_storage.data(); } /* -------------------------------------------------------------------------- */ template ArrayDataLayer & ArrayDataLayer::operator=(const ArrayDataLayer & other) { if (this != &other) { this->data_storage = other.data_storage; this->nb_component = other.nb_component; this->size_ = other.size_; this->values = this->data_storage.data(); } return *this; } /* -------------------------------------------------------------------------- */ template void ArrayDataLayer::allocate(UInt new_size, UInt nb_component) { this->nb_component = nb_component; this->resize(new_size); } /* -------------------------------------------------------------------------- */ template void ArrayDataLayer::allocate(UInt new_size, UInt nb_component, const T & val) { this->nb_component = nb_component; this->resize(new_size, val); } /* -------------------------------------------------------------------------- */ template void ArrayDataLayer::resize(UInt new_size) { this->data_storage.resize(new_size * this->nb_component); this->values = this->data_storage.data(); this->size_ = new_size; } /* -------------------------------------------------------------------------- */ template void ArrayDataLayer::resize(UInt new_size, const T & value) { this->data_storage.resize(new_size * this->nb_component, value); this->values = this->data_storage.data(); this->size_ = new_size; } /* -------------------------------------------------------------------------- */ template void ArrayDataLayer::reserve(UInt size, UInt new_size) { if (new_size != UInt(-1)) { this->data_storage.resize(new_size * this->nb_component); } this->data_storage.reserve(size * this->nb_component); this->values = this->data_storage.data(); } /* -------------------------------------------------------------------------- */ /** * append a tuple to the array with the value value for all components * @param value the new last tuple or the array will contain nb_component copies * of value */ template inline void ArrayDataLayer::push_back(const T & value) { this->data_storage.push_back(value); this->values = this->data_storage.data(); this->size_ += 1; } /* -------------------------------------------------------------------------- */ /** * append a matrix or a vector to the array * @param new_elem a reference to a Matrix or Vector */ template template