/usr/share/doc/libsundials-serial-dev/examples/kinsol/serial/kinFerTron_dns.c is in libsundials-serial-dev 2.5.0-3ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | /*
* -----------------------------------------------------------------
* $Revision: 1.2 $
* $Date: 2008/12/17 19:38:48 $
* -----------------------------------------------------------------
* Programmer(s): Radu Serban @ LLNL
* -----------------------------------------------------------------
* Example (serial):
*
* This example solves a nonlinear system from.
*
* Source: "Handbook of Test Problems in Local and Global Optimization",
* C.A. Floudas, P.M. Pardalos et al.
* Kluwer Academic Publishers, 1999.
* Test problem 4 from Section 14.1, Chapter 14: Ferraris and Tronconi
*
* This problem involves a blend of trigonometric and exponential terms.
* 0.5 sin(x1 x2) - 0.25 x2/pi - 0.5 x1 = 0
* (1-0.25/pi) ( exp(2 x1)-e ) + e x2 / pi - 2 e x1 = 0
* such that
* 0.25 <= x1 <=1.0
* 1.5 <= x2 <= 2 pi
*
* The treatment of the bound constraints on x1 and x2 is done using
* the additional variables
* l1 = x1 - x1_min >= 0
* L1 = x1 - x1_max <= 0
* l2 = x2 - x2_min >= 0
* L2 = x2 - x2_max >= 0
*
* and using the constraint feature in KINSOL to impose
* l1 >= 0 l2 >= 0
* L1 <= 0 L2 <= 0
*
* The Ferraris-Tronconi test problem has two known solutions.
* The nonlinear system is solved by KINSOL using different
* combinations of globalization and Jacobian update strategies
* and with different initial guesses (leading to one or the other
* of the known solutions).
*
*
* Constraints are imposed to make all components of the solution
* positive.
* -----------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <kinsol/kinsol.h>
#include <kinsol/kinsol_dense.h>
#include <nvector/nvector_serial.h>
#include <sundials/sundials_types.h>
#include <sundials/sundials_math.h>
/* Problem Constants */
#define NVAR 2
#define NEQ 3*NVAR
#define FTOL RCONST(1.e-5) /* function tolerance */
#define STOL RCONST(1.e-5) /* step tolerance */
#define ZERO RCONST(0.0)
#define PT25 RCONST(0.25)
#define PT5 RCONST(0.5)
#define ONE RCONST(1.0)
#define ONEPT5 RCONST(1.5)
#define TWO RCONST(2.0)
#define PI RCONST(3.1415926)
#define E RCONST(2.7182818)
typedef struct {
realtype lb[NVAR];
realtype ub[NVAR];
} *UserData;
/* Accessor macro */
#define Ith(v,i) NV_Ith_S(v,i-1)
/* Functions Called by the KINSOL Solver */
static int func(N_Vector u, N_Vector f, void *user_data);
/* Private Helper Functions */
static void SetInitialGuess1(N_Vector u, UserData data);
static void SetInitialGuess2(N_Vector u, UserData data);
static int SolveIt(void *kmem, N_Vector u, N_Vector s, int glstr, int mset);
static void PrintHeader(int globalstrategy, realtype fnormtol,
realtype scsteptol);
static void PrintOutput(N_Vector u);
static void PrintFinalStats(void *kmem);
static int check_flag(void *flagvalue, char *funcname, int opt);
/*
*--------------------------------------------------------------------
* MAIN PROGRAM
*--------------------------------------------------------------------
*/
int main()
{
UserData data;
realtype fnormtol, scsteptol;
N_Vector u1, u2, u, s, c;
int glstr, mset, flag;
void *kmem;
u1 = u2 = u = NULL;
s = c = NULL;
kmem = NULL;
data = NULL;
glstr = KIN_NONE;
/* User data */
data = (UserData)malloc(sizeof *data);
data->lb[0] = PT25; data->ub[0] = ONE;
data->lb[1] = ONEPT5; data->ub[1] = TWO*PI;
/* Create serial vectors of length NEQ */
u1 = N_VNew_Serial(NEQ);
if (check_flag((void *)u1, "N_VNew_Serial", 0)) return(1);
u2 = N_VNew_Serial(NEQ);
if (check_flag((void *)u2, "N_VNew_Serial", 0)) return(1);
u = N_VNew_Serial(NEQ);
if (check_flag((void *)u, "N_VNew_Serial", 0)) return(1);
s = N_VNew_Serial(NEQ);
if (check_flag((void *)s, "N_VNew_Serial", 0)) return(1);
c = N_VNew_Serial(NEQ);
if (check_flag((void *)c, "N_VNew_Serial", 0)) return(1);
SetInitialGuess1(u1,data);
SetInitialGuess2(u2,data);
N_VConst_Serial(ONE,s); /* no scaling */
Ith(c,1) = ZERO; /* no constraint on x1 */
Ith(c,2) = ZERO; /* no constraint on x2 */
Ith(c,3) = ONE; /* l1 = x1 - x1_min >= 0 */
Ith(c,4) = -ONE; /* L1 = x1 - x1_max <= 0 */
Ith(c,5) = ONE; /* l2 = x2 - x2_min >= 0 */
Ith(c,6) = -ONE; /* L2 = x2 - x22_min <= 0 */
fnormtol=FTOL; scsteptol=STOL;
kmem = KINCreate();
if (check_flag((void *)kmem, "KINCreate", 0)) return(1);
flag = KINSetUserData(kmem, data);
if (check_flag(&flag, "KINSetUserData", 1)) return(1);
flag = KINSetConstraints(kmem, c);
if (check_flag(&flag, "KINSetConstraints", 1)) return(1);
flag = KINSetFuncNormTol(kmem, fnormtol);
if (check_flag(&flag, "KINSetFuncNormTol", 1)) return(1);
flag = KINSetScaledStepTol(kmem, scsteptol);
if (check_flag(&flag, "KINSetScaledStepTol", 1)) return(1);
flag = KINInit(kmem, func, u);
if (check_flag(&flag, "KINInit", 1)) return(1);
/* Call KINDense to specify the linear solver */
flag = KINDense(kmem, NEQ);
if (check_flag(&flag, "KINDense", 1)) return(1);
/* Print out the problem size, solution parameters, initial guess. */
PrintHeader(glstr, fnormtol, scsteptol);
/* --------------------------- */
printf("\n------------------------------------------\n");
printf("\nInitial guess on lower bounds\n");
printf(" [x1,x2] = ");
PrintOutput(u1);
N_VScale_Serial(ONE,u1,u);
glstr = KIN_NONE;
mset = 1;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
N_VScale_Serial(ONE,u1,u);
glstr = KIN_LINESEARCH;
mset = 1;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
N_VScale_Serial(ONE,u1,u);
glstr = KIN_NONE;
mset = 0;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
N_VScale_Serial(ONE,u1,u);
glstr = KIN_LINESEARCH;
mset = 0;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
printf("\n------------------------------------------\n");
printf("\nInitial guess in middle of feasible region\n");
printf(" [x1,x2] = ");
PrintOutput(u2);
N_VScale_Serial(ONE,u2,u);
glstr = KIN_NONE;
mset = 1;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
N_VScale_Serial(ONE,u2,u);
glstr = KIN_LINESEARCH;
mset = 1;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
N_VScale_Serial(ONE,u2,u);
glstr = KIN_NONE;
mset = 0;
SolveIt(kmem, u, s, glstr, mset);
/* --------------------------- */
N_VScale_Serial(ONE,u2,u);
glstr = KIN_LINESEARCH;
mset = 0;
SolveIt(kmem, u, s, glstr, mset);
/* Free memory */
N_VDestroy_Serial(u);
N_VDestroy_Serial(s);
N_VDestroy_Serial(c);
KINFree(&kmem);
free(data);
return(0);
}
static int SolveIt(void *kmem, N_Vector u, N_Vector s, int glstr, int mset)
{
int flag;
printf("\n");
if (mset==1)
printf("Exact Newton");
else
printf("Modified Newton");
if (glstr == KIN_NONE)
printf("\n");
else
printf(" with line search\n");
flag = KINSetMaxSetupCalls(kmem, mset);
if (check_flag(&flag, "KINSetMaxSetupCalls", 1)) return(1);
flag = KINSol(kmem, u, glstr, s, s);
if (check_flag(&flag, "KINSol", 1)) return(1);
printf("Solution:\n [x1,x2] = ");
PrintOutput(u);
PrintFinalStats(kmem);
return(0);
}
/*
*--------------------------------------------------------------------
* FUNCTIONS CALLED BY KINSOL
*--------------------------------------------------------------------
*/
/*
* System function for predator-prey system
*/
static int func(N_Vector u, N_Vector f, void *user_data)
{
realtype *udata, *fdata;
realtype x1, l1, L1, x2, l2, L2;
realtype *lb, *ub;
UserData data;
data = (UserData)user_data;
lb = data->lb;
ub = data->ub;
udata = NV_DATA_S(u);
fdata = NV_DATA_S(f);
x1 = udata[0];
x2 = udata[1];
l1 = udata[2];
L1 = udata[3];
l2 = udata[4];
L2 = udata[5];
fdata[0] = PT5 * sin(x1*x2) - PT25 * x2 / PI - PT5 * x1;
fdata[1] = (ONE - PT25/PI)*(EXP(TWO*x1)-E) + E*x2/PI - TWO*E*x1;
fdata[2] = l1 - x1 + lb[0];
fdata[3] = L1 - x1 + ub[0];
fdata[4] = l2 - x2 + lb[1];
fdata[5] = L2 - x2 + ub[1];
return(0);
}
/*
*--------------------------------------------------------------------
* PRIVATE FUNCTIONS
*--------------------------------------------------------------------
*/
/*
* Initial guesses
*/
static void SetInitialGuess1(N_Vector u, UserData data)
{
realtype x1, x2;
realtype *udata;
realtype *lb, *ub;
udata = NV_DATA_S(u);
lb = data->lb;
ub = data->ub;
/* There are two known solutions for this problem */
/* this init. guess should take us to (0.29945; 2.83693) */
x1 = lb[0];
x2 = lb[1];
udata[0] = x1;
udata[1] = x2;
udata[2] = x1 - lb[0];
udata[3] = x1 - ub[0];
udata[4] = x2 - lb[1];
udata[5] = x2 - ub[1];
}
static void SetInitialGuess2(N_Vector u, UserData data)
{
realtype x1, x2;
realtype *udata;
realtype *lb, *ub;
udata = NV_DATA_S(u);
lb = data->lb;
ub = data->ub;
/* There are two known solutions for this problem */
/* this init. guess should take us to (0.5; 3.1415926) */
x1 = PT5 * (lb[0] + ub[0]);
x2 = PT5 * (lb[1] + ub[1]);
udata[0] = x1;
udata[1] = x2;
udata[2] = x1 - lb[0];
udata[3] = x1 - ub[0];
udata[4] = x2 - lb[1];
udata[5] = x2 - ub[1];
}
/*
* Print first lines of output (problem description)
*/
static void PrintHeader(int globalstrategy, realtype fnormtol,
realtype scsteptol)
{
printf("\nFerraris and Tronconi test problem\n");
printf("Tolerance parameters:\n");
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf(" fnormtol = %10.6Lg\n scsteptol = %10.6Lg\n",
fnormtol, scsteptol);
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf(" fnormtol = %10.6lg\n scsteptol = %10.6lg\n",
fnormtol, scsteptol);
#else
printf(" fnormtol = %10.6g\n scsteptol = %10.6g\n",
fnormtol, scsteptol);
#endif
}
/*
* Print solution
*/
static void PrintOutput(N_Vector u)
{
#if defined(SUNDIALS_EXTENDED_PRECISION)
printf(" %8.6Lg %8.6Lg\n", Ith(u,1), Ith(u,2));
#elif defined(SUNDIALS_DOUBLE_PRECISION)
printf(" %8.6lg %8.6lg\n", Ith(u,1), Ith(u,2));
#else
printf(" %8.6g %8.6g\n", Ith(u,1), Ith(u,2));
#endif
}
/*
* Print final statistics contained in iopt
*/
static void PrintFinalStats(void *kmem)
{
long int nni, nfe, nje, nfeD;
int flag;
flag = KINGetNumNonlinSolvIters(kmem, &nni);
check_flag(&flag, "KINGetNumNonlinSolvIters", 1);
flag = KINGetNumFuncEvals(kmem, &nfe);
check_flag(&flag, "KINGetNumFuncEvals", 1);
flag = KINDlsGetNumJacEvals(kmem, &nje);
check_flag(&flag, "KINDlsGetNumJacEvals", 1);
flag = KINDlsGetNumFuncEvals(kmem, &nfeD);
check_flag(&flag, "KINDlsGetNumFuncEvals", 1);
printf("Final Statistics:\n");
printf(" nni = %5ld nfe = %5ld \n", nni, nfe);
printf(" nje = %5ld nfeD = %5ld \n", nje, nfeD);
}
/*
* Check function return value...
* opt == 0 means SUNDIALS function allocates memory so check if
* returned NULL pointer
* opt == 1 means SUNDIALS function returns a flag so check if
* flag >= 0
* opt == 2 means function allocates memory so check if returned
* NULL pointer
*/
static int check_flag(void *flagvalue, char *funcname, int opt)
{
int *errflag;
/* Check if SUNDIALS function returned NULL pointer - no memory allocated */
if (opt == 0 && flagvalue == NULL) {
fprintf(stderr,
"\nSUNDIALS_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return(1);
}
/* Check if flag < 0 */
else if (opt == 1) {
errflag = (int *) flagvalue;
if (*errflag < 0) {
fprintf(stderr,
"\nSUNDIALS_ERROR: %s() failed with flag = %d\n\n",
funcname, *errflag);
return(1);
}
}
/* Check if function returned NULL pointer - no memory allocated */
else if (opt == 2 && flagvalue == NULL) {
fprintf(stderr,
"\nMEMORY_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return(1);
}
return(0);
}
|