Ignore:
Timestamp:
05/03/12 16:14:45 (13 years ago)
Author:
Mathieu Morlighem
Message:

Added Kriging object for semivariograms

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

Legend:

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

    r12183 r12200  
    1111#include <gsl/gsl_linalg.h>
    1212
     13#include "../../objects/Kriging/GaussianVariogram.h"
    1314int Krigingx(double** ppredictions,double* x, double* y, double* observations, int n_obs,double* x_interp,double* y_interp,int n_interp,Options* options){
    1415
     
    1718
    1819        /*Intermediaries*/
    19         int     i,j;
    20         double  numerator,denominator,ratio;
    21         double *Gamma     = NULL;
    22         double *GinvG0    = NULL;
    23         double *Ginv1     = NULL;
    24         double *GinvZ     = NULL;
    25         double *gamma0    = NULL;
    26         double *ones      = NULL;
     20        int        i,j;
     21        double     numerator,denominator,ratio;
     22        char      *model     = NULL;
     23        double    *Gamma     = NULL;
     24        double    *GinvG0    = NULL;
     25        double    *Ginv1     = NULL;
     26        double    *GinvZ     = NULL;
     27        double    *gamma0    = NULL;
     28        double    *ones      = NULL;
     29        Variogram *variogram = NULL;
    2730
    2831        /*Memory allocation*/
     
    3235        ones        =(double*)xmalloc(n_obs*sizeof(double));
    3336
     37        /*Create Semi-Variogram object*/
     38        if(options->GetOption("model")){
     39                options->Get(&model,"model");
     40                if     (strcmp(model,"gaussian")==0)    variogram = new GaussianVariogram(options);
     41                else if(strcmp(model,"exponential")==0) variogram = new ExponentialVariogram(options);
     42                else if(strcmp(model,"spherical")==0)   variogram = new SphericalVariogram(options);
     43                else _error_("only gaussian semivariogram models supported yet");
     44        }
     45        else variogram = new GaussianVariogram(options);
     46        variogram->Echo();
     47
    3448        /*First: Create semivariogram matrix for observations*/
    3549        for(i=0;i<n_obs;i++){
    3650                for(j=0;j<=i;j++){
    37                         SemiVariogram(&Gamma[i*n_obs+j],x[i],y[i],x[j],y[j]);
     51                        Gamma[i*n_obs+j] = variogram->SemiVariogram(x[i]-x[j],y[i]-y[j]);
    3852                        Gamma[j*n_obs+i] = Gamma[i*n_obs+j];
    3953                }
     
    4761
    4862                /*Get semivariogram vector associated to this location*/
    49                 for(i=0;i<n_obs;i++) SemiVariogram(&gamma0[i],x[i],y[i],x_interp[idx],y_interp[idx]);
     63                for(i=0;i<n_obs;i++) gamma0[i] = variogram->SemiVariogram(x[i]-x_interp[idx],y[i]-y_interp[idx]);
    5064
    5165                /*Solve the three linear systems*/
     
    7185
    7286        /*clean-up and Assign output pointer*/
     87        delete variogram;
    7388        xfree((void**)&Gamma);
    7489        xfree((void**)&gamma0);
    7590        xfree((void**)&ones);
     91        xfree((void**)&model);
    7692        *ppredictions=predictions;
    77 }
    78 
    79 void SemiVariogram(double* gamma,double x1,double y1,double x2,double y2){
    80 
    81         /*Intermediaries*/
    82         double a,c0,c1;
    83 
    84         /*Calculate distance*/
    85         double r=sqrt(pow(x1-x2,2.)+pow(y1-y2,2.));
    86 
    87         /*Switch between variogram models*/
    88         switch(2){
    89                 case 1:/*Exponential*/
    90                         c0=0.2;
    91                         c1=0.8;
    92                         a =1;
    93                         *gamma = c0 + c1*(1-exp(-r/a));
    94                         return;
    95                 case 2:/*Gaussian*/
    96                         c0=0.2;
    97                         c1=0.8;
    98                         a =1;
    99                         *gamma = c0 + c1*(1-exp(-pow(r,2.)/pow(a,2.)));
    100                         return;
    101                 default:
    102                         _error_("Not implemented yet");
    103         }
    10493}
    10594
  • issm/trunk-jpl/src/c/modules/Krigingx/Krigingx.h

    r12183 r12200  
    1010
    1111int  Krigingx(double** ppredictions,double* x, double* y, double* observations, int n_obs,double* x_interp,double* y_interp,int n_interp,Options* options);
    12 void SemiVariogram(double* gamma,double x1,double y1,double x2,double y2);
    1312void GslSolve(double** pX,double* A,double* B,int n);
    1413
Note: See TracChangeset for help on using the changeset viewer.