Changeset 12174


Ignore:
Timestamp:
05/02/12 12:56:30 (13 years ago)
Author:
Mathieu Morlighem
Message:

Final version of Kriging (still not working)

Location:
issm/trunk-jpl/src/c/modules/Krigingx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/modules/Krigingx/Krigingx.cpp

    r12164 r12174  
    1515        double *predictions = NULL;
    1616
    17         /*Allocate output*/
    18         predictions=(double*)xmalloc(n_interp*sizeof(double));
     17        /*Intermediaries*/
     18        int     i,j;
     19        double  numerator,denominator,ratio;
     20        double *Gamma     = NULL;
     21        double *GinvG0    = NULL;
     22        double *Ginv1     = NULL;
     23        double *GinvZ     = NULL;
     24        double *gamma0    = NULL;
     25        double *ones      = NULL;
    1926
    20         /*Assign output pointer*/
     27        /*Memory allocation*/
     28        predictions =(double*)xmalloc(n_interp*sizeof(double));
     29        Gamma       =(double*)xmalloc(n_obs*n_obs*sizeof(double));
     30        gamma0      =(double*)xmalloc(n_obs*sizeof(double));
     31        ones        =(double*)xmalloc(n_obs*sizeof(double));
     32
     33        /*First: Create semivariogram matrix for observations*/
     34        for(i=0;i<n_obs;i++){
     35                for(j=0;j<=i;j++){
     36                        SemiVariogram(&Gamma[i*n_obs+j],x[i],y[i],x[j],y[j]);
     37                        Gamma[j*n_obs+i] = Gamma[i*n_obs+j];
     38                }
     39        }
     40        for(i=0;i<n_obs;i++) ones[i]=1;
     41
     42        /*Loop over all interpolations*/
     43        for(int idx=0;idx<n_interp;idx++){
     44
     45                /*Get semivariogram vector associated to this location*/
     46                for(i=0;i<n_obs;i++) SemiVariogram(&gamma0[i],x[i],y[i],x_interp[idx],y_interp[idx]);
     47
     48                /*Solve the three linear systems*/
     49                MumpsSolve(&GinvG0,Gamma,gamma0,n_obs);       // Gamma^-1 gamma0
     50                MumpsSolve(&Ginv1, Gamma,ones,n_obs);         // Gamma^-1 ones
     51                MumpsSolve(&GinvZ, Gamma,observations,n_obs); // Gamma^-1 Z
     52
     53                /*Prepare predictor*/
     54                numerator=0.; denominator=0.;
     55                for(i=0;i<n_obs;i++) numerator  +=GinvG0[i];
     56                for(i=0;i<n_obs;i++) denominator+=Ginv1[i];
     57                ratio=numerator/denominator;
     58
     59                predictions[idx]=0;
     60                for(i=0;i<n_obs;i++) predictions[idx] += (gamma0[i]-ratio)*GinvZ[i];
     61
     62                /*clean-up*/
     63                xfree((void**)&GinvG0);
     64                xfree((void**)&Ginv1);
     65                xfree((void**)&GinvZ);
     66        }
     67
     68        /*clean-up and Assign output pointer*/
     69        xfree((void**)&Gamma);
     70        xfree((void**)&gamma0);
     71        xfree((void**)&ones);
    2172        *ppredictions=predictions;
    2273}
     74
     75void SemiVariogram(double* gamma,double x1,double y1,double x2,double y2){
     76
     77        /*Calculate distance*/
     78        double r=sqrt(pow(x1-x2,2.)+pow(y1-y2,2.));
     79
     80        /*Switch between variogram models*/
     81        switch(1){
     82                case 1:{ /*Exponential*/
     83                        double c0=0.2;
     84                        double c1=0.8;
     85                        double a =1;
     86                        *gamma = c0 + c1*(1-exp(-r/a));
     87                        return;}
     88                default:
     89                        _error_("Not implemented yet");
     90        }
     91}
     92
     93void MumpsSolve(double** Xdouble,double* Adouble,double* Bdouble,int n){
     94#ifdef _HAVE_PETSC_
     95
     96        /*Intermediaries*/
     97        Mat A = NULL;
     98        Vec B = NULL;
     99        Vec X = NULL;
     100        KSP ksp = NULL;
     101        PC  pc  = NULL;
     102
     103        /*Create parameters and select MUMPS solver*/
     104        PetscOptionsSetFromOptions();
     105        PetscOptionsClear();
     106        PetscOptionsInsertMultipleString("-mat_type mpiaij \
     107                                -ksp_type preonly \
     108                                -pc_type lu \
     109                                -pc_factor_mat_solver_package mumps \
     110                                -mat_mumps_icntl_14 120 \
     111                                -pc_factor_shift_positive_definite true");
     112
     113        /*Create indexing and ones*/
     114        int* idx=(int*)xmalloc(n*sizeof(int));
     115        for(int i=0;i<n;i++) idx[i]=i;
     116
     117        /*Convert matrix to Dense PETSc matrix*/
     118        MatCreateSeqDense(PETSC_COMM_SELF,n,n,NULL,&A);
     119        MatSetValues(A,n,idx,n,idx,Adouble,INSERT_VALUES);
     120        MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     121        MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     122
     123        /*Convert vector to PETSc vector*/
     124        VecCreateSeq(PETSC_COMM_SELF,n,&B);
     125        VecSetValues(B,n,idx,Bdouble,INSERT_VALUES);
     126        VecAssemblyBegin(B);
     127        VecAssemblyEnd(B);
     128
     129        /*Allocate output*/
     130        VecCreateSeq(PETSC_COMM_SELF,n,&X);
     131
     132        /*Go Solve*/
     133        KSPCreate(MPI_COMM_WORLD,&ksp);
     134        KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);
     135        KSPSetFromOptions(ksp);
     136        KSPGetPC(ksp,&pc);
     137        PCFactorSetMatSolverPackage(pc,MATSOLVERMUMPS);
     138        KSPSolve(ksp,B,X);
     139        KSPFree(&ksp);
     140
     141        /*Serialize vector*/
     142        VecToMPISerial(Xdouble,X);
     143#else
     144        error_("PETSc support required");
     145#endif
     146}
  • issm/trunk-jpl/src/c/modules/Krigingx/Krigingx.h

    r12164 r12174  
    99#include "../../toolkits/toolkits.h"
    1010
    11 
    1211int Krigingx(double** ppredictions,double* x, double* y, double* observations, int n_obs,double* x_interp,double* y_interp,int n_interp);
     12void SemiVariogram(double* gamma,double x1,double y1,double x2,double y2);
     13void MumpsSolve(double** X,double* A,double* B,int n); //Solve X for the linear system AX = B
    1314
    1415#endif /* _KRIGINGX_H */
    15 
Note: See TracChangeset for help on using the changeset viewer.