vector delta, sum_jacobx

This commit is contained in:
David Parkhurst 2021-03-16 15:50:20 -06:00
parent f0707aa0b3
commit 51514eb125
4 changed files with 39 additions and 109 deletions

View File

@ -576,18 +576,7 @@ void Phreeqc::init(void)
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
* Jacobian and Mass balance lists * Jacobian and Mass balance lists
*---------------------------------------------------------------------- */ *---------------------------------------------------------------------- */
count_sum_jacob0 = 0;
max_sum_jacob0 = 0;
sum_jacob0 = NULL;
count_sum_jacob1 = 0;
max_sum_jacob1 = 0;
sum_jacob1 = NULL;
count_sum_jacob2 = 0;
max_sum_jacob2 = 0;
sum_jacob2 = NULL;
count_sum_delta = 0;
max_sum_delta = 0;
sum_delta = NULL;
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
* Solution * Solution
*---------------------------------------------------------------------- */ *---------------------------------------------------------------------- */

View File

@ -1321,28 +1321,18 @@ protected:
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
* Jacobian and Mass balance lists * Jacobian and Mass balance lists
*---------------------------------------------------------------------- */ *---------------------------------------------------------------------- */
std::vector<struct list0> sum_jacob0; /* array of pointers to targets and coefficients for array */
int count_sum_jacob0; /* number of elements in sum_jacob0 */
int max_sum_jacob0; /* calculated maximum number of elements in sum_jacob0 */
struct list0 *sum_jacob0; /* array of pointers to targets and coefficients for array */
std::vector<struct list1> sum_mb1; /* array of pointers to sources and targets for mass std::vector<struct list1> sum_mb1; /* array of pointers to sources and targets for mass
balance summations with coef = 1.0 */ balance summations with coef = 1.0 */
int count_sum_jacob1; /* number of elements in sum_jacob1 */ std::vector<struct list1> sum_jacob1; /* array of pointers to sources and targets for array
int max_sum_jacob1; /* calculated maximum number of elements in sum_jacob1 */ equations with coef = 1.0 */
struct list1 *sum_jacob1; /* array of pointers to sources and targets for array
equations with coef = 1.0 */
std::vector<struct list2> sum_mb2; /* array of coefficients and pointers to sources and std::vector<struct list2> sum_mb2; /* array of coefficients and pointers to sources and
targets for mass balance summations with coef != 1.0 */ targets for mass balance summations with coef != 1.0 */
int count_sum_jacob2; /* number of elements in sum_jacob2 */ std::vector<struct list2> sum_jacob2; /* array of coefficients and pointers to sources and
int max_sum_jacob2; /* calculated maximum number of elements in sum_jacob2 */ targets, coef != 1.0 */
struct list2 *sum_jacob2; /* array of coefficients and pointers to sources and std::vector<struct list2> sum_delta; /* array of pointers to sources, targets and coefficients for
targets, coef != 1.0 */ summing deltas for mass balance equations */
int count_sum_delta; /* number of elements in sum_delta */
int max_sum_delta; /* calculated maximum number of elements in sum_delta */
struct list2 *sum_delta; /* array of pointers to sources, targets and coefficients for
summing deltas for mass balance equations */
/*---------------------------------------------------------------------- /*----------------------------------------------------------------------
* Solution * Solution
*---------------------------------------------------------------------- */ *---------------------------------------------------------------------- */

View File

