g2mblmlprivated.h

Go to the documentation of this file.
00001 
00002 /* ///////////////////////////////////////////////////////////////////////// */  
00003 /* This file is a part of the BSTools package                                */
00004 /* written by Przemyslaw Kiciak                                              */   
00005 /* ///////////////////////////////////////////////////////////////////////// */
00006 /* (C) Copyright by Przemyslaw Kiciak, 2011, 2012                            */
00007 /* this package is distributed under the terms of the                        */
00008 /* Lesser GNU Public License, see the file COPYING.LIB                       */
00009 /* ///////////////////////////////////////////////////////////////////////// */
00010 
00011 /* header file for the procedures implementing the multilevel optimization */
00012 /* algorithm */
00013 
00014 #ifndef _CONST
00015 #define _CONST const
00016 #endif
00017 
00018 
00019 /* a divided block must have at least that many variable control points */
00020 #define MIN_NVCP    100
00021 /* a block with at most that many variable control points will use      */
00022 /* band matrix Hessian representation (Cholesky, not CG). Larger blocks */
00023 /* will use band matrix Hessian if they have not been divided.          */
00024 #define MAX_NVCP   3500
00025 /*#define MAX_NVCP   50*/
00026 /* this is an analoguous threshold for the shape only optimization procedure */
00027 #define MAX_NVCPS 10000
00028 /*#define MAX_NVCPS 100*/
00029 
00030 /* a subblock of a block with n vertices will have                         */
00031 /* SUBBLOCK_STEP/(2*SUBBLOCK_STEP-1) vertices. At first it was fixed at 3, */
00032 /* experiments might be used to fine-tune it. Perhaps different values are */
00033 /* proper for the methods with and without the coarse mesh preconditioner. */
00034 #define SUBBLOCK_STEP    4
00035 #define SUBBLOCK_STEP_CP 5
00036 
00037 #define MYINFINITY    1.0e+308
00038 #define EPS1          1.0e-4
00039 #define EPS2          1.0e-7
00040 #define EPS3          1.0e-8
00041 #define EPS4          1.0e-8
00042 #define DELTA4        1.0e-7
00043 #define BETA          0.125
00044 #define CG_EPS        1.0e-3
00045 #define CG_DELTA      1.0e-20
00046 #define MAXCGN      512
00047 #define MAXNTN       16
00048 #define MAXBTN       20
00049 #define MAXCTN       24
00050 #define MAXGTN       10 /*16*/
00051 #define GRTHR         0.02
00052 
00053 #define FLAG_F       0x0001
00054 #define FLAG_G       0x0002
00055 #define FLAG_H       0x0004
00056 #define FLAG_LH      0x0008
00057 #define FLAG_CH      0x0010
00058 #define FLAG_CMH     0x0020
00059 #define FLAG_CMLH    0x0040
00060 #define FLAG_ADVANCE 0x0080
00061 
00062 /*#define GRDIV(a,b) ((1.0-TAU)*(a)+TAU*(b)) */
00063 #define GRDIV(a,b) (exp((1.0-TAU)*log(a)+TAU*log(b)))
00064 
00065 typedef struct {
00066     int           nvcp, nvars, ndomel, seed;
00067     int           *vncpi, *domelind;
00068         /* coarse mesh vertices */
00069     int           nwcp, *wncpi;
00070         /* refinement submatrix */
00071     int           rmnnz;
00072     index3        *rmnzi;
00073         /* data for fast matrix multiplications related with */
00074         /* the coarse mesh preconditioner */
00075     int           nnz1, nnz2;
00076         /* Hessian representation for a non-bottom block */
00077     int           nHbl;  /* total number of nonzero 3x3 or 1x1 blocks */
00078     nzHbl_rowdesc *iHbl;
00079     int           *cHbl,
00080                   *tHbl;
00081         /* Hessian representation for a bottom block or a sparse grid block */
00082     int           *hprof, hsize;
00083     double        **hrows, **lhrows;
00084         /* function, gradient and Hessian valid flags */
00085     short         fghflag;
00086   } mlblock_desc;
00087 
00088 typedef struct {
00089         /* mesh data */
00090     int          nv, nhe, nfac;
00091     BSMvertex    *mv;
00092     int          *mvhei;
00093     point3d      *mvcp;
00094     BSMhalfedge  *mhe;
00095     BSMfacet     *mfac;
00096     int          *mfhei;
00097     char         *mvtag;
00098 
00099     int          cnv; /* number of the coarse mesh vertices */
00100 
00101         /* refinement matrix - for the optional preconditioner */
00102         /* obtained with a coarse mesh */
00103     int          rmnnz;      /* number of nonzero coefficients */
00104     index2       *rmnzi;     /* their positions */
00105     double       *rmnzc;     /* in this array */  
00106 
00107         /* normal vectors associated with the control points, */
00108         /* used only by the shape-only optimization procedure */
00109     vector3d     *mvcpn;
00110 
00111         /* domain element description */
00112     int          tdomelems,   /* total number of elements */
00113                  ndomelems,   /* number of relevant elements */
00114                  ndomelcpind; /* number of vertex indices for the elements */
00115     meshdom_elem *domelem;
00116     int          *domelcpind;
00117     boolean      eltypes[GH_MAX_K-2];  /* which special elements are present? */
00118 
00119         /* quadrature knots and values of basis functions and their derivatives */
00120     int          nkn1, nkn2;
00121     double       *aqcoeff, *bqcoeff;
00122     double       *aNitabs[GH_MAX_K-2], *aNijtabs[GH_MAX_K-2],
00123                  *aMijtabs[GH_MAX_K-2], *aJac[GH_MAX_K-2],
00124                  *bNitabs[GH_MAX_K-2], *bJac[GH_MAX_K-2];
00125 
00126         /* tables of integrals over the elements */
00127     int          hti, bls;
00128     double       *ftab1, *ftab2, *gtab1, *gtab2, *htab1;
00129 
00130         /* 3x3 or 1x1 nonzero blocks of the Hessian matrix */
00131     int          Hblsize;  /* number of nonzero Hessian blocks */
00132     double       *Hbl;     /* actual 3x3 or 1x1 blocks */
00133 
00134         /* number of control points to be optimized */
00135     int          nvcp, nvars;
00136     int          *nncpi;  /* numbers of variable vertices */
00137                           /* (-1 for fixed vertices) */
00138 
00139         /* blocks */
00140     short        nlevels, nblocks;
00141     mlblock_desc *bd;
00142 
00143         /* iteration specific data */
00144     short        currentblock, dirtyblock, lastblock, nextlevel;
00145     double       nu[2];
00146 
00147         /* diagnostic ouput */
00148     short        log_level;   /* printf: 0 - none, 1 - iterations, 2 - values */
00149     short        error_code;  /* 0 - none */
00150 #ifdef G2MBL_TIME_IT
00151     int          time_prep, time_h, time_cg;
00152 #endif
00153   } mesh_ml_optdata;
00154 
00155 
00156 typedef struct {
00157     mesh_ml_optdata *d;
00158     short           bl, cbl;
00159     double          nu;
00160     boolean         failure;
00161   } mesh_ml_cg_data;
00162 
00163 typedef boolean (*cg_mult)(int nvars, void *usrdata, const double *x, double *Ax );
00164 
00165 
00166 boolean _g2mbl_MLDivideBlock ( int nv, BSMvertex *mv, int *mvhei,
00167                                int nhe, BSMhalfedge *mhe,
00168                                int nfac, BSMfacet *mfac, int *mfhei,
00169                                int nvcp, int *vncpi,
00170                                int *nvbcp1, int **vnbcp1, int *seed1,
00171                                int *nvbcp2, int **vnbcp2, int *seed2,
00172                                int step );
00173 boolean _g2mbl_MLAssignMeshd ( mesh_ml_optdata *d,
00174                                int nv, BSMvertex *mv, int *mvhei, point3d *mvcp,
00175                                int nhe, BSMhalfedge *mhe,
00176                                int nfac, BSMfacet *mfac, int *mfhei,
00177                                byte *mkcp );
00178 boolean _g2mbl_MLSetupBlocksd ( mesh_ml_optdata *d,
00179                                 short nlevels, short bsize, int step );
00180 boolean _g2mbl_MLOptAllocBFArraysd ( mesh_ml_optdata *d, int nkn1, int nkn2,
00181                                      boolean reparam );
00182 boolean _g2mbl_MLFindElementsd ( mesh_ml_optdata *d, int bls, boolean dnkn );
00183 boolean _g2mbl_MLFindBlockElementsd ( mesh_ml_optdata *d );
00184 boolean _g2mbl_MLFindVCPNeighboursd ( mesh_ml_optdata *d,       
00185                                       nzHbl_rowdesc **vn, int **vni );
00186 boolean _g2mbl_MLSetupBlockCGHessiand ( mesh_ml_optdata *d, int bn );
00187 boolean _g2mbl_MLSetupBlockCholHessiand ( mesh_ml_optdata *d, int bn );
00188 boolean _g2mbl_MLSetupBlockHessiansd ( mesh_ml_optdata *d );
00189 boolean _g2mbl_MLSetupElemConstd ( mesh_ml_optdata *d,
00190                                    double dM, double dO, double C );
00191 
00192 double g2mbl_MLFuncd ( int nkn, double *qcoeff, double **Nitabs, double **Jac,
00193                        int nv, point3d *mvcp, int ndomel, int *domelind,
00194                        meshdom_elem *domelem, int *domelcpind,
00195                        boolean recalc, double *ftab );
00196 boolean g2mbl_MLFuncGradd ( int nkn, double *qcoeff, double **Nitabs, double **Jac,
00197                             int nv, point3d *mvcp, int ndomel, int *domelind,
00198                             meshdom_elem *domelem, int *domelcpind,
00199                             int nvcp, int *vncpi,
00200                             boolean recalc, double *ftab, double *gtab,
00201                             double *func, double *grad );
00202 boolean g2mbl_MLFuncGradHessianAd ( int nkn, double *qcoeff,
00203              double **Nitabs, double **Nijtabs, double **Mijtabs, double **Jac,
00204              int nv, point3d *mvcp, int ndomel, int *domelind,
00205              meshdom_elem *domelem, int *domelcpind,
00206              int nvcp, int *vncpi,
00207              boolean recalc, double *ftab, double *gtab, double *htab,
00208              double *func, double *grad,
00209              int nHbl, nzHbl_rowdesc *iHbl, int *cHbl, int *tHbl, double *Hbl );
00210 boolean g2mbl_MLFuncGradHessianBd ( int nkn, double *qcoeff,
00211              double **Nitabs, double **Nijtabs, double **Mijtabs, double **Jac,
00212              int nv, point3d *mvcp, int ndomel, int *domelind,
00213              meshdom_elem *domelem, int *domelcpind,
00214              int nvcp, int *vncpi,
00215              boolean recalc, double *ftab, double *gtab, double *htab,
00216              double *func, double *grad,
00217              int hsize, int *hprof, double **hrows );
00218 boolean g2mbl_MLGetHessianRowsd ( int nv, int nvcp1, int *vncpi1,
00219                                   int nHbl, nzHbl_rowdesc *iHbl,
00220                                   int *cHbl, int *tHbl, double *Hbl,
00221                                   int nvcp, int *vncpi,
00222                                   int hsize, int *hprof, double **hrows );
00223 
00224 boolean _g2mbl_MLmultAxd ( int nvars, void *usrdata,
00225                            _CONST double *x, double *Ax );
00226 boolean _g2mbl_MLmultQIxd ( int nvars, void *usrdata,
00227                             _CONST double *x, double *Qix );
00228 boolean _g2mbl_MLDecomposeBlockPrecond ( mesh_ml_optdata *d, short int bl,
00229                                          double nu, boolean *positive );
00230 boolean g2mbl_MLOptBlockAd ( void *data, int bl );
00231 boolean g2mbl_MLOptBlockBd ( void *data, int bl );
00232 
00233 /* ///////////////////////////////////////////////////////////////////////// */
00234 /* procedures used by the multilevel algorithm with the preconditioner using */
00235 /* the coarse mesh */
00236 boolean _g2mbl_CMPAssignMeshd ( mesh_ml_optdata *d,
00237                     int fnv, BSMvertex *fmv, int *fmvhei, point3d *fmvcp,
00238                     int fnhe, BSMhalfedge *fmhe,
00239                     int fnfac, BSMfacet *fmfac, int *fmfhei, byte *fmkcp,
00240                     int cnv, int rmnnz, index2 *rmnzi, double *rmnzc );
00241 boolean _g2mbl_CMPFindCoarseMeshBlock (
00242                      int fnv, int cnv, int rmnnz, index2 *rmnzi,
00243                      int nvbcp, int *vnbcp,
00244                      int *nwbcp, int **wnbcp );
00245 boolean _g2mbl_CMPFindBlockCGPmatrix (
00246                              int fnv, int cnv, int rmnnz, index2 *rmnzi,
00247                              int nvbcp, int *vnbcp, int nwbcp, int *wnbcp,
00248                              int *smnnz, index3 **smi );
00249 boolean _g2mbl_CMPOrderCoarsePoints ( int nwcp, int nnz, index2 *nzci,
00250                                       int brmnnz, index3 *brmnzi,
00251                                       int blsize, int *hsize, int *hprof );
00252 boolean _g2mbl_CMPSetupBlockCGPrecondd ( mesh_ml_optdata *d, int bn );
00253 boolean _g2mbl_CMPSetupBlockHessiansd ( mesh_ml_optdata *d );
00254 
00255 boolean _g2mbl_CMPMultRTHR3x3d ( int nrowsa, int nnza, index3 *ai, double *ac,
00256                                  int ncolsb, int nnzb, index3 *bi, double *bc,
00257                                  int nnz1,
00258                                  double nu,
00259                                  int hsize, int *hprof, double **hrows );
00260 boolean _g2mbl_CMPSetupCoarseHessiand ( mesh_ml_optdata *d, int bl, double nu );
00261 
00262 /* ///////////////////////////////////////////////////////////////////////// */
00263 /* procedures used by the shape-only optimization algorithm */
00264 boolean g2mbl_MLSFuncGradd ( int nkn, double *qcoeff,
00265               double **Nitabs, double **Jac,
00266               int nv, point3d *mvcp, vector3d *mvcpn,
00267               int ndomel, int *domelind, meshdom_elem *domelem, int *domelcpind,
00268               int nvcp, int *vncpi,
00269               boolean recalc, double *ftab, double *gtab,
00270               double *func, double *grad );
00271 boolean g2mbl_MLSFuncGradHessianAd ( int nkn, double *qcoeff,
00272               double **Nitabs, double **Nijtabs, double **Mijtabs, double **Jac,
00273               int nv, point3d *mvcp, vector3d *mvcpn,
00274               int ndomel, int *domelind, meshdom_elem *domelem, int *domelcpind,
00275               int nvcp, int *vncpi,
00276               boolean recalc, double *ftab, double *gtab, double *htab,
00277               double *func, double *grad,
00278               int nHbl, nzHbl_rowdesc *iHbl, int *cHbl, int *tHbl, double *Hbl );
00279 boolean g2mbl_MLSFuncGradHessianBd ( int nkn, double *qcoeff,
00280               double **Nitabs, double **Nijtabs, double **Mijtabs, double **Jac,
00281               int nv, point3d *mvcp, vector3d *mvcpn,
00282               int ndomel, int *domelind, meshdom_elem *domelem, int *domelcpind,
00283               int nvcp, int *vncpi,
00284               boolean recalc, double *ftab, double *gtab, double *htab,
00285               double *func, double *grad,
00286               int hsize, int *hprof, double **hrows );
00287 
00288 boolean g2mbl_MLSGetHessianRowsd ( int nv, int nvcp1, int *vncpi1,
00289                                    int nHbl, nzHbl_rowdesc *iHbl,
00290                                    int *cHbl, int *tHbl, double *Hbl,
00291                                    int nvcp, int *vncpi,
00292                                    int hsize, int *hprof, double **hrows );
00293 
00294 boolean _g2mbl_MLSSetupBlockCGHessiand ( mesh_ml_optdata *d, int bn );
00295 boolean _g2mbl_MLSSetupBlockCholHessiand ( mesh_ml_optdata *d, int bn );
00296 boolean _g2mbl_MLSSetupBlockHessiansd ( mesh_ml_optdata *d );
00297 boolean _g2mbl_MLSFindCPNormalsd ( mesh_ml_optdata *d );
00298 boolean _g2mbl_CMPSSetupCoarseHessiand ( mesh_ml_optdata *d, int bl, double nu );
00299 void _g2mbl_MLInvalSmallBlocks ( mesh_ml_optdata *d, int bl );
00300 boolean _g2mbl_MLSmultAxd ( int nvars, void *usrdata, double *x, double *Ax );
00301 boolean _g2mbl_MLSmultQIxd ( int nvars, void *usrdata, double *x, double *Qix );
00302 boolean _g2mbl_MLSDecomposeBlockPrecond ( mesh_ml_optdata *d, short int bl,
00303                                           double nu, boolean *positive );
00304 void _g2mbl_MLSAddCPIncrement ( int nvcp, int *vncpi,
00305                                 point3d *mvcp, vector3d *mvcpn,
00306                                 double *incr, point3d *omvcp );
00307 boolean g2mbl_MLSOptBlockAd ( void *data, int bl );
00308 boolean g2mbl_MLSOptBlockBd ( void *data, int bl );
00309 
00310 boolean _g2mbl_CMPSSetupBlockCGPrecondd ( mesh_ml_optdata *d, int bn );
00311 boolean _g2mbl_CMPSSetupBlockHessiansd ( mesh_ml_optdata *d );
00312 
00313 int _g2mbl_MLNextBlockNumd ( mesh_ml_optdata *d, boolean advance );
00314