Skip to content

add Python bindings for controlroutines and Solvers #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"dockerComposeFile": [
"../docker-compose.yml"
],
"workspaceFolder": "/workspaces/ikarus",
"service": "OpenIkarusContainer",
"shutdownAction": "stopCompose",

"customizations": {
"vscode": {
"extensions": [
"ms-vscode.cpptools",
"ms-vscode.cmake-tools",
"ms-vscode.cpptools-extension-pack",
"llvm-vs-code-extensions.vscode-clangd",
"ms-python.python",
"eamodio.gitlens",
"ms-python.black-formatter",
"benjamin-simmonds.pythoncpp-debug"
],
"settings": {
"C_Cpp.intelliSenseEngine": "disabled",
"cmake.configureArgs": [
"-DADD_FORMATTARGET=1"
],
"python.pythonPath": "/dune/dune-common/build-cmake/dune-env/bin/python",
"python.defaultInterpreterPath": "/dune/dune-common/build-cmake/dune-env/bin/python",
"clangd.arguments": [
"--compile-commands-dir=${workspaceFolder}/build-cmake",
"--completion-style=detailed",
"--header-insertion=never",
//"-include config.h",
"-pretty"
],
"cmake.automaticReconfigure": false

}
}
},
"postStartCommand": "apt update && apt install python3-gmsh -y && pip install gmsh"

}
38 changes: 38 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python C++ Debugger",
"type": "pythoncpp",
"request": "launch",
"pythonLaunchName": "Python: Current File",
"cppAttachName": "(gdb) Attach"
},
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/dune/dune-common/build-cmake/dune-env/bin/python",
"processId": "",
"MIMode": "gdb",
// "miDebuggerPath": "/path/to/gdb or remove this attribute for the path to be found automatically",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ project(
LANGUAGES C CXX
)

add_definitions(-DDUNE_LOCALFEFUNCTIONS_USE_EIGEN=1)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#add_definitions(-DDUNE_LOCALFEFUNCTIONS_USE_EIGEN=1)
add_definitions(-DDUNE_DISABLE_PYTHON_DEPRECATION_WARNINGS=1)
add_definitions(-DCMAKE_DISABLE_FIND_PACKAGE_Vc=1)
add_definitions(-DCMAKE_DISABLE_FIND_PACKAGE_LATEX=1)
add_definitions(-DBUILD_SHARED_LIBS=1)
Expand Down
26 changes: 26 additions & 0 deletions cmake/modules/AddDuneLocalFeFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers [email protected]
# SPDX-License-Identifier: LGPL-3.0-or-later

# set HAVE_dune-localfefunctions for config.h
set(HAVE_DUNE_LOCALFEFUNCTIONS ${dune-localfefunctions_FOUND})
set(DUNE_LOCALFEFUNCTIONS_USE_EIGEN 1)

# register all dune-localfefunctions related flags
if(dune-localfefunctions_FOUND)
dune_register_package_flags(
COMPILE_DEFINITIONS "ENABLE_DUNE_LOCALFEFUNCTIONS=1;DUNE_LOCALFEFUNCTIONS_USE_EIGEN=TRUE"
)
endif()

# add function to link against the dune-localfefunctions library
function(add_dune_dune-localfefunctions_flags _targets)
if(dune-localfefunctions_FOUND)
foreach(_target ${_targets})
target_compile_definitions(
${_target}
PUBLIC ENABLE_DUNE_LOCALFEFUNCTIONS=1
PUBLIC DUNE_LOCALFEFUNCTIONS_USE_EIGEN=TRUE
)
endforeach(_target)
endif()
endfunction(add_dune_dune-localfefunctions_flags)
10 changes: 8 additions & 2 deletions cmake/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers [email protected]
# SPDX-License-Identifier: LGPL-3.0-or-later

set(modules IkarusMacros.cmake AddAutoDiffFlags.cmake AddEigenFlags.cmake AddMatplotppFlags.cmake
AddSpdlogFlags.cmake AddSpectraFlags.cmake
set(modules
IkarusMacros.cmake
AddAutoDiffFlags.cmake
AddEigenFlags.cmake
AddMatplotppFlags.cmake
AddSpdlogFlags.cmake
AddSpectraFlags.cmake
AddDuneLocalFeFlags.cmake
)
install(FILES ${modules} DESTINATION ${DUNE_INSTALL_MODULEDIR})
2 changes: 2 additions & 0 deletions cmake/modules/IkarusMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

include(CMakeFindDependencyMacro)

include(AddDuneLocalFeFlags)

find_package(spdlog)
include(AddSpdlogFlags)

Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3.3"
services:
OpenIkarusContainer:
image: ikarusproject/ikarus-dev:latest
user: root
entrypoint: /bin/bash
tty: true
volumes:
- ./:/workspaces/ikarus
#- C:\Users\Alex\Documents\Paper_Mueller-Bischoff-Keip-MicroMagnetoStatics\simulations:/mshfiles
working_dir: /workspaces/ikarus
cap_add:
- SYS_PTRACE
1 change: 1 addition & 0 deletions docs/website/01_framework/feRequirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
!!! hint "Affordance"
It is good style to indicate that you cannot fulfill an affordance by throwing an appropriate exception!

### FE result requirements

Check failure on line 46 in docs/website/01_framework/feRequirements.md

View workflow job for this annotation

GitHub Actions / Check for markdown errors

Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "### FE result requirements"]
!!! bug "Bug"

