26 #include "fast_newton.hpp"
28 #include "../core/linsol_internal.hpp"
33 int CASADI_ROOTFINDER_FAST_NEWTON_EXPORT
36 plugin->name =
"fast_newton";
38 plugin->version = CASADI_VERSION;
61 "Stopping criterion tolerance on ||g||__inf)"}},
64 "Stopping criterion tolerance on step size"}},
67 "Maximum number of Newton iterations to perform before returning."}}
82 for (
auto&& op : opts) {
83 if (op.first==
"max_iter") {
85 }
else if (op.first==
"abstol") {
87 }
else if (op.first==
"abstolStep") {
93 "Newton: the supplied f must have at least one input.");
95 "Newton::init: linear_solver must be supplied");
115 casadi_int*& iw,
double*& w)
const {
148 for (m->iter=0; m->iter<
max_iter_; ++m->iter) {
151 for (casadi_int i=0;i<
n_in_;++i) m->arg[i] = m->iarg[i];
153 for (casadi_int i=0;i<
n_out_;++i) m->res[i+1] = m->ires[i];
154 m->res[0] = M->jac_g_x;
155 m->res[1+
iout_] = M->g;
156 jac_g_x_(m->arg, m->res, m->iw, m->w);
159 if (m->return_status)
break;
164 m->success = m->return_status>0;
173 g.
local(
"m",
"struct casadi_newton_mem");
175 g <<
"m.n = " <<
n_ <<
";\n";
176 g <<
"m.abstol = " <<
abstol_ <<
";\n";
179 casadi_int w_offset = 0;
180 g <<
"m.x = w;\n"; w_offset+=
n_;
181 g <<
"m.g = w+" +
str(w_offset) +
";\n"; w_offset+=
n_;
182 g <<
"m.jac_g_x = w+" +
str(w_offset) +
";\n"; w_offset+=
sp_jac_.
nnz();
191 g <<
"m.lin_v = w+" +
str(w_offset) +
";\n"; w_offset+=
sp_v_.
nnz();
192 g <<
"m.lin_r = w+" +
str(w_offset) +
";\n"; w_offset+=
sp_r_.
nnz();
193 g <<
"m.lin_beta = w+" +
str(w_offset) +
";\n"; w_offset+=
sp_jac_.
size2();
195 g.
comment(
"Get the initial guess");
198 g.
local(
"iter",
"casadi_int");
199 g <<
"for (iter=0; iter<" +
str(
max_iter_) +
"; ++iter) {\n";
201 g.
comment(
"(re)calculate f and J");
203 for (casadi_int i=0;i<
n_in_;++i) {
207 for (casadi_int i=0;i<
n_out_;++i) {
212 g <<
"if (" << flag <<
") return 1;\n";
213 g <<
"if (casadi_newton(&m)) break;\n";
236 return "max_iteration_reached";
238 return "converged_abstol";
240 return "converged_abstol_step";
249 stats[
"return_status"] =
return_code(m->return_status);
250 stats[
"iter_count"] = m->iter;
Helper class for C code generation.
std::string add_dependency(const Function &f)
Add a function dependency.
std::string arg(casadi_int i) const
Refer to argument.
std::string copy(const std::string &arg, std::size_t n, const std::string &res)
Create a copy operation.
void comment(const std::string &s)
Write a comment line (ignored if not verbose)
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string res(casadi_int i) const
Refer to resuly.
std::string sparsity(const Sparsity &sp, bool canonical=true)
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
int solve(void *mem) const override
Solve the system of equations and calculate derivatives.
static Rootfinder * creator(const std::string &name, const Function &f)
Create a new Rootfinder.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
Sparsity sp_v_
Data for qr.
static const std::string meta_doc
A documentation string.
int init_mem(void *mem) const override
Initalize memory block.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize into MX.
Function jac_g_x_
Reference to jacobian function.
double abstol_
Absolute tolerance that should be met on residual.
static const Options options_
Options.
void codegen_declarations(CodeGenerator &g) const override
Generate code for the declarations of the C function.
FastNewton(const std::string &name, const Function &f)
Constructor.
casadi_int max_iter_
Maximum number of Newton iterations.
void init(const Dict &opts) override
Initialize.
Dict get_stats(void *mem) const override
Get all statistics.
double abstolStep_
Absolute tolerance that should be met on step.
void codegen_body(CodeGenerator &g) const override
Generate code for the function body.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
~FastNewton() override
Destructor.
std::vector< casadi_int > pc_
std::vector< casadi_int > prinv_
size_t n_in_
Number of inputs and outputs.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
casadi_int n_in() const
Get the number of function inputs.
bool is_null() const
Is a null pointer?
Function oracle_
Oracle: Used to generate other functions.
std::vector< std::string > get_function() const override
Get list of dependency functions.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
void clear_mem()
Clear all memory (called from destructor)
int init_mem(void *mem) const override
Initalize memory block.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
casadi_int n_
Number of equations.
Dict get_stats(void *mem) const override
Get all statistics.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
casadi_int iin_
Indices of the input and output that correspond to the actual root-finding.
static const Options options_
Options.
Linsol linsol_
Linear solver.
void init(const Dict &opts) override
Initialize.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
casadi_int size1() const
Get the number of rows.
casadi_int nnz() const
Get the number of (structural) non-zeros.
casadi_int size2() const
Get the number of columns.
void qr_sparse(Sparsity &V, Sparsity &R, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true) const
Symbolic QR factorization.
int CASADI_ROOTFINDER_FAST_NEWTON_EXPORT casadi_register_rootfinder_fast_newton(Rootfinder::Plugin *plugin)
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
void CASADI_ROOTFINDER_FAST_NEWTON_EXPORT casadi_load_rootfinder_fast_newton()
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::string return_code(casadi_int status)
int casadi_newton(const casadi_newton_mem< T1 > *m)
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
Options metadata for a class.