8 #include "gsl/gsl_blas.h" 9 #include "gsl/gsl_errno.h" 10 #include "gsl/gsl_linalg.h" 11 #include "gsl/gsl_math.h" 12 #include "gsl/gsl_matrix.h" 13 #include "gsl/gsl_multimin.h" 14 #include "gsl/gsl_vector.h" 32 : m_argum( arg ), m_eqs( &funcs ) {
33 const size_t N = funcs.size();
34 for (
size_t i = 0; i <
N; ++i ) {
36 for (
size_t j = 0; j <
N; ++j ) {
37 Genfun::GENFUNCTION fij = funcs[i]->partial( j );
38 last.push_back( fij.clone() );
40 m_jac.push_back( last );
49 while ( !last.empty() ) {
63 int fun_gsl(
const gsl_vector* v,
void* params, gsl_vector* f ) {
65 const EqSolver::Equations& eqs = *( local->
equations() );
68 for (
unsigned int i = 0; i < v->size; ++i ) { arg[i] = gsl_vector_get( v, i ); }
70 for (
unsigned int i = 0; i < v->size; ++i ) { gsl_vector_set( f, i, ( *eqs[i] )( arg ) ); }
76 int dfun_gsl(
const gsl_vector* v,
void* params, gsl_matrix* df ) {
81 for (
unsigned int i = 0; i < v->size; ++i ) { arg[i] = gsl_vector_get( v, i ); }
83 for (
unsigned int i = 0; i < v->size; ++i ) {
84 for (
unsigned int j = 0; j < v->size; ++j ) {
85 Genfun::GENFUNCTION f = *( jac[i][j] );
86 gsl_matrix_set( df, i, j, f( arg ) );
95 int fdfun_gsl(
const gsl_vector* v,
void* params, gsl_vector* f, gsl_matrix* df ) {
97 const EqSolver::Equations& eqs = *( local->
equations() );
101 for (
unsigned int i = 0; i < v->size; ++i ) { arg[i] = gsl_vector_get( v, i ); }
103 for (
unsigned int i = 0; i < v->size; ++i ) {
104 gsl_vector_set( f, i, ( *eqs[i] )( arg ) );
105 for (
unsigned int j = 0; j < v->size; ++j ) {
106 Genfun::GENFUNCTION f1 = *( jac[i][j] );
107 gsl_matrix_set( df, i, j, f1( arg ) );
124 gsl_vector_view vect = gsl_vector_view_array( &arg[0], arg.dimension() );
127 const gsl_multiroot_fdfsolver_type* T =
m_type;
128 gsl_multiroot_fdfsolver*
s =
nullptr;
132 gsl_multiroot_function_fdf
function;
134 function.f = &fun_gsl;
135 function.df = &dfun_gsl;
136 function.fdf = &fdfun_gsl;
137 function.n = vect.vector.size;
138 function.params = &local;
140 s = gsl_multiroot_fdfsolver_alloc( T, vect.vector.size );
141 gsl_multiroot_fdfsolver_set( s, &
function, &vect.vector );
144 status = gsl_multiroot_fdfsolver_iterate( s );
146 return Error(
"Error from gsl_gsl_multiroot_fdfsolver_iterate '" +
std::string( gsl_strerror( status ) ) +
"'" );
151 if ( status != GSL_CONTINUE ) {
break; }
154 for (
unsigned int i = 0; i < vect.vector.size; ++i ) {
155 gsl_vector_set( &vect.vector, i, gsl_vector_get( s->x, i ) );
158 if ( status == GSL_SUCCESS ) {
159 debug() <<
"We stopped in the method on the " << iter <<
" iteration (we have maximum " << m_max_iter
160 <<
" iterations)" <<
endmsg;
161 }
else if ( status == GSL_CONTINUE && iter <= m_max_iter ) {
162 return Error(
"Method finished with '" +
std::string( gsl_strerror( status ) ) +
"' error" );
164 return Error(
"Method finished with '" +
std::string( gsl_strerror( status ) ) +
"' error" );
167 gsl_multiroot_fdfsolver_free( s );
169 if ( status ) {
return Error(
"Method finished with '" +
std::string( gsl_strerror( status ) ) +
"' error" ); }
177 if ( sc.
isFailure() ) {
return Error(
"Could not initiliaze base class GaudiTool", sc ); }
182 if (
"fdfsolver_hybridsj" ==
m_algType ) {
183 m_type = gsl_multiroot_fdfsolver_hybridsj;
184 debug() <<
"Root finding algorithm to be used: " 185 <<
"'gsl_multiroot_fdfsolver_hybridsj'" <<
endmsg;
186 }
else if (
"fdfsolver_hybridj" ==
m_algType ) {
187 m_type = gsl_multiroot_fdfsolver_hybridj;
188 debug() <<
"Root finding algorithm to be used: " 189 <<
"'gsl_multiroot_fdfsolver_hybridj'" <<
endmsg;
190 }
else if (
"fdfsolver_newton" ==
m_algType ) {
191 m_type = gsl_multiroot_fdfsolver_newton;
192 debug() <<
"Root findind algorithm to be used: " 193 <<
"'gsl_multiroot_fdfsolver_newton'" <<
endmsg;
194 }
else if (
"fdfsolver_gnewton" ==
m_algType ) {
195 m_type = gsl_multiroot_fdfsolver_gnewton;
196 debug() <<
"Root findind algorithm to be used: " 197 <<
"'gsl_multiroot_fdfsolver_gnewton'" <<
endmsg;
constexpr static const auto SUCCESS
const Jacobi & jacobi() const
#define DECLARE_COMPONENT(type)
Gaudi::Property< std::string > m_algType
The simplest concrete implementation of IEqSolver interface.
Gaudi::Property< double > m_max_iter
StatusCode initialize() override
Overriding initialize.
const Arg & argument() const
This class is used for returning status codes from appropriate routines.
const Equations * equations() const
StatusCode solver(const Equations &funcs, Arg &arg) const override
Solving nonlinear system with N equations in N unknowns of the function "GenFunc".
const gsl_multiroot_fdfsolver_type * m_type
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Gaudi::Property< double > m_norm_residual