!!! bug "Bug"

Expand Down
8 changes: 4 additions & 4 deletions ikarus/controlroutines/pathfollowingfunctions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Ikarus {
* This structure holds various arguments used by subsidiary functions in control routines.
* \ingroup controlroutines
*/
struct SubsidiaryArgs
struct SubsidiaryArgs
{
double stepSize; ///< The step size in the control routine.
Eigen::VectorX<double> DD; ///< The vector representing the solution increment.
Expand Down Expand Up @@ -98,7 +98,7 @@ struct ArcLength
template <typename NLO>
void initialPrediction(NLO& nonLinearOperator, SubsidiaryArgs& args) {
SolverTypeTag solverTag;
using JacobianType = std::remove_cvref_t<typename NLO::DerivativeType>;
using JacobianType = std::remove_cvref_t<typename NLO::template FunctionReturnType<1>>;
static_assert((traits::isSpecializationTypeAndNonTypes<Eigen::Matrix, JacobianType>::value) or
(traits::isSpecializationTypeNonTypeAndType<Eigen::SparseMatrix, JacobianType>::value),
"Linear solver not implemented for the chosen derivative type of the non-linear operator");
Expand All @@ -114,8 +114,8 @@ struct ArcLength
const auto& K = nonLinearOperator.derivative();

static constexpr bool isLinearSolver =
Ikarus::Concepts::LinearSolverCheck<decltype(LinearSolver(solverTag)), typename NLO::DerivativeType,
typename NLO::ValueType>;
Ikarus::Concepts::LinearSolverCheck<decltype(LinearSolver(solverTag)),
typename NLO::template FunctionReturnType<1>, typename NLO::ValueType>;
static_assert(isLinearSolver,
"Initial predictor step in the standard arc-length method doesn't have a linear solver");

Expand Down
52 changes: 52 additions & 0 deletions ikarus/controlroutines/pathfollowingfunctionsinterface.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers [email protected]
// SPDX-License-Identifier: LGPL-3.0-or-later

/**
* \file pathfollowingfunctionsinterface.hh
* \brief

*/
#pragma once
#include <ikarus/controlroutines/pathfollowingfunctions.hh>
namespace Ikarus{
template<typename NLO>
class SubsidaryFunction {

struct SFConcept {
virtual ~SFConcept() = default;
virtual std::unique_ptr<SFConcept> copy_() const = 0;
virtual void call_(SubsidiaryArgs& args) const = 0;
virtual void initialPrediction_(NLO& nonLinearOperator, SubsidiaryArgs& args) = 0;
virtual void intermediatePrediction_(NLO& nonLinearOperator, SubsidiaryArgs& args) = 0;
virtual std::string name_() const = 0;
};

template <typename T> struct SFModel : SFConcept {
SFModel(const T& t) : value_(t) {}
std::unique_ptr<SFConcept> copy_() const override {
return std::make_unique<SFModel>(value_);
}
void call_(SubsidiaryArgs& args) const override { value_.call(args); }
void initialPrediction_(NLO& nonLinearOperator, SubsidiaryArgs& args) override { value_.initialPrediction(nonLinearOperator, args); }
void intermediatePrediction_(NLO& nonLinearOperator, SubsidiaryArgs& args) override { value_.intermediatePrediction(nonLinearOperator, args); }
std::string name_() const override { return value_.name(); }

T value_;
};

public:
template <typename T> SubsidaryFunction(const T &t) {
value_ = std::make_unique<SFModel<T>>(t);
}
SubsidaryFunction(const SubsidaryFunction& other) : value_(other.value_->copy_()) {}

void operator()(SubsidiaryArgs& args) const { value_->call_(args); }
void initialPrediction(NLO& nonLinearOperator, SubsidiaryArgs& args) { value_->initialPrediction_(nonLinearOperator, args); }
void intermediatePrediction(NLO& nonLinearOperator, SubsidiaryArgs& args) { value_->intermediatePrediction_(nonLinearOperator, args); }
std::string name() const { return value_->name_(); }

private:
std::unique_ptr<SFConcept> value_;
};

}
35 changes: 34 additions & 1 deletion ikarus/finiteelements/ferequirements.hh
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,17 @@ concept FEAffordance = std::is_same_v<std::remove_cvref_t<T>, ScalarAffordance>
*/
template <FEAffordance... Affordances>
requires(sizeof...(Affordances) <= 3)
struct AffordanceCollection : public std::tuple<Affordances...>
struct AffordanceCollection : std::tuple<Affordances...>
{
using Base = std::tuple<Affordances...>;

AffordanceCollection() = default;

constexpr AffordanceCollection(Affordances... affordances)
requires(sizeof...(Affordances) > 0)
: Base(affordances...) {}

static constexpr size_t size = sizeof...(Affordances);
static constexpr bool hasScalarAffordance = traits::hasType<ScalarAffordance, std::tuple<Affordances...>>::value;
static constexpr bool hasVectorAffordance = traits::hasType<VectorAffordance, std::tuple<Affordances...>>::value;
static constexpr bool hasMatrixAffordance = traits::hasType<MatrixAffordance, std::tuple<Affordances...>>::value;
Expand Down Expand Up @@ -166,6 +169,27 @@ struct AffordanceCollection : public std::tuple<Affordances...>
}
};

/**
* @brief Type trait to check if a type is an AffordanceCollection.
*
* @tparam T Type to check.
*/
template <typename T>
struct IsAffordanceCollection : std::false_type
{
};

#ifndef DOXYGEN

template <typename... Ts>
struct IsAffordanceCollection<AffordanceCollection<Ts...>> : std::true_type
{
};
#endif

template <typename T>
constexpr bool IsAffordanceCollection_v = IsAffordanceCollection<T>::value;

inline constexpr VectorAffordance forces = VectorAffordance::forces;

inline constexpr MatrixAffordance stiffness = MatrixAffordance::stiffness;
Expand Down Expand Up @@ -360,3 +384,12 @@ public:
};

} // namespace Ikarus

