예제 #1
0
/* ************************************************************************* */
int compare_array_2d(N_array_2d * a, N_array_2d * b)
{
    int rows, cols, type;
    int i, j, res = 0;

    cols = a->cols;
    rows = a->rows;
    type = N_get_array_2d_type(a);

    #pragma omp parallel for private (i, j) shared (cols, rows, type, a, b) reduction(+:res)
    for (j = 0; j < rows; j++) {
        for (i = 0; i < cols; i++) {
            if (type == CELL_TYPE) {
                if (N_get_array_2d_c_value(a, i, j) !=
                        N_get_array_2d_c_value(b, i, j))
                    res++;
            }
            if (type == FCELL_TYPE) {
                if (N_get_array_2d_f_value(a, i, j) !=
                        N_get_array_2d_f_value(b, i, j))
                    res++;
            }
            if (type == DCELL_TYPE) {
                if (N_get_array_2d_d_value(a, i, j) !=
                        N_get_array_2d_d_value(b, i, j))
                    res++;
            }
        }
    }

    return res;
}
예제 #2
0
/* ************************************************************************* */
int fill_array_2d(N_array_2d * a)
{
    int rows, cols, type;
    int i, j, res = 0;

    rows = a->rows;
    cols = a->cols;
    type = N_get_array_2d_type(a);

    #pragma omp parallel for private (i, j) shared (cols, rows, type, a) reduction(+:res)
    for (j = 0; j < rows; j++) {
        for (i = 0; i < cols; i++) {
            if (type == CELL_TYPE) {
                N_put_array_2d_c_value(a, i, j, (CELL) i * (CELL) j);
                if (N_get_array_2d_c_value(a, i, j) != (CELL) i * (CELL) j)
                    res++;
            }
            if (type == FCELL_TYPE) {
                N_put_array_2d_f_value(a, i, j, (FCELL) i * (FCELL) j);
                if (N_get_array_2d_f_value(a, i, j) != (FCELL) i * (FCELL) j)
                    res++;
            }
            if (type == DCELL_TYPE) {
                N_put_array_2d_d_value(a, i, j, (DCELL) i * (DCELL) j);
                if (N_get_array_2d_d_value(a, i, j) != (DCELL) i * (DCELL) j)
                    res++;
            }
        }
    }

    return res;
}
예제 #3
0
파일: main.c 프로젝트: imincik/pkg-grass
/* ************************************************************************* */
void
copy_result(N_array_2d * status, N_array_2d * phead_start, double *result,
	    struct Cell_head *region, N_array_2d * target)
{
    int y, x, rows, cols, count, stat;
    double d1 = 0;
    DCELL val;

    rows = region->rows;
    cols = region->cols;

    count = 0;
    for (y = 0; y < rows; y++) {
	G_percent(y, rows - 1, 10);
	for (x = 0; x < cols; x++) {
	    stat = N_get_array_2d_c_value(status, x, y);
	    if (stat == N_CELL_ACTIVE) {	/*only active cells */
		d1 = result[count];
		val = (DCELL) d1;
		count++;
	    }
	    else if (stat == N_CELL_DIRICHLET) {	/*dirichlet cells */
		d1 = N_get_array_2d_d_value(phead_start, x, y);
		val = (DCELL) d1;
		count++;
	    }
	    else {
		G_set_null_value(&val, 1, DCELL_TYPE);
	    }
	    N_put_array_2d_d_value(target, x, y, val);
	}
    }

