/****************************************************************************************************************************************************/ /* C source file for mex function to solve finite-difference equations for 2D axi-symmetric non-uniform confined ground water flow using ADI See mexADIconf.m for syntax discription Copyright (c) 2011, Ghent University (Andy Louwyck), Belgium All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Ghent University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GHENT UNIVERSITY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /****************************************************************************************************************************************************/ /* header */ #include #include #include #include /****************************************************************************************************************************************************/ /* solve equations using ADI for confined system */ void ADIconf(double *s, double *niter, // output mwSize nz, mwSize nr, mwSize nper, mwSize *nt, double *dt, double *qrc, double *qzc, double *qrzc, double *qssc, mwSize nq, mwSize *idq, double *q, mwSize ns0, mwSize *ids0, double *s0, signed char *ibound, double delta, mwSize mni) { // declarations mwSize i, j, p, t, ni; // i = layer index; j = row index; p = stress period index; t = time step index; ni = iteration index mwSize ij, ij2, ijt, tt; // ij = linear index for (i,j); ij2 = linear index for active; ijt = linear index for s(i,j,t) = ij+tt mwSize nznr, nz2, k, m, n; // nznr = nz*nr; nz2 = nz+2; k = index for v and e; m = nz+2 + 2*nr-1; n = (nr+1)*(nz+2)-1 unsigned char rowwise; // flag indicating if iteration is row-wise (rowwise=1) or column-wise (rowwise=0) double *rhs, *qp; // equation's right hand side; stress period discharges double *e, *v, d; // ADI variables double difs; // maximum absolute drawdown difference // set variables nz2 = nz+2; m = nz2 + 2*nr-1; n = (nr+1)*nz2 - 1; nznr = nz*nr; // allocate arrays rhs = (double *) mxMalloc(nznr*sizeof(double)); qp = (double *) mxMalloc(nznr*sizeof(double)); e = (double *) mxMalloc(nznr*sizeof(double)); v = (double *) mxMalloc(nznr*sizeof(double)); // stress period loop tt = 0; for (p=0; p 0) { k = 0; ij = nznr * *(nt+p); while (k < ns0 && p >= *(ids0+k)) { if (p == *(ids0+k)) *(s+ij+*(ids0+ns0+k)) += *(s0+k); k++; } } // determine discharge matrix for (ij=0; ij 0) { k = 0; while (k < nq && p >= *(idq+k)) { if (p == *(idq+k)) *(qp+*(idq+nq+k)) = *(q+k); k++; } } // time step loop for (t=*(nt+p); t<*(nt+p+1); t++){ // calculate rhs and copy s(t-1) to s(t) ij = 0; ij2 = nz2; for (j=0; j 0) { *(qssc+ij) /= *(dt+t); *(rhs+ij) = -*(qssc+ij) * *(s+ijt) + *(qp+ij); } if (*(ibound+ij2)!=0) *(s+nznr+ijt) = *(s+ijt); ij++; ij2++; } ij2++; } tt += nznr; // iteration loop ni = 0; // iteration index rowwise = 1; difs = delta + 1.0; while (difs>delta && ni 0) { // forward substitution k = 0; for (i=0; i 0) { *(v+k) = *(rhs+ij); d = -*(qrzc+ij) - *(qssc+ij); if (nz>1 && *(ibound+ij2-1)!=0) *(v+k) = *(v+k) - *(qzc+ij) * *(s+ijt-1); else if (nz>1 && *(ibound+ij2-1)<0) d += *(qzc+ij); if (nz>1 && *(ibound+ij2+1)!=0) *(v+k) = *(v+k) - *(qzc+ij+1) * *(s+ijt+1); else if (nz>1 && i=0; i--) { ijt = ij+tt; ij2 = ij+m; for (j=nr-1; j>=0; j--) { if (*(ibound+ij2) > 0) { if (jdifs) difs = fabs(*(v+k)-*(s+ijt)); *(s+ijt) = *(v+k); } ijt -= nz; ij2 -= nz2; k--; } ij--; } // end backward loop // column-wise } else { // forward substitution ij = 0; ijt = tt; ij2 = nz2; for (j=0; j 0) { *(v+ij) = *(rhs+ij); d = -*(qrzc+ij) - *(qssc+ij); if (*(ibound+ij2-nz2)!=0) *(v+ij) = *(v+ij) - *(qrc+ij) * *(s+ijt-nz); else if (*(ibound+ij2-nz2)<0) d += *(qrc+ij); if (*(ibound+ij2+nz2)!=0) *(v+ij) = *(v+ij) - *(qrc+ij+nz) * *(s+ijt+nz); else if (j=0; j--) { ij2--; for (i=nz-1; i>=0; i--) { if (*(ibound+ij2) > 0) { if (idifs) difs = fabs(*(v+ij)-*(s+ijt)); *(s+ijt) = *(v+ij); } ij--; ijt--; ij2--; } ij2--; } // end backward loop } // end row-wise branch // update counters ni++; if (nz>1) rowwise = 1 - rowwise; } // end of iteration loop // copy number of iterations *(niter+t) = (double) ni; for (ij=0; ij