@ -1951,21 +1951,21 @@ jacobian_sums(void)
/* /*
* Add constant terms * Add constant terms
*/ */
for (k = 0; k < count_sum_jacob0; k++) for (k = 0; k < (int)sum_jacob0.size(); k++)
{ {
*sum_jacob0[k].target += sum_jacob0[k].coef; *sum_jacob0[k].target += sum_jacob0[k].coef;
} }
/* /*
* Add terms with coefficients of 1.0 * Add terms with coefficients of 1.0
*/ */
for (k = 0; k < count_sum_jacob1; k++) for (k = 0; k < (int)sum_jacob1.size(); k++)
{ {
*sum_jacob1[k].target += *sum_jacob1[k].source; *sum_jacob1[k].target += *sum_jacob1[k].source;
} }
/* /*
* Add terms with coefficients != 1.0 * Add terms with coefficients != 1.0
*/ */
for (k = 0; k < count_sum_jacob2; k++) for (k = 0; k < (int)sum_jacob2.size(); k++)
{ {
*sum_jacob2[k].target += *sum_jacob2[k].source * sum_jacob2[k].coef; *sum_jacob2[k].target += *sum_jacob2[k].source * sum_jacob2[k].coef;
} }
@ -2135,7 +2135,6 @@ mb_sums(void)
for (k = 0; k < (int)sum_mb1.size(); k++) for (k = 0; k < (int)sum_mb1.size(); k++)
{ {
*sum_mb1[k].target += *sum_mb1[k].source; *sum_mb1[k].target += *sum_mb1[k].source;
/* { k += 1; k -= 1;} */
} }
/* /*
* Add terms with coefficients != 1.0 * Add terms with coefficients != 1.0
@ -2143,7 +2142,6 @@ mb_sums(void)
for (k = 0; k < (int)sum_mb2.size(); k++) for (k = 0; k < (int)sum_mb2.size(); k++)
{ {
*sum_mb2[k].target += *sum_mb2[k].source * sum_mb2[k].coef; *sum_mb2[k].target += *sum_mb2[k].source * sum_mb2[k].coef;
/* { k += 1; k -= 1;} */
} }
return (OK); return (OK);
} }
@ -3155,7 +3153,7 @@ reset(void)
x[i]->delta = 0.0; x[i]->delta = 0.0;
} }
for (i = 0; i < count_sum_delta; i++) for (i = 0; i < (int)sum_delta.size(); i++)
{ {
*sum_delta[i].target += *sum_delta[i].source * sum_delta[i].coef; *sum_delta[i].target += *sum_delta[i].source * sum_delta[i].coef;
} }
@ -5180,14 +5178,10 @@ free_model_allocs(void)
s_x.clear(); s_x.clear();
sum_mb1.clear(); sum_mb1.clear();
sum_mb2.clear(); sum_mb2.clear();
sum_jacob0 = (struct list0 *) free_check_null(sum_jacob0); sum_jacob0.clear();
count_sum_jacob0 = 0; sum_jacob1.clear();
sum_jacob1 = (struct list1 *) free_check_null(sum_jacob1); sum_jacob2.clear();
count_sum_jacob1 = 0; sum_delta.clear();
sum_jacob2 = (struct list2 *) free_check_null(sum_jacob2);
count_sum_jacob2 = 0;
sum_delta = (struct list2 *) free_check_null(sum_delta);
count_sum_delta = 0;
return (OK); return (OK);
} }

View File