    return;
}
예제 #4
0
/* **************************************************************** */
int make_les_entry_2d(int i, int j, int offset_i, int offset_j, int count,
		      int pos, N_les * les, G_math_spvector * spvect,
		      N_array_2d * cell_count, N_array_2d * status,
		      N_array_2d * start_val, double entry, int cell_type)
{
    int K;
    int di = offset_i;
    int dj = offset_j;

    K = N_get_array_2d_c_value(cell_count, i + di, j + dj) -
	N_get_array_2d_c_value(cell_count, i, j);

    /* active cells build the linear equation system */
    if (cell_type == N_CELL_ACTIVE) {
	/* dirichlet or transmission cells must be handled like this */
	if (N_get_array_2d_c_value(status, i + di, j + dj) > N_CELL_ACTIVE &&
	    N_get_array_2d_c_value(status, i + di, j + dj) < N_MAX_CELL_STATE)
	    les->b[count] -=
		N_get_array_2d_d_value(start_val, i + di, j + dj) * entry;
	else if (N_get_array_2d_c_value(status, i + di, j + dj) ==
		 N_CELL_ACTIVE) {
	    if ((count + K) >= 0 && (count + K) < les->cols) {
		G_debug(5,
			" make_les_entry_2d: (N_CELL_ACTIVE) create matrix entry at row[%i] col[%i] value %g\n",
			count, count + K, entry);
		pos++;
		if (les->type == N_SPARSE_LES) {
		    spvect->index[pos] = count + K;
		    spvect->values[pos] = entry;
		}
		else {
		    les->A[count][count + K] = entry;
		}
	    }
	}
    }				/* if dirichlet cells should be used then check for all valid cell neighbours */
    else if (cell_type == N_CELL_DIRICHLET) {
	/* all valid cells */
	if (N_get_array_2d_c_value(status, i + di, j + dj) > N_CELL_INACTIVE
	    && N_get_array_2d_c_value(status, i + di,
				      j + dj) < N_MAX_CELL_STATE) {
	    if ((count + K) >= 0 && (count + K) < les->cols) {
		G_debug(5,
			" make_les_entry_2d: (N_CELL_DIRICHLET) create matrix entry at row[%i] col[%i] value %g\n",
			count, count + K, entry);
		pos++;
		if (les->type == N_SPARSE_LES) {
		    spvect->index[pos] = count + K;
		    spvect->values[pos] = entry;
		}
		else {
		    les->A[count][count + K] = entry;
		}
	    }
	}
    }

    return pos;
}
예제 #5
0
/*!
 * \brief Write info and content of the N_array_2d struct to stdout
 *
 * Offsets are ignored
 *
 * \param data N_array_2d *
 * \return void
 * */
void N_print_array_2d(N_array_2d * data)
{
    int i, j;

    N_print_array_2d_info(data);

    for (j = 0 - data->offset; j < data->rows + data->offset; j++) {
	for (i = 0 - data->offset; i < data->cols + data->offset; i++) {
	    if (data->type == CELL_TYPE)
		fprintf(stdout, "%6d ", N_get_array_2d_c_value(data, i, j));
	    else if (data->type == FCELL_TYPE)
		fprintf(stdout, "%6.6f ", N_get_array_2d_f_value(data, i, j));
	    else if (data->type == DCELL_TYPE)
		printf("%6.6f ", N_get_array_2d_d_value(data, i, j));
	}
	fprintf(stdout, "\n");
    }
    fprintf(stdout, "\n");

    return;
}
예제 #6
0
/*!
 * \brief Integrate Dirichlet or Transmission boundary conditions into the les (2s)
 *
 * Dirichlet and Transmission boundary conditions will be integrated into
 * the provided linear equation system. This is meaningfull if
 * the les was created with #N_assemble_les_2d_dirichlet, because in
 * this case Dirichlet boundary conditions are not automatically included.
 *
 * The provided les will be modified:
 *
 * Ax = b will be splitted into Ax_u + Ax_d = b
 *
 * x_u - the unknowns
 * x_d - the Dirichlet cells
 *
 * Ax_u = b -Ax_d will be computed. Then the matrix A will be modified to
 *
 * | A_u  0 | x_u
 * |  0   I | x_d
 *
 * \param les N_les* -- the linear equation system
 * \param geom N_geom_data* -- geometrical data information
 * \param status N_array_2d* -- the status array containing the cell types
 * \param start_val N_array_2d* -- an array with start values
 * \return int -- 1 = success, 0 = failure
 * */
