31 #include "ipopt/IpStdCInterface.h" 63 static Bool eval_f (Index n, Number* x, Bool new_x,
64 Number* obj_value, UserDataPtr user_data);
85 static Bool eval_grad_f (Index n, Number* x, Bool new_x,
86 Number* grad_f, UserDataPtr user_data);
107 static Bool eval_g (Index n, Number* x, Bool new_x,
108 Index m, Number* g, UserDataPtr user_data);
132 static Bool eval_jac_g (Index n, Number *x, Bool new_x,
133 Index m, Index nele_jac,
134 Index *iRow, Index *jCol, Number *values,
135 UserDataPtr user_data);
164 static Bool eval_h (Index n, Number *x, Bool new_x, Number obj_factor,
165 Index m, Number *lambda, Bool new_lambda,
166 Index nele_hess, Index *iRow, Index *jCol,
167 Number *values, UserDataPtr user_data);
175 static Bool intermediate_cb (Index alg_mod, Index iter_count, Number obj_value,
176 Number inf_pr, Number inf_du, Number mu, Number d_norm,
177 Number regularization_size, Number alpha_du,
178 Number alpha_pr, Index ls_trials, UserDataPtr user_data);
188 IpoptProblem nlp = NULL;
189 enum ApplicationReturnStatus status;
191 Number* mult_g = NULL;
193 Number* mult_x_L = NULL;
195 Number* mult_x_U = NULL;
206 Index index_style = 0;
211 x_L = (Number*)malloc(
sizeof(Number) * n);
212 x_U = (Number*)malloc(
sizeof(Number) * n);
215 for (i = 0; i < n; i++)
223 g_L = (Number*)malloc(
sizeof(Number) * m);
224 g_U = (Number*)malloc(
sizeof(Number) * m);
227 for (i = 0; i < m; i++)
234 nlp = CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess,
235 index_style, &eval_f, &eval_g, &eval_grad_f,
236 &eval_jac_g, &eval_h);
247 AddIpoptNumOption(nlp,
"tol", 1e-10);
248 AddIpoptStrOption(nlp,
"mu_strategy",
"adaptive");
249 AddIpoptStrOption(nlp,
"hessian_approximation",
"limited-memory");
250 AddIpoptIntOption(nlp,
"print_level", 0);
254 x = (Number*)malloc(
sizeof(Number) * n);
256 for (i = 0; i < n; i ++)
262 mult_g = (Number*)malloc(
sizeof(Number) * m);
263 mult_x_L = (Number*)malloc(
sizeof(Number) * n);
264 mult_x_U = (Number*)malloc(
sizeof(Number) * n);
272 status = IpoptSolve(nlp, x, NULL, &obj, mult_g, mult_x_L, mult_x_U, NULL);
274 if (status == Solve_Succeeded)
276 printf(
"\nSUCCESSED IPOPT OPTIMIZATION.\n");
281 printf(
"\nERROR OCCURRED DURING IPOPT OPTIMIZATION.\n");
285 if (status == Solve_Succeeded)
287 n_eq = (
double *)malloc (
sizeof(
double) * n);
289 for (i=0; i < n; i++)
300 FreeIpoptProblem(nlp);
310 static Bool eval_f(Index n, Number* x, Bool new_x,
311 Number* obj_value, UserDataPtr user_data)
322 int species_num_index = 0;
332 ngi[j] = x[j + species_num_index];
350 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
351 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
354 *obj_value += ngi[j] * (G0 +
R * T * log (ngi[j]/sumn) / 1000);
356 species_num_index ++;
368 naqi[j] = x[j + species_num_index];
386 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
387 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
391 *obj_value += naqi[j] * (G0 +
R * T * log (aj) / 1000);
393 species_num_index ++;
405 nsi[j] = x[j + species_num_index];
418 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
419 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
421 *obj_value += nsi[j] * G0;
423 species_num_index ++;
435 nssi[j] = x[j + species_num_index];
453 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
454 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
458 *obj_value += nssi[j] * (G0 +
R * T * log (aj) / 1000);
460 species_num_index ++;
467 static Bool eval_grad_f(Index n, Number* x, Bool new_x,
468 Number* grad_f, UserDataPtr user_data)
476 int species_num_index = 0;
488 ngi[j] = x[j + species_num_index];
506 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
507 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
509 grad_f[species_num_index] = (G0 +
R * T * log (ngi[j]/sumn) / 1000);
511 species_num_index ++;
523 naqi[j] = x[j + species_num_index];
541 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
542 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
546 grad_f[species_num_index] = (G0 +
R * T * log (aj) / 1000);
548 species_num_index ++;
560 nsi[j] = x[j + species_num_index];
578 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
579 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
581 grad_f[species_num_index] = (G0);
583 species_num_index ++;
595 nssi[j] = x[j + species_num_index];
613 double G0 = A * (t - t*log(t)) - B/2.0 *t*t - C/6.0 *t*t*t -
614 D/12.0 * t*t*t*t - E/2.0/t + F - G * t;
618 grad_f[species_num_index] = (G0 +
R * T * log(aj) / 1000);
620 species_num_index ++;
627 static Bool eval_g(Index n, Number* x, Bool new_x,
628 Index m, Number* g, UserDataPtr user_data)
636 for (i = 0; i < m - 1; i ++)
640 int species_num_index = 0;
649 species_num_index ++;
660 species_num_index ++;
671 species_num_index ++;
682 species_num_index ++;
693 int species_num_index = 0;
702 species_num_index ++;
713 species_num_index ++;
724 species_num_index ++;
735 species_num_index ++;
745 static Bool eval_jac_g(Index n, Number *x, Bool new_x,
746 Index m, Index nele_jac,
747 Index *iRow, Index *jCol, Number *values,
748 UserDataPtr user_data)
757 for (i = 0; i < m; i ++)
759 for (j = 0; j < n; j ++)
770 int species_num_index = 0;
776 for (k = 0; k < m - 1; k ++)
784 species_num_index ++;
793 for (k = 0; k < m - 1; k ++)
800 species_num_index ++;
816 species_num_index ++;
832 species_num_index ++;
840 static Bool eval_h(Index n, Number *x, Bool new_x, Number obj_factor,
841 Index m, Number *lambda, Bool new_lambda,
842 Index nele_hess, Index *iRow, Index *jCol,
843 Number *values, UserDataPtr user_data)
848 static Bool intermediate_cb(Index alg_mod, Index iter_count, Number obj_value,
849 Number inf_pr, Number inf_du, Number mu, Number d_norm,
850 Number regularization_size, Number alpha_du,
851 Number alpha_pr, Index ls_trials, UserDataPtr user_data)
853 printf(
"Testing intermediate callback in iteration %d\n", iter_count);
854 if (inf_pr < 1e-4)
return FALSE;
double rkg_a(SOLIDSOLUTION_PHASE ss, int index, double *n, double T, double P)
int solidsolution_phase_num
double * total_components
double psc_a(AQUEOUS_PHASE aq, int index, double *n, double T, double P)
SPECIES * aqueous_species
int gem_ipopt()
Perform Gibbs energy minimization (GEM) using the IPOPT algrithium.
SOLIDSOLUTION_PHASE * solidsolution
SPECIES * solidsolution_species