@ -1136,39 +1136,12 @@ build_model(void)
sum_species_map_db.clear(); sum_species_map_db.clear();
sum_species_map.clear(); sum_species_map.clear();
s_x.clear(); s_x.clear();
//max_sum_mb1 = MAX_SUM_MB;
//count_sum_mb1 = 0;
//space((void **) ((void *) &sum_mb1), INIT, &max_sum_mb1,
// sizeof(struct list1));
sum_mb1.clear(); sum_mb1.clear();
//max_sum_mb2 = MAX_SUM_MB;
//count_sum_mb2 = 0;
//space((void **) ((void *) &sum_mb2), INIT, &max_sum_mb2,
// sizeof(struct list2
sum_mb2.clear(); sum_mb2.clear();
sum_jacob0.clear();
max_sum_jacob0 = MAX_SUM_JACOB0; sum_jacob1.clear();
count_sum_jacob0 = 0; sum_jacob2.clear();
space((void **) ((void *) &sum_jacob0), INIT, &max_sum_jacob0, sum_delta.clear();
sizeof(struct list0));
max_sum_jacob1 = MAX_SUM_JACOB1;
count_sum_jacob1 = 0;
space((void **) ((void *) &sum_jacob1), INIT, &max_sum_jacob1,
sizeof(struct list1));
max_sum_jacob2 = MAX_SUM_JACOB2;
count_sum_jacob2 = 0;
space((void **) ((void *) &sum_jacob2), INIT, &max_sum_jacob2,
sizeof(struct list2));
max_sum_delta = MAX_SUM_JACOB0;
count_sum_delta = 0;
space((void **) ((void *) &sum_delta), INIT, &max_sum_delta,
sizeof(struct list2));
max_species_list = 5 * MAX_S; max_species_list = 5 * MAX_S;
count_species_list = 0; count_species_list = 0;
@ -2642,10 +2615,10 @@ reprep(void)
s_x.clear(); s_x.clear();
sum_mb1.clear(); sum_mb1.clear();
sum_mb2.clear(); sum_mb2.clear();
sum_jacob0 = (struct list0 *) free_check_null(sum_jacob0); sum_jacob0.clear();
sum_jacob1 = (struct list1 *) free_check_null(sum_jacob1); sum_jacob1.clear();
sum_jacob2 = (struct list2 *) free_check_null(sum_jacob2); sum_jacob2.clear();
sum_delta = (struct list2 *) free_check_null(sum_delta); sum_delta.clear();
/* /*
* Build model again * Build model again
*/ */
@ -4913,34 +4886,26 @@ store_jacob(LDBLE * source, LDBLE * target, LDBLE coef)
*/ */
if (equal(coef, 1.0, TOL) == TRUE) if (equal(coef, 1.0, TOL) == TRUE)
{ {
size_t count_sum_jacob1 = sum_jacob1.size();
sum_jacob1.resize(count_sum_jacob1 + 1);
if (debug_prep == TRUE) if (debug_prep == TRUE)
{ {
output_msg(sformatf( "\tjacob1 %d\n", count_sum_jacob1)); output_msg(sformatf( "\tjacob1 %d\n", (int)count_sum_jacob1));
} }
sum_jacob1[count_sum_jacob1].source = source; sum_jacob1[count_sum_jacob1].source = source;
sum_jacob1[count_sum_jacob1++].target = target; sum_jacob1[count_sum_jacob1].target = target;
/* Check space */
if (count_sum_jacob1 >= max_sum_jacob1)
{
space((void **) ((void *) &sum_jacob1), count_sum_jacob1,
&max_sum_jacob1, sizeof(struct list1));
}
} }
else else
{ {
size_t count_sum_jacob2 = sum_jacob2.size();
sum_jacob2.resize(count_sum_jacob2 + 1);
if (debug_prep == TRUE) if (debug_prep == TRUE)
{ {
output_msg(sformatf( "\tjacob2 %d\n", count_sum_jacob2)); output_msg(sformatf("\tjacob2 %d\n", count_sum_jacob2));
} }
sum_jacob2[count_sum_jacob2].source = source; sum_jacob2[count_sum_jacob2].source = source;
sum_jacob2[count_sum_jacob2].target = target; sum_jacob2[count_sum_jacob2].target = target;
sum_jacob2[count_sum_jacob2++].coef = coef; sum_jacob2[count_sum_jacob2].coef = coef;
/* Check space */
if (count_sum_jacob2 >= max_sum_jacob2)
{
space((void **) ((void *) &sum_jacob2), count_sum_jacob2,
&max_sum_jacob2, sizeof(struct list2));
}
} }
return (OK); return (OK);
} }
@ -4953,15 +4918,11 @@ store_jacob0(int row, int column, LDBLE coef)
/* /*
* Stores in list a constant coef which will be added into jacobian array * Stores in list a constant coef which will be added into jacobian array
*/ */
size_t count_sum_jacob0 = sum_jacob0.size();
sum_jacob0.resize(count_sum_jacob0 + 1);
sum_jacob0[count_sum_jacob0].target = sum_jacob0[count_sum_jacob0].target =
&(my_array[row * (count_unknowns + 1) + column]); &(my_array[(size_t)row * ((size_t)count_unknowns + 1) + (size_t)column]);
sum_jacob0[count_sum_jacob0++].coef = coef; sum_jacob0[count_sum_jacob0].coef = coef;
/* Check space */
if (count_sum_jacob0 >= max_sum_jacob0)
{
space((void **) ((void *) &sum_jacob0), count_sum_jacob0,
&max_sum_jacob0, sizeof(struct list0));
}
return (OK); return (OK);
} }
@ -5004,15 +4965,11 @@ store_sum_deltas(LDBLE * source, LDBLE * target, LDBLE coef)
* in x[i]->delta. These may be multiplied by a factor under some * in x[i]->delta. These may be multiplied by a factor under some
* situations where the entire calculated step is not taken * situations where the entire calculated step is not taken
*/ */
size_t count_sum_delta = sum_delta.size();
sum_delta.resize(count_sum_delta + 1);
sum_delta[count_sum_delta].source = source; sum_delta[count_sum_delta].source = source;
sum_delta[count_sum_delta].target = target; sum_delta[count_sum_delta].target = target;
sum_delta[count_sum_delta++].coef = coef; sum_delta[count_sum_delta].coef = coef;
/* Check space */
if (count_sum_delta >= max_sum_delta)
{
space((void **) ((void *) &sum_delta), count_sum_delta,
&max_sum_delta, sizeof(struct list2));
}
return (OK); return (OK);
} }