int N_les_integrate_dirichlet_2d(N_les * les, N_geom_data * geom,
				 N_array_2d * status, N_array_2d * start_val)
{
    int rows, cols;
    int count = 0;
    int i, j, x, y, stat;
    double *dvect1;
    double *dvect2;

    G_debug(2,
	    "N_les_integrate_dirichlet_2d: integrating the dirichlet boundary condition");

    rows = geom->rows;
    cols = geom->cols;

    /*we nned to additional vectors */
    dvect1 = (double *)G_calloc(les->cols, sizeof(double));
    dvect2 = (double *)G_calloc(les->cols, sizeof(double));

    /*fill the first one with the x vector data of Dirichlet cells */
    count = 0;
    for (y = 0; y < rows; y++) {
	for (x = 0; x < cols; x++) {
	    stat = N_get_array_2d_c_value(status, x, y);
	    if (stat > N_CELL_ACTIVE && stat < N_MAX_CELL_STATE) {
		dvect1[count] = N_get_array_2d_d_value(start_val, x, y);
		count++;
	    }
	    else if (stat == N_CELL_ACTIVE) {
		dvect1[count] = 0.0;
		count++;
	    }
	}
    }

#pragma omp parallel default(shared)
    {
	/*perform the matrix vector product and */
	if (les->type == N_SPARSE_LES)
	    G_math_Ax_sparse(les->Asp, dvect1, dvect2, les->rows);
	else
	    G_math_d_Ax(les->A, dvect1, dvect2, les->rows, les->cols);
#pragma omp for schedule (static) private(i)
	for (i = 0; i < les->cols; i++)
	    les->b[i] = les->b[i] - dvect2[i];
    }

    /*now set the Dirichlet cell rows and cols to zero and the 
     * diagonal entry to 1*/
    count = 0;
    for (y = 0; y < rows; y++) {
	for (x = 0; x < cols; x++) {
	    stat = N_get_array_2d_c_value(status, x, y);
	    if (stat > N_CELL_ACTIVE && stat < N_MAX_CELL_STATE) {
		if (les->type == N_SPARSE_LES) {
		    /*set the rows to zero */
		    for (i = 0; i < les->Asp[count]->cols; i++)
			les->Asp[count]->values[i] = 0.0;
		    /*set the cols to zero */
		    for (i = 0; i < les->rows; i++) {
			for (j = 0; j < les->Asp[i]->cols; j++) {
			    if (les->Asp[i]->index[j] == count)
				les->Asp[i]->values[j] = 0.0;
			}
		    }

		    /*entry on the diagonal */
		    les->Asp[count]->values[0] = 1.0;

		}
		else {
		    /*set the rows to zero */
		    for (i = 0; i < les->cols; i++)
			les->A[count][i] = 0.0;
		    /*set the cols to zero */
		    for (i = 0; i < les->rows; i++)
			les->A[i][count] = 0.0;

		    /*entry on the diagonal */
		    les->A[count][count] = 1.0;
		}
	    }
	    if (stat >= N_CELL_ACTIVE)
		count++;
	}
    }

    return 0;

}
예제 #7
0
/*!
 * \brief Assemble a linear equation system (les) based on 2d location data  (raster)
 *
 * 
 * The linear equation system type can be set to N_NORMAL_LES to create a regular
 * matrix, or to N_SPARSE_LES to create a sparse matrix. This function returns
 * a new created linear equation system which can be solved with 
 * linear equation solvers. An 2d array with start values and an 2d status array
 * must be provided as well as the location geometry and a void pointer to data 
 * passed to the callback which creates the les row entries. This callback
 * must be defined in the N_les_callback_2d strcuture.
 *
 * The creation of the les is parallelized with OpenMP. 
 * If you implement new callbacks, please make sure that the 
 * function calls are thread safe.
 *
 *
 * the les can be created in two ways, with dirichlet and similar cells and without them,
 * to spare some memory. If the les is created with dirichlet cell, the dirichlet boundary condition
 * must be added.
 *
 * \param les_type int
 * \param geom      N_geom_data*
 * \param status    N_array_2d *
 * \param start_val N_array_2d *
 * \param data void *
 * \param cell_type int  -- les assemble based on N_CELL_ACTIVE or N_CELL_DIRICHLET
 * \param call N_les_callback_2d *
 * \return N_les *
 * */
