int main (void) { mpf_set_default_prec (300000); mpf_t x0, y0, resA, resB, Z, var; mpf_init_set_ui (x0, 1); mpf_init_set_d (y0, 0.5); mpf_sqrt (y0, y0); mpf_init (resA); mpf_init (resB); mpf_init_set_d (Z, 0.25); mpf_init (var); int n = 1; for(int i=0; i<8; i++){ agm(x0, y0, resA, resB); mpf_sub(var, resA, x0); mpf_mul(var, var, var); mpf_mul_ui(var, var, n); mpf_sub(Z, Z, var); n += n; agm(resA, resB, x0, y0); mpf_sub(var, x0, resA); mpf_mul(var, var, var); mpf_mul_ui(var, var, n); mpf_sub(Z, Z, var); n += n; } mpf_mul(x0, x0, x0); mpf_div(x0, x0, Z); gmp_printf ("%.100000Ff\n", x0); return 0; }
/* merit (rop, t, v, m) -- calculate merit for spectral test result in dimension T, see Knuth p. 105. BUGS: Only valid for 2 <= T <= 6. */ void merit (mpf_t rop, unsigned int t, mpf_t v, mpz_t m) { int f; mpf_t f_m, f_const, f_pi; mpf_init (f_m); mpf_set_z (f_m, m); mpf_init_set_d (f_const, M_PI); mpf_init_set_d (f_pi, M_PI); switch (t) { case 2: /* PI */ break; case 3: /* PI * 4/3 */ mpf_mul_ui (f_const, f_const, 4); mpf_div_ui (f_const, f_const, 3); break; case 4: /* PI^2 * 1/2 */ mpf_mul (f_const, f_const, f_pi); mpf_div_ui (f_const, f_const, 2); break; case 5: /* PI^2 * 8/15 */ mpf_mul (f_const, f_const, f_pi); mpf_mul_ui (f_const, f_const, 8); mpf_div_ui (f_const, f_const, 15); break; case 6: /* PI^3 * 1/6 */ mpf_mul (f_const, f_const, f_pi); mpf_mul (f_const, f_const, f_pi); mpf_div_ui (f_const, f_const, 6); break; default: fprintf (stderr, "spect (merit): can't calculate merit for dimensions > 6\n"); mpf_set_ui (f_const, 0); break; } /* rop = v^t */ mpf_set (rop, v); for (f = 1; f < t; f++) mpf_mul (rop, rop, v); mpf_mul (rop, rop, f_const); mpf_div (rop, rop, f_m); mpf_clear (f_m); mpf_clear (f_const); mpf_clear (f_pi); }
void mpgnu::init() { #ifndef GMP std::cerr << "FATAL ERROR (mpgnu): GNU Multi-Precision not implemented - cannot initialise." << std::endl; exit(1); #else static bool ready = false; if(!ready) { mpf_init_set_d(dblmin, DBL_MIN); mpf_init_set_d(dblmax, DBL_MAX); ready = true; } #endif }
void extrapolate(index_t num_samples, mpf_t *samples, mpf_t ans) { // The Richardson extrapolation recursive formula is // // A_n+1(x) = (2^n A_n(2x) - A_n(x)) / (2^n - 1) mpf_t mult; // mult = 2^n mpf_init_set_d(mult, 1); mpf_t denom; // denom = 1 / (mult - 1) mp_bitcnt_t precision = mpf_get_prec(ans); mpf_init2(denom, precision); mpf_t *end = samples + num_samples; for (mpf_t *lim = samples; lim < end; lim++) { // lim - samples = n mpf_mul_ui(mult, mult, 2); mpf_set(denom, mult); mpf_sub_ui(denom, denom, 1); mpf_ui_div(denom, 1, denom); // evaluate all extrapolations for (mpf_t *ptr = end; --ptr > lim; ) { // A_n+1(x) = (mult A_n(2x) - A_n(x)) * denom mpf_mul(*ptr, *ptr, mult); mpf_sub(*ptr, *ptr, *(ptr - 1)); mpf_mul(*ptr, *ptr, denom); } } mpf_set(ans, *(end - 1)); // move to ans // Clean mpf_clear(mult); mpf_clear(denom); }
//------------------------------------------------------------------------------ // Name: //------------------------------------------------------------------------------ knumber_base *knumber_float::reciprocal() { mpf_t mpf; mpf_init_set_d(mpf, 1.0); mpf_div(mpf_, mpf, mpf_); return this; }
//------------------------------------------------------------------------------ // Name: //------------------------------------------------------------------------------ knumber_float::knumber_float(double value) { Q_ASSERT(!isinf(value)); Q_ASSERT(!isnan(value)); mpf_init_set_d(mpf_, value); }
/* * Function: compute_bbp_first_sum_gmp * -------------------- * Computes the first summand in the BBP formula using GNU GMP. * * d: digit to be calculated * base: the base * c: a fixed positive integer * p: a simple polynomial like x or x^2 * start_at_0: start the summation at k=0, if true, at k=1, otherwise. Most * instances of the BBP formula, such as pi, have you start at 0. * But some, such as log(2), have you start at 1. * * returns: the value of the first sum */ void compute_bbp_first_sum_gmp(mpf_t sum, unsigned long int d, int base, long int c, void (*p)(mpz_t, mpz_t), bool start_at_0) { mpf_set_d(sum, 0.0); signed long int k_start = start_at_0 ? 0 : 1; mpz_t k; mpz_init_set_si(k, k_start); double upper = floor((double) d / (double) c); while (mpz_cmp_d(k, upper) <= 0) { mpz_t poly_result; mpz_init(poly_result); (*p)(poly_result, k); mpz_t num; mpz_init(num); mpz_t exponent; mpz_init_set(exponent, k); mpz_mul_si(exponent, exponent, c); mpz_mul_si(exponent, exponent, -1); mpz_add_ui(exponent, exponent, d); modular_pow_gmp(num, base, exponent, poly_result); mpf_t num_float; mpf_init(num_float); mpf_set_z(num_float, num); mpz_clear(num); mpz_clear(exponent); mpf_t denom; mpf_init_set_d(denom, mpz_get_d(poly_result)); mpz_clear(poly_result); mpf_t quotient; mpf_init(quotient); mpf_div(quotient, num_float, denom); mpf_clear(num_float); mpf_clear(denom); mpf_add(sum, sum, quotient); mpf_clear(quotient); mod_one_gmp(sum, sum); mpz_add_ui(k, k, 1); } mpz_clear(k); mod_one_gmp(sum, sum); }
int main(void){ /*inicializa as variaveis*/ int cont, numThread[NUMTHREADS]; mpf_set_default_prec((long)(DIGITS*BITS_PER_DIGIT+16)); mpf_t PI, dentroCircunferencia, TOTAL; pthread_t tID[NUMTHREADS]; // ID das threads mpf_init(PI); mpf_init_set_d(dentroCircunferencia, 0.0); mpf_init_set_d(TOTAL, 0.0); srand48(time(NULL)); // Para todas as threads, cria a i-esima thread for (cont = 0; cont< NUMTHREADS ; cont++){ numThread[cont] = cont; pthread_create (&tID[cont], NULL, calcula, &numThread[cont]); } // Para cada thread, espera que as threads terminem for (cont = 0; cont< NUMTHREADS ; cont++) pthread_join (tID[cont], NULL); //Para cada thread, soma a sua parcela na conta total for (cont = 0; cont< NUMTHREADS ; cont++){ mpf_add_ui(TOTAL, TOTAL, TOTALParcial[cont]); mpf_add_ui(dentroCircunferencia, dentroCircunferencia, dentroCircunferenciaParcial[cont]); } mpf_div (PI, dentroCircunferencia, TOTAL); mpf_mul_ui (PI, PI, 4); mpf_out_str(stdout, 10, DIGITS, PI); printf("\n"); mpf_clear (PI); mpf_clear (TOTAL); mpf_clear (dentroCircunferencia); return 0; }
int main (void) { mpf_set_default_prec (65568); mpf_t x0, y0, resA, resB; mpf_init_set_ui (y0, 1); mpf_init_set_d (x0, 0.5); mpf_sqrt (x0, x0); mpf_init (resA); mpf_init (resB); for(int i=0; i<7; i++){ agm(x0, y0, resA, resB); agm(resA, resB, x0, y0); } gmp_printf ("%.20000Ff\n", x0); gmp_printf ("%.20000Ff\n\n", y0); return 0; }
void correction_factor_Q(mpf_t Q, int N,mpf_t rho,int k){ unsigned long int f; mpf_t tmp,tmp1,tmp2; /* if (PNK_N_1_rho_1_PN == 0) cout << "Algun factor es 0 N = " << N << " rho = " << rho << " k = " << k << " P__0 = " << P__0 << " P__N = " << P__N << endl; */ mpf_init(tmp); mpf_init_set_d(tmp1,pow(N,k-1)); // N^(k-1) f = 1; mpf_set_ui(Q,0); for (unsigned long int j = k;j < N;j++) { /*Q += (N - j) * pow(N,j) * pow(rho,j - k) * P__0 * N_k_1 / (factorial(j - k) * PNK_N_1_rho_1_PN); */ /* Q += [(N - j) * N^j * rho^(j - k) / (j - k)!] * P__0 * N_k_1 / PNK_N_1_rho_1_PN; */ mpf_mul_ui(tmp1,tmp1,N); // * N if (j - k > 1) mpf_div_ui(tmp1,tmp1,j - k); // / (j - k) mpf_mul_ui(tmp,tmp1,N - j); // * (N - j) mpf_add(Q,Q,tmp); // Q += (N - j) * N^j * rho^(j - k) / (j - k)! mpf_mul(tmp1,tmp1,rho); // * rho } mpf_init(tmp2); mpf_ui_sub(tmp2,1,P__N); // 1 - P__N mpf_pow_ui(tmp1,tmp2,k); // (1 - P__N)^k mpf_mul(tmp2,tmp2,rho); // rho * (1 - P__N) mpf_ui_sub(tmp2,1,tmp2); // 1 - rho * (1 - P__N) mpf_mul(Q,Q,P__0); // * P__0 mpf_mul_ui(Q,Q,factorial(N-k-1)); // * (N - k - 1)! /* pow(1 - P__N,k) * factorial(N) * (1 - rho * (1 - P__N)) */ mpf_mul(tmp2,tmp1,tmp2); // (1 - P__N)^k * (1 - rho * (1 - P__N)) mpf_mul_ui(tmp2,tmp2,factorial(N)); // (1 - P__N)^k * (1 - rho * (1 - P__N)) * N! mpf_div(Q,Q,tmp2); // / [(1 - P__N)^k * (1 - rho * (1 - P__N)) * N!] mpf_clear(tmp); mpf_clear(tmp1); mpf_clear(tmp2); }
Float::Float(const double f) { mpf_init_set_d(value, f); }
/* * Function: compute_bbp_second_sum_gmp * -------------------- * Computes the second summand in the BBP formula. * * d: digit to be calculated * base: the base * c: a fixed positive integer * p: a simple polynomial like x or x^2 * * returns: the value of the second sum */ void compute_bbp_second_sum_gmp(mpf_t sum, int d, int base, int c, void (*p)(mpz_t, mpz_t)) { mpf_set_d(sum, 0.0); mpz_t k; mpz_init_set_si(k, floor((double) d / (double) c) + 1); mpf_t prev_sum; mpf_init(prev_sum); mpf_set(prev_sum, sum); mpf_t base_gmp; mpf_init(base_gmp); mpf_set_si(base_gmp, base); double d_diff = 0.0; do { mpf_set(prev_sum, sum); mpz_t poly_result; mpz_init(poly_result); (*p)(poly_result, k); mpf_t num; mpf_init(num); mpz_t exponent; mpz_init_set(exponent, k); mpz_mul_si(exponent, exponent, c); mpz_mul_si(exponent, exponent, -1); mpz_add_ui(exponent, exponent, d); signed long int exp = mpz_get_si(exponent); unsigned long int neg_exp = -1 * exp; mpf_pow_ui(num, base_gmp, neg_exp); mpf_ui_div(num, 1, num); mpz_clear(exponent); mpf_t denom; mpf_init_set_d(denom, mpz_get_d(poly_result)); mpz_clear(poly_result); mpf_t quotient; mpf_init(quotient); mpf_div(quotient, num, denom); mpf_clear(num); mpf_clear(denom); mpf_add(sum, sum, quotient); mpf_clear(quotient); mpz_add_ui(k, k, 1); mpf_t diff; mpf_init(diff); mpf_sub(diff, prev_sum, sum); d_diff = mpf_get_d(diff); d_diff = fabs(d_diff); mpf_clear(diff); } while (d_diff > 0.00000001); mpz_clear(k); mpf_clear(base_gmp); mpf_clear(prev_sum); }
int cl1mp (int k, int l, int m, int n, int nklmd, int n2d, LDBLE * q_arg, int *kode_arg, LDBLE toler_arg, int *iter, LDBLE * x_arg, LDBLE * res_arg, LDBLE * error_arg, LDBLE * cu_arg, int *iu, int *s, int check, LDBLE censor_arg) { /* System generated locals */ union double_or_int { int ival; mpf_t dval; } *q2; /* Local variables */ static int nklm; static int iout, i, j; static int maxit, n1, n2; static int ia, ii, kk, in, nk, js; static int iphase, kforce; static int klm, jmn, nkl, jpn; static int klm1; static int *kode; int q_dim, cu_dim; int iswitch; mpf_t *q; mpf_t *x; mpf_t *res; mpf_t error; mpf_t *cu; mpf_t dummy, dummy1, sum, z, zu, zv, xmax, minus_one, toler, check_toler; /*mpf_t *scratch; */ mpf_t pivot, xmin, cuv, tpivot, sn; mpf_t zero; int censor; mpf_t censor_tol; /* THIS SUBROUTINE USES A MODIFICATION OF THE SIMPLEX */ /* METHOD OF LINEAR PROGRAMMING TO CALCULATE AN L1 SOLUTION */ /* TO A K BY N SYSTEM OF LINEAR EQUATIONS */ /* AX=B */ /* SUBJECT TO L LINEAR EQUALITY CONSTRAINTS */ /* CX=D */ /* AND M LINEAR INEQUALITY CONSTRAINTS */ /* EX.LE.F. */ /* DESCRIPTION OF PARAMETERS */ /* K NUMBER OF ROWS OF THE MATRIX A (K.GE.1). */ /* L NUMBER OF ROWS OF THE MATRIX C (L.GE.0). */ /* M NUMBER OF ROWS OF THE MATRIX E (M.GE.0). */ /* N NUMBER OF COLUMNS OF THE MATRICES A,C,E (N.GE.1). */ /* KLMD SET TO AT LEAST K+L+M FOR ADJUSTABLE DIMENSIONS. */ /* KLM2D SET TO AT LEAST K+L+M+2 FOR ADJUSTABLE DIMENSIONS. */ /* NKLMD SET TO AT LEAST N+K+L+M FOR ADJUSTABLE DIMENSIONS. */ /* N2D SET TO AT LEAST N+2 FOR ADJUSTABLE DIMENSIONS */ /* Q TWO DIMENSIONAL REAL ARRAY WITH KLM2D ROWS AND */ /* AT LEAST N2D COLUMNS. */ /* ON ENTRY THE MATRICES A,C AND E, AND THE VECTORS */ /* B,D AND F MUST BE STORED IN THE FIRST K+L+M ROWS */ /* AND N+1 COLUMNS OF Q AS FOLLOWS */ /* A B */ /* Q = C D */ /* E F */ /* THESE VALUES ARE DESTROYED BY THE SUBROUTINE. */ /* KODE A CODE USED ON ENTRY TO, AND EXIT */ /* FROM, THE SUBROUTINE. */ /* ON ENTRY, THIS SHOULD NORMALLY BE SET TO 0. */ /* HOWEVER, IF CERTAIN NONNEGATIVITY CONSTRAINTS */ /* ARE TO BE INCLUDED IMPLICITLY, RATHER THAN */ /* EXPLICITLY IN THE CONSTRAINTS EX.LE.F, THEN KODE */ /* SHOULD BE SET TO 1, AND THE NONNEGATIVITY */ /* CONSTRAINTS INCLUDED IN THE ARRAYS X AND */ /* RES (SEE BELOW). */ /* ON EXIT, KODE HAS ONE OF THE */ /* FOLLOWING VALUES */ /* 0- OPTIMAL SOLUTION FOUND, */ /* 1- NO FEASIBLE SOLUTION TO THE */ /* CONSTRAINTS, */ /* 2- CALCULATIONS TERMINATED */ /* PREMATURELY DUE TO ROUNDING ERRORS, */ /* 3- MAXIMUM NUMBER OF ITERATIONS REACHED. */ /* TOLER A SMALL POSITIVE TOLERANCE. EMPIRICAL */ /* EVIDENCE SUGGESTS TOLER = 10**(-D*2/3), */ /* WHERE D REPRESENTS THE NUMBER OF DECIMAL */ /* DIGITS OF ACCURACY AVAILABLE. ESSENTIALLY, */ /* THE SUBROUTINE CANNOT DISTINGUISH BETWEEN ZERO */ /* AND ANY QUANTITY WHOSE MAGNITUDE DOES NOT EXCEED */ /* TOLER. IN PARTICULAR, IT WILL NOT PIVOT ON ANY */ /* NUMBER WHOSE MAGNITUDE DOES NOT EXCEED TOLER. */ /* ITER ON ENTRY ITER MUST CONTAIN AN UPPER BOUND ON */ /* THE MAXIMUM NUMBER OF ITERATIONS ALLOWED. */ /* A SUGGESTED VALUE IS 10*(K+L+M). ON EXIT ITER */ /* GIVES THE NUMBER OF SIMPLEX ITERATIONS. */ /* X ONE DIMENSIONAL REAL ARRAY OF SIZE AT LEAST N2D. */ /* ON EXIT THIS ARRAY CONTAINS A */ /* SOLUTION TO THE L1 PROBLEM. IF KODE=1 */ /* ON ENTRY, THIS ARRAY IS ALSO USED TO INCLUDE */ /* SIMPLE NONNEGATIVITY CONSTRAINTS ON THE */ /* VARIABLES. THE VALUES -1, 0, OR 1 */ /* FOR X(J) INDICATE THAT THE J-TH VARIABLE */ /* IS RESTRICTED TO BE .LE.0, UNRESTRICTED, */ /* OR .GE.0 RESPECTIVELY. */ /* RES ONE DIMENSIONAL REAL ARRAY OF SIZE AT LEAST KLMD. */ /* ON EXIT THIS CONTAINS THE RESIDUALS B-AX */ /* IN THE FIRST K COMPONENTS, D-CX IN THE */ /* NEXT L COMPONENTS (THESE WILL BE =0),AND */ /* F-EX IN THE NEXT M COMPONENTS. IF KODE=1 ON */ /* ENTRY, THIS ARRAY IS ALSO USED TO INCLUDE SIMPLE */ /* NONNEGATIVITY CONSTRAINTS ON THE RESIDUALS */ /* B-AX. THE VALUES -1, 0, OR 1 FOR RES(I) */ /* INDICATE THAT THE I-TH RESIDUAL (1.LE.I.LE.K) IS */ /* RESTRICTED TO BE .LE.0, UNRESTRICTED, OR .GE.0 */ /* RESPECTIVELY. */ /* ERROR ON EXIT, THIS GIVES THE MINIMUM SUM OF */ /* ABSOLUTE VALUES OF THE RESIDUALS. */ /* CU A TWO DIMENSIONAL REAL ARRAY WITH TWO ROWS AND */ /* AT LEAST NKLMD COLUMNS USED FOR WORKSPACE. */ /* IU A TWO DIMENSIONAL INTEGER ARRAY WITH TWO ROWS AND */ /* AT LEAST NKLMD COLUMNS USED FOR WORKSPACE. */ /* S INTEGER ARRAY OF SIZE AT LEAST KLMD, USED FOR */ /* WORKSPACE. */ /* DOUBLE PRECISION DBLE */ /* REAL */ /* INITIALIZATION. */ if (svnid == NULL) fprintf (stderr, " "); /* * mp variables */ censor = 1; if (censor_arg == 0.0) censor = 0; mpf_set_default_prec (96); mpf_init (zero); mpf_init (dummy); mpf_init (dummy1); mpf_init_set_d (censor_tol, censor_arg); q = (mpf_t *) PHRQ_malloc ((size_t) (max_row_count * max_column_count * sizeof (mpf_t))); if (q == NULL) malloc_error (); for (i = 0; i < max_row_count * max_column_count; i++) { mpf_init_set_d (q[i], q_arg[i]); if (censor == 1) { if (mpf_cmp (q[i], zero) != 0) { mpf_abs (dummy1, q[i]); if (mpf_cmp (dummy1, censor_tol) <= 0) { mpf_set_si (q[i], 0); } } } } x = (mpf_t *) PHRQ_malloc ((size_t) (n2d * sizeof (mpf_t))); if (x == NULL) malloc_error (); for (i = 0; i < n2d; i++) { mpf_init_set_d (x[i], x_arg[i]); } res = (mpf_t *) PHRQ_malloc ((size_t) ((k + l + m) * sizeof (mpf_t))); if (res == NULL) malloc_error (); for (i = 0; i < k + l + m; i++) { mpf_init_set_d (res[i], res_arg[i]); } cu = (mpf_t *) PHRQ_malloc ((size_t) (2 * nklmd * sizeof (mpf_t))); if (cu == NULL) malloc_error (); for (i = 0; i < 2 * nklmd; i++) { mpf_init_set_d (cu[i], cu_arg[i]); } kode = (int *) PHRQ_malloc (sizeof (int)); if (kode == NULL) malloc_error (); *kode = *kode_arg; mpf_init (sum); mpf_init (error); mpf_init (z); mpf_init (zu); mpf_init (zv); mpf_init (xmax); mpf_init_set_si (minus_one, -1); mpf_init_set_d (toler, toler_arg); mpf_init_set_d (check_toler, toler_arg); mpf_init (pivot); mpf_init (xmin); mpf_init (cuv); mpf_init (tpivot); mpf_init (sn); /* Parameter adjustments */ q_dim = n2d; q2 = (union double_or_int *) q; cu_dim = nklmd; /* Function Body */ maxit = *iter; n1 = n + 1; n2 = n + 2; nk = n + k; nkl = nk + l; klm = k + l + m; klm1 = klm + 1; nklm = n + klm; kforce = 1; *iter = 0; js = 0; ia = -1; /* Make scratch space */ /* scratch = (LDBLE *) PHRQ_malloc( (size_t) nklmd * sizeof(LDBLE)); if (scratch == NULL) malloc_error(); for (i=0; i < nklmd; i++) { scratch[i] = 0.0; } */ /* scratch = (mpf_t *) PHRQ_malloc( (size_t) nklmd * sizeof(mpf_t)); if (scratch == NULL) malloc_error(); for (i=0; i < nklmd; i++) { mpf_init(scratch[i]); } */ /* SET UP LABELS IN Q. */ for (j = 0; j < n; ++j) { q2[klm1 * q_dim + j].ival = j + 1; } /* L10: */ for (i = 0; i < klm; ++i) { q2[i * q_dim + n1].ival = n + i + 1; if (mpf_cmp_d (q2[i * q_dim + n].dval, 0.0) < 0) { for (j = 0; j < n1; ++j) { /* q2[ i * q_dim + j ].dval = -q2[ i * q_dim + j ].dval; */ mpf_neg (q2[i * q_dim + j].dval, q2[i * q_dim + j].dval); } q2[i * q_dim + n1].ival = -q2[i * q_dim + n1].ival; /* L20: */ } } /* L30: */ /* SET UP PHASE 1 COSTS. */ iphase = 2; #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "Set up phase 1 costs\n"); #endif /* Zero first row of cu and iu */ /*memcpy( (void *) &(cu[0]), (void *) &(scratch[0]), (size_t) nklm * sizeof(mpf_t) ); */ for (j = 0; j < nklm; ++j) { mpf_set_si (cu[j], 0); iu[j] = 0; } /* L40: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L40\n"); #endif if (l != 0) { for (j = nk; j < nkl; ++j) { mpf_set_si (cu[j], 1); /*cu[ j ] = 1.; */ iu[j] = 1; } /* L50: */ iphase = 1; } /* Copy first row of cu and iu to second row */ /*memcpy( (void *) &(cu[cu_dim]), (void *) &(cu[0]), (size_t) nklm * sizeof(mpf_t) ); */ for (i = 0; i < nklm; i++) { mpf_set (cu[cu_dim + i], cu[i]); } memcpy ((void *) &(iu[cu_dim]), (void *) &(iu[0]), (size_t) nklm * sizeof (int)); /* L60: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L60\n"); #endif if (m != 0) { for (j = nkl; j < nklm; ++j) { /* cu[ cu_dim + j ] = 1.; */ mpf_set_si (cu[cu_dim + j], 1); iu[cu_dim + j] = 1; jmn = j - n; if (q2[jmn * q_dim + n1].ival < 0) { iphase = 1; } } /* L70: */ } /* L80: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L80\n"); #endif if (*kode != 0) { for (j = 0; j < n; ++j) { /* if ( x[j] < 0.) { */ if (mpf_cmp_si (x[j], 0) < 0) { /* L90: */ /* cu[ j ] = 1.; */ mpf_set_si (cu[j], 1); iu[j] = 1; /* } else if (x[j] > 0.) { */ } else if (mpf_cmp_si (x[j], 0) > 0) { /* cu[ cu_dim + j ] = 1.; */ mpf_set_si (cu[cu_dim + j], 1); iu[cu_dim + j] = 1; } } /* L110: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L110\n"); #endif for (j = 0; j < k; ++j) { jpn = j + n; /* if (res[j] < 0.) { */ if (mpf_cmp_si (res[j], 0) < 0) { /* L120: */ /* cu[ jpn ] = 1.; */ mpf_set_si (cu[jpn], 1); iu[jpn] = 1; if (q2[j * q_dim + n1].ival > 0) { iphase = 1; } /* } else if (res[j] > 0.) { */ } else if (mpf_cmp_si (res[j], 0) > 0) { /* L130: */ /* cu[ cu_dim + jpn ] = 1.; */ mpf_set_si (cu[cu_dim + jpn], 1); iu[cu_dim + jpn] = 1; if (q2[j * q_dim + n1].ival < 0) { iphase = 1; } } } /* L140: */ } /* L150: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L150\n"); #endif if (iphase == 2) { goto L500; } /* COMPUTE THE MARGINAL COSTS. */ L160: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L160\n"); #endif for (j = js; j < n1; ++j) { mpf_set_si (sum, 0); for (i = 0; i < klm; ++i) { ii = q2[i * q_dim + n1].ival; if (ii < 0) { /* z = cu[ cu_dim - ii - 1 ]; */ mpf_set (z, cu[cu_dim - ii - 1]); } else { /*z = cu[ ii - 1 ]; */ mpf_set (z, cu[ii - 1]); } /*sum += q2[ i * q_dim + j ].dval * z; */ mpf_mul (dummy, q2[i * q_dim + j].dval, z); mpf_add (sum, sum, dummy); } /*q2[ klm * q_dim + j ].dval = sum; */ mpf_set (q2[klm * q_dim + j].dval, sum); } for (j = js; j < n; ++j) { ii = q2[klm1 * q_dim + j].ival; if (ii < 0) { /*z = cu[ cu_dim - ii - 1 ]; */ mpf_set (z, cu[cu_dim - ii - 1]); } else { /*z = cu[ ii - 1 ]; */ mpf_set (z, cu[ii - 1]); } /*q2[ klm * q_dim + j ].dval -= z; */ mpf_sub (q2[klm * q_dim + j].dval, q2[klm * q_dim + j].dval, z); } /* DETERMINE THE VECTOR TO ENTER THE BASIS. */ L240: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L240, xmax %e\n", mpf_get_d (xmax)); #endif /*xmax = 0.; */ mpf_set_si (xmax, 0); if (js >= n) { goto L490; /* test for optimality */ } for (j = js; j < n; ++j) { /*zu = q2[ klm * q_dim + j ].dval; */ mpf_set (zu, q2[klm * q_dim + j].dval); ii = q2[klm1 * q_dim + j].ival; if (ii > 0) { /*zv = -zu - cu[ ii - 1 ] - cu[ cu_dim + ii - 1 ]; */ mpf_mul (dummy, cu[cu_dim + ii - 1], minus_one); mpf_sub (dummy, dummy, cu[ii - 1]); mpf_sub (zv, dummy, zu); } else { ii = -ii; /* zv = zu; */ mpf_set (zv, zu); /* zu = -zu - cu[ ii - 1 ] - cu[ cu_dim + ii - 1 ]; */ mpf_mul (dummy, cu[cu_dim + ii - 1], minus_one); mpf_sub (dummy, dummy, cu[ii - 1]); mpf_sub (zu, dummy, zu); } /* L260 */ if (kforce == 1 && ii > n) { continue; } /*if (iu[ ii - 1 ] != 1 && zu > xmax){ */ if ((iu[ii - 1] != 1) && (mpf_cmp (zu, xmax) > 0)) { /*xmax = zu; */ mpf_set (xmax, zu); in = j; } /* L270 */ /*if (iu[ cu_dim + ii - 1 ] != 1 && zv > xmax ) { */ if ((iu[cu_dim + ii - 1] != 1) && (mpf_cmp (zv, xmax) > 0)) { /*xmax = zv; */ mpf_set (xmax, zv); in = j; } } /* L280 */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L280 xmax %e, toler %e\n", mpf_get_d (xmax), mpf_get_d (toler)); #endif /*if (xmax <= toler) { */ if (mpf_cmp (xmax, toler) <= 0) { #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "xmax before optimality test %e\n", mpf_get_d (xmax)); #endif goto L490; /* test for optimality */ } /*if (q2[ klm * q_dim + in ].dval != xmax) { */ if (mpf_cmp (q2[klm * q_dim + in].dval, xmax) != 0) { for (i = 0; i < klm1; ++i) { /*q2[ i * q_dim + in ].dval = -q2[ i * q_dim + in ].dval; */ mpf_neg (q2[i * q_dim + in].dval, q2[i * q_dim + in].dval); } q2[klm1 * q_dim + in].ival = -q2[klm1 * q_dim + in].ival; /* L290: */ /*q2[ klm * q_dim + in ].dval = xmax; */ mpf_set (q2[klm * q_dim + in].dval, xmax); } /* DETERMINE THE VECTOR TO LEAVE THE BASIS. */ if (iphase != 1 && ia != -1) { /*xmax = 0.; */ mpf_set_si (xmax, 0); /* find maximum absolute value in column "in" */ for (i = 0; i <= ia; ++i) { /*z = fabs(q2[ i * q_dim + in ].dval); */ mpf_abs (z, q2[i * q_dim + in].dval); /*if (z > xmax) { */ if (mpf_cmp (z, xmax) > 0) { /*xmax = z; */ mpf_set (xmax, z); iout = i; } } /* L310: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L310, xmax %e\n", mpf_get_d (xmax)); #endif /* switch row ia with row iout, use memcpy */ /*if (xmax > toler) { */ if (mpf_cmp (xmax, toler) > 0) { /* memcpy( (void *) &(scratch[0]), (void *) &(q2[ ia * q_dim]), (size_t) n2 * sizeof(mpf_t) ); memcpy( (void *) &(q2[ ia * q_dim ]), (void *) &(q2[ iout * q_dim]), (size_t) n2 * sizeof(mpf_t) ); memcpy( (void *) &(q2[ iout * q_dim ]), (void *) &(scratch[ 0 ]), (size_t) n2 * sizeof(mpf_t) ); */ for (i = 0; i < n1; i++) { mpf_set (dummy, q2[ia * q_dim + i].dval); mpf_set (q2[ia * q_dim + i].dval, q2[iout * q_dim + i].dval); mpf_set (q2[iout * q_dim + i].dval, dummy); } j = q2[ia * q_dim + n1].ival; q2[ia * q_dim + n1].ival = q2[iout * q_dim + n1].ival; q2[iout * q_dim + n1].ival = j; /* L320: */ /* set pivot to row ia, column in */ iout = ia; --ia; /*pivot = q2[ iout * q_dim + in ].dval; */ mpf_set (pivot, q2[iout * q_dim + in].dval); goto L420; /* Gauss Jordan */ } } /* L330: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L330, xmax %e\n", mpf_get_d (xmax)); #endif kk = -1; /* divide column n1 by positive value in column "in" greater than toler */ for (i = 0; i < klm; ++i) { /*z = q2[ i * q_dim + in ].dval; */ mpf_set (z, q2[i * q_dim + in].dval); /*if (z > toler) { */ if (mpf_cmp (z, toler) > 0) { ++kk; /*res[kk] = q2[ i * q_dim + n ].dval / z; */ mpf_div (res[kk], q2[i * q_dim + n].dval, z); s[kk] = i; } } /* L340: */ if (kk < 0) { output_msg (OUTPUT_MESSAGE, "kode = 2 in loop 340.\n"); } L350: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L350, xmax %e\n", mpf_get_d (xmax)); #endif if (kk < 0) { /* no positive value found in L340 or bypass intermediate verticies */ *kode = 2; goto L590; } /* L360: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L360, xmax %e\n", mpf_get_d (xmax)); #endif /* find minimum residual */ /*xmin = res[ 0 ]; */ mpf_set (xmin, res[0]); iout = s[0]; j = 0; if (kk != 0) { for (i = 1; i <= kk; ++i) { /*if (res[i] < xmin) { */ if (mpf_cmp (res[i], xmin) < 0) { j = i; /*xmin = res[i]; */ mpf_set (xmin, res[i]); iout = s[i]; } } /* L370: */ /* put kk in position j */ /*res[j] = res[kk]; */ mpf_set (res[j], res[kk]); s[j] = s[kk]; } /* L380: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L380 iout %d, xmin %e, xmax %e\n", iout, mpf_get_d (xmin), mpf_get_d (xmax)); #endif --kk; /*pivot = q2[ iout * q_dim + in ].dval; */ mpf_set (pivot, q2[iout * q_dim + in].dval); ii = q2[iout * q_dim + n1].ival; if (iphase != 1) { if (ii < 0) { /* L390: */ if (iu[-ii - 1] == 1) { goto L420; } } else { if (iu[cu_dim + ii - 1] == 1) { goto L420; } } } /* L400: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L400\n"); #endif ii = abs (ii); /*cuv = cu[ ii - 1 ] + cu[ cu_dim + ii - 1]; */ mpf_add (cuv, cu[ii - 1], cu[cu_dim + ii - 1]); /*if (q2[ klm * q_dim + in ].dval - pivot * cuv > toler) { */ mpf_mul (dummy, pivot, cuv); mpf_sub (dummy, q2[klm * q_dim + in].dval, dummy); if (mpf_cmp (dummy, toler) > 0) { /* BYPASS INTERMEDIATE VERTICES. */ for (j = js; j < n1; ++j) { /*z = q2[ iout * q_dim + j ].dval; */ mpf_set (z, q2[iout * q_dim + j].dval); /*q2[ klm * q_dim + j ].dval -= z * cuv; */ mpf_mul (dummy1, z, cuv); mpf_sub (q2[klm * q_dim + j].dval, q2[klm * q_dim + j].dval, dummy1); if (censor == 1) { if (mpf_cmp (q2[klm * q_dim + j].dval, zero) != 0) { mpf_abs (dummy1, q2[klm * q_dim + j].dval); if (mpf_cmp (dummy1, censor_tol) <= 0) { mpf_set_si (q2[klm * q_dim + j].dval, 0); } } } /*q2[ iout * q_dim + j ].dval = -z; */ mpf_neg (q2[iout * q_dim + j].dval, z); } /* L410: */ q2[iout * q_dim + n1].ival = -q2[iout * q_dim + n1].ival; goto L350; } /* GAUSS-JORDAN ELIMINATION. */ L420: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "Gauss Jordon %d\n", *iter); #endif if (*iter >= maxit) { *kode = 3; goto L590; } /* L430: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L430\n"); #endif ++(*iter); for (j = js; j < n1; ++j) { if (j != in) { /*q2[ iout * q_dim + j ].dval /= pivot; */ mpf_div (q2[iout * q_dim + j].dval, q2[iout * q_dim + j].dval, pivot); } } /* L440: */ for (j = js; j < n1; ++j) { if (j != in) { /*z = -q2[ iout * q_dim + j ].dval; */ mpf_neg (z, q2[iout * q_dim + j].dval); for (i = 0; i < klm1; ++i) { if (i != iout) { /*q2[ i * q_dim + j ].dval += z * q2[ i * q_dim + in ].dval; */ mpf_mul (dummy, z, q2[i * q_dim + in].dval); mpf_add (q2[i * q_dim + j].dval, q2[i * q_dim + j].dval, dummy); if (censor == 1) { if (mpf_cmp (q2[i * q_dim + j].dval, zero) != 0) { mpf_abs (dummy1, q2[i * q_dim + j].dval); if (mpf_cmp (dummy1, censor_tol) <= 0) { mpf_set_si (q2[i * q_dim + j].dval, 0); } } } } } /* L450: */ } } /* L460: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L460\n"); #endif /*tpivot = -pivot; */ mpf_neg (tpivot, pivot); for (i = 0; i < klm1; ++i) { if (i != iout) { /*q2[ i * q_dim + in ].dval /= tpivot; */ mpf_div (q2[i * q_dim + in].dval, q2[i * q_dim + in].dval, tpivot); } } /* L470: */ /*q2[ iout * q_dim + in ].dval = 1. / pivot; */ mpf_set_si (dummy, 1); mpf_div (q2[iout * q_dim + in].dval, dummy, pivot); ii = q2[iout * q_dim + n1].ival; q2[iout * q_dim + n1].ival = q2[klm1 * q_dim + in].ival; q2[klm1 * q_dim + in].ival = ii; ii = abs (ii); if (iu[ii - 1] == 0 || iu[cu_dim + ii - 1] == 0) { goto L240; } /* switch column */ for (i = 0; i < klm1; ++i) { /*z = q2[ i * q_dim + in ].dval; */ mpf_set (z, q2[i * q_dim + in].dval); /*q2[ i * q_dim + in ].dval = q2[ i * q_dim + js ].dval; */ mpf_set (q2[i * q_dim + in].dval, q2[i * q_dim + js].dval); /*q2[ i * q_dim + js ].dval = z; */ mpf_set (q2[i * q_dim + js].dval, z); } i = q2[klm1 * q_dim + in].ival; q2[klm1 * q_dim + in].ival = q2[klm1 * q_dim + js].ival; q2[klm1 * q_dim + js].ival = i; /* L480: */ ++js; goto L240; /* TEST FOR OPTIMALITY. */ L490: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L490\n"); #endif if (kforce == 0) { if (iphase == 1) { /*if (q2[ klm * q_dim + n ].dval <= toler) { */ if (mpf_cmp (q2[klm * q_dim + n].dval, toler) <= 0) { goto L500; } #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "q2[klm1-1, n1-1] > *toler. %e\n", mpf_get_d (q2[(klm1 - 1) * q_dim + n1 - 1].dval)); #endif *kode = 1; goto L590; } *kode = 0; goto L590; } /*if (iphase != 1 || q2[ klm * q_dim + n ].dval > toler) { */ if ((iphase != 1) || (mpf_cmp (q2[klm * q_dim + n].dval, toler) > 0)) { kforce = 0; goto L240; } /* SET UP PHASE 2 COSTS. */ L500: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "Set up phase 2 costs %d\n", *iter); #endif iphase = 2; for (j = 0; j < nklm; ++j) { /*cu[ j ] = 0.; */ mpf_set_si (cu[j], 0); } /* L510: */ for (j = n; j < nk; ++j) { /*cu[ j ] = 1.; */ mpf_set_si (cu[j], 1); } /* memcpy( (void *) &(cu[cu_dim]), (void *) &(cu[0]), (size_t) nklm * sizeof(LDBLE) ); */ for (i = 0; i < nklm; i++) { mpf_set (cu[cu_dim + i], cu[i]); } /* L520: */ for (i = 0; i < klm; ++i) { ii = q2[i * q_dim + n1].ival; if (ii <= 0) { if (iu[cu_dim - ii - 1] == 0) { continue; } /*cu[ cu_dim - ii - 1 ] = 0.; */ mpf_set_si (cu[cu_dim - ii - 1], 0); } else { /* L530: */ if (iu[ii - 1] == 0) { continue; } /*cu[ ii - 1 ] = 0.; */ mpf_set_si (cu[ii - 1], 0); } /* L540: */ ++ia; /* switch row */ /* memcpy( (void *) &(scratch[0]), (void *) &(q2[ ia * q_dim]), (size_t) n2 * sizeof(LDBLE) ); memcpy( (void *) &(q2[ ia * q_dim ]), (void *) &(q2[ i * q_dim]), (size_t) n2 * sizeof(LDBLE) ); memcpy( (void *) &(q2[ i * q_dim ]), (void *) &(scratch[ 0 ]), (size_t) n2 * sizeof(LDBLE) ); */ for (iswitch = 0; iswitch < n1; iswitch++) { mpf_set (dummy, q2[ia * q_dim + iswitch].dval); mpf_set (q2[ia * q_dim + iswitch].dval, q2[i * q_dim + iswitch].dval); mpf_set (q2[i * q_dim + iswitch].dval, dummy); } iswitch = q2[ia * q_dim + n1].ival; q2[ia * q_dim + n1].ival = q2[i * q_dim + n1].ival; q2[i * q_dim + n1].ival = iswitch; /* L550: */ } /* L560: */ goto L160; /* PREPARE OUTPUT. */ L590: #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L590\n"); #endif /*sum = 0.; */ mpf_set_si (sum, 0); for (j = 0; j < n; ++j) { /*x[j] = 0.; */ mpf_set_si (x[j], 0); } /* L600: */ for (i = 0; i < klm; ++i) { /*res[i] = 0.; */ mpf_set_si (res[i], 0); } /* L610: */ for (i = 0; i < klm; ++i) { ii = q2[i * q_dim + n1].ival; /*sn = 1.; */ mpf_set_si (sn, 1); if (ii < 0) { ii = -ii; /*sn = -1.; */ mpf_set_si (sn, -1); } if (ii <= n) { /* L620: */ /*x[ii - 1] = sn * q2[ i * q_dim + n ].dval; */ mpf_mul (x[ii - 1], sn, q2[i * q_dim + n].dval); } else { /* L630: */ /*res[ii - n - 1] = sn * q2[ i * q_dim + n ].dval; */ mpf_mul (res[ii - n - 1], sn, q2[i * q_dim + n].dval); if (ii >= n1 && ii <= nk) { /* * DBLE(Q(I,N1)) */ /*sum += q2[ i * q_dim + n ].dval; */ mpf_add (sum, sum, q2[i * q_dim + n].dval); } } } /* L640: */ #ifdef DEBUG_CL1 output_msg (OUTPUT_MESSAGE, "L640\n"); #endif /* * Check calculation */ mpf_set_si (dummy, 100); mpf_mul (check_toler, toler, dummy); if (check && *kode == 0) { /* * Check optimization constraints */ if (*kode_arg == 1) { for (i = 0; i < k; i++) { if (res_arg[i] < 0.0) { mpf_sub (dummy, res[i], check_toler); mpf_set_si (dummy1, 0); if (mpf_cmp (dummy, dummy1) > 0) { #ifdef CHECK_ERRORS output_msg (OUTPUT_MESSAGE, "\tCL1MP: optimization constraint not satisfied row %d, res %e, constraint %f.\n", i, mpf_get_d (res[i]), res_arg[i]); #endif *kode = 1; } } else if (res_arg[i] > 0.0) { mpf_add (dummy, res[i], check_toler); mpf_set_si (dummy1, 0); if (mpf_cmp (dummy, dummy1) < 0) { #ifdef CHECK_ERRORS output_msg (OUTPUT_MESSAGE, "\tCL1MP: optimization constraint not satisfied row %d, res %e, constraint %f.\n", i, mpf_get_d (res[i]), res_arg[i]); #endif *kode = 1; } } } } /* * Check equalities */ for (i = k; i < k + l; i++) { mpf_abs (dummy, res[i]); if (mpf_cmp (dummy, check_toler) > 0) { #ifdef CHECK_ERRORS output_msg (OUTPUT_MESSAGE, "\tCL1MP: equality constraint not satisfied row %d, res %e, tolerance %e.\n", i, mpf_get_d (res[i]), mpf_get_d (check_toler)); #endif *kode = 1; } } /* * Check inequalities */ for (i = k + l; i < k + l + m; i++) { mpf_neg (dummy, check_toler); if (mpf_cmp (res[i], dummy) < 0) { #ifdef CHECK_ERRORS output_msg (OUTPUT_MESSAGE, "\tCL1MP: inequality constraint not satisfied row %d, res %e, tolerance %e.\n", i, mpf_get_d (res[i]), mpf_get_d (check_toler)); #endif *kode = 1; } } /* * Check dissolution/precipitation constraints */ if (*kode_arg == 1) { for (i = 0; i < n; i++) { if (x_arg[i] < 0.0) { mpf_sub (dummy, x[i], check_toler); mpf_set_si (dummy1, 0); if (mpf_cmp (dummy, dummy1) > 0) { #ifdef CHECK_ERRORS output_msg (OUTPUT_MESSAGE, "\tCL1MP: dis/pre constraint not satisfied column %d, x %e, constraint %f.\n", i, mpf_get_d (x[i]), x_arg[i]); #endif *kode = 1; } } else if (x_arg[i] > 0.0) { mpf_add (dummy, x[i], check_toler); mpf_set_si (dummy1, 0); if (mpf_cmp (dummy, dummy1) < 0) { #ifdef CHECK_ERRORS output_msg (OUTPUT_MESSAGE, "\tCL1MP: dis/pre constraint not satisfied column %d, x %e, constraint %f.\n", i, mpf_get_d (x[i]), x_arg[i]); #endif *kode = 1; } } } } if (*kode == 1) { output_msg (OUTPUT_MESSAGE, "\n\tCL1MP: Roundoff errors in optimization.\n\t Deleting model.\n"); } } /* * set return variables */ /**error = sum;*/ mpf_set (error, sum); *error_arg = mpf_get_d (error); *kode_arg = *kode; for (i = 0; i < n2d; i++) { x_arg[i] = mpf_get_d (x[i]); } for (i = 0; i < k + l + m; i++) { res_arg[i] = mpf_get_d (res[i]); } /*scratch = free_check_null (scratch); */ for (i = 0; i < max_row_count * max_column_count; i++) { mpf_clear (q[i]); } q = (mpf_t *) free_check_null (q); for (i = 0; i < n2d; i++) { mpf_clear (x[i]); } x = (mpf_t *) free_check_null (x); for (i = 0; i < k + l + m; i++) { mpf_clear (res[i]); } res = (mpf_t *) free_check_null (res); for (i = 0; i < 2 * nklmd; i++) { mpf_clear (cu[i]); } cu = (mpf_t *) free_check_null (cu); mpf_clear (dummy); mpf_clear (dummy1); mpf_clear (sum); mpf_clear (error); mpf_clear (z); mpf_clear (zu); mpf_clear (zv); mpf_clear (xmax); mpf_clear (minus_one); mpf_clear (toler); mpf_clear (check_toler); mpf_clear (pivot); mpf_clear (xmin); mpf_clear (cuv); mpf_clear (tpivot); mpf_clear (sn); mpf_clear (censor_tol); kode = (int *) free_check_null (kode); return 0; }
Tmp(double const t) { mpf_init_set_d(tv,t); }
//initArray :: [bigFloat] -> [double] -> uint32 -> Effect void initArray(bigFloat* dest, double* inputs, uint32 numVars) { uint32 i; for(i = 0; i < numVars; i++) { mpf_init_set_d(dest[i], inputs[i]); } }
//Jianmin :: NLSystemContext* -> Effect -> IO void Jianmin(NLSystemContext* ctx) { uint32 i; unsigned long exponent; //Reactions: bigFloat r[78]; //temp-storage during calculations bigFloat r_f[78]; //forward reaction bigFloat r_b[78]; //backward reaction //Note: 'theta_H' is initialized in second bigFloat group below. bigFloat theta_A, theta_B, theta_C, theta_D, theta_E, theta_F, theta_G, theta_I, theta_J, theta_K, theta_L, theta_M, theta_N, theta_O, theta_P, theta_Q, theta_R, theta_S, theta_T, theta_U, theta_V, theta_W, theta_X, theta_Y, theta_Z, theta_Z1; bigFloat free, theta_H, theta_OH, theta_H2O, theta_CH, theta_CH2, theta_CH3, theta_CH4, theta_CO, theta_CHO, theta_CH2O, theta_CH3O, theta_CH3OH; bigFloat K_H; bigFloat temp; bigFloat e; // define constants for forward and reverse rate constants. //NOTE: constants for k_f[55]-k_f[70] are not defined! (same for k_b[].) double in_k_f[78] = { 6.249e7, 1.831e3, 9.567e2, 8.447e3, 1.863e5, 5.509e8, 5.982, 2.106e10, 7.139e4, 2.243e8, 2.418e7, 1.247e8, 1.100e2, 5.791e12, 1.114e9, 9.955e3, 5.396e2, 3.706e3, 2.705e8, 7.040e9, 5.501e8, 2.335e4, 1.630e10, 6.622e2, 6.464e2, 2.109e8, 8.910, 3.268e5, 1.890e5, 9.014e11, 7.631e3, 6.303e2, 1.075e2, 9.362e7, 9.540e4, 2.636e8, 3.368e8, 1.615e10, 3.290e-3, 1.004e3, 1.457e5, 2.380e2, 3.845e7, 3.778e7, 9.432e3, 1.666e3, 3.094e8, 1.557e7, 6.575e1, 1.372e2, 3.003, 3.044e13, 1.047e16, 2.092e12, 1.020e3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5.056e9, 1.396e9, 4.831e9, 9.712e6, 4.000, 2.403e6, 1.404e-1}; double in_k_b[78] = { 5.885e4, 3.070e6, 2.885e7, 8.560e1, 8.721, 3.131e5, 6.828e-12, 4.823e3, 1.020e4, 1.566, 5.024e5, 3.056e6, 6.318e-1, 1.247e8, 8.518e-3, 6.388e10, 1.625e7, 1.514e5, 4.864e5, 1.941e2, 1.750e6, 8.974e1, 5.505e-2, 1.555e1, 2.380e7, 1.986, 1.865e7, 1.668, 1.108e7, 1.962, 1.902e11, 5.235e1, 1.311e7, 2.729, 2.606e8, 5.388e6, 4.689, 1.170e4, 2.457e-15, 1.833e-6, 7.225e-5, 5.142e8, 4.046e12, 9.921e9, 3.620e7, 2.431e7, 1.802e13, 2.232e8, 7.117e7, 6.635e7, 7.879e7, 1.230e8, 1.740e8, 1.640e8, 6.696e7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.126e8, 1.611, 1.848e6, 1.828e2, 3.558e8, 1.593e5, 1.336e10}; bigFloat k_f[78]; bigFloat k_b[78]; // define partial pressures, Boltzman constant and temperature. // guaiacol, H2, catechol, phenol, benzene, anisole. bigFloat p_A, p_H2, p_M, p_S, p_X, p_K, p_CO, p_H2O, p_CH4, p_CH3OH, KB, T; double partialPressures[12] = {1.0, 1.0, 1.0e-5, 1.0e-5, 1.0e-5, 1.0e-5, 1.0e-6, 0.0, 0.0, 0.0, 8.6173324e-5, 573}; //--------------------------------------------------------- //INITIALIZATIONS: //init r[]: for(i = 0; i < 78; i++) { mpf_init(r[i]); } //init r_f[]: for(i = 0; i < 78; i++) { mpf_init(r_f[i]); } //init r_b[]: for(i = 0; i < 78; i++) { mpf_init(r_b[i]); } //init forward and reverse rate constants: initArray(k_f, in_k_f, 78); initArray(k_b, in_k_b, 78); //init the various thetas: mpf_init_set(theta_A , ctx->state[0]); mpf_init_set(theta_E , ctx->state[1]); mpf_init_set(theta_F , ctx->state[2]); mpf_init_set(theta_G , ctx->state[3]); mpf_init_set(theta_I , ctx->state[4]); mpf_init_set(theta_L , ctx->state[5]); mpf_init_set(theta_M , ctx->state[6]); mpf_init_set(theta_N , ctx->state[7]); mpf_init_set(theta_O , ctx->state[8]); mpf_init_set(theta_R , ctx->state[9]); mpf_init_set(theta_S , ctx->state[10]); mpf_init_set(theta_U , ctx->state[11]); mpf_init_set(theta_V , ctx->state[12]); mpf_init_set(theta_X , ctx->state[13]); mpf_init_set(free , ctx->state[14]); mpf_init_set(theta_H , ctx->state[15]); mpf_init_set(theta_OH , ctx->state[16]); mpf_init_set(theta_H2O , ctx->state[17]); mpf_init_set(theta_CH , ctx->state[18]); mpf_init_set(theta_CH2 , ctx->state[19]); mpf_init_set(theta_CH3 , ctx->state[20]); mpf_init_set(theta_CH4 , ctx->state[21]); mpf_init_set(theta_CHO , ctx->state[22]); mpf_init_set(theta_CH2O , ctx->state[23]); mpf_init_set(theta_CH3O , ctx->state[24]); mpf_init_set(theta_CH3OH , ctx->state[25]); mpf_init_set(theta_W , ctx->state[26]); mpf_init_set(theta_T , ctx->state[27]); mpf_init_set(theta_B , ctx->state[28]); mpf_init_set(theta_J , ctx->state[29]); mpf_init_set(theta_P , ctx->state[30]); mpf_init_set(theta_C , ctx->state[31]); mpf_init_set(theta_D , ctx->state[32]); mpf_init_set(theta_K , ctx->state[33]); mpf_init_set(theta_Q , ctx->state[34]); mpf_init_set(theta_Y , ctx->state[35]); mpf_init_set(theta_Z , ctx->state[36]); mpf_init_set(theta_Z1 , ctx->state[37]); mpf_init_set(theta_CO , ctx->state[38]); //init K_H, temp, and e: mpf_init(K_H); mpf_init(temp); mpf_init(e); //define e to 50 digits of precision: gmp_sscanf("2.71828182845904523536028747135266249775724709369995", "%F", e); //init partial pressures: mpf_init_set_d(p_A , partialPressures[0]); mpf_init_set_d(p_H2 , partialPressures[1]); mpf_init_set_d(p_M , partialPressures[2]); mpf_init_set_d(p_S , partialPressures[3]); mpf_init_set_d(p_X , partialPressures[4]); mpf_init_set_d(p_K , partialPressures[5]); mpf_init_set_d(p_CO , partialPressures[6]); mpf_init_set_d(p_H2O , partialPressures[7]); mpf_init_set_d(p_CH4 , partialPressures[8]); mpf_init_set_d(p_CH3OH , partialPressures[9]); mpf_init_set_d(KB , partialPressures[10]); mpf_init_set_d(T , partialPressures[11]); //--------------------------------------------------------- //MAIN MATH KERNEL: //FORWARD REACTION RATES: // calculate rates for all elementary steps. mpf_mul( r_f[0], k_f[0], p_A); mpf_mul( r_f[0], r_f[0], free); mpf_mul( r_f[0], r_f[0], free); mpf_mul( r_f[0], r_f[0], free); mpf_mul( r_f[0], r_f[0], free); mpf_mul( r_f[1], k_f[1], theta_A); mpf_mul( r_f[1], r_f[1], theta_H); //r_f[2] and r_f[3] handled later on... mpf_mul( r_f[4], k_f[4], theta_A); mpf_mul( r_f[4], r_f[4], free); mpf_mul( r_f[5], k_f[5], theta_A); mpf_mul( r_f[5], r_f[5], free); mpf_mul( r_f[6], k_f[6], theta_A); mpf_mul( r_f[6], r_f[6], free); mpf_mul( r_f[7], k_f[7], theta_A); mpf_mul( r_f[7], r_f[7], free); mpf_mul( r_f[8], k_f[8], theta_B); mpf_mul( r_f[8], r_f[8], free); mpf_mul( r_f[8], r_f[8], free); //r_f[9] and r_f[10] handled later on... mpf_mul( r_f[11], k_f[11], theta_E); mpf_mul( r_f[11], r_f[11], theta_H); mpf_mul( r_f[12], k_f[12], theta_F); mpf_mul( r_f[12], r_f[12], free); mpf_mul( r_f[13], k_f[13], theta_F); mpf_mul( r_f[13], r_f[13], free); mpf_mul( r_f[14], k_f[14], theta_F); mpf_mul( r_f[15], k_f[15], theta_G); mpf_mul( r_f[15], r_f[15], theta_H); mpf_mul( r_f[16], k_f[16], theta_G); mpf_mul( r_f[16], r_f[16], free); mpf_mul( r_f[17], k_f[17], theta_I); mpf_mul( r_f[17], r_f[17], free); mpf_mul( r_f[18], k_f[18], theta_I); mpf_mul( r_f[18], r_f[18], free); mpf_mul( r_f[18], r_f[18], free); mpf_mul( r_f[19], k_f[19], theta_J); //r_f[20] handled later on... mpf_mul( r_f[21], k_f[21], theta_L); mpf_mul( r_f[21], r_f[21], free); mpf_mul( r_f[22], k_f[22], theta_L); mpf_mul( r_f[22], r_f[22], free); mpf_mul( r_f[23], k_f[23], theta_M); mpf_mul( r_f[23], r_f[23], free); mpf_mul( r_f[24], k_f[24], theta_M); mpf_mul( r_f[24], r_f[24], theta_H); mpf_mul( r_f[25], k_f[25], theta_N); mpf_mul( r_f[25], r_f[25], theta_H); mpf_mul( r_f[26], k_f[26], theta_O); mpf_mul( r_f[26], r_f[26], free); mpf_mul( r_f[27], k_f[27], theta_O); mpf_mul( r_f[27], r_f[27], free); mpf_mul( r_f[28], k_f[28], theta_P); mpf_mul( r_f[28], r_f[28], theta_H); //r[29] handled later on... mpf_mul( r_f[30], k_f[30], theta_R); mpf_mul( r_f[30], r_f[30], theta_H); mpf_mul( r_f[31], k_f[31], theta_S); mpf_mul( r_f[31], r_f[31], free); mpf_mul( r_f[32], k_f[32], theta_S); mpf_mul( r_f[32], r_f[32], theta_H); mpf_mul( r_f[33], k_f[33], theta_T); mpf_mul( r_f[33], r_f[33], free); mpf_mul( r_f[34], k_f[34], theta_U); mpf_mul( r_f[34], r_f[34], theta_H); mpf_mul( r_f[35], k_f[35], theta_V); mpf_mul( r_f[35], r_f[35], theta_H); mpf_mul( r_f[36], k_f[36], theta_W); mpf_mul( r_f[37], k_f[37], theta_F); mpf_mul( r_f[37], r_f[37], free); //what happened to r_38?! mpf_mul( r_f[39], k_f[39], theta_I); mpf_mul( r_f[39], r_f[39], free); mpf_mul( r_f[39], r_f[39], free); mpf_mul( r_f[40], k_f[40], theta_B); mpf_mul( r_f[40], r_f[40], free); mpf_mul( r_f[40], r_f[40], free); mpf_mul( r_f[41], k_f[41], theta_CH3O); mpf_mul( r_f[41], r_f[41], theta_H); mpf_mul( r_f[42], k_f[42], theta_CH); mpf_mul( r_f[42], r_f[42], theta_H); mpf_mul( r_f[43], k_f[43], theta_CH2); mpf_mul( r_f[43], r_f[43], theta_H); mpf_mul( r_f[44], k_f[44], theta_CH3); mpf_mul( r_f[44], r_f[44], theta_H); mpf_mul( r_f[45], k_f[45], theta_OH); mpf_mul( r_f[45], r_f[45], theta_H); mpf_mul( r_f[46], k_f[46], theta_CHO); mpf_mul( r_f[46], r_f[46], theta_H); mpf_mul( r_f[47], k_f[47], theta_CH2O); mpf_mul( r_f[47], r_f[47], theta_H); mpf_mul( r_f[48], k_f[48], theta_S); mpf_mul( r_f[49], k_f[49], theta_M); mpf_mul( r_f[50], k_f[50], theta_X); mpf_mul( r_f[51], k_f[51], theta_CH3OH); mpf_mul( r_f[52], k_f[52], theta_CH4); mpf_mul( r_f[53], k_f[53], theta_H2O); mpf_mul( r_f[54], k_f[54], theta_K); //not using r_f[55] through r_f[74]?! //also, consider making below assignments in-order (might help optimizer). mpf_mul( r_f[71], k_f[71], theta_O); mpf_mul( r_f[71], r_f[71], free); mpf_mul( r_f[72], k_f[72], theta_Z); mpf_mul( r_f[72], r_f[72], free); mpf_mul( r_f[73], k_f[73], theta_Z); mpf_mul( r_f[73], r_f[73], free); mpf_mul( r_f[74], k_f[74], theta_Z1); mpf_mul( r_f[75], k_f[75], theta_G); mpf_mul( r_f[75], r_f[75], theta_H); mpf_mul( r_f[76], k_f[76], theta_Y); mpf_mul( r_f[76], r_f[76], free); mpf_mul( r_f[77], k_f[77], theta_CO); mpf_mul( r_f[77], r_f[77], theta_H); // // steps including C, D, K, Q. mpf_mul( r_f[2], k_f[2], theta_A); mpf_mul( r_f[2], r_f[2], theta_H); mpf_mul( r_f[3], k_f[3], theta_A); mpf_mul( r_f[3], r_f[3], free); mpf_mul( r_f[9], k_f[9], theta_C); mpf_mul( r_f[9], r_f[9], free); mpf_mul( r_f[10], k_f[10], theta_D); mpf_mul( r_f[10], r_f[10], theta_H); mpf_mul( r_f[20], k_f[20], theta_K); mpf_mul( r_f[20], r_f[20], free); mpf_mul( r_f[29], k_f[29], theta_Q); mpf_mul( r_f[29], r_f[29], free); //REVERSE REACTION RATES: mpf_mul( r_b[0], k_b[0], theta_A); mpf_mul( r_b[1], k_b[1], theta_B); mpf_mul( r_b[1], r_b[1], free); //r[2] and r[3] handled later on... mpf_mul( r_b[4], k_b[4], theta_E); mpf_mul( r_b[4], r_b[4], theta_CH3O); mpf_mul( r_b[5], k_b[5], theta_F); mpf_mul( r_b[5], r_b[5], theta_H); mpf_mul( r_b[6], k_b[6], theta_G); mpf_mul( r_b[6], r_b[6], theta_CH3); mpf_mul( r_b[7], k_b[7], theta_I); mpf_mul( r_b[7], r_b[7], theta_H); mpf_mul( r_b[8], k_b[8], theta_J); mpf_mul( r_b[8], r_b[8], theta_H); //r[9] and r[10] handled later on... mpf_mul( r_b[11], k_b[11], theta_S); mpf_mul( r_b[11], r_b[11], free); mpf_mul( r_b[12], k_b[12], theta_E); mpf_mul( r_b[12], r_b[12], theta_CH2O); mpf_mul( r_b[13], k_b[13], theta_L); mpf_mul( r_b[13], r_b[13], theta_H); mpf_mul( r_b[14], k_b[14], theta_G); mpf_mul( r_b[14], r_b[14], theta_CH2); mpf_mul( r_b[15], k_b[15], theta_M); mpf_mul( r_b[15], r_b[15], free); mpf_mul( r_b[16], k_b[16], theta_N); mpf_mul( r_b[16], r_b[16], theta_OH); mpf_mul( r_b[17], k_b[17], theta_N); mpf_mul( r_b[17], r_b[17], theta_CH3O); mpf_mul( r_b[18], k_b[18], theta_O); mpf_mul( r_b[18], r_b[18], theta_H); mpf_mul( r_b[19], k_b[19], theta_P); mpf_mul( r_b[19], r_b[19], theta_CH2); //r[20] handled later on... mpf_mul( r_b[21], k_b[21], theta_E); mpf_mul( r_b[21], r_b[21], theta_CHO); mpf_mul( r_b[22], k_b[22], theta_G); mpf_mul( r_b[22], r_b[22], theta_CH); mpf_mul( r_b[23], k_b[23], theta_E); mpf_mul( r_b[23], r_b[23], theta_OH); mpf_mul( r_b[24], k_b[24], theta_T); mpf_mul( r_b[25], k_b[25], theta_R); mpf_mul( r_b[25], r_b[25], free); mpf_mul( r_b[26], k_b[26], theta_N); mpf_mul( r_b[26], r_b[26], theta_CH2O); mpf_mul( r_b[27], k_b[27], theta_U); mpf_mul( r_b[27], r_b[27], theta_CH2); mpf_mul( r_b[28], k_b[28], theta_T); mpf_mul( r_b[28], r_b[28], free); //r[29] handled later on... mpf_mul( r_b[30], k_b[30], theta_S); mpf_mul( r_b[30], r_b[30], free); mpf_mul( r_b[31], k_b[31], theta_V); mpf_mul( r_b[31], r_b[31], theta_OH); mpf_mul( r_b[32], k_b[32], theta_W); mpf_mul( r_b[32], r_b[32], free); mpf_mul( r_b[33], k_b[33], theta_S); mpf_mul( r_b[33], r_b[33], theta_OH); mpf_mul( r_b[34], k_b[34], theta_G); mpf_mul( r_b[34], r_b[34], free); mpf_mul( r_b[34], r_b[34], free); mpf_mul( r_b[35], k_b[35], theta_X); mpf_mul( r_b[35], r_b[35], free); mpf_mul( r_b[35], r_b[35], free); mpf_mul( r_b[36], k_b[36], theta_X); mpf_mul( r_b[36], r_b[36], theta_OH); mpf_mul( r_b[37], k_b[37], theta_O); mpf_mul( r_b[37], r_b[37], theta_H); //what happened to r_38?! mpf_mul( r_b[39], k_b[39], theta_U); mpf_mul( r_b[39], r_b[39], theta_CH3); mpf_mul( r_b[40], k_b[40], theta_S); mpf_mul( r_b[40], r_b[40], theta_CH3O); mpf_mul( r_b[41], k_b[41], theta_CH3OH); mpf_mul( r_b[41], r_b[41], free); mpf_mul( r_b[42], k_b[42], theta_CH2); mpf_mul( r_b[42], r_b[42], free); mpf_mul( r_b[43], k_b[43], theta_CH3); mpf_mul( r_b[43], r_b[43], free); mpf_mul( r_b[44], k_b[44], theta_CH4); mpf_mul( r_b[44], r_b[44], free); mpf_mul( r_b[45], k_b[45], theta_H2O); mpf_mul( r_b[45], r_b[45], free); mpf_mul( r_b[46], k_b[46], theta_CH2O); mpf_mul( r_b[46], r_b[46], free); mpf_mul( r_b[47], k_b[47], theta_CH3O); mpf_mul( r_b[47], r_b[47], free); mpf_mul( r_b[47], r_b[47], free); mpf_mul( r_b[48], k_b[48], p_S); mpf_mul( r_b[48], r_b[48], free); mpf_mul( r_b[48], r_b[48], free); mpf_mul( r_b[48], r_b[48], free); mpf_mul( r_b[48], r_b[48], free); mpf_mul( r_b[49], k_b[49], p_M); mpf_mul( r_b[49], r_b[49], free); mpf_mul( r_b[49], r_b[49], free); mpf_mul( r_b[49], r_b[49], free); mpf_mul( r_b[49], r_b[49], free); mpf_mul( r_b[50], k_b[50], p_X); mpf_mul( r_b[50], r_b[50], free); mpf_mul( r_b[50], r_b[50], free); mpf_mul( r_b[50], r_b[50], free); mpf_mul( r_b[51], k_b[51], p_CH3OH); mpf_mul( r_b[51], r_b[51], free); mpf_mul( r_b[52], k_b[52], p_CH4); mpf_mul( r_b[52], r_b[52], free); mpf_mul( r_b[53], k_b[53], p_H2O); mpf_mul( r_b[53], r_b[53], free); mpf_mul( r_b[54], k_b[54], p_K); mpf_mul( r_b[54], r_b[54], free); mpf_mul( r_b[54], r_b[54], free); mpf_mul( r_b[54], r_b[54], free); mpf_mul( r_b[54], r_b[54], free); //not using r[55] through r[74]?! //also, consider making below assignments in-order (might help optimizer). mpf_mul( r_b[71], k_b[71], theta_Z); mpf_mul( r_b[71], r_b[71], theta_H); mpf_mul( r_b[72], k_b[72], theta_U); mpf_mul( r_b[72], r_b[72], theta_CH); mpf_mul( r_b[73], k_b[73], theta_Z1); mpf_mul( r_b[73], r_b[73], theta_H); mpf_mul( r_b[74], k_b[74], theta_N); mpf_mul( r_b[74], r_b[74], theta_CO); mpf_mul( r_b[75], k_b[75], theta_Y); mpf_mul( r_b[75], r_b[75], free); mpf_mul( r_b[76], k_b[76], theta_R); mpf_mul( r_b[76], r_b[76], theta_OH); mpf_mul( r_b[77], k_b[77], theta_CHO); // // steps including C, D, K, Q. mpf_mul( r_b[2], k_b[2], theta_C); mpf_mul( r_b[2], r_b[2], free); mpf_mul( r_b[3], k_b[3], theta_D); mpf_mul( r_b[3], r_b[3], theta_OH); mpf_mul( r_b[9], k_b[9], theta_K); mpf_mul( r_b[9], r_b[9], theta_OH); mpf_mul( r_b[10], k_b[10], theta_K); mpf_mul( r_b[10], r_b[10], free); mpf_mul( r_b[20], k_b[20], theta_Q); mpf_mul( r_b[20], r_b[20], theta_H); mpf_mul( r_b[29], k_b[29], theta_R); mpf_mul( r_b[29], r_b[29], theta_CH2); //OVERALL REACTION RATES: for(i = 0; i < 78; i++) { mpf_sub( r[i], r_f[i], r_b[i]); } //--------------------------------------------------------- // apply the steady state approximation for all thetas. //NOTE: need to put in GNU MP ops + rewrite to array accesses. /*out[0] = r_04 + r_12 + r_21 + r_23 - r_11; // d(theta_E)/dt=0 out[1] = r_05 - r_12 - r_13 - r_14 - r_37; // d(theta_F)/dt=0 out[2] = r_06 + r_14 + r_22 + r_34 - r_15 - r_16 - r_75; // d(theta_G)/dt=0 out[3] = r_07 - r_17 - r_18 - r_39; // d(theta_I)/dt=0 out[4] = r_13 - r_21 - r_22; // d(theta_L)/dt=0 out[5] = r_15 - r_23 - r_24 - r_49; // d(theta_M)/dt=0 catechol out[6] = r_16 + r_17 + r_26 + r_74 - r_25; // d(theta_N)/dt=0 out[7] = r_18 + r_37 - r_26 - r_27 - r_71; // d(theta_O)/dt=0 out[8] = r_25 + r_29 + r_76 - r_30; // d(theta_R)/dt=0 out[9] = r_11 + r_30 + r_33 + r_40 - r_31 - r_32 - r_48; // d(theta_S)/dt=0 phenol out[10] = r_27 + r_39 + r_72 - r_34; // d(theta_U)/dt=0 out[11] = r_31 - r_35; // d(theta_V)/dt=0 out[12] = r_35 + r_36 - r_50; // d(theta_X)/dt=0 benzene out[13] = r_03 + r_09 + r_23 + r_31 + r_31 + r_33 - r_45; // d(theta_OH)/dt=0 out[14] = r_45 - r_53; // d(theta_H2O)/dt=0 out[15] = r_22 + r_72 - r_42; // d(theta_CH)/dt =0 out[16] = r_14 + r_19 + r_27 + r_29 + r_42 - r_43; // d(theta_CH2)/dt=0 out[17] = r_06 + r_43 - r_44; // d(theta_CH3)/dt=0 out[18] = r_44 - r_52; // d(theta_CH4)/dt=0 out[19] = r_21 + r_77 - r_46; // d(theta_CHO)/dt=0 out[20] = r_12 + r_26 + r_46 - r_47; // d(theta_CH2O)/dt=0 out[21] = r_04 + r_17 + r_47 - r_41; // d(theta_CH3O)/dt=0 out[22] = r_41 - r_51; // d(theta_CH3OH)/dt=0 out[23] = r_00 - r_01 - r_04 - r_05 - r_06 - r_07; // d(theta_A)/dt=0 // out[23] = theta_A - k_00_f/k_00_b*p_A*free*free*free*free; // d(theta_A)/dt=0 if step 0 is in equilibrium. out[24] = r_32 - r_36; // d(theta_W)/dt=0 out[25] = r_24 + r_28 - r_33; // d(theta_T)/dt=0 out[26] = r_01 - r_08 - r_40; // d(theta_B)/dt=0 out[27] = r_08 - r_19; // d(theta_J)/dt=0 out[28] = r_19 - r_28; // d(theta_P)/dt=0 out[29] = theta_H - exp(-( (-1.374+0.076+0.683)/2 + 2*0.084*(theta_H-0.139))/KB/T)*pow(p_H2,0.5)*free; //theta_H out[30] = r_02 - r_09; // d(theta_C)/dt=0 out[31] = r_03 - r_10; // d(theta_D)/dt=0 out[32] = r_09 + r_10 - r_20 - r_54; // d(theta_K)/dt=0 out[33] = r_20 - r_29; // d(theta_Q)/dt=0 out[34] = r_75 - r_76; // d(theta_Y)/dt=0 out[35] = r_71 - r_72 - r_73; // d(theta_Z)/dt=0 out[36] = r_73 - r_74; // d(theta_Z1)/dt=0 out[37] = theta_CO - exp(-(-2.131+0.028+1.764)/KB/T)*p_CO*free; // d(theta_CO)/dt=0*/ // finally the summation of all thetas should be 1. //NOTE: need to put in GNU MP ops. /*mpf_set(out[38], ( 4*ctx->state[0] + 4*ctx->state[1] + 4*ctx->state[2] + 4*ctx->state[3] + 4*ctx->state[4] + 4*ctx->state[5] + 4*ctx->state[6] + 4*ctx->state[7] + 4*ctx->state[8] + 4*ctx->state[9] + 4*ctx->state[10] + 5*ctx->state[11] + 3*ctx->state[12] + 3*ctx->state[13] + ctx->state[14] + ctx->state[15] + ctx->state[16] + ctx->state[17] + ctx->state[18] + 2*ctx->state[19] + ctx->state[20] + ctx->state[21] + 2*ctx->state[22] + 2*ctx->state[23] + ctx->state[24] + ctx->state[25] + 4*ctx->state[26] + 5*ctx->state[27] + 4*ctx->state[28] + 4*ctx->state[29] + 5*ctx->state[30] + 4*ctx->state[31] + 4*ctx->state[32] + 4*ctx->state[33] + 5*ctx->state[34] + 4*ctx->state[35] + 4*ctx->state[36] + 4*ctx->state[37] + ctx->state[38] - 1.00 ) );*/ //NOTE: need to put in GNU MP ops + rewrite to array accesses. /*K_H = exp(-( (-1.374+0.076+0.683)/2 + 2*0.084*(theta_H-0.139))/KB/T);*/ //--------------------------------------------------------- //OUTPUT: //To send output to a file, run this program like so: //`$ ./theta.exe > output.txt` printf("OVERALL REACTION RATES:\n"); for(i = 0; i < 78; i++) { gmp_printf("r[%d] = %Fg\n", i, r[i]); } gmp_printf("K_H = %Fg\n", K_H); printf("\nFORWARD REACTION RATES:\n"); for(i = 0; i < 78; i++) { gmp_printf("r_f[%d] = %Fg\n", i, r_f[i]); } printf("\nREVERSE REACTION RATES:\n"); for(i = 0; i < 78; i++) { gmp_printf("r_b[%d] = %Fg\n", i, r_b[i]); } //--------------------------------------------------------- //FREES: //free up the memory GNU MP allocated behind the scenes: //free r[]: for(i = 0; i < 78; i++) { mpf_clear(r[i]); } //free r_f[]: for(i = 0; i < 78; i++) { mpf_clear(r_f[i]); } //free r_b[]: for(i = 0; i < 78; i++) { mpf_clear(r_b[i]); } //free forward and reverse rate constants: clearArray(k_f, 78); clearArray(k_b, 78); //free the various thetas: mpf_clear(theta_A); mpf_clear(theta_E); mpf_clear(theta_F); mpf_clear(theta_G); mpf_clear(theta_I); mpf_clear(theta_L); mpf_clear(theta_M); mpf_clear(theta_N); mpf_clear(theta_O); mpf_clear(theta_R); mpf_clear(theta_S); mpf_clear(theta_U); mpf_clear(theta_V); mpf_clear(theta_X); mpf_clear(free); mpf_clear(theta_H); mpf_clear(theta_OH); mpf_clear(theta_H2O); mpf_clear(theta_CH); mpf_clear(theta_CH2); mpf_clear(theta_CH3); mpf_clear(theta_CH4); mpf_clear(theta_CHO); mpf_clear(theta_CH2O); mpf_clear(theta_CH3O); mpf_clear(theta_CH3OH); mpf_clear(theta_W); mpf_clear(theta_T); mpf_clear(theta_B); mpf_clear(theta_J); mpf_clear(theta_P); mpf_clear(theta_C); mpf_clear(theta_D); mpf_clear(theta_K); mpf_clear(theta_Q); mpf_clear(theta_Y); mpf_clear(theta_Z); mpf_clear(theta_Z1); mpf_clear(theta_CO); //free K_H, temp, and e: mpf_clear(K_H); mpf_clear(temp); mpf_clear(e); //free partial pressures: mpf_clear(p_A); mpf_clear(p_H2); mpf_clear(p_M); mpf_clear(p_S); mpf_clear(p_X); mpf_clear(p_K); mpf_clear(p_CO); mpf_clear(p_H2O); mpf_clear(p_CH4); mpf_clear(p_CH3OH); mpf_clear(KB); mpf_clear(T); //--------------------------------------------------------- }
int main (int argc, char *argv[]) { const char usage[] = "usage: findlc [-dv] m2exp [low_merit [high_merit]]\n"; int f; int v_lose, m_lose, v_best, m_best; int c; int debug = 1; int cnt_high_merit; mpz_t m; unsigned long int m2exp; #define DIMS 6 /* dimensions run in spectral test */ mpf_t v[DIMS-1]; /* spectral test result (there's no v for 1st dimension */ mpf_t f_merit, low_merit, high_merit; mpz_t acc, minus8; mpz_t min, max; mpz_t s; mpz_init (m); mpz_init (a); for (f = 0; f < DIMS-1; f++) mpf_init (v[f]); mpf_init (f_merit); mpf_init_set_d (low_merit, .1); mpf_init_set_d (high_merit, .1); while ((c = getopt (argc, argv, "a:di:hv")) != -1) switch (c) { case 'd': /* debug */ g_debug++; break; case 'v': /* print version */ puts (rcsid[1]); exit (0); case 'h': case '?': default: fputs (usage, stderr); exit (1); } argc -= optind; argv += optind; if (argc < 1) { fputs (usage, stderr); exit (1); } /* Install signal handler. */ if (SIG_ERR == signal (SIGSEGV, sh_status)) { perror ("signal (SIGSEGV)"); exit (1); } if (SIG_ERR == signal (SIGHUP, sh_status)) { perror ("signal (SIGHUP)"); exit (1); } printf ("findlc: version: %s\n", rcsid[1]); m2exp = atol (argv[0]); mpz_init_set_ui (m, 1); mpz_mul_2exp (m, m, m2exp); printf ("m = 0x"); mpz_out_str (stdout, 16, m); puts (""); if (argc > 1) /* have low_merit */ mpf_set_str (low_merit, argv[1], 0); if (argc > 2) /* have high_merit */ mpf_set_str (high_merit, argv[2], 0); if (debug) { fprintf (stderr, "low_merit = "); mpf_out_str (stderr, 10, 2, low_merit); fprintf (stderr, "; high_merit = "); mpf_out_str (stderr, 10, 2, high_merit); fputs ("\n", stderr); } mpz_init (minus8); mpz_set_si (minus8, -8L); mpz_init_set_ui (acc, 0); mpz_init (s); mpz_init_set_d (min, 0.01 * pow (2.0, (double) m2exp)); mpz_init_set_d (max, 0.99 * pow (2.0, (double) m2exp)); mpz_true_random (s, m2exp); /* Start. */ mpz_setbit (s, 0); /* Make it odd. */ v_best = m_best = 2*(DIMS-1); for (;;) { mpz_add (acc, acc, s); mpz_mod_2exp (acc, acc, m2exp); #if later mpz_and_si (a, acc, -8L); #else mpz_and (a, acc, minus8); #endif mpz_add_ui (a, a, 5); if (mpz_cmp (a, min) <= 0 || mpz_cmp (a, max) >= 0) continue; spectral_test (v, DIMS, a, m); for (f = 0, v_lose = m_lose = 0, cnt_high_merit = DIMS-1; f < DIMS-1; f++) { merit (f_merit, f + 2, v[f], m); if (mpf_cmp_ui (v[f], 1 << (30 / (f + 2) + (f == 2))) < 0) v_lose++; if (mpf_cmp (f_merit, low_merit) < 0) m_lose++; if (mpf_cmp (f_merit, high_merit) >= 0) cnt_high_merit--; } if (0 == v_lose && 0 == m_lose) { mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); if (0 == cnt_high_merit) break; /* leave loop */ } if (v_lose < v_best) { v_best = v_lose; printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose); mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); } if (m_lose < m_best) { m_best = m_lose; printf ("best (v_lose=%d; m_lose=%d): ", v_lose, m_lose); mpz_out_str (stdout, 10, a); puts (""); fflush (stdout); } } mpz_clear (m); mpz_clear (a); for (f = 0; f < DIMS-1; f++) mpf_clear (v[f]); mpf_clear (f_merit); mpf_clear (low_merit); mpf_clear (high_merit); printf ("done.\n"); return 0; }
int pi_worker( unsigned long start_x , unsigned long end_x ) { mpf_t _4 ; unsigned long x ; int flag ; mpf_t pi_incr ; mpf_t pi ; char output[ 1024 + 1 ] ; DC4CSetAppLogFile( "pi_worker" ); SetLogLevel( LOGLEVEL_INFO ); InfoLog( __FILE__ , __LINE__ , "pi_worker" ); if( start_x % 2 == 0 ) start_x++; if( end_x % 2 == 0 ) end_x++; if( start_x < 1 ) start_x = 1 ; if( end_x < start_x ) end_x = start_x ; mpf_init_set_d( _4 , 4.00 ); mpf_init( pi_incr ); mpf_init( pi ); /* 0 1 2 3 4 1234567890123456789012345678901234567890 1 1 1 1 1 PI = 4 * ( _ - _ + _ - _ + _ ... ) = 3.1415926535897932384626433832795 1 3 5 7 9 4 1000000000 3.14159265158640477943 14.962s 1000000000 3.14159265557624002834 56.315s 4 100000000 3.14159263358945602263 1.460s 100000000 3.14159267358843756024 5.888s 10000000 3.14159285358961767296 0.621s 1000000 3.14159465358577968703 0.091s 100000 3.14161265318979787768 0.011s 10000 3.14179261359579270235 0.003s */ mpf_set_d( pi , 0.00 ); flag = ((start_x/2)%2)?'-':' ' ; for( x = start_x ; x <= end_x ; x += 2 ) { mpf_div_ui( pi_incr , _4 , x ); if( flag == '-' ) mpf_neg( pi_incr , pi_incr ); mpf_add( pi , pi , pi_incr ); flag = '-' + ' ' - flag ; } memset( output , 0x00 , sizeof(output) ); gmp_snprintf( output , sizeof(output)-1 , "%.Ff" , pi ); InfoLog( __FILE__ , __LINE__ , "pi_worker() - start_x[%lu] end_x[%lu] - PI[%s]" , start_x , end_x , output ); DC4CSetReplyInfo( output ); mpf_clear( _4 ); mpf_clear( pi_incr ); mpf_clear( pi ); return 0; }
int pi_master( char *rservers_ip_port , unsigned long max_x , int tasks_count ) { struct Dc4cApiEnv *penv = NULL ; struct Dc4cBatchTask *tasks_array = NULL ; int workers_count ; unsigned long dd_x ; unsigned long start_x , end_x ; int i ; struct Dc4cBatchTask *p_task = NULL ; mpf_t pi_incr ; mpf_t pi ; char output[ 1024 + 1 ] ; int nret = 0 ; DC4CSetAppLogFile( "pi_master" ); SetLogLevel( LOGLEVEL_INFO ); InfoLog( __FILE__ , __LINE__ , "pi_master" ); nret = DC4CInitEnv( & penv , rservers_ip_port ) ; if( nret ) { ErrorLog( __FILE__ , __LINE__ , "DC4CInitEnv failed[%d]" , nret ); return -1; } else { InfoLog( __FILE__ , __LINE__ , "DC4CInitEnv ok" ); } DC4CSetOptions( penv , DC4C_OPTIONS_BIND_CPU ); if( tasks_count == -2 ) { nret = DC4CQueryWorkers( penv ) ; if( nret ) { ErrorLog( __FILE__ , __LINE__ , "DC4CQueryWorkers failed[%d]" , nret ); return -1; } tasks_count = DC4CGetUnusedWorkersCount( penv ) ; if( tasks_count <= 0 ) { ErrorLog( __FILE__ , __LINE__ , "tasks_count[%d] invalid" , tasks_count ); return -1; } } tasks_array = (struct Dc4cBatchTask *)malloc( sizeof(struct Dc4cBatchTask) * tasks_count ) ; if( tasks_array == NULL ) { ErrorLog( __FILE__ , __LINE__ , "alloc failed , errno[%d]" , errno ); return -1; } memset( tasks_array , 0x00 , sizeof(struct Dc4cBatchTask) * tasks_count ); dd_x = ( max_x - tasks_count ) / tasks_count ; start_x = 1 ; for( i = 0 , p_task = tasks_array ; i < tasks_count ; i++ , p_task++ ) { end_x = start_x + dd_x ; snprintf( p_task->program_and_params , sizeof(p_task->program_and_params) , "dc4c_test_worker_pi %lu %lu" , start_x , end_x ); p_task->timeout = DC4CGetTimeout(penv) ; start_x = end_x + 2 ; } workers_count = tasks_count ; nret = DC4CDoBatchTasks( penv , workers_count , tasks_array , tasks_count ) ; free( tasks_array ); if( nret ) { ErrorLog( __FILE__ , __LINE__ , "DC4CDoBatchTasks failed[%d]" , nret ); return -1; } else { InfoLog( __FILE__ , __LINE__ , "DC4CDoBatchTasks ok" ); } mpf_init( pi_incr ); mpf_init_set_d( pi , 0.00 ); for( i = 0 ; i < tasks_count ; i++ ) { mpf_set_str( pi_incr , DC4CGetBatchTasksInfo(penv,i) , 10 ); mpf_add( pi , pi , pi_incr ); } memset( output , 0x00 , sizeof(output) ); gmp_snprintf( output , sizeof(output)-1 , "%.Ff" , pi ); InfoLog( __FILE__ , __LINE__ , "pi_master() - max_x[%u] tasks_count[%d] - PI[%s]" , max_x , tasks_count , output ); mpf_clear( pi_incr ); mpf_clear( pi ); DC4CCleanEnv( & penv ); InfoLog( __FILE__ , __LINE__ , "DC4CCleanEnv ok" ); return 0; }
void omega(long int n, long int m, double tau, long int q, long int k1, long int k2, mpf_t *factoriales, mpf_t pi) { mpf_t aux, aux2, aux3, sqrf, acum, Ltau, z, zelev; int j; unsigned long int qsqr; /* * Set n = min(n,m), and m = max(n,m) */ j = n; n = min(n,m); m = max(j,m); /* * The sqrt(n!m!) pre-factor */ mpf_init(aux); mpf_init(sqrf); mpf_mul(aux, factoriales[n], factoriales[m]); mpf_sqrt(sqrf, aux); /* * Calculus of tau */ mpf_init(aux2); mpf_init_set_d(Ltau, tau); mpf_init(z); mpf_init(zelev); qsqr = (unsigned long int) q; mpf_set_d(aux, (double) pow((double) k1, (double) 2)); mpf_mul(aux, aux, Ltau); mpf_set_d(aux2, (double) pow((double) k2, (double) 2)); mpf_div(aux, aux, Ltau); mpf_add(aux, aux, aux2); mpf_div_ui(aux, aux, qsqr); mpf_mul(z, aux, pi); if ( ((m-n)%2) == 0) mpf_pow_ui(zelev, z, (unsigned long int) (m-n)/2); else { mpf_pow_ui(zelev, z, m-n); mpf_sqrt(zelev, zelev); } /* mpf_pow_ui(zelev, z, m-n); *mpf_pow_ui(z, z, 2); */ /* mpf_out_str(stdout, 10, 20, z); printf("\n");*/ /* * The loop */ mpf_init(acum); mpf_init(aux3); mpf_set_ui(acum, (unsigned long int) 0); for (j=0; j <= n; j++) { mpf_mul(aux, factoriales[j], factoriales[n-j]); mpf_mul(aux2, aux, factoriales[j+m-n]); mpf_pow_ui(aux3, z, j); mpf_div(aux, aux3, aux2); if ((j%2) == 0) mpf_set(aux2, aux); else mpf_neg(aux2, aux); mpf_add(acum, acum, aux2); } mpf_mul(aux, acum, sqrf); mpf_mul(aux, aux, zelev); gmp_printf("%4d %4d %4d %4d %20.20Fe\n", n, m, k1, k2, aux); /*mpf_out_str(stdout, 10, 20, aux);*/ }
int main () { int i = 0; /*Contador de iterações*/ int k = 8; /*Fator de multiplicação*/ mpf_t pi_pas, pi_novo; mpf_t a_pas, a_novo; mpf_t y_pas, y_novo; mpf_t temp1, temp2; mpf_t e; FILE *fileTime; /*Ponteiro do arquivo de saída*/ clock_t start, end; double cpu_time_used; mpf_set_default_prec(BITS_PER_DIGIT * 11000000); /*Precisão default*/ /*Inicialização das variáveis*/ mpf_init(pi_pas); mpf_init(pi_novo); mpf_init(a_pas); mpf_init(y_pas); mpf_init(temp1); mpf_init(temp2); mpf_init_set_d(a_novo, 32.0); mpf_sqrt(a_novo, a_novo); mpf_ui_sub(a_novo, 6, a_novo); mpf_init_set_d(y_novo, 2.0); mpf_sqrt(y_novo, y_novo); mpf_sub_ui(y_novo, y_novo, 1); mpf_init_set_str(e, "1e-10000000", 10); mpf_ui_div(pi_novo, 1, a_novo); start = clock(); /*Calcula as iterações*/ do { mpf_swap(pi_pas, pi_novo); mpf_swap(a_pas, a_novo); mpf_swap(y_pas, y_novo); mpf_pow_ui(y_pas, y_pas, 4); mpf_ui_sub(y_pas, 1, y_pas); mpf_sqrt(y_pas, y_pas); mpf_sqrt(y_pas, y_pas); mpf_add_ui(temp1, y_pas, 1); mpf_ui_sub(y_novo, 1, y_pas); mpf_div(y_novo, y_novo, temp1); mpf_add_ui(temp1, y_novo, 1); mpf_pow_ui(temp2, y_novo, 2); mpf_add(temp2, temp2, temp1); mpf_mul(temp2, temp2, y_novo); mpf_mul_ui(temp2, temp2, k); k *= 4; mpf_pow_ui(temp1, temp1, 4); mpf_mul(temp1, temp1, a_pas); mpf_sub(a_novo, temp1, temp2); mpf_ui_div(pi_novo, 1, a_novo); mpf_sub(pi_pas, pi_novo, pi_pas); mpf_abs(pi_pas, pi_pas); gmp_printf("\nIteracao %d | pi = %.25Ff", i, pi_novo); i++; } while ( mpf_cmp(e, pi_pas) < 0 ); end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; fileTime = fopen("execution_time.txt", "w"); fprintf(fileTime, "Execution time: %f\n", cpu_time_used); fclose(fileTime); /*Libera espaço de memória*/ mpf_clear(pi_pas); mpf_clear(pi_novo); mpf_clear(a_pas); mpf_clear(a_novo); mpf_clear(y_pas); mpf_clear(y_novo); mpf_clear(temp1); mpf_clear(temp2); mpf_clear(e); return 0; }