26 #ifdef MANGO_GSL_AVAILABLE
27 #include <gsl/gsl_vector.h>
28 #include <gsl/gsl_matrix.h>
29 #include <gsl/gsl_multifit_nlinear.h>
33 #ifdef MANGO_GSL_AVAILABLE
34 if (solver->
verbose>0) std::cout <<
"Hello from optimize_least_squares_gsl" << std::endl;
36 gsl_vector *gsl_residual = gsl_vector_alloc(solver->
N_terms);
37 gsl_vector *gsl_state_vector = gsl_vector_alloc(solver->
N_parameters);
38 gsl_multifit_nlinear_fdf gsl_optimizer;
39 gsl_multifit_nlinear_parameters gsl_optimizer_params = gsl_multifit_nlinear_default_parameters();
41 gsl_optimizer.f = gsl_residual_function;
42 gsl_optimizer.df = gsl_residual_function_and_Jacobian;
44 gsl_optimizer.n = solver->
N_terms;
46 gsl_optimizer.params = (
void*)solver;
53 gsl_optimizer_params.trs = gsl_multifit_nlinear_trs_lm;
56 gsl_optimizer_params.trs = gsl_multifit_nlinear_trs_dogleg;
59 gsl_optimizer_params.trs = gsl_multifit_nlinear_trs_ddogleg;
62 gsl_optimizer_params.trs = gsl_multifit_nlinear_trs_subspace2D;
65 throw std::runtime_error(
"Error! in optimize_least_squares_gsl.cpp switch! Should not get here!");
69 gsl_optimizer_params.solver = gsl_multifit_nlinear_solver_svd;
70 const gsl_multifit_nlinear_type *T = gsl_multifit_nlinear_trust;
72 const double xtol = 1.0e-8;
73 const double gtol = 1.0e-8;
74 const double ftol = 1.0e-8;
75 gsl_multifit_nlinear_workspace *work = gsl_multifit_nlinear_alloc(T, &gsl_optimizer_params, solver->
N_terms, solver->
N_parameters);
76 gsl_vector * f = gsl_multifit_nlinear_residual(work);
77 gsl_vector * x = gsl_multifit_nlinear_position(work);
81 gsl_multifit_nlinear_init(gsl_state_vector, &gsl_optimizer, work);
82 gsl_multifit_nlinear_driver(max_iter, xtol, gtol, ftol, NULL, NULL, &info, work);
84 gsl_multifit_nlinear_free(work);
85 gsl_vector_free(gsl_residual);
86 gsl_vector_free(gsl_state_vector);
87 if (solver->
verbose>0) std::cout <<
"Goodbye from optimize_least_squares_gsl" << std::endl;
89 throw std::runtime_error(
"Error! A GSL algorithm was requested, but Mango was compiled without GSL support.");
95 #ifdef MANGO_GSL_AVAILABLE
97 int mango::Package_gsl::gsl_residual_function(
const gsl_vector * x,
void *params, gsl_vector * f) {
100 if (solver->
verbose > 0) std::cout <<
"Hello from gsl_residual_function. f stride = " << f->stride << std::endl << std::flush;
107 assert(x->stride == 1);
113 for (
int j=0; j<solver->
N_terms; j++) {
118 if (solver->
verbose > 0) std::cout <<
"Goodbye from gsl_residual_function" << std::endl << std::flush;
125 int mango::Package_gsl::gsl_residual_function_and_Jacobian (
const gsl_vector * x,
void *params, gsl_matrix * J) {
126 Least_squares_solver* solver = (Least_squares_solver*)params;
128 if (solver->verbose > 0) std::cout <<
"Hello from gsl_residual_function_and_Jacobian" << std::endl << std::flush;
130 assert(solver->mpi_partition->get_proc0_world());
135 assert(x->stride == 1);
138 double* mango_Jacobian =
new double[solver->N_parameters * solver->N_terms];
139 solver->finite_difference_Jacobian(x->data, solver->residuals, mango_Jacobian);
145 int N_parameters = solver->N_parameters;
146 int N_terms = solver->N_terms;
150 for (
int j_parameter = 0; j_parameter < N_parameters; j_parameter++) {
151 for (
int j_term = 0; j_term < N_terms; j_term++) {
154 gsl_matrix_set(J, j_term, j_parameter, mango_Jacobian[j_parameter*N_terms+j_term] / solver->sigmas[j_term]);
158 delete[] mango_Jacobian;
160 if (solver->verbose > 0) std::cout <<
"Goodbye from gsl_residual_function_and_Jacobian" << std::endl << std::flush;