namespace std {
template <Ikarus::FEAffordance... Affordances>
requires(sizeof...(Affordances) <= 3)
struct tuple_size<Ikarus::AffordanceCollection<Affordances...>>
: std::integral_constant<std::size_t, sizeof...(Affordances)>
{
};
} // namespace std
2 changes: 2 additions & 0 deletions ikarus/finiteelements/mechanics/enhancedassumedstrains.hh
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ protected:
using namespace Dune::DerivativeDirections;
using namespace Dune;
easApplicabilityCheck();
std::cout << "calculateMatrixImpl of EAS called with " + std::to_string(numberOfEASParameters()) + " Parameters."
<< std::endl;
if (isDisplacementBased())
return;

Expand Down
2 changes: 2 additions & 0 deletions ikarus/finiteelements/mechanics/kirchhoffloveshell.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <dune/localfefunctions/cachedlocalBasis/cachedlocalBasis.hh>
#include <dune/localfefunctions/impl/standardLocalFunction.hh>

#include <Eigen/Core>

#include <ikarus/finiteelements/fehelper.hh>
#include <ikarus/finiteelements/ferequirements.hh>
#include <ikarus/finiteelements/feresulttypes.hh>
Expand Down
4 changes: 3 additions & 1 deletion ikarus/finiteelements/mechanics/linearelastic.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#pragma once

#if HAVE_DUNE_LOCALFEFUNCTIONS

#include <optional>
#include <type_traits>

#include <dune/geometry/quadraturerules.hh>
#include <dune/localfefunctions/expressions/linearStrainsExpr.hh>
#include <dune/localfefunctions/impl/standardLocalFunction.hh>

#include <Eigen/Dense>

#include <ikarus/finiteelements/fehelper.hh>
#include <ikarus/finiteelements/ferequirements.hh>
#include <ikarus/finiteelements/feresulttypes.hh>
Expand Down Expand Up @@ -199,6 +200,7 @@ protected:
const auto eps = strainFunction(par, dx);
using namespace Dune::DerivativeDirections;
using namespace Dune;
std::cout << "calculateMatrixImpl of LinearElastic called" << std::endl;

const auto C = materialTangent();
for (const auto& [gpIndex, gp] : eps.viewOverIntegrationPoints()) {
Expand Down
2 changes: 2 additions & 0 deletions ikarus/finiteelements/mechanics/loads/traction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <dune/localfefunctions/derivativetransformators.hh>
#include <dune/localfefunctions/meta.hh>

#include <Eigen/Dense>

#include <ikarus/finiteelements/ferequirements.hh>

namespace Ikarus {
Expand Down
2 changes: 2 additions & 0 deletions ikarus/finiteelements/mechanics/loads/volume.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <dune/localfefunctions/derivativetransformators.hh>
#include <dune/localfefunctions/meta.hh>

#include <Eigen/Core>

#include <ikarus/finiteelements/ferequirements.hh>
#include <ikarus/utils/concepts.hh>
#include <ikarus/utils/traits.hh>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private:
};
// THE CTAD is broken for designated initializers in clang 16, when we drop support this can be simplified
NewtonRaphsonConfig<decltype(linearSolver), decltype(updateFunction)> nrs{
.parameters = {.tol = tol_, .maxIter = 100},
.parameters = {.res_tol = tol_, .maxIter = 100},
.linearSolver = linearSolver, .updateFunction = updateFunction
};

Expand Down
2 changes: 2 additions & 0 deletions ikarus/finiteelements/mechanics/nonlinearelastic.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <dune/localfefunctions/impl/standardLocalFunction.hh>
#include <dune/localfefunctions/manifolds/realTuple.hh>

#include <Eigen/Core>

#include <ikarus/finiteelements/febase.hh>
#include <ikarus/finiteelements/fehelper.hh>
#include <ikarus/finiteelements/ferequirements.hh>
Expand Down
Loading
Loading