Exemplo n.º 1
0
//---------------------------------------------------------
bool CGrid_Plotter::On_Execute(void)
{
	pResult		= Parameters("RESULT")->asGrid();

	double xmin	= Parameters("XMIN")->asDouble();
	double ymin	= Parameters("YMIN")->asDouble();
	double xmax	= Parameters("XMAX")->asDouble();
	double ymax	= Parameters("YMAX")->asDouble();

	const SG_Char *formel  = Parameters("FORMUL")->asString();

	CSG_Formula Formel;

	Formel.Set_Formula(formel);

	CSG_String Msg;
	if (Formel.Get_Error(Msg))
	{
		Message_Add(Msg);
		
		return false;
	}

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	for(int x=0; x<Get_NX(); x++)
	{
		pResult->Set_Value(x,y,Formel.Get_Value(SG_T("xy"),(xmax-xmin)*((double)x/Get_NX())+xmin,(ymax-ymin)*((double)y/Get_NY())+ymin)); 
	}
	return( true );
}
Exemplo n.º 2
0
//---------------------------------------------------------
bool CGrid_Plotter::On_Execute(void)
{
	//-----------------------------------------------------
	CSG_Formula	Formula;
	
	if( !Formula.Set_Formula(Parameters("FORMULA")->asString()) )
	{
		CSG_String	Message;

		if( !Formula.Get_Error(Message) )
		{
			Message	= _TL("unknown errror parsing formula");
		}

		Error_Set(Message);

		return( false );
	}

	//-----------------------------------------------------
	CSG_Grid	*pFunction	= m_Grid_Target.Get_Grid("FUNCTION");

	if( !pFunction )
	{
		Error_Set(_TL("could not create target grid"));

		return( false );
	}

	//-----------------------------------------------------
	double xMin		= Parameters("X_RANGE")->asRange()->Get_LoVal();
	double xRange	= Parameters("X_RANGE")->asRange()->Get_HiVal() - xMin;

	double yMin		= Parameters("Y_RANGE")->asRange()->Get_LoVal();
	double yRange	= Parameters("Y_RANGE")->asRange()->Get_HiVal() - yMin;

	//-----------------------------------------------------
	for(int y=0; y<pFunction->Get_NY() && Set_Progress(y); y++)
	{
		double	py	= yMin + yRange * (y / (double)pFunction->Get_NY());

		#pragma omp parallel for
		for(int x=0; x<pFunction->Get_NX(); x++)
		{
			double	px	= xMin + xRange * (x / (double)pFunction->Get_NX());

			pFunction->Set_Value(x, y, Formula.Get_Value(SG_T("xy"), px, py));
		}
	}

	//-----------------------------------------------------
	return( true );
}
Exemplo n.º 3
0
int CFit::On_Parameter_Changed(CSG_Parameters *pParameters, CSG_Parameter *pParameter)
{
	if (!SG_STR_CMP(pParameter->Get_Identifier(), SG_T("FORMEL")) )
	{
		CSG_String Msg;

		Formel.Set_Formula(pParameters->Get_Parameter("FORMEL")->asString());
	
		if( Formel.Get_Error(Msg) )
		{
			Error_Set  (Msg);
			Message_Dlg(Msg);

			return( -1 );
		}
	}

	return( 0 );
}
Exemplo n.º 4
0
bool CFit::On_Execute(void)
{
	int i, j,  NrVars;
	vector < double> x, y, StartValue, Result;
	CSG_String	msg;	
	
	CSG_Parameters StartParameters;

	const SG_Char *formel	=	Parameters("FORMEL")->asString();

	Formel.Add_Function(SG_T("NUG"), (TSG_PFNC_Formula_1) NUG, 1, 0);
	Formel.Add_Function(SG_T("SPH"), (TSG_PFNC_Formula_1) SPH, 2, 0);
	Formel.Add_Function(SG_T("EXP"), (TSG_PFNC_Formula_1) EXP, 2, 0);
    Formel.Add_Function(SG_T("LIN"), (TSG_PFNC_Formula_1) LIN, 2, 0);
	
	
	Formel.Set_Formula(formel);
	
	
	if (Formel.Get_Error(msg))
	{
		Message_Add(msg);

		return false;
	}
	
	const SG_Char *uservars = NULL;
	
	uservars = Formel.Get_Used_Variables();
	

	NrVars	=	0;
	for (i = 0; i < SG_STR_LEN(uservars); i++)
	{
		if (uservars[i] >='a' && uservars[i] <= 'z')
		{
			if (uservars[i] != 'x')
				vars[NrVars++] = uservars[i];
		}
	}
	
	vars[NrVars] =(char) 0;
	
	StartParameters.Add_Info_String(NULL, _TL(""), _TL("Formula"), _TL("Formula"), formel);
	
	for (i = 0; i < strlen(vars); i++)
	{
		CSG_String	c(vars[i]);
		StartParameters.Add_Value(NULL, c, c, _TL("Start Value"), PARAMETER_TYPE_Double, 1.0);
	}
	
	Dlg_Parameters(&StartParameters, _TL("Start Values"));
	
	for (i = 0; i < strlen(vars); i++)
	{
		char c[3];
		sprintf(c, "%c", vars[i]);
		StartValue.push_back(StartParameters(c)->asDouble());
	}
	
	CSG_Table	*pTable	= Parameters("SOURCE")->asTable();
	int Record_Count = pTable->Get_Record_Count();
	
	int	yField		= Parameters("YFIELD")->asInt();
	int	xField		= Parameters("XFIELD")->asInt();
	bool Use_X		= Parameters("USE_X")->asBool();
	
	pTable->Add_Field(_TL("Fit")				, SG_DATATYPE_Double);	
	
	for (i = 0; i < Record_Count; i++)
	{
		CSG_Table_Record *	Record = pTable->Get_Record(i);
		if (Use_X)
		{
			x.push_back(Record->asDouble(xField));
		}
		else
		{
			x.push_back(i);
		}
		
		y.push_back(Record->asDouble(yField));
	}
	
	TLMFit *Fit;
	
	Fit = new TLMFit(x, y, StartValue,  FitFunc);
	
	int max_iter = Parameters("ITER")->asInt();
	double Max_lamda = Parameters("LAMDA")->asInt();
	
	int iter = 0; 
	
	try
	{
		Fit->Fit();
		
		while ((Fit->Alamda() < Max_lamda) &&(iter < max_iter) &&Process_Get_Okay(true))
		{
			Fit->Fit();
			iter++;
		}
	}
	catch (ESingularMatrix &E)
	{
		if (E.Type == 1 || E.Type == 2)
		{
			msg.Printf(_TL("Matrix signular\n"));
			
			Message_Add(msg);
			
			return false;
		}
	}
	
	Result    = Fit->Param();
	
	for (i = 0; i < NrVars; i++)
	{
		Formel.Set_Variable(vars[i], (double) Result[i]);
	}
	
	msg.Printf(_TL("Model Parameters:"));
	Message_Add(msg);
	for (i = 0; i < NrVars; i++)
	{
		msg.Printf(SG_T("%c = %f\n"), vars[i], Result[i]);
		Message_Add(msg);
	}
	
	msg.Printf(_TL("\nRMS  of Residuals (stdfit):\t%f\n"), sqrt(Fit->Chisq()/x.size()));
	Message_Add(msg);
	
	msg.Printf(_TL("Correlation Matrix of the Fit Parameters:\n"));
	Message_Add(msg);
	
	vector< vector < double> > covar = Fit->Covar();
	
	msg.Printf(_TL(""));
	for (j = 0; j < NrVars; j++)
		msg.Printf(SG_T("%s\t%c"), msg.c_str(), vars[j]);
	
	msg.Printf(SG_T("%s\n"), msg.c_str());
	
	Message_Add(msg);
	
	for (i = 0; i < NrVars; i++)
	{
		msg.Printf(SG_T("%c"), vars[i]);
		for (j = 0; j <= i; j++)
		{	
			msg.Printf(SG_T("%s\t%f"), msg.c_str(), covar[i][j]/covar[i][i]);
		}
		msg.Printf(SG_T("%s\n"), msg.c_str());
		
		Message_Add(msg);
	}
	
	int Field_Count  = pTable->Get_Field_Count();
	
	for (i = 0; i < Record_Count; i++)
	{
		CSG_Table_Record *	Record = pTable->Get_Record(i);
		
		Record->Set_Value(Field_Count - 1, Formel.Get_Value(x[i]));
	}

//	API_FREE (uservars);
	return (true);
}
Exemplo n.º 5
0
//---------------------------------------------------------
bool CGrid_Calculator::On_Execute(void)
{
	double					Result, *Values;
	CSG_Formula				Formula;
	CSG_Parameter_Grid_List	*pGrids;
	CSG_Grid				*pResult;

	//-----------------------------------------------------
	pResult		= Parameters("RESULT")	->asGrid();
	pGrids		= Parameters("GRIDS")	->asGridList();

	//-----------------------------------------------------
	if( pGrids->Get_Count() <= 0 )
	{
		Message_Add(_TL("No grid in list"));

		return( false );
	}

	//-----------------------------------------------------
	if( !Formula.Set_Formula(Parameters("FORMULA")->asString()) )
	{
		int			Position;
		CSG_String	Message, s;

		s	+= _TL("Error in formula");
		s	+= SG_T("\n") + Formula.Get_Formula();

		if( Formula.Get_Error(&Position, &Message) )
		{
			s	+= SG_T("\n") + Message;
			s	+= CSG_String::Format(SG_T("\n%s: %d"), _TL("Position") , Position);

			if( Position >= 0 && Position < (int)Formula.Get_Formula().Length() )
			{
				s	+= SG_T("\n")
					+  Formula.Get_Formula().Left(Position - 1) + SG_T("[")
					+  Formula.Get_Formula()[Position] + SG_T("]")
					+  Formula.Get_Formula().Right(Formula.Get_Formula().Length() - (Position + 1));
			}
		}

		Message_Add(s, false);

		return( false );
	}

	//-----------------------------------------------------
	pResult->Set_Name(Formula.Get_Formula());

	Values	= new double[pGrids->Get_Count()];

	for(int y=0; y<Get_NY() && Set_Progress(y); y++)
	{
		for(int x=0; x<Get_NX(); x++)
		{
			bool	bNoData	= false;

			for(int i=0; i<pGrids->Get_Count() && !bNoData; i++)
			{
				if( pGrids->asGrid(i)->is_NoData(x, y) )
				{
					bNoData		= true;
				}
				else
				{
					Values[i]	= pGrids->asGrid(i)->asDouble(x, y);
				}
			}

			if( bNoData || _finite(Result = Formula.Get_Value(Values, pGrids->Get_Count())) == false )
			{
				pResult->Set_NoData(x, y);
			}
			else
			{
				pResult->Set_Value(x, y, Result);
			}
		}
	}

	delete[](Values);

	//-----------------------------------------------------
	return( true );
}