void
RockPhysicsInversion4D::smoothAllDirectionsAndNormalize()
{
  DivideAndSmoothTable(1,priorDistribution_,smoothingFilter_);

  for(int i=0;i<2;i++)
  for (int j=0; j<nf_[0]; j++){
      meanRockPrediction_(i,j)->setAccessMode(FFTGrid::RANDOMACCESS);
   }

  for(int i0=0;i0<nf_[0];i0++)
     for(int i1=0;i1<nf_[1];i1++)
        for(int i2=0;i2<nf_[2];i2++)
           for(int i3=0;i3<nf_[3];i3++)
           {
             double number = GetGridValue(1,i0,i1,i2,i3);
             double normalizing =GetGridValue(0,i0,i1,i2,i3);
             double value;
             if(normalizing<1e-5)
               value=RMISSING;
             else;
               value=number/normalizing;

             SetGridValue(1,i0,i1,i2,i3,value);
           }


  for(int i=0;i<2;i++)
    for (int j=0; j<nf_[0]; j++){
      meanRockPrediction_(i,j)->endAccess();
    }

}
double
RockPhysicsInversion4D::getPredictedValue(NRLib::Vector f)
{
  //interploates in a 4D table
  double value;
  std::vector<std::vector<int> > indLoHi(2,std::vector<int>(4));
  std::vector<std::vector<double> > wLoHi(2,std::vector<double>(4));
  double w;
  int index;
  GetLowerIndexAndW(minf_(0),maxf_(0),nf_[0],f(0),index, w);
  wLoHi[0][0]=1-w;
  wLoHi[1][0]=w;
  indLoHi[0][0]=index;
  indLoHi[1][0]=std::min(nf_[0]-1,index+1);

  GetLowerIndexAndW(minf_(1),maxf_(1),nf_[1],f(1),index, w);
  wLoHi[0][1]=1-w;
  wLoHi[1][1]=w;
  indLoHi[0][1]=index;
  indLoHi[1][1]=std::min(nf_[1]-1,index+1);

  GetLowerIndexAndW(minf_(2),maxf_(2),nf_[2],f(2),index, w);
  wLoHi[0][2]=1-w;
  wLoHi[1][2]=w;
  indLoHi[0][2]=index;
  indLoHi[1][2]=std::min(nf_[2]-1,index+1);

  GetLowerIndexAndW(minf_(3),maxf_(3),nf_[3],f(3),index, w);
  wLoHi[0][3]=1-w;
  wLoHi[1][3]=w;
  indLoHi[0][3]=index;
  indLoHi[1][3]=std::min(nf_[3]-1,index+1);

  value=0.0;
  for(int i0=0;i0<2;i0++)
    for(int i1=0;i1<2;i1++)
      for(int i2=0;i2<2;i2++)
        for(int i3=0;i3<2;i3++)
        {
          w=wLoHi[i0][0]*wLoHi[i1][1]*wLoHi[i2][2]*wLoHi[i3][3];
          value+=w*GetGridValue(1,indLoHi[i0][0],indLoHi[i1][1],indLoHi[i2][2],indLoHi[i3][3]);
        }
return value;
}
Пример #3
0
short PDClick(DialogPtr dialog, short itemNum, long lParam, VOIDPTR data)
{
	short oldScreen;
	long menuID_menuItem;
	long newMode = ADVANCEDMODE;
	Boolean closeFile = false, bOkToChange, disableDST;
	
#pragma unused(lParam)
#pragma unused(data)
	
	switch (itemNum) {
		case M1OK:

			if (GetButton(dialog, M1USERWIZARD)) newMode = NOVICEMODE;
			//else if (GetButton(dialog, M1USERINTERMEDIATE)) newMode = INTERMEDIATEMODE;
			else if (GetButton(dialog, M1USERADVANCED)) newMode = ADVANCEDMODE;

			// if they are switching to modes, we must check with the wizard
			// if there is a wizard file, we must close it because they may have changed stuff
			// if they are switching to advanced mode and have have a wizard file open
			//	we need to tell them they cannot get back
			// without starting all over
			bOkToChange = model->fWizard->OKToChangeWizardMode(model -> GetModelMode (),newMode,&closeFile);
			if(!bOkToChange) break;
			if(closeFile) 
			{
				OSErr err = CloseSaveFile(FALSE,FALSE);// wizard gave them a chance to back out
				if(err == USERCANCEL) break;
			}
			
			// point of no return
			///////////
			settings.backgroundColor = 1; // white JLM ???
				
			settings.showLatLongLines = GetButton(dialog, D1SHOWLATLONG);
			if (GetButton(dialog, D1DEG)) settings.latLongFormat = DEGREES;
			if (GetButton(dialog, D1DEGMIN)) settings.latLongFormat = DEGMIN;
			if (GetButton(dialog, D1DEGMINSEC)) settings.latLongFormat = DMS;
			if (GetButton(dialog, D1ABOVE)) settings.llPosition = LL_ABOVE;
			if (GetButton(dialog, D1BELOW)) settings.llPosition = LL_BELOW;
			settings.showIntermediateLines = GetButton(dialog, D1INTERMEDIATELINES);
			settings.customGrid = GetButton(dialog, D1CUSTOM);
			settings.longLineSpace = GetGridValue(dialog, D1LONGLINE);
			settings.longLabelSpace = GetGridValue(dialog, D1LONGLABEL);
			settings.latLineSpace = GetGridValue(dialog, D1LATLINE);
			settings.latLabelSpace = GetGridValue(dialog, D1LATLABEL);
			settings.longLineUnits = GetPopSelection(dialog, D1LONGLINEUNITSPOPUP) - 1;
			settings.longLabelUnits = GetPopSelection(dialog, D1LONGLABELUNITSPOPUP) - 1;
			settings.latLineUnits = GetPopSelection(dialog, D1LATLINEUNITSPOPUP) - 1;
			settings.latLabelUnits = GetPopSelection(dialog, D1LATLABELUNITSPOPUP) - 1;
			
//			settings.massUnits = GetPopSelection(dialog, E1MASSPOPUP);
//			settings.areaUnits = GetPopSelection(dialog, E1AREAPOPUP);
		
			if (GetButton(dialog, M1USERWIZARD)) model -> SetModelMode (NOVICEMODE);
			//if (GetButton(dialog, M1USERINTERMEDIATE)) model -> SetModelMode (INTERMEDIATEMODE);
			if (GetButton(dialog, M1USERADVANCED)) model -> SetModelMode (ADVANCEDMODE);
			if (GetButton(dialog, M1STARTUPWIZARD)) settings.modelStartMode = NOVICEMODE;
			//if (GetButton(dialog, M1STARTUPINTERMEDIATE)) settings.modelStartMode = INTERMEDIATEMODE;
			if (GetButton(dialog, M1STARTUPADVANCED)) settings.modelStartMode = ADVANCEDMODE;
			
			disableDST = GetButton(dialog, C1DSTCHECKBOX);
			if (disableDST) settings.daylightSavingsTimeFlag = DAYLIGHTSAVINGSOFF;
			else settings.daylightSavingsTimeFlag = DAYLIGHTSAVINGSON;

			return M1OK;
		
		case M1CANCEL: return M1CANCEL;
		
		case M1TAB1:
		case M1TAB2:
		case M1TAB3:
		case M1TAB4:
		case M1TAB5:
			if (settings.preferencesScreen == itemNum)
				break;
			oldScreen = settings.preferencesScreen;
			settings.preferencesScreen = itemNum;
			DrawTab(dialog, oldScreen, FALSE);
			DrawTab(dialog, settings.preferencesScreen, TRUE);
			ShowPreferenceScreen(dialog, oldScreen);
			break;
		
		
		
		case D1SHOWLATLONG:
			ToggleButton(dialog, D1SHOWLATLONG);
			EnableLatLongControls(dialog);
			break;
		
		case D1DEG:
		case D1DEGMIN:
		case D1DEGMINSEC:
			SetButton(dialog, D1DEG, itemNum == D1DEG);
			SetButton(dialog, D1DEGMIN, itemNum == D1DEGMIN);
			SetButton(dialog, D1DEGMINSEC, itemNum == D1DEGMINSEC);
			break;
		
		case D1ABOVE:
		case D1BELOW:
			SetButton(dialog, D1ABOVE, itemNum == D1ABOVE);
			SetButton(dialog, D1BELOW, itemNum == D1BELOW);
			break;
		
		case D1INTERMEDIATELINES:
			ToggleButton(dialog, D1INTERMEDIATELINES);
			break;
		
		case D1CUSTOM:
			ToggleButton(dialog, D1CUSTOM);
			EnableLatLongControls(dialog);
			break;
		
		case D1LONGLINE:
		case D1LONGLABEL:
		case D1LATLINE:
		case D1LATLABEL:
			CheckNumberTextItem(dialog, itemNum, FALSE);
			break;
		
		//case M1BACKCOLORPOPUP:
		case D1LONGLINEUNITSPOPUP:
		case D1LONGLABELUNITSPOPUP:
		case D1LATLINEUNITSPOPUP:
		case D1LATLABELUNITSPOPUP:
//		case E1MASSPOPUP:
//		case E1AREAPOPUP:
			PopClick(dialog, itemNum, &menuID_menuItem);
			break;

		case M1USERWIZARD:
		//case M1USERINTERMEDIATE:
		case M1USERADVANCED:
			SetButton(dialog, M1USERWIZARD, itemNum == M1USERWIZARD);
			//SetButton(dialog, M1USERINTERMEDIATE, itemNum == M1USERINTERMEDIATE);
			SetButton(dialog, M1USERADVANCED, itemNum == M1USERADVANCED);
			break;
		
		case M1STARTUPWIZARD:
		//case M1STARTUPINTERMEDIATE:
		case M1STARTUPADVANCED:
			SetButton(dialog, M1STARTUPWIZARD, itemNum == M1STARTUPWIZARD);
			//SetButton(dialog, M1STARTUPINTERMEDIATE, itemNum == M1STARTUPINTERMEDIATE);
			SetButton(dialog, M1STARTUPADVANCED, itemNum == M1STARTUPADVANCED);
			break;
		
		case C1DSTCHECKBOX:
			ToggleButton(dialog, C1DSTCHECKBOX);
			break;
		
	}
	
	return 0;
}
void
RockPhysicsInversion4D::DivideAndSmoothTable(int tableInd,std::vector<std::vector<double> > priorDistribution, std::vector<fftw_complex*> smoothingFilter)
{
  for (int j=0; j<nf_[0]; j++){
      meanRockPrediction_(tableInd,j)->setAccessMode(FFTGrid::RANDOMACCESS);
   }

  int cnfp=nfp_/2+1;
  int rnfp=2*cnfp;

  fftw_real*    rTemp = static_cast<fftw_real*>(fftw_malloc(sizeof(float)*rnfp));
  fftw_complex* cTemp = reinterpret_cast<fftw_complex*>(rTemp);

  double minDivisor = 1e-3;

  LogKit::LogFormatted(LogKit::Low,"\n Smoothing direction 1 of 4\n");
  float monitorSize = std::max(1.0f, static_cast<float>(nf_[0]*nf_[1]*nf_[2]*nf_[3])*0.02f);
  float nextMonitor = monitorSize;
  std::cout
    << "\n  0%       20%       40%       60%       80%      100%"
    << "\n  |    |    |    |    |    |    |    |    |    |    |  "
    << "\n  ^";

  // divide and smooth direction1
  for(int i1=0;i1<nf_[1];i1++)
    for(int i2=0;i2<nf_[2];i2++)
      for(int i3=0;i3<nf_[3];i3++)
      {
        for(int i0=0;i0<nf_[0];i0++)
        {
          double divisor=std::max(minDivisor,priorDistribution_[0][i0]);
          rTemp[i0]=float(GetGridValue(tableInd,i0,i1,i2,i3)/ divisor);
        }
        for(int i0=nf_[0];i0<nfp_;i0++)
          rTemp[i0]=0.0f;

        rfftwnd_one_real_to_complex(fftplan1_,rTemp,cTemp);

        for(int i0=0;i0<cnfp;i0++)
        {
          cTemp[i0].re=cTemp[i0].re*smoothingFilter[0][i0].re;
          cTemp[i0].im=cTemp[i0].im*smoothingFilter[0][i0].re;
        }

        rfftwnd_one_complex_to_real(fftplan2_,cTemp,rTemp);
        for(int i0=0;i0<nf_[0];i0++)
        {
          SetGridValue(tableInd,i0,i1,i2,i3, rTemp[i0]);
          if ( i1*nf_[2]*nf_[3]*nf_[0] +i2*nf_[3]*nf_[0]+ i3*nf_[0] + i0 + 1 >= static_cast<int>(nextMonitor)) {
            nextMonitor += monitorSize;
            std::cout << "^";
          }
        }
      }

  LogKit::LogFormatted(LogKit::Low,"\n\n Smoothing direction 2 of 4\n");
  monitorSize = std::max(1.0f, static_cast<float>(nf_[0]*nf_[1]*nf_[2]*nf_[3])*0.02f);
  nextMonitor = monitorSize;
  std::cout
    << "\n  0%       20%       40%       60%       80%      100%"
    << "\n  |    |    |    |    |    |    |    |    |    |    |  "
    << "\n  ^";

  // divide and smoothdirection2
  for(int i0=0;i0<nf_[0];i0++)
    for(int i2=0;i2<nf_[2];i2++)
      for(int i3=0;i3<nf_[3];i3++)
      {
        for(int i1=0;i1<nf_[1];i1++)
        {
          double divisor=std::max(minDivisor,priorDistribution_[1][i1]);
          rTemp[i1]=float(GetGridValue(tableInd,i0,i1,i2,i3)/divisor);
        }
        for(int i1=nf_[1];i1<nfp_;i1++)
          rTemp[i1]=0.0f;

        rfftwnd_one_real_to_complex(fftplan1_,rTemp,cTemp);

        for(int i1=0;i1<cnfp;i1++)
        {
          cTemp[i1].re=cTemp[i1].re*smoothingFilter[1][i1].re;
          cTemp[i1].im=cTemp[i1].im*smoothingFilter[1][i1].re;
        }

        rfftwnd_one_complex_to_real(fftplan2_,cTemp,rTemp);
        for(int i1=0;i1<nf_[1];i1++)
        {
          SetGridValue(tableInd,i0,i1,i2,i3, rTemp[i1]);
          if ( i0*nf_[2]*nf_[3]*nf_[1] +i2*nf_[3]*nf_[1]+ i3*nf_[1] + i1 + 1 >= static_cast<int>(nextMonitor)) {
            nextMonitor += monitorSize;
            std::cout << "^";
          }
        }
      }

  LogKit::LogFormatted(LogKit::Low,"\n\n Smoothing direction 3 of 4\n");
  monitorSize = std::max(1.0f, static_cast<float>(nf_[0]*nf_[1]*nf_[2]*nf_[3])*0.02f);
  nextMonitor = monitorSize;
  std::cout
    << "\n  0%       20%       40%       60%       80%      100%"
    << "\n  |    |    |    |    |    |    |    |    |    |    |  "
    << "\n  ^";

  //  divide and smoothdirection 3
  for(int i0=0;i0<nf_[0];i0++)
    for(int i1=0;i1<nf_[1];i1++)
      for(int i3=0;i3<nf_[3];i3++)
      {
        for(int i2=0;i2<nf_[2];i2++)
        {
          double divisor=std::max(minDivisor,priorDistribution_[2][i2]);
          rTemp[i2]=float(GetGridValue(tableInd,i0,i1,i2,i3)/divisor);

        }
        for(int i2=nf_[2];i2<nfp_;i2++)
          rTemp[i2]=0.0f;

        rfftwnd_one_real_to_complex(fftplan1_,rTemp,cTemp);

        for(int i2=0;i2<cnfp;i2++)
        {
          cTemp[i2].re=cTemp[i2].re*smoothingFilter[2][i2].re;
          cTemp[i2].im=cTemp[i2].im*smoothingFilter[2][i2].re;
        }

        rfftwnd_one_complex_to_real(fftplan2_,cTemp,rTemp);
        for(int i2=0;i2<nf_[2];i2++)
        {
          SetGridValue(tableInd,i0,i1,i2,i3, rTemp[i2]);
          if ( i0*nf_[1]*nf_[3]*nf_[2] +i1*nf_[3]*nf_[2]+ i3*nf_[2] + i2 + 1 >= static_cast<int>(nextMonitor)) {
            nextMonitor += monitorSize;
            std::cout << "^";
          }
        }
      }

  LogKit::LogFormatted(LogKit::Low,"\n Smoothing last direction \n");
  monitorSize = std::max(1.0f, static_cast<float>(nf_[0]*nf_[1]*nf_[2]*nf_[3])*0.02f);
  nextMonitor = monitorSize;
  std::cout
    << "\n  0%       20%       40%       60%       80%      100%"
    << "\n  |    |    |    |    |    |    |    |    |    |    |  "
    << "\n  ^";
 //  divide and smoothdirection 4
   for(int i0=0;i0<nf_[0];i0++)
    for(int i1=0;i1<nf_[1];i1++)
      for(int i2=0;i2<nf_[2];i2++)
      {
        for(int i3=0;i3<nf_[3];i3++)
        {
          double divisor=std::max(minDivisor,priorDistribution_[3][i3]);
          rTemp[i3]=float(GetGridValue(tableInd,i0,i1,i2,i3)/divisor);
        }
        for(int i3=nf_[3];i3<nfp_;i3++)
          rTemp[i3]=0.0f;

        rfftwnd_one_real_to_complex(fftplan1_,rTemp,cTemp);

        for(int i3=0;i3<cnfp;i3++)
        {
          cTemp[i3].re=cTemp[i3].re*smoothingFilter[3][i3].re;
          cTemp[i3].im=cTemp[i3].im*smoothingFilter[3][i3].re;
        }

        rfftwnd_one_complex_to_real(fftplan2_,cTemp,rTemp);
        for(int i3=0;i3<nf_[3];i3++)
        {
          SetGridValue(tableInd,i0,i1,i2,i3, rTemp[i3]);
          if ( i0*nf_[1]*nf_[2]*nf_[3] +i1*nf_[2]*nf_[3]+ i2*nf_[3] + i3 + 1 >= static_cast<int>(nextMonitor)) {
            nextMonitor += monitorSize;
            std::cout << "^";
          }
        }
      }

  for (int j=0; j<nf_[0]; j++){
      meanRockPrediction_(tableInd,j)->endAccess();
  }

  fftw_free(rTemp);

}