Skip to content
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

Capd helpers #170

Merged
merged 14 commits into from
Jan 10, 2025
Merged
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
7 changes: 1 addition & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@
option(WITH_CAPD "Using CAPD for accurate integration of ODEs" OFF)

if(WITH_CAPD)
# This looks for capd.pc file
include(FindPkgConfig)
pkg_search_module(PKG_CAPD REQUIRED capd capd-gui mpcapd mpcapd-gui)
include_directories(${PKG_CAPD_INCLUDE_DIRS})
#message(STATUS "[capd2codac] PKG_CAPD_INCLUDE_DIRS = ${PKG_CAPD_INCLUDE_DIRS}")
#message(STATUS "[capd2codac] PKG_CAPD_LDFLAGS = ${PKG_CAPD_LDFLAGS}")
find_package(CAPD REQUIRED)
endif()


Expand Down
40 changes: 40 additions & 0 deletions examples/05_capd_solver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ==================================================================
# codac / basics example - cmake configuration file
# ==================================================================

cmake_minimum_required(VERSION 3.0.2)
project(codac_example LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Adding IBEX

# In case you installed IBEX in a local directory, you need
# to specify its path with the CMAKE_PREFIX_PATH option.
# set(CMAKE_PREFIX_PATH "~/ibex-lib/build_install")

find_package(IBEX REQUIRED)
ibex_init_common() # IBEX should have installed this function
message(STATUS "Found IBEX version ${IBEX_VERSION}")

# Adding Codac

# In case you installed Codac in a local directory, you need
# to specify its path with the CMAKE_PREFIX_PATH option.
# set(CMAKE_PREFIX_PATH "~/codac/build_install")

find_package(CODAC REQUIRED)
message(STATUS "Found Codac version ${CODAC_VERSION}")

# Compilation

if(FAST_RELEASE)
add_compile_definitions(FAST_RELEASE)
message(STATUS "You are running Codac in fast release mode. (option -DCMAKE_BUILD_TYPE=Release is required)")
endif()

add_executable(${PROJECT_NAME} main.cpp)
target_compile_options(${PROJECT_NAME} PUBLIC ${CODAC_CXX_FLAGS})
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CODAC_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${CODAC_LIBRARIES} Ibex::ibex)
64 changes: 64 additions & 0 deletions examples/05_capd_solver/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Author : Maël GODARD
// Adapted from CAPD examples

#include <codac>
#include <codac-capd.h>
#include <capd/capdlib.h>

using namespace std;
using namespace codac2;


int main()
{
// Equation of the pendulum with friction
capd::IMap vectorField("par:l,g;var:t,w;fun:w,-sin(t)*g/l - 0.5*w;");

vectorField.setParameter("l",capd::interval(2.));
vectorField.setParameter("g",capd::interval(10.));

// the solver, is uses high order enclosure method to verify the existence
// of the solution. The order is set to 20.
capd::IOdeSolver solver(vectorField,20);
solver.setAbsoluteTolerance(1e-10);
solver.setRelativeTolerance(1e-10);

capd::ITimeMap timeMap(solver);
capd::interval initialTime (0.);
capd::interval finalTime (20.);
capd::ITimeMap::SolutionCurve solution(initialTime);

// initial set
capd::IVector c(2);
c[0] = -M_PI/2.;
c[1] = 0.;
// take some box around c
c[0] += capd::interval(-1,1)*1e-2;
c[1] += capd::interval(-1,1)*1e-2;


// define a doubleton representation of the interval vector c
capd::C0HORect2Set s(c);

timeMap(finalTime,s,solution);

// we integrate the set s over the time T
capd::interval T(1);

cout << "\ninitial set: " << c;
cout << "\ndiam(initial set): " << diam(c) << endl;

capd::IVector result = timeMap(T,s);

cout << "\n\nafter time=" << T << " the image is: " << result;
cout << "\ndiam(image): " << diam(result) << endl << endl;

DefaultView::set_axes(axis(0,{-2,1.5}),axis(1,{-2,3}));

for (float t=0.;t<20.;t+=0.05)
DefaultView::draw_box(to_codac(solution(t)));

DefaultView::draw_box(to_codac(c),Color::green());
DefaultView::draw_box(to_codac(result),Color::red());

}
18 changes: 11 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#if(WITH_PYTHON)
# add_subdirectory(sympy)
#endif()