N_les *N_assemble_les_2d_param(int les_type, N_geom_data * geom,
			       N_array_2d * status, N_array_2d * start_val,
			       void *data, N_les_callback_2d * call,
			       int cell_type)
{
    int i, j, count = 0, pos = 0;
    int cell_type_count = 0;
    int **index_ij;
    N_array_2d *cell_count;
    N_les *les = NULL;

    G_debug(2,
	    "N_assemble_les_2d: starting to assemble the linear equation system");

    /* At first count the number of valid cells and save 
     * each number in a new 2d array. Those numbers are used 
     * to create the linear equation system.
     * */

    cell_count = N_alloc_array_2d(geom->cols, geom->rows, 1, CELL_TYPE);

    /* include dirichlet cells in the les */
    if (cell_type == N_CELL_DIRICHLET) {
	for (j = 0; j < geom->rows; j++) {
	    for (i = 0; i < geom->cols; i++) {
		/*use all non-inactive cells for les creation */
		if (N_CELL_INACTIVE < N_get_array_2d_c_value(status, i, j) &&
		    N_get_array_2d_c_value(status, i, j) < N_MAX_CELL_STATE)
		    cell_type_count++;
	    }
	}
    }
    /*use only active cell in the les */
    if (cell_type == N_CELL_ACTIVE) {
	for (j = 0; j < geom->rows; j++) {
	    for (i = 0; i < geom->cols; i++) {
		/*count only active cells */
		if (N_CELL_ACTIVE == N_get_array_2d_d_value(status, i, j))
		    cell_type_count++;
	    }
	}
    }

    G_debug(2, "N_assemble_les_2d: number of used cells %i\n",
	    cell_type_count);

    if (cell_type_count == 0)
	G_fatal_error
	    ("Not enough cells [%i] to create the linear equation system. Check the cell status. Only active cells (value = 1) are used to create the equation system.",
	     cell_type_count);

    /* Then allocate the memory for the linear equation system (les). 
     * Only valid cells are used to create the les. */
    index_ij = (int **)G_calloc(cell_type_count, sizeof(int *));
    for (i = 0; i < cell_type_count; i++)
	index_ij[i] = (int *)G_calloc(2, sizeof(int));

    les = N_alloc_les_Ax_b(cell_type_count, les_type);

    count = 0;

    /*count the number of cells which should be used to create the linear equation system */
    /*save the i and j indices and create a ordered numbering */
    for (j = 0; j < geom->rows; j++) {
	for (i = 0; i < geom->cols; i++) {
	    /*count every non-inactive cell */
	    if (cell_type == N_CELL_DIRICHLET) {
		if (N_CELL_INACTIVE < N_get_array_2d_c_value(status, i, j) &&
		    N_get_array_2d_c_value(status, i, j) < N_MAX_CELL_STATE) {
		    N_put_array_2d_c_value(cell_count, i, j, count);
		    index_ij[count][0] = i;
		    index_ij[count][1] = j;
		    count++;
		    G_debug(5,
			    "N_assemble_les_2d: non-inactive cells count %i at pos x[%i] y[%i]\n",
			    count, i, j);
		}
		/*count every active cell */
	    }
	    else if (N_CELL_ACTIVE == N_get_array_2d_c_value(status, i, j)) {
		N_put_array_2d_c_value(cell_count, i, j, count);
		index_ij[count][0] = i;
		index_ij[count][1] = j;
		count++;
		G_debug(5,
			"N_assemble_les_2d: active cells count %i at pos x[%i] y[%i]\n",
			count, i, j);
	    }
	}
    }

    G_debug(2, "N_assemble_les_2d: starting the parallel assemble loop");

    /* Assemble the matrix in parallel */
#pragma omp parallel for private(i, j, pos, count) schedule(static)
    for (count = 0; count < cell_type_count; count++) {
	i = index_ij[count][0];
	j = index_ij[count][1];

	/*create the entries for the */
	N_data_star *items = call->callback(data, geom, i, j);

	/* we need a sparse vector pointer anytime */
	G_math_spvector *spvect = NULL;

	/*allocate a sprase vector */
	if (les_type == N_SPARSE_LES) {
	    spvect = G_math_alloc_spvector(items->count);
	}
	/* initial conditions */
	les->x[count] = N_get_array_2d_d_value(start_val, i, j);

	/* the entry in the vector b */
	les->b[count] = items->V;

	/* pos describes the position in the sparse vector.
	 * the first entry is always the diagonal entry of the matrix*/
	pos = 0;

	if (les_type == N_SPARSE_LES) {
	    spvect->index[pos] = count;
	    spvect->values[pos] = items->C;
	}
	else {
	    les->A[count][count] = items->C;
	}
	/* western neighbour, entry is col - 1 */
	if (i > 0) {
	    pos = make_les_entry_2d(i, j, -1, 0, count, pos, les, spvect,
				    cell_count, status, start_val, items->W,
				    cell_type);
	}
	/* eastern neighbour, entry col + 1 */
	if (i < geom->cols - 1) {
	    pos = make_les_entry_2d(i, j, 1, 0, count, pos, les, spvect,
				    cell_count, status, start_val, items->E,
				    cell_type);
	}
	/* northern neighbour, entry row - 1 */
	if (j > 0) {
	    pos =
		make_les_entry_2d(i, j, 0, -1, count, pos, les, spvect,
				  cell_count, status, start_val, items->N,
				  cell_type);
	}
	/* southern neighbour, entry row + 1 */
	if (j < geom->rows - 1) {
	    pos = make_les_entry_2d(i, j, 0, 1, count, pos, les, spvect,
				    cell_count, status, start_val, items->S,
				    cell_type);
	}
	/*in case of a nine point star, we have additional entries */
	if (items->type == N_9_POINT_STAR) {
	    /* north-western neighbour, entry is col - 1 row - 1 */
	    if (i > 0 && j > 0) {
		pos = make_les_entry_2d(i, j, -1, -1, count, pos, les, spvect,
					cell_count, status, start_val,
					items->NW, cell_type);
	    }
	    /* north-eastern neighbour, entry col + 1 row - 1 */
	    if (i < geom->cols - 1 && j > 0) {
		pos = make_les_entry_2d(i, j, 1, -1, count, pos, les, spvect,
					cell_count, status, start_val,
					items->NE, cell_type);
	    }
	    /* south-western neighbour, entry is col - 1 row + 1 */
	    if (i > 0 && j < geom->rows - 1) {
		pos = make_les_entry_2d(i, j, -1, 1, count, pos, les, spvect,
					cell_count, status, start_val,
					items->SW, cell_type);
	    }
	    /* south-eastern neighbour, entry col + 1 row + 1 */
	    if (i < geom->cols - 1 && j < geom->rows - 1) {
		pos = make_les_entry_2d(i, j, 1, 1, count, pos, les, spvect,
					cell_count, status, start_val,
					items->SE, cell_type);
	    }
	}

	/*How many entries in the les */
	if (les->type == N_SPARSE_LES) {
	    spvect->cols = pos + 1;
	    G_math_add_spvector(les->Asp, spvect, count);
	}

	if (items)
	    G_free(items);
    }

    /*release memory */
    N_free_array_2d(cell_count);

    for (i = 0; i < cell_type_count; i++)
	G_free(index_ij[i]);

    G_free(index_ij);

    return les;
}
예제 #8
0
파일: main.c 프로젝트: imincik/pkg-grass
/* ************************************************************************* */
int main(int argc, char *argv[])
{
    struct GModule *module = NULL;
    N_gwflow_data2d *data = NULL;
    N_geom_data *geom = NULL;
    N_les *les = NULL;
    N_les_callback_2d *call = NULL;
    double *tmp_vect = NULL;
    struct Cell_head region;
    double error, sor, max_norm = 0, tmp;
    int maxit, i, inner_count = 0;
    char *solver;
    int x, y, stat;
    N_gradient_field_2d *field = NULL;
    N_array_2d *xcomp = NULL;
    N_array_2d *ycomp = NULL;
    char *buff = NULL;
    int with_river = 0, with_drain = 0;

    /* Initialize GRASS */
    G_gisinit(argv[0]);

    module = G_define_module();
    module->keywords = _("raster, hydrology");
    module->description =
	_("Numerical calculation program for transient, confined and unconfined groundwater flow in two dimensions.");

    /* Get parameters from user */
    set_params();

    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    /* Make sure that the current projection is not lat/long */
    if ((G_projection() == PROJECTION_LL))
	G_fatal_error(_("Lat/Long location is not supported by %s. Please reproject map first."),
		      G_program_name());

    /*Check the river  parameters */
    if (param.river_leak->answer == NULL && param.river_bed->answer == NULL &&
	param.river_head->answer == NULL) {
	with_river = 0;
    }
    else if (param.river_leak->answer != NULL &&
	     param.river_bed->answer != NULL &&
	     param.river_head->answer != NULL) {
	with_river = 1;
    }
    else {
	G_fatal_error
	    (_("Please provide river_head, river_leak and river_bed maps"));
    }

    /*Check the drainage parameters */
    if (param.drain_leak->answer == NULL && param.drain_bed->answer == NULL) {
	with_drain = 0;
    }
    else if (param.drain_leak->answer != NULL &&
	     param.drain_bed->answer != NULL) {
	with_drain = 1;
    }
    else {
	G_fatal_error(_("Please provide drain_head and drain_leak maps"));
    }

    /*Set the maximum iterations */
    sscanf(param.maxit->answer, "%i", &(maxit));
    /*Set the calculation error break criteria */
    sscanf(param.error->answer, "%lf", &(error));
    sscanf(param.sor->answer, "%lf", &(sor));
    /*set the solver */
    solver = param.solver->answer;

    if (strcmp(solver, N_SOLVER_DIRECT_LU) == 0 && param.sparse->answer)
	G_fatal_error(_("The direct LU solver do not work with sparse matrices"));
    if (strcmp(solver, N_SOLVER_DIRECT_GAUSS) == 0 && param.sparse->answer)
	G_fatal_error(_("The direct Gauss solver do not work with sparse matrices"));
    if (strcmp(solver, N_SOLVER_DIRECT_CHOLESKY) == 0 && param.sparse->answer)
	G_fatal_error(_("The direct cholesky solver do not work with sparse matrices"));

    /*get the current region */
    G_get_set_window(&region);

    /*allocate the geometry structure  for geometry and area calculation */
    geom = N_init_geom_data_2d(&region, geom);

    /*Set the function callback to the groundwater flow function */
    call = N_alloc_les_callback_2d();
    N_set_les_callback_2d_func(call, (*N_callback_gwflow_2d));	/*gwflow 2d */

    /*Allocate the groundwater flow data structure */
    data =
	N_alloc_gwflow_data2d(geom->cols, geom->rows, with_river, with_drain);

    /* set the groundwater type */
    if (param.type->answer) {
	if (strncmp("unconfined", param.type->answer, 10) == 0) {
	    data->gwtype = N_GW_UNCONFINED;
	}
	else {
	    data->gwtype = N_GW_CONFINED;
	}
    }

    /*Set the calculation time */
    sscanf(param.dt->answer, "%lf", &(data->dt));
    G_message("Calculation time: %g", data->dt);

    /*read all input maps into the memory and take care of the
     * null values.*/
    N_read_rast_to_array_2d(param.phead->answer, data->phead);
    N_convert_array_2d_null_to_zero(data->phead);
    N_copy_array_2d(data->phead, data->phead_start);
    N_read_rast_to_array_2d(param.status->answer, data->status);
    N_convert_array_2d_null_to_zero(data->status);
    N_read_rast_to_array_2d(param.hc_x->answer, data->hc_x);
    N_convert_array_2d_null_to_zero(data->hc_x);
    N_read_rast_to_array_2d(param.hc_y->answer, data->hc_y);
    N_convert_array_2d_null_to_zero(data->hc_y);
    N_read_rast_to_array_2d(param.s->answer, data->s);
    N_convert_array_2d_null_to_zero(data->s);
    N_read_rast_to_array_2d(param.top->answer, data->top);
    N_convert_array_2d_null_to_zero(data->top);
    N_read_rast_to_array_2d(param.bottom->answer, data->bottom);
    N_convert_array_2d_null_to_zero(data->bottom);

    /*river is optional */
    if (with_river) {
	N_read_rast_to_array_2d(param.river_bed->answer, data->river_bed);
	N_read_rast_to_array_2d(param.river_head->answer, data->river_head);
	N_read_rast_to_array_2d(param.river_leak->answer, data->river_leak);
	N_convert_array_2d_null_to_zero(data->river_bed);
	N_convert_array_2d_null_to_zero(data->river_head);
	N_convert_array_2d_null_to_zero(data->river_leak);
    }

    /*drainage is optional */
    if (with_drain) {
	N_read_rast_to_array_2d(param.drain_bed->answer, data->drain_bed);
	N_read_rast_to_array_2d(param.drain_leak->answer, data->drain_leak);
	N_convert_array_2d_null_to_zero(data->drain_bed);
	N_convert_array_2d_null_to_zero(data->drain_leak);
    }

    /*Recharge is optional */
    if (param.r->answer) {
	N_read_rast_to_array_2d(param.r->answer, data->r);
	N_convert_array_2d_null_to_zero(data->r);
    }

    /*Sources or sinks are optional */
    if (param.q->answer) {
        N_read_rast_to_array_2d(param.q->answer, data->q);
        N_convert_array_2d_null_to_zero(data->q);
    }

    /* Set the inactive values to zero, to assure a no flow boundary */
    for (y = 0; y < geom->rows; y++) {
	for (x = 0; x < geom->cols; x++) {
	    stat = N_get_array_2d_c_value(data->status, x, y);
	    if (stat == N_CELL_INACTIVE) {	/*only inactive cells */
		N_put_array_2d_d_value(data->hc_x, x, y, 0);
		N_put_array_2d_d_value(data->hc_y, x, y, 0);
		N_put_array_2d_d_value(data->s, x, y, 0);
		N_put_array_2d_d_value(data->q, x, y, 0);
	    }
	}
    }

    /*assemble the linear equation system  and solve it */
    les = create_solve_les(geom, data, call, solver, maxit, error, sor);

    /* copy the result into the phead array for output or unconfined calculation */
    copy_result(data->status, data->phead_start, les->x, &region,
		data->phead);
    N_convert_array_2d_null_to_zero(data->phead);

  /****************************************************/
    /*explicite calculation of free groundwater surface */

  /****************************************************/
    if (data->gwtype == N_GW_UNCONFINED) {
	/* allocate memory and copy the result into a new temporal vector */
	if (!(tmp_vect = (double *)calloc(les->rows, sizeof(double))))
	    G_fatal_error(_("Out of memory"));

	/*copy data */
	for (i = 0; i < les->rows; i++)
	    tmp_vect[i] = les->x[i];

	/*count the number of inner iterations */
	inner_count = 0;

	do {
	    G_message(_("Calculation of unconfined groundwater flow loop %i\n"),
		      inner_count + 1);

	    /* we will allocate a new les for each loop */
	    if (les)
		N_free_les(les);

	    /*assemble the linear equation system  and solve it */
	    les =
		create_solve_les(geom, data, call, solver, maxit, error, sor);

	    /*calculate the maximum norm of the groundwater height difference */
	    tmp = 0;
	    max_norm = 0;
	    for (i = 0; i < les->rows; i++) {
		tmp = fabs(les->x[i] - tmp_vect[i]);
		if (max_norm < tmp)
		    max_norm = tmp;

		/*copy the result */
		tmp_vect[i] = les->x[i];
	    }

	    G_message(_("Maximum difference between this and last increment: %g"),
		      max_norm);

	    /* copy the result into the phead array */
	    copy_result(data->status, data->phead_start, les->x, &region,
			data->phead);
	    N_convert_array_2d_null_to_zero(data->phead);
	     /**/ inner_count++;
	}
	while (max_norm > 0.01 && inner_count < 50);

	if (tmp_vect)
	    free(tmp_vect);
    }

    /*write the result to the output file */
    N_write_array_2d_to_rast(data->phead, param.output->answer);

    /*release the memory */
    if (les)
	N_free_les(les);

    /*Compute the the velocity field if required and write the result into three rast maps */
    if (param.vector->answer) {
	field =
	    N_compute_gradient_field_2d(data->phead, data->hc_x, data->hc_y,
					geom, NULL);

	xcomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);
	ycomp = N_alloc_array_2d(geom->cols, geom->rows, 1, DCELL_TYPE);

	N_compute_gradient_field_components_2d(field, xcomp, ycomp);

	G_asprintf(&buff, "%s_x", param.vector->answer);
	N_write_array_2d_to_rast(xcomp, buff);
	G_asprintf(&buff, "%s_y", param.vector->answer);
	N_write_array_2d_to_rast(ycomp, buff);
	if (buff)
	    G_free(buff);

	if (xcomp)
	    N_free_array_2d(xcomp);
	if (ycomp)
	    N_free_array_2d(ycomp);
	if (field)
	    N_free_gradient_field_2d(field);
    }

    if (data)
	N_free_gwflow_data2d(data);
    if (geom)
	N_free_geom_data(geom);
    if (call)
	G_free(call);

    return (EXIT_SUCCESS);
}