From 1e596ab8ce9650b3fc3d6f8c1762134e410a7106 Mon Sep 17 00:00:00 2001 From: David Bommes Date: Tue, 21 Oct 2014 12:28:15 +0000 Subject: [PATCH] added FEM stuff git-svn-id: http://www.openflipper.org/svnrepo/CoMISo/trunk@269 1355f012-dd97-4b2f-ae87-10fa9f823a57 --- CoMISo/CMakeLists.txt | 26 ++- CoMISo/Config/config.hh.in | 1 + .../small_finite_element/CMakeLists.txt | 11 ++ CoMISo/Examples/small_finite_element/main.cc | 150 ++++++++++++++++++ CoMISo/cmake/CoMISoExample.cmake | 5 + CoMISo/cmake/FindCoMISo.cmake | 20 ++- CoMISo/cmake/FindDCO.cmake | 32 ++++ 7 files changed, 238 insertions(+), 7 deletions(-) create mode 100644 CoMISo/Examples/small_finite_element/CMakeLists.txt create mode 100644 CoMISo/Examples/small_finite_element/main.cc create mode 100644 CoMISo/cmake/FindDCO.cmake diff --git a/CoMISo/CMakeLists.txt b/CoMISo/CMakeLists.txt index 413ce69..40d0efc 100644 --- a/CoMISo/CMakeLists.txt +++ b/CoMISo/CMakeLists.txt @@ -209,6 +209,15 @@ else () set (COMISO_EIGEN3_CONFIG_FILE_SETTINGS "#define COMISO_EIGEN3_AVAILABLE 0" ) endif () +find_package (DCO) +if (DCO_FOUND ) + set (COMISO_DCO_CONFIG_FILE_SETTINGS "#define COMISO_DCO_AVAILABLE 1" ) + list( APPEND COMISO_INCLUDE_DIRECTORIES ${DCO_INCLUDE_DIR} ) +else () + message (STATUS "DCO not found!") + set (COMISO_DCO_CONFIG_FILE_SETTINGS "#define COMISO_DCO_AVAILABLE 0" ) +endif () + find_package (Taucs) if (TAUCS_FOUND ) set (COMISO_TAUCS_CONFIG_FILE_SETTINGS "#define COMISO_TAUCS_AVAILABLE 1" ) @@ -403,16 +412,23 @@ endif() if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/small_adolc/CMakeLists.txt" ) add_subdirectory (Examples/small_adolc) endif() +if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/small_dco/CMakeLists.txt" ) + add_subdirectory (Examples/small_dco) +endif() if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/vector1_adolc/CMakeLists.txt" ) add_subdirectory (Examples/vector1_adolc) endif() -if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/vector2_adolc/CMakeLists.txt" ) - add_subdirectory (Examples/vector2_adolc) -endif() if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/small_linear_problem/CMakeLists.txt" ) - add_subdirectory (Examples/small_linear_problem) -endif() + add_subdirectory (Examples/small_linear_problem) +endif() if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/crossfield3d/CMakeLists.txt" ) add_subdirectory (Examples/crossfield3d) endif() +if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/crossfield3d/CMakeLists.txt" ) + add_subdirectory (Examples/crossfield3d_dco) +endif() +if( EXISTS "${CMAKE_SOURCE_DIR}/Examples/small_finite_element/CMakeLists.txt" ) + add_subdirectory (Examples/small_finite_element) +endif() + diff --git a/CoMISo/Config/config.hh.in b/CoMISo/Config/config.hh.in index 664e1ea..9fb1195 100644 --- a/CoMISo/Config/config.hh.in +++ b/CoMISo/Config/config.hh.in @@ -20,5 +20,6 @@ @COMISO_ARPACK_CONFIG_FILE_SETTINGS@ @COMISO_CPLEX_CONFIG_FILE_SETTINGS@ @COMISO_EIGEN3_CONFIG_FILE_SETTINGS@ +@COMISO_DCO_CONFIG_FILE_SETTINGS@ diff --git a/CoMISo/Examples/small_finite_element/CMakeLists.txt b/CoMISo/Examples/small_finite_element/CMakeLists.txt new file mode 100644 index 0000000..89c183e --- /dev/null +++ b/CoMISo/Examples/small_finite_element/CMakeLists.txt @@ -0,0 +1,11 @@ +include (CoMISoExample) + +acg_add_executable (small_finite_element ${sources} ${headers} ) + +# enable rpath linking +set_target_properties(small_finite_element PROPERTIES INSTALL_RPATH_USE_LINK_PATH 1) + +target_link_libraries (small_finite_element + CoMISo + ${COMISO_LINK_LIBRARIES} +) diff --git a/CoMISo/Examples/small_finite_element/main.cc b/CoMISo/Examples/small_finite_element/main.cc new file mode 100644 index 0000000..05aee53 --- /dev/null +++ b/CoMISo/Examples/small_finite_element/main.cc @@ -0,0 +1,150 @@ +/*===========================================================================*\ + * * + * CoMISo * + * Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen * + * www.rwth-graphics.de * + * * + *---------------------------------------------------------------------------* + * This file is part of CoMISo. * + * * + * CoMISo is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * CoMISo 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with CoMISo. If not, see . * + * * +\*===========================================================================*/ + +#include +#include +#include +#include +#include +#include + + +// create a simple finite element (x-y)^2 +class SimpleElement +{ +public: + + // define dimensions + const static int NV = 2; + const static int NC = 0; + + typedef Eigen::Matrix VecI; + typedef Eigen::Matrix VecV; + typedef Eigen::Matrix VecC; + typedef Eigen::Triplet Triplet; + + inline double eval_f (const VecV& _x, const VecC& _c) const + { + return std::pow(_x[0]-_x[1],2); + } + + inline void eval_gradient(const VecV& _x, const VecC& _c, VecV& _g) const + { + _g[0] = 2.0*(_x[0]-_x[1]); + _g[1] = -_g[0]; + } + + inline void eval_hessian (const VecV& _x, const VecC& _c, std::vector& _triplets) const + { + _triplets.clear(); + _triplets.push_back(Triplet(0,0,2)); + _triplets.push_back(Triplet(1,1,2)); + _triplets.push_back(Triplet(0,1,-2)); + _triplets.push_back(Triplet(1,0,-2)); + } +}; + +// create a simple finite element (x-c)^2 +class SimpleElement2 +{ +public: + + // define dimensions + const static int NV = 1; + const static int NC = 1; + + typedef Eigen::Matrix VecI; + typedef Eigen::Matrix VecV; + typedef Eigen::Matrix VecC; + typedef Eigen::Triplet Triplet; + + inline double eval_f(const VecV& _x, const VecC& _c) const + { + return std::pow(_x[0]-_c[0],2); + } + + inline void eval_gradient(const VecV& _x, const VecC& _c, VecV& _g) const + { + _g[0] = 2.0*(_x[0]-_c[0]); + } + + inline void eval_hessian (const VecV& _x, const VecC& _c, std::vector& _triplets) const + { + _triplets.clear(); + _triplets.push_back(Triplet(0,0,2)); + } +}; + + +//------------------------------------------------------------------------------------------------------ + +// Example main +int main(void) +{ + std::cout << "---------- 1) Get an instance of a FiniteElementProblem..." << std::endl; + + // first create sets of different finite elements + COMISO::FiniteElementSet fe_set; + + fe_set.instances().add_element(SimpleElement::VecI(0,1), SimpleElement::VecC()); + fe_set.instances().add_element(SimpleElement::VecI(1,2), SimpleElement::VecC()); + + // second set for boundary conditions + COMISO::FiniteElementSet fe_set2; + SimpleElement2::VecI vi; + SimpleElement2::VecV v; + vi[0] = 0; + v [0] = 0; + fe_set2.instances().add_element(vi, v); + vi[0] = 2; + v [0] = 2; + fe_set2.instances().add_element(vi, v); + + // then create finite element problem and add sets + COMISO::FiniteElementProblem fe_problem(3); + fe_problem.add_set(&fe_set ); + fe_problem.add_set(&fe_set2); + + std::cout << "---------- 2) (optional for debugging) Check derivatives of problem..." << std::endl; + COMISO::NPDerivativeChecker npd; + npd.check_all(&fe_problem); + + // check if IPOPT solver available in current configuration + #if( COMISO_IPOPT_AVAILABLE) + std::cout << "---------- 3) Get IPOPT solver... " << std::endl; + COMISO::IPOPTSolver ipsol; + + std::cout << "---------- 4) Solve..." << std::endl; + // there are no constraints -> provide an empty vector + std::vector constraints; + ipsol.solve(&fe_problem, constraints); + #endif + + // print result + for(unsigned int i=0; i