if(WITH_CAPD)
add_subdirectory(capd)
endif()


# Generating PKG file
Expand Down Expand Up @@ -71,7 +75,6 @@
set(CODAC_CXX_FLAGS \"\")
")


#if(WITH_PYTHON)
#
# file(APPEND ${CODAC_CMAKE_CONFIG_FILE} "
Expand All @@ -90,25 +93,26 @@
#
#endif()


if(WITH_CAPD)

file(APPEND ${CODAC_CMAKE_CONFIG_FILE} "

# Optional 3rd party:

find_path(CODAC_CAPD_INCLUDE_DIR ${PROJECT_NAME}-sympy.h
PATH_SUFFIXES include/${PROJECT_NAME}-sympy)
find_package(CAPD REQUIRED)

find_path(CODAC_CAPD_INCLUDE_DIR ${PROJECT_NAME}-capd.h
PATH_SUFFIXES include/${PROJECT_NAME}-capd)
set(CODAC_INCLUDE_DIRS \${CODAC_INCLUDE_DIRS} \${CODAC_CAPD_INCLUDE_DIR})

find_library(CODAC_CAPD_LIBRARY NAMES ${PROJECT_NAME}-sympy
find_library(CODAC_CAPD_LIBRARY NAMES ${PROJECT_NAME}-capd
PATH_SUFFIXES lib)
set(CODAC_LIBRARIES \${CODAC_LIBRARIES} \${CODAC_CAPD_LIBRARY})

set(CODAC_LIBRARIES \${CODAC_LIBRARIES} \${CODAC_CAPD_LIBRARY} capd::capd)
")

endif()


install(FILES ${CODAC_CMAKE_CONFIG_FILE} DESTINATION ${CMAKE_INSTALL_CMAKE})


Expand Down
60 changes: 60 additions & 0 deletions src/capd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ==================================================================
# Codac - cmake configuration file
# ==================================================================

list(APPEND CODAC_CAPD_SRC

${CMAKE_CURRENT_SOURCE_DIR}/codac2_capd.cpp
${CMAKE_CURRENT_SOURCE_DIR}/codac2_capd.h
)

################################################################################
# Create the target for libcodac-capd
################################################################################

#if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#endif()

add_library(${PROJECT_NAME}-capd ${CODAC_CAPD_SRC})
target_link_libraries(${PROJECT_NAME}-capd PUBLIC ${PROJECT_NAME}-core Ibex::ibex Eigen3::Eigen capd::capd)


################################################################################
# For the generation of the PKG file
################################################################################

set(CODAC_PKG_CONFIG_CFLAGS "${CODAC_PKG_CONFIG_CFLAGS} -I\${includedir}/${PROJECT_NAME}-capd" PARENT_SCOPE)
set(CODAC_PKG_CONFIG_LIBS "${CODAC_PKG_CONFIG_LIBS} -l${PROJECT_NAME}-capd" PARENT_SCOPE)


################################################################################
# Installation of libcodac-capd files
################################################################################

# Getting header files from sources

foreach(srcfile ${CODAC_CAPD_SRC})
if(srcfile MATCHES "\\.h$" OR srcfile MATCHES "\\.hpp$")
list(APPEND CODAC_CAPD_HDR ${srcfile})
file(COPY ${srcfile} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include)
endif()
endforeach()

# Generating the file codac-capd.h

set(CODAC_CAPD_MAIN_HEADER ${CMAKE_CURRENT_BINARY_DIR}/codac-capd.h)
file(WRITE ${CODAC_CAPD_MAIN_HEADER} "/* This file is generated by CMake */\n\n")
file(APPEND ${CODAC_CAPD_MAIN_HEADER} "#pragma once\n\n")
foreach(header_path ${CODAC_CAPD_HDR})
get_filename_component(header_name ${header_path} NAME)
file(APPEND ${CODAC_CAPD_MAIN_HEADER} "#include <${header_name}>\n")
endforeach()
file(COPY ${CODAC_CAPD_MAIN_HEADER} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/../../include)

# Install files in system directories

install(TARGETS ${PROJECT_NAME}-capd DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${CODAC_CAPD_HDR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-capd)
install(FILES ${CODAC_CAPD_MAIN_HEADER} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-capd)
66 changes: 66 additions & 0 deletions src/capd/codac2_capd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* codac2_capd.cpp
* ----------------------------------------------------------------------------
* \date 2024
* \author Maël Godard
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include <cassert>
#include "codac2_capd.h"

using namespace std;

namespace codac2
{
capd::Interval to_capd(const Interval &x)
{
return {x.lb(), x.ub()};
}

Interval to_codac(const capd::Interval &x)
{
return {x.leftBound(), x.rightBound()};
}

capd::IVector to_capd(const IntervalVector &x)
{
capd::IVector y(x.size());
for (Index i = 0; i < (Index)x.size(); i++)
{
y[i] = to_capd(x[i]);
}
return y;
}

IntervalVector to_codac(const capd::IVector &x)
{
IntervalVector y(x.dimension());
for (Index i = 0; i < (Index)x.dimension(); i++)
{
y[i] = to_codac(x[i]);
}
return y;
}

capd::IMatrix to_capd(const IntervalMatrix &x)
{
capd::IMatrix y(x.rows(), x.cols());
for (Index i = 0; i < x.rows(); i++)
for (Index j = 0; j < x.cols(); j++)
y[i][j] = to_capd(x(i, j));
return y;
}

IntervalMatrix to_codac(const capd::IMatrix &x)
{
IntervalMatrix y(x.numberOfRows(), x.numberOfColumns());
for (Index i = 0; i < (Index)x.numberOfRows(); i++)
for (Index j = 0; j < (Index)x.numberOfColumns(); j++)
y(i, j) = to_codac(x[i][j]);
return y;
}


}
70 changes: 70 additions & 0 deletions src/capd/codac2_capd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* \file codac2_capd.h
* ----------------------------------------------------------------------------
* \date 2024
* \author Maël Godard
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#pragma once

// #include "capd/capdlib.h"
#include <capd/intervals/lib.h>
#include <capd/vectalg/lib.h>
#include "codac2_IntervalVector.h"
#include "codac2_Matrix.h"
#include "codac2_IntervalMatrix.h"

namespace codac2
{

/**
* \brief Casts a Codac Interval object into an CAPD Interval object
*
* \param x const Codac type Interval
* \return CAPD type Interval
*/
capd::Interval to_capd(const codac2::Interval& x);

/**
* \brief Casts an CAPD Interval object into a Codac Interval object
*
* \param x const CAPD type Interval
* \return Codac type Interval
*/
codac2::Interval to_codac(const capd::Interval& x);

/**
* \brief Casts a Codac IntervalVector object into an CAPD IntervalVector object
*
* \param x const Codac type IntervalVector
* \return CAPD type IntervalVector
*/
capd::IVector to_capd(const codac2::IntervalVector& x);

/**
* \brief Casts an CAPD IntervalVector object into a Codac IntervalVector object
*
* \param x const CAPD type IntervalVector
* \return Codac type IntervalVector
*/
codac2::IntervalVector to_codac(const capd::IVector& x);

/**
* \brief Casts a Codac IntervalMatrix object into an CAPD IntervalMatrix object
*
* \param x const Codac type IntervalMatrix
* \return CAPD type IntervalMatrix
*/
capd::IMatrix to_capd(const codac2::IntervalMatrix& x);

/**
* \brief Casts an CAPD IntervalMatrix object into a Codac IntervalMatrix object
*
* \param x const CAPD type IntervalMatrix
* \return Codac type IntervalMatrix
*/
codac2::IntervalMatrix to_codac(const capd::IMatrix& x);

}
2 changes: 1 addition & 1 deletion src/core/3rd/codac2_ibex.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* codac2_ibex.h
* codac2_ibex.cpp
* ----------------------------------------------------------------------------
* \date 2024
* \author Gilles Chabert, (Simon Rohou)
Expand Down
Loading
Loading