Skip to content

Commit

Permalink
Merge branch 'develop' into issue_2333
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeandersson committed Dec 9, 2018
2 parents b2d1f2c + e3e3148 commit c7d7b6c
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 243 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ if (WITH_DEPRECATED_FEATURES)
endif()

include_directories(.)
include_directories(${CMAKE_BINARY_DIR})
include_directories(${PROJECT_BINARY_DIR})

#######################################################################
################# 3rd party interface #################################
Expand Down
6 changes: 5 additions & 1 deletion casadi/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ set(CASADI_PUBLIC
dae_builder.hpp
optistack.hpp
serializer.hpp
serializing_stream.hpp

# User include class with the most essential includes
core.hpp
Expand Down Expand Up @@ -179,7 +180,7 @@ set(CASADI_INTERNAL
variable.cpp
dae_builder.cpp
optistack.cpp optistack_internal.cpp optistack_internal.hpp
serializer.cpp serializing_stream.cpp serializing_stream.hpp
serializer.cpp serializing_stream.cpp

# Runtime headers
runtime/casadi_runtime.hpp
Expand All @@ -199,6 +200,9 @@ target_include_directories(casadi
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/runtime>
$<INSTALL_INTERFACE:${TREL_INCLUDE_PREFIX}>
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../../> # <casadi.casadi.hpp>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/../../> # <casadi/core/casadi_export.h>
)

if(WITH_DL)
Expand Down
48 changes: 9 additions & 39 deletions casadi/core/calculus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,54 +215,24 @@ namespace casadi {
using std::pow;
using std::fmod;
using std::atan2;
using std::erf;
using std::fmin;
using std::fmax;
using std::fabs;
using std::atanh;
using std::asinh;
using std::acosh;
using std::isnan;
using std::isinf;
///@}

///@{
// Implement "missing" operations
inline double atanh(double x) throw() {
if (x==-1) return -inf;
if (x==1) return inf;
return 0.5*log((1+x)/(1-x));
}

inline double asinh(double x) throw() {
return log(x + sqrt(1+x*x));
}

inline double acosh(double x) throw() {
return log(x + sqrt(1+x)*sqrt(x-1));
}

inline casadi_int isnan(double x) throw() { return x!=x;}
inline casadi_int isinf(double x) throw() { return isnan(x-x);}

/// Sign function, note that sign(nan) == nan
inline double sign(double x) { return x<0 ? -1 : x>0 ? 1 : x;}

/// fmin, fmax and erf should be available if C99 and/or C++11 required
inline double fmin(double x, double y) throw() { return std::min(x, y);}
inline casadi_int fmin(casadi_int x, casadi_int y) throw() { return std::min(x, y);}
inline double fmax(double x, double y) throw() { return std::max(x, y);}
inline casadi_int fmax(casadi_int x, casadi_int y) throw() { return std::max(x, y);}

/// fabs(casadi_int) was added in C++11
inline casadi_int fabs(casadi_int x) throw() { return std::abs(x);}
///@}

#ifdef HAS_ERF
using ::erf;
#else // HAS ERF
inline double erf(double x) throw() {
// Approximation found in Sourceforge and modified,
// originally from numerical recipes in Fortran
double sx = x<0 ? -1 : x>0 ? 1 : x;
double z = sx*x;
double t = 1.0/(1.0+0.5*z);
return 1.-sx*(t*exp(-z*z-1.26551223+t*(1.00002368+t*(0.37409196+t*(0.09678418+
t*(-0.18628806+t*(0.27886807+t*(-1.13520398+t*(1.48851587+
t*(-0.82215223+t*0.17087277))))))))));
}
#endif // HAS ERF
///@}

///@{
Expand Down
1 change: 1 addition & 0 deletions casadi/core/casadi_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ namespace casadi {
/** \brief Function pointer types for the C API */
typedef void (*signal_t)(void);
typedef casadi_int (*getint_t)(void);
typedef double (*default_t)(casadi_int i);
typedef const char* (*name_t)(casadi_int i);
typedef const casadi_int* (*sparsity_t)(casadi_int i);
typedef void* (*alloc_mem_t)(void);
Expand Down
20 changes: 20 additions & 0 deletions casadi/core/code_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,26 @@ namespace casadi {
return s.str();
}

void CodeGenerator::copy_check(const string& arg, size_t n, const string& res,
bool check_lhs, bool check_rhs) {
std::vector<std::string> checks;
if (check_lhs) checks.push_back(arg);
if (check_rhs) checks.push_back(res);
if (!checks.empty()) *this << "if (" << join(checks, " && ") << ") ";
*this << copy(arg, n, res) << "\n";
}

void CodeGenerator::copy_default(const string& arg, size_t n, const string& res,
const string& def, bool check_rhs) {
*this << "if (" << arg << ") {\n";
if (check_rhs) *this << "if (" << res << ") ";
*this << copy(arg, n, res) << "\n";
*this << "} else {\n";
if (check_rhs) *this << "if (" << res << ") ";
*this << fill(res, n, def) << "\n";
*this << "}\n";
}

string CodeGenerator::fill(const string& res,
std::size_t n, const string& v) {
stringstream s;
Expand Down
4 changes: 4 additions & 0 deletions casadi/core/code_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ namespace casadi {

/** \brief Create a copy operation */
std::string copy(const std::string& arg, std::size_t n, const std::string& res);
void copy_check(const std::string& arg, std::size_t n, const std::string& res,
bool check_lhs=true, bool check_rhs=true);
void copy_default(const std::string& arg, std::size_t n, const std::string& res,
const std::string& def, bool check_rhs=true);

/** \brief Create a fill operation */
std::string fill(const std::string& res, std::size_t n, const std::string& v);
Expand Down
4 changes: 2 additions & 2 deletions casadi/core/conic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,11 @@ namespace casadi {
int Conic::
eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const {
if (print_problem_) {
uout() << "H:" << std::endl;
uout() << "H:";
DM::print_dense(uout(), H_, arg[CONIC_H], false);
uout() << std::endl;
uout() << "G:" << std::vector<double>(arg[CONIC_G], arg[CONIC_G]+nx_) << std::endl;
uout() << "A" << std::endl;
uout() << "A:";
DM::print_dense(uout(), A_, arg[CONIC_A], false);
uout() << std::endl;
uout() << "lba:" << std::vector<double>(arg[CONIC_LBA], arg[CONIC_LBA]+na_) << std::endl;
Expand Down
12 changes: 12 additions & 0 deletions casadi/core/external.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ namespace casadi {
incref_ = (signal_t)li_.get_function(name_ + "_incref");
decref_ = (signal_t)li_.get_function(name_ + "_decref");

// Getting default arguments
get_default_in_ = (default_t)li_.get_function(name_ + "_default_in");

// Getting number of inputs and outputs
get_n_in_ = (getint_t)li_.get_function(name_ + "_n_in");
get_n_out_ = (getint_t)li_.get_function(name_ + "_n_out");
Expand Down Expand Up @@ -122,6 +125,15 @@ namespace casadi {
}
}

double External::get_default_in(casadi_int i) const {
if (get_default_in_) {
return get_default_in_(i);
} else {
// Fall back to base class
return FunctionInternal::get_default_in(i);
}
}

string External::get_name_in(casadi_int i) {
if (get_name_in_) {
// Use function pointer
Expand Down
6 changes: 6 additions & 0 deletions casadi/core/external_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ namespace casadi {
/** \brief Names of inputs and outputs */
name_t get_name_in_, get_name_out_;

/** \brief Get default inputs */
default_t get_default_in_;

/** \brief Work vector sizes */
work_t work_;

Expand Down Expand Up @@ -105,6 +108,9 @@ namespace casadi {
size_t get_n_out() override;
///@}

/** \brief Default inputs */
double get_default_in(casadi_int i) const override;

///@{
/** \brief Names of function input and outputs */
std::string get_name_in(casadi_int i) override;
Expand Down
16 changes: 16 additions & 0 deletions casadi/core/function_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1785,6 +1785,22 @@ namespace casadi {
<< g.declare("casadi_int " + name_ + "_n_out(void)")
<< " { return " << n_out_ << ";}\n\n";

// Default inputs
bool add_defaults = false;
for (casadi_int i=0; i<n_in_; ++i) {
add_defaults |= get_default_in(i)!=0;
}
if (add_defaults) {
g << g.declare("casadi_real " + name_ + "_default_in(casadi_int i)") << "{\n"
<< "switch (i) {\n";
for (casadi_int i=0; i<n_in_; ++i) {
double def = get_default_in(i);
if (def!=0) g << "case " << i << ": return " << g.constant(def) << ";\n";
}
g << "default: return 0;\n}\n"
<< "}\n\n";
}

// Input names
g << g.declare("const char* " + name_ + "_name_in(casadi_int i)") << "{\n"
<< "switch (i) {\n";
Expand Down
1 change: 1 addition & 0 deletions casadi/interfaces/ipopt/ipopt_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ namespace casadi {
{"hess:gamma:x:x"}, {{"gamma", {"f", "g"}}});
}
hesslag_sp_ = get_function("nlp_hess_l").sparsity_out(0);
casadi_assert(hesslag_sp_.is_triu(), "Hessian must be upper triangular");
} else if (pass_nonlinear_variables_) {
nl_ex_ = oracle_.which_depends("x", {"f", "g"}, 2, false);
}
Expand Down
22 changes: 11 additions & 11 deletions casadi/solvers/qrqp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,16 @@ namespace casadi {
g << "casadi_qp_init(&d, &iw, &w);\n";

g.comment("Pass bounds on z");
g << g.copy("arg[" + str(CONIC_LBX)+ "]", nx_, "d.lbz") << "\n";
g << g.copy("arg[" + str(CONIC_LBA)+ "]", na_, "d.lbz+" + str(nx_)) << "\n";
g << g.copy("arg[" + str(CONIC_UBX)+ "]", nx_, "d.ubz") << "\n";
g << g.copy("arg[" + str(CONIC_UBA)+ "]", na_, "d.ubz+" + str(nx_)) << "\n";
g.copy_default("arg[" + str(CONIC_LBX)+ "]", nx_, "d.lbz", "-casadi_inf", false);
g.copy_default("arg[" + str(CONIC_LBA)+ "]", na_, "d.lbz+" + str(nx_), "-casadi_inf", false);
g.copy_default("arg[" + str(CONIC_UBX)+ "]", nx_, "d.ubz", "casadi_inf", false);
g.copy_default("arg[" + str(CONIC_UBA)+ "]", na_, "d.ubz+" + str(nx_), "casadi_inf", false);

g.comment("Pass initial guess");
g << g.copy("arg[" + str(CONIC_X0)+ "]", nx_, "d.z") << "\n";
g.copy_default("arg[" + str(CONIC_X0)+ "]", nx_, "d.z", "0", false);
g << g.fill("d.z+"+str(nx_), na_, "NAN") << "\n";
g << g.copy("arg[" + str(CONIC_LAM_X0)+ "]", nx_, "d.lam") << "\n";
g << g.copy("arg[" + str(CONIC_LAM_A0)+ "]", na_, "d.lam+" + str(nx_)) << "\n";
g.copy_default("arg[" + str(CONIC_LAM_X0)+ "]", nx_, "d.lam", "0", false);
g.copy_default("arg[" + str(CONIC_LAM_A0)+ "]", na_, "d.lam+" + str(nx_), "0", false);

g.comment("Reset solver");
g << "if (casadi_qp_reset(&d)) return 1;\n";
Expand Down Expand Up @@ -353,10 +353,10 @@ namespace casadi {
g << "}\n";

g.comment("Get solution");
g << g.copy("&d.f", 1, "res[" + str(CONIC_COST) + "]") << "\n";
g << g.copy("d.z", nx_, "res[" + str(CONIC_X) + "]") << "\n";
g << g.copy("d.lam", nx_, "res[" + str(CONIC_LAM_X) + "]") << "\n";
g << g.copy("d.lam+"+str(nx_), na_, "res[" + str(CONIC_LAM_A) + "]") << "\n";
g.copy_check("&d.f", 1, "res[" + str(CONIC_COST) + "]", false, true);
g.copy_check("d.z", nx_, "res[" + str(CONIC_X) + "]", false, true);
g.copy_check("d.lam", nx_, "res[" + str(CONIC_LAM_X) + "]", false, true);
g.copy_check("d.lam+"+str(nx_), na_, "res[" + str(CONIC_LAM_A) + "]", false, true);

g << "return flag;\n";
}
Expand Down

0 comments on commit c7d7b6c

Please sign in to comment.