32 #include <gsl/gsl_rng.h> 33 #include <gsl/gsl_randist.h> 34 #include <gsl/gsl_vector.h> 35 #include <gsl/gsl_blas.h> 36 #include <gsl/gsl_multifit_nlin.h> 49 printf (
"iter: %4lu |f(x)| = %g\n",
50 iter, gsl_blas_dnrm2 (s->f));
54 printf (
"%0.10E\n", gsl_vector_get (s->x, i));
60 int species_num_index = 0;
118 for (k = 0; k < 6; k ++)
149 for (k = 0; k < 6; k ++)
180 for (k = 0; k < 6; k ++)
224 for (k = 0; k < 6; k ++)
243 for (k = 0; k < 6; k ++)
275 for (k = 0; k < 6; k ++)
295 printf (
"Inconsistent Fitted Parameter Number!!!\n");
399 double objfun (
const gsl_vector *x,
void *data_point,
int i)
444 OF = 1.0/expaw[i].
sigma * (log(aw) - log(expaw[i].value));
486 OF = 1.0/expph[i].
sigma * (-log(ah)/log(10) - expph[i - exp_sum].
value);
523 expsol[i - exp_sum].
T,
524 expsol[i - exp_sum].
P);
526 OF += (log(ai[k])) * expsol[i - exp_sum].species[k];
529 OF = 1.0/expsol[i - exp_sum].
sigma * (OF - expsol[i - exp_sum].
lnk);
535 int objfun_f (
const gsl_vector *x,
void *data_point, gsl_vector *f)
582 Pi = 1.0/expaw[i].
sigma * (log(aw) - log(expaw[i].value));
586 gsl_vector_set (f, i+exp_sum, Pi);
619 Pi = 1.0/expph[i].
sigma * (-log(ah)/log(10) - expph[i].
value);
621 gsl_vector_set (f, i + exp_sum, Pi);
651 Pi += (log(ai[k])) * expsol[i].species[k];
654 Pi = 1.0/expsol[i].
sigma * (Pi - expsol[i].
lnk);
658 gsl_vector_set (f, i + exp_sum, Pi);
664 int objfun_df (
const gsl_vector *x,
void *data_point, gsl_matrix *J)
674 F1 =
objfun (x, data_point, i);
676 x->data[j] = x->data[j] + h;
677 F2 =
objfun (x, data_point, i);
679 double Jij = (F2 - F1) / h;
681 if (F2 == 0 || F1 == 0) {Jij = 0;}
683 gsl_matrix_set (J, i, j, Jij);
692 int objfun_fdf (
const gsl_vector *x,
void *data_point, gsl_vector *f, gsl_matrix *J)
704 const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
705 gsl_multifit_fdfsolver *s;
712 double chi, chi0, dof;
724 double *x_init = (
double *)malloc(
sizeof (
double) * P);
731 gsl_vector_view x = gsl_vector_view_array (x_init, P);
733 gsl_matrix *J = gsl_matrix_alloc (N, P);
734 gsl_multifit_function_fdf f;
743 s = gsl_multifit_fdfsolver_alloc (T, N, P);
744 gsl_multifit_fdfsolver_set (s, &f, &x.vector);
750 status = gsl_multifit_fdfsolver_iterate (s);
752 printf (
"status = %s\n", gsl_strerror (status));
759 status = gsl_multifit_test_delta (s->dx, s->x, 1.0e-4, 1.0e-4);
761 while (status == GSL_CONTINUE && iter < 1000);
763 printf (
"status = %s\n", gsl_strerror (status));
765 chi = gsl_blas_dnrm2 (s->f);
768 printf (
"OF = %g\n", sqrt(chi / dof));
int objfun_fdf(const gsl_vector *x, void *data_point, gsl_vector *f, gsl_matrix *J)
IIBINPARAM * ii_bin_params
void renew_fitted_params(const gsl_vector *x)
double * total_components
double psc_a(AQUEOUS_PHASE aq, int index, double *n, double T, double P)
SPECIES * aqueous_species
NNBINPARAM * nn_bin_params
int objfun_f(const gsl_vector *x, void *data_point, gsl_vector *f)
NIITERPARAM * nii_ter_params
int gem_ipopt()
Perform Gibbs energy minimization (GEM) using the IPOPT algrithium.
void print_state(size_t iter, gsl_multifit_fdfsolver *s)
void set_aqueous_species()
double objfun(const gsl_vector *x, void *data_point, int i)
int objfun_df(const gsl_vector *x, void *data_point, gsl_matrix *J)
IIITERPARAM * iii_ter_params
NNIIQUAPARAM * nnii_qua_params
NIIIQUAPARAM * niii_qua_params