Ejemplo n.º 1
0
SwitchingFunction::SwitchingFunction(const SwitchingFunction&sf):
  init(sf.init),
  type(sf.type),
  invr0(sf.invr0),
  d0(sf.d0),
  dmax(sf.dmax),
  nn(sf.nn),
  mm(sf.mm),
  a(sf.a),
  b(sf.b),
  c(sf.c),
  d(sf.d),
  lambda(sf.lambda),
  beta(sf.beta),
  ref(sf.ref),
  invr0_2(sf.invr0_2),
  dmax_2(sf.dmax_2),
  stretch(sf.stretch),
  shift(sf.shift),
  evaluator(NULL),
  evaluator_deriv(NULL)
{
#ifdef __PLUMED_HAS_MATHEVAL
  if(sf.evaluator) evaluator=evaluator_create(evaluator_get_string(sf.evaluator));
  if(sf.evaluator_deriv) evaluator_deriv=evaluator_create(evaluator_get_string(sf.evaluator_deriv));
#endif
}
Ejemplo n.º 2
0
void rb_monitor_get_op_variables(const rb_monitor_t *monitor,
				 char ***vars,
				 size_t *vars_size) {
	void *evaluator = NULL;
	(*vars) = NULL;
	*vars_size = 0;

	struct {
		char **vars;
		int count;
	} all_vars;

	if (monitor->type != RB_MONITOR_T__OP) {
		goto no_deps;
	}

	evaluator = evaluator_create((char *)monitor->cmd_arg);
	if (NULL == evaluator) {
		rdlog(LOG_ERR,
		      "Couldn't create an evaluator from %s",
		      monitor->cmd_arg);
		goto no_deps;
	}

	evaluator_get_variables(evaluator, &all_vars.vars, &all_vars.count);
	(*vars) = malloc((size_t)all_vars.count * sizeof((*vars)[0]));
	if (*vars == NULL) {
		rdlog(LOG_CRIT,
		      "Couldn't allocate memory for %d vars",
		      all_vars.count);
		goto no_deps;
	}
	for (int i = 0; i < all_vars.count; ++i) {
		(*vars)[i] = strdup(all_vars.vars[i]);
		if (NULL == (*vars)[i]) {
			rdlog(LOG_ERR,
			      "Couldn't strdup %s (OOM?)",
			      all_vars.vars[i]);
			for (int j = 0; j < i; ++j) {
				free((*vars)[j]);
				(*vars)[j] = NULL;
			}
			goto no_deps;
		}
	}
	*vars_size = (size_t)all_vars.count;
	evaluator_destroy(evaluator);
	return;

no_deps:
	if (*vars) {
		free(*vars);
	}
	*vars = NULL;
	*vars_size = 0;

	if (evaluator) {
		evaluator_destroy(evaluator);
	}
}
Ejemplo n.º 3
0
double newtonrap(double x0, double es, double *ea, int imax, int *iter, int tabla, char *buffer, char *buffer2)
{
    double x1=x0;

    void *f, *f2;		/*  Evaluators for function and function derivative.  */
 
  /*  Create evaluator for function.  */
    f = evaluator_create (buffer);
    f2 = evaluator_create (buffer2);
    assert (f);
    assert (f2);
    
    if(tabla == 1)
    {
 
        printf ("\n\n|  i |      Raiz      |      ea      |     f(xr)    | \n");
        printf ("______________________________________________________\n");
    }

    *iter = 0;
    do{
        x0 = x1;
        x1 = x0-(evaluator_evaluate_x(f,x0)/evaluator_evaluate_x(f2,x0));
        *iter=*iter+1;
        if(x1!=0)
        {
            *ea=fabs((x1-x0)/x1)*100;
        }
        if ( tabla == 1 ) {
            printf ("| %2d | %12.8f | %12.8f | %12.8f |\n", *iter, x1, *ea, evaluator_evaluate_x(f,x1) );
	    }
    }while(es < *ea && *iter <= imax);

    evaluator_destroy(f);
    evaluator_destroy(f2);

    if ( tabla == 1 ) {
        printf ("\n\nLa Raiz es %6.4lf en %d iteraciones\n",x1,*iter);
        printf ("\n\nEl error relativo aproximado es %6.4lf\n",*ea);
        return 0;
    }
    else
        return x1;
}
Ejemplo n.º 4
0
Polynomial::Polynomial( const std::string& e, const std::string& n, Object *p )
 : Algorithm( n, p )
{
	char* f = strdup( e.c_str() );
	m_e = evaluator_create( f );
	free( f );
	if( !m_e )
	{
		throw std::invalid_argument( e + ": invalid polynomial" );
	}
	
	parse_parameters();
}
Ejemplo n.º 5
0
void EVALOBJ::setExpression(char *expr)
{
	int len = strlen(expr);
    if (expression != NULL)
    	delete expression;
    expression = new char[len+1];
    strcpy(expression, expr);
	setexp=TRUE;
    if (evaluator != NULL)
    	evaluator_destroy(evaluator);
    evaluator = evaluator_create(expr);
	setexp=FALSE;
    if (evaluator == NULL)
    	report_error("Unable to parse expression");
}
Ejemplo n.º 6
0
double pfijo(double x0, double es, double *ea, int imax, int *iter, int tabla, char *buffer)
{
    double xr=x0, xrold;

    void *f;		/*  Evaluators for function and function derivative.  */
 
  /*  Create evaluator for function.  */
    f = evaluator_create (buffer);
    assert (f);

    if(tabla == 1)
    {
 
        printf ("\n\n|  i |      Raiz      |      ea      |     f(xr)    | \n");
        printf ("______________________________________________________\n");
    }

    *iter = 0;
    do{
        xrold = xr;
        xr = evaluator_evaluate_x(f,xrold);
        *iter=*iter+1;
        if(xr!=0)
        {
            *ea=fabs((xr-xrold)/xr)*100;
        }
        if ( tabla == 1 ) {
            printf ("| %2d | %12.8f | %12.8f | %12.8f |\n", *iter, xr, *ea, evaluator_evaluate_x(f,xr) );
	    }

    }while(es < *ea && *iter <= imax);

    evaluator_destroy(f);

    if ( tabla == 1 ) {
        printf ("\n\nLa Raiz es %6.4lf en %d iteraciones\n",xr,*iter);
        printf ("\n\nEl error relativo aproximado es %6.4lf\n",*ea);
        return 0;
    }
    else
        return xr;
}
Ejemplo n.º 7
0
/*  TODO - Revisar que la formula sea correcta:
 *  No tenga caracteres diferentes a una constante permitida y x.
 *  Revisar que la formula tenga sentido para libmatheval... si no es el caso, explotar!
 */
void
gs_function_plot_set_formula (GsFunctionPlot *plot, const gchar *formula)
{
	g_return_if_fail (GS_IS_FUNCTION_PLOT (plot));

	void *f = evaluator_create ((char *) formula);

	g_assert (f != NULL);

/*	if (f == NULL) {
		return FALSE;
	}*/

	if (plot->priv->matheval_c != NULL) {
		evaluator_destroy (plot->priv->matheval_c);
	}

	plot->priv->matheval_c = f;

	g_signal_emit_by_name (G_OBJECT (plot), "plot-changed");
}
Ejemplo n.º 8
0
/* lcpi_parse_file */
int lcpi_parse_file(const char *file) {
    char buffer[BUFFER_SIZE];
    FILE *lcpi_FP = NULL;
    lcpi_t *lcpi = NULL;
    int line = 0;

    OUTPUT_VERBOSE((4, "%s", _BLUE("Loading LCPI metrics")));

    if (NULL == (lcpi_FP = fopen(file, "r"))) {
        OUTPUT(("%s (%s)", _ERROR("Error: unable to open LCPI file"), file));
        return PERFEXPERT_ERROR;
    }

    bzero(buffer, BUFFER_SIZE);
    while (NULL != fgets(buffer, BUFFER_SIZE - 1, lcpi_FP)) {
        char *token = NULL;

        line++;

        /* Ignore comments and blank lines */
        if ((0 == strncmp("#", buffer, 1)) ||
            (strspn(buffer, " \t\r\n") == strlen(buffer))) {
            continue;
        }

        /* Remove the end \n character */
        buffer[strlen(buffer) - 1] = '\0';

        /* Replace some characters just to provide a safe expression */
        perfexpert_string_replace_char(buffer, ':', '_');

        /* Allocate and set LCPI data */
        PERFEXPERT_ALLOC(lcpi_t, lcpi, sizeof(lcpi_t));
        lcpi->value = 0.0;

        token = strtok(buffer, "=");
        PERFEXPERT_ALLOC(char, lcpi->name, strlen(token) + 1);
        strcpy(lcpi->name, perfexpert_string_remove_char(token, ' '));
        strcpy(lcpi->name_md5, perfexpert_md5_string(lcpi->name));

        token = strtok(NULL, "=");
        lcpi->expression = evaluator_create(token);
        if (NULL == lcpi->expression) {
            OUTPUT(("%s (%s)", _ERROR("Error: invalid expression at line"),
                line));
            return PERFEXPERT_ERROR;
        }

        /* Add LCPI to global hash of LCPIs */
        perfexpert_hash_add_str(globals.lcpi_by_name, name_md5, lcpi);

        OUTPUT_VERBOSE((7, "   [%s]=[%s] (%s)", lcpi->name,
            evaluator_get_string(lcpi->expression), lcpi->name_md5));
    }

    OUTPUT_VERBOSE((4, "   (%d) %s",
        perfexpert_hash_count_str(globals.lcpi_by_name),
        _MAGENTA("LCPI metric(s) found")));

    return PERFEXPERT_SUCCESS;
}
Ejemplo n.º 9
0
/*!
 \brief convert_before_download() converts the value passed using the
 conversions bound to the widget
 \param widget (GtkWidget *) widget to extract the conversion info from
 \param value (gfloat *) the "real world" value from the tuning gui before
 translation to MS-units
 \returns the integere ms-units form after conversion
 */
G_MODULE_EXPORT gint convert_before_download(GtkWidget *widget, gfloat value)
{
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	gint return_value = 0;
	gint tmpi = 0;
	gchar * conv_expr = NULL;
	void *evaluator = NULL;
	DataSize size = MTX_U08;
	float lower = 0.0;
	float upper = 0.0;
	gfloat *multiplier = NULL;
	gfloat *adder = NULL;
	guint i = 0;
	GHashTable *mhash = NULL;
	GHashTable *ahash = NULL;
	gchar *key_list = NULL;
	gchar *mult_list = NULL;
	gchar *add_list = NULL;
	gchar **keys = NULL;
	gchar **mults = NULL;
	gchar **adds = NULL;
	gint table_num = 0;
	gchar *tmpbuf = NULL;
	gchar * source_key = NULL;
	gchar * hash_key = NULL;
	gint *algorithm = NULL;
	GHashTable *sources_hash = NULL;

	sources_hash = DATA_GET(global_data,"sources_hash");
	algorithm = DATA_GET(global_data,"algorithm");

	g_static_mutex_lock(&mutex);


	if (!OBJ_GET(widget,"size"))
		printf(__FILE__"%s %s\n",_(": convert_before_download, FATAL ERROR, size undefined for widget %s "),glade_get_widget_name(widget));

	size = (DataSize)OBJ_GET(widget,"size");
	if (OBJ_GET(widget,"raw_lower"))
		lower = (gfloat)strtol(OBJ_GET(widget,"raw_lower"),NULL,10);
	else
		lower = (gfloat)get_extreme_from_size(size,LOWER);
	if (OBJ_GET(widget,"raw_upper"))
		upper = (gfloat)strtol(OBJ_GET(widget,"raw_upper"),NULL,10);
	else
		upper = (gfloat)get_extreme_from_size(size,UPPER);

	/* MULTI EXPRESSION ONLY! */
	if (OBJ_GET(widget,"multi_expr_keys"))
	{
		if ((!OBJ_GET(widget,"mhash")) && (!OBJ_GET(widget,"ahash")))
		{
			mhash = g_hash_table_new_full(g_str_hash,g_str_equal,g_free,g_free);
			ahash = g_hash_table_new_full(g_str_hash,g_str_equal,g_free,g_free);
			key_list = OBJ_GET(widget,"multi_expr_keys");
			mult_list = OBJ_GET(widget,"fromecu_mults");
			add_list = OBJ_GET(widget,"fromecu_addds");
			keys = g_strsplit(key_list,",",-1);
			mults = g_strsplit(mult_list,",",-1);
			adds = g_strsplit(add_list,",",-1);
			for (i=0;i<MIN(g_strv_length(keys),g_strv_length(mults));i++)
			{
				multiplier = g_new0(gfloat, 1);
				*multiplier = (gfloat)g_strtod(mults[i],NULL);
				g_hash_table_insert(mhash,g_strdup(keys[i]),multiplier);
				adder = g_new0(gfloat, 1);
				*adder = (gfloat)g_strtod(adds[i],NULL);
				g_hash_table_insert(ahash,g_strdup(keys[i]),adder);
			}
			g_strfreev(keys);
			g_strfreev(mults);
			g_strfreev(adds);

			OBJ_SET_FULL(widget,"mhash",mhash,g_hash_table_destroy);
			OBJ_SET_FULL(widget,"ahash",ahash,g_hash_table_destroy);
		}
		mhash = OBJ_GET(widget,"mhash");
		ahash = OBJ_GET(widget,"ahash");
		source_key = OBJ_GET(widget,"source_key");
		if (!source_key)
			printf(_("big problem, source key is undefined!!\n"));
		hash_key = (gchar *)g_hash_table_lookup(sources_hash,source_key);
		tmpbuf = (gchar *)OBJ_GET(widget,"table_num");
		if (tmpbuf)
			table_num = (GINT)strtol(tmpbuf,NULL,10);
		if (table_num == -1) /* Not a table */
		{
			if (!hash_key)
			{
				multiplier = g_hash_table_lookup(mhash,"DEFAULT");
				adder = g_hash_table_lookup(ahash,"DEFAULT");
			}
			else
			{
				multiplier = g_hash_table_lookup(mhash,(gchar *)hash_key);
				adder = g_hash_table_lookup(ahash,(gchar *)hash_key);
			}
		}
		else /* This is a 3d table */
		{
			switch (algorithm[table_num])
			{
				case SPEED_DENSITY:
					if (!hash_key)
					{
						multiplier = g_hash_table_lookup(mhash,"DEFAULT");
						adder = g_hash_table_lookup(ahash,"DEFAULT");
					}
					else
					{
						multiplier = g_hash_table_lookup(mhash,hash_key);
						adder = g_hash_table_lookup(ahash,hash_key);
					}
					break;
				case ALPHA_N:
					multiplier = g_hash_table_lookup(mhash,"DEFAULT");
					adder = g_hash_table_lookup(ahash,"DEFAULT");
					break;
				case MAF:
					multiplier = g_hash_table_lookup(mhash,"AFM_VOLTS");
					adder = g_hash_table_lookup(ahash,"AFM_VOLTS");
					break;
			}
		}
		/* Reverse calc due to this being TO the ecu */
		if ((multiplier) && (adder))
			return_value = (GINT)((value - (*adder))/(*multiplier));
		else if (multiplier)
			return_value = (GINT)(value/(*multiplier));
		else
			return_value = (GINT)value;
	}
	else /* NON Multi Expression */
	{
		conv_expr = (gchar *)OBJ_GET(widget,"toecu_conv_expr");

		/* Expression is NOT multi expression but has more complex math*/
		if (conv_expr)
		{
			evaluator = (void *)OBJ_GET(widget,"dl_evaluator");
			if (!evaluator)
			{
				evaluator = evaluator_create(conv_expr);
				assert(evaluator);
				OBJ_SET_FULL(widget,"dl_evaluator",(gpointer)evaluator,evaluator_destroy);
			}
			return_value = (GINT)evaluator_evaluate_x(evaluator,value); 
		}
		else
		{
			multiplier = (gfloat *)OBJ_GET(widget,"fromecu_mult");
			adder = (gfloat *)OBJ_GET(widget,"fromecu_add");
			/* Handle all cases of with or without multiplier/adder*/
			if ((multiplier) && (adder))
				return_value = (GINT)((value - (*adder))/(*multiplier));
			else if (multiplier)
				return_value = (GINT)(value/(*multiplier));
			else
				return_value = (GINT)value;
		}
	}
	dbg_func(CONVERSIONS,g_strdup_printf(__FILE__": convert_before_dl():\n\t widget %s raw %.2f, sent %i\n",glade_get_widget_name(widget),value,return_value));
	if (return_value > upper) 
	{
		dbg_func(CONVERSIONS|CRITICAL,g_strdup_printf(__FILE__": convert_before_download()\n\t WARNING value clamped at %f (%f <- %f -> %f)!!\n",upper,lower,value,upper));
		return_value = upper;
	}
	if (return_value < lower)
	{
		dbg_func(CONVERSIONS|CRITICAL,g_strdup_printf(__FILE__": convert_before_download()\n\t WARNING value clamped at %f (%f <- %f -> %f)!!\n",lower,lower,value,upper));
		return_value = lower;
	}

	tmpi = return_value;
	if (OBJ_GET(widget,"lookuptable"))
		return_value = (GINT)reverse_lookup_obj(G_OBJECT(widget),tmpi);

	g_static_mutex_unlock(&mutex);
	return (return_value);
}
Ejemplo n.º 10
0
/*!
 \brief convert_after_upload() converts the ms-units data to the real world
 units for display on the GUI
 \param widget (GtkWidget *) to extract the conversion info from to perform
 the necessary math
 \returns the real world value for the GUI
 */
G_MODULE_EXPORT gfloat convert_after_upload(GtkWidget * widget)
{
	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
	static gint (*get_ecu_data_f)(gpointer);
	static void (*send_to_ecu_f)(gpointer, gint, gboolean) = NULL;
	gfloat return_value = 0.0;
	gchar * conv_expr = NULL;
	void *evaluator = NULL;
	gint tmpi = 0;
	DataSize size = 0;
	gfloat lower = 0.0;
	gfloat upper = 0.0;
	gboolean fromecu_complex = FALSE;
	guint i = 0;
	gint table_num = -1;
	GHashTable *mhash = NULL;
	GHashTable *ahash = NULL;
	gchar *key_list = NULL;
	gchar *mult_list = NULL;
	gchar *add_list = NULL;
	gchar **keys = NULL;
	gchar **mults = NULL;
	gchar **adds = NULL;
	gchar * tmpbuf = NULL;
	gchar * source_key = NULL;
	gchar * hash_key = NULL;
	gfloat *multiplier = NULL;
	gfloat *adder = NULL;
	gint *algorithm = NULL;
	GHashTable *sources_hash = NULL;
	extern gconstpointer *global_data;


	if (!get_ecu_data_f)
		get_symbol("get_ecu_data",(void *)&get_ecu_data_f);
	if (!send_to_ecu_f)
		get_symbol("send_to_ecu",(void *)&send_to_ecu_f);
	g_return_val_if_fail(get_ecu_data_f,0.0);
	g_return_val_if_fail(send_to_ecu_f,0.0);

	g_static_mutex_lock(&mutex);

	size = (DataSize)OBJ_GET(widget,"size");
	if (size == 0)
	{
		printf(_("BIG PROBLEM, size undefined! widget %s, default to U08 \n"),(gchar *)glade_get_widget_name(widget));
		size = MTX_U08;
	}

	if (OBJ_GET(widget,"raw_lower"))
		lower = (gfloat)strtol(OBJ_GET(widget,"raw_lower"),NULL,10);
	else
		lower = (gfloat)get_extreme_from_size(size,LOWER);
	if (OBJ_GET(widget,"raw_upper"))
		upper = (gfloat)strtol(OBJ_GET(widget,"raw_upper"),NULL,10);
	else
		upper = (gfloat)get_extreme_from_size(size,UPPER);

	fromecu_complex = (GBOOLEAN)OBJ_GET(widget,"fromecu_complex");
	if (fromecu_complex)
	{
		g_static_mutex_unlock(&mutex);
		/*printf("Complex upload conversion for widget at page %i, offset %i, name %s\n",(GINT)OBJ_GET(widget,"page"),(GINT)OBJ_GET(widget,"offset"),glade_get_widget_name(widget));
		  */
		return handle_complex_expr_obj(G_OBJECT(widget),NULL,UPLOAD);
	}

	if (OBJ_GET(widget,"lookuptable"))
		tmpi = lookup_data_obj(G_OBJECT(widget),get_ecu_data_f(widget));
	else
		tmpi = get_ecu_data_f(widget);
	if (tmpi < lower)
	{
		dbg_func(CONVERSIONS|CRITICAL,g_strdup_printf(__FILE__": convert_after_upload()\n\t WARNING RAW value  out of range for widget %s, clamped at %.1f (%.1f <- %i -> %.1f), updating ECU with valid value within limits!!\n",(gchar *)glade_get_widget_name(widget),lower,lower,tmpi,upper));
		tmpi = lower;
		send_to_ecu_f(widget,tmpi,TRUE);
	}
	if (tmpi > upper)
	{
		dbg_func(CONVERSIONS|CRITICAL,g_strdup_printf(__FILE__": convert_after_upload()\n\t WARNING RAW value out of range for widget %s, clamped at %.1f (%.1f <- %i -> %.1f), updating ECU with valid value within limits!!\n",(gchar *)glade_get_widget_name(widget),lower,lower,tmpi,upper));
		tmpi = upper;
		send_to_ecu_f(widget,tmpi,TRUE);
	}
	/* MULTI EXPRESSION ONLY! */
	if (OBJ_GET(widget,"multi_expr_keys"))
	{
		sources_hash = DATA_GET(global_data,"sources_hash");
		if ((!OBJ_GET(widget,"mhash")) && (!OBJ_GET(widget,"ahash")))
		{
			mhash = g_hash_table_new_full(g_str_hash,g_str_equal,g_free,g_free);
			ahash = g_hash_table_new_full(g_str_hash,g_str_equal,g_free,g_free);
			key_list = OBJ_GET(widget,"multi_expr_keys");
			mult_list = OBJ_GET(widget,"fromecu_mults");
			add_list = OBJ_GET(widget,"fromecu_adds");
			if (!mult_list)
				printf("BUG, widget %s is multi_expression but doesn't have fromecu_mults defined!\n",glade_get_widget_name(widget));
			if (!add_list)
				printf("BUG, widget %s is multi_expression but doesn't have fromecu_adds defined!\n",glade_get_widget_name(widget));
			keys = g_strsplit(key_list,",",-1);
			mults = g_strsplit(mult_list,",",-1);
			adds = g_strsplit(add_list,",",-1);
			for (i=0;i<MIN(g_strv_length(keys),g_strv_length(mults));i++)
			{
				multiplier = g_new0(gfloat, 1);
				*multiplier = (gfloat)g_strtod(mults[i],NULL);
				g_hash_table_insert(mhash,g_strdup(keys[i]),multiplier);
				adder = g_new0(gfloat, 1);
				*adder = (gfloat)g_strtod(adds[i],NULL);
				g_hash_table_insert(ahash,g_strdup(keys[i]),adder);
			}
			g_strfreev(keys);
			g_strfreev(mults);
			g_strfreev(adds);

			OBJ_SET_FULL(widget,"mhash",mhash,g_hash_table_destroy);
			OBJ_SET_FULL(widget,"ahash",ahash,g_hash_table_destroy);
		}
		mhash = OBJ_GET(widget,"mhash");
		ahash = OBJ_GET(widget,"ahash");
		source_key = OBJ_GET(widget,"source_key");
		if (!source_key)
			printf(_("big problem, source key is undefined!!\n"));
		hash_key = (gchar *)g_hash_table_lookup(sources_hash,source_key);
		tmpbuf = (gchar *)OBJ_GET(widget,"table_num");
		if (tmpbuf)
			table_num = (GINT)strtol(tmpbuf,NULL,10);
		if (table_num == -1)
		{
			if (!hash_key)
			{
				multiplier = g_hash_table_lookup(mhash,"DEFAULT");
				adder = g_hash_table_lookup(ahash,"DEFAULT");
			}
			else
			{
				multiplier = g_hash_table_lookup(mhash,(gchar *)hash_key);
				adder = g_hash_table_lookup(ahash,(gchar *)hash_key);
			}
		}
		else
		{
			algorithm = DATA_GET(global_data,"algorithm");
			switch (algorithm[table_num])
			{
				case SPEED_DENSITY:
					if (!hash_key)
					{
						multiplier = g_hash_table_lookup(mhash,"DEFAULT");
						adder = g_hash_table_lookup(ahash,"DEFAULT");
					}
					else
					{
						multiplier = g_hash_table_lookup(mhash,hash_key);
						adder = g_hash_table_lookup(ahash,hash_key);
					}
					break;
				case ALPHA_N:
					multiplier = g_hash_table_lookup(mhash,"DEFAULT");
					adder = g_hash_table_lookup(ahash,"DEFAULT");
					break;
				case MAF:
					multiplier = g_hash_table_lookup(mhash,"AFM_VOLTS");
					adder = g_hash_table_lookup(ahash,"AFM_VOLTS");
					break;
			}
		}
		if ((multiplier) && (adder))
			return_value = (((gfloat)tmpi * (*multiplier)) + (*adder));
		else if (multiplier)
			return_value = (gfloat)tmpi * (*multiplier);
		else
			return_value = (gfloat)tmpi;
	}
	else
	{
		conv_expr = (gchar *)OBJ_GET(widget,"fromecu_conv_expr");
		if (conv_expr)
		{
			evaluator = (void *)OBJ_GET(widget,"ul_evaluator");
			if (!evaluator)
			{
				evaluator = evaluator_create(conv_expr);
				assert(evaluator);
				OBJ_SET_FULL(widget,"ul_evaluator",(gpointer)evaluator,evaluator_destroy);
			}
			return_value = evaluator_evaluate_x(evaluator,tmpi);
		}
		else
		{
			multiplier = OBJ_GET(widget,"fromecu_mult");
			adder = OBJ_GET(widget,"fromecu_add");
			if ((multiplier) && (adder))
				return_value = (((gfloat)tmpi * (*multiplier)) + (*adder));
			else if (multiplier)
				return_value = (gfloat)tmpi * (*multiplier);
			else
				return_value = (gfloat)tmpi;
			/*dbg_func(CONVERSIONS,g_strdup_printf(__FILE__": convert_after_ul():\n\tNo/Fast CONVERSION defined for  widget %s, value %f\n",(gchar *)glade_get_widget_name(widget), return_value));*/
		}

	}
//	dbg_func(CONVERSIONS,g_strdup_printf(__FILE__": convert_after_ul()\n\t page %i,offset %i, raw %i, val %f\n",page,offset,tmpi,return_value));
	g_static_mutex_unlock(&mutex);
	return (return_value);
}
Ejemplo n.º 11
0
char *oph_predicate2(UDF_INIT * initid, UDF_ARGS * args, char *result, unsigned long *length, char *is_null, char *error)
{
	oph_string measure;
	char *buffer;
	char **names;
	int count;
	oph_predicate2_param *param;

	int i = 0;

	if (core_set_type(&(measure), args->args[0], &(args->lengths[0]))) {
		pmesg(1, __FILE__, __LINE__, "Type not recognized\n");
		*length = 0;
		*is_null = 0;
		*error = 1;
		return NULL;
	}

	measure.content = args->args[2];
	measure.length = &(args->lengths[2]);
	measure.missingvalue = NULL;

	core_set_elemsize(&(measure));

	if (core_set_numelem(&(measure))) {
		pmesg(1, __FILE__, __LINE__, "Error on counting elements\n");
		*length = 0;
		*is_null = 0;
		*error = 1;
		return NULL;
	}

	if (!initid->ptr) {
		initid->ptr = (char *) malloc(sizeof(oph_predicate2_param));
		if (!initid->ptr) {
			pmesg(1, __FILE__, __LINE__, "Error allocating result string\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		param = (oph_predicate2_param *) initid->ptr;

		for (i = 0; i < 4; ++i)
			param->f[i] = NULL;
		param->occurrence = 0;	// ALL

		buffer = (char *) malloc(1 + args->lengths[3]);
		strncpy(buffer, args->args[3], args->lengths[3]);
		buffer[args->lengths[3]] = '\0';
		pthread_rwlock_wrlock(&lock);
		param->f[1] = evaluator_create(buffer);
		pthread_rwlock_unlock(&lock);
		free(buffer);
		if (!param->f[1]) {
			pmesg(1, __FILE__, __LINE__, "Error allocating evaluator\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		evaluator_get_variables(param->f[1], &names, &count);
		if (count > 1) {
			pmesg(1, __FILE__, __LINE__, "Too variables in expression\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		// Comparison operator
		if (core_set_comp(&param->op, args->args[4], &(args->lengths[4]))) {
			pmesg(1, __FILE__, __LINE__, "Comparison operator not recognized\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		if (args->args[5] && args->lengths[5]) {
			buffer = (char *) malloc(1 + args->lengths[5]);
			core_strncpy(buffer, args->args[5], &(args->lengths[5]));
			if (!strcasecmp(buffer, "nan"))
				sprintf(buffer, "0/0");
		} else
			buffer = strdup("0/0");
		pthread_rwlock_wrlock(&lock);
		param->f[2] = evaluator_create(buffer);
		pthread_rwlock_unlock(&lock);
		free(buffer);
		if (!param->f[2]) {
			pmesg(1, __FILE__, __LINE__, "Error allocating evaluator\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		evaluator_get_variables(param->f[2], &names, &count);
		if (count > 1) {
			pmesg(1, __FILE__, __LINE__, "Too variables in expression\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		if (args->args[6] && args->lengths[6]) {
			buffer = (char *) malloc(1 + args->lengths[6]);
			core_strncpy(buffer, args->args[6], &(args->lengths[6]));
			if (!strcasecmp(buffer, "nan"))
				sprintf(buffer, "0/0");
		} else
			buffer = strdup("0/0");
		pthread_rwlock_wrlock(&lock);
		param->f[3] = evaluator_create(buffer);
		pthread_rwlock_unlock(&lock);
		free(buffer);
		if (!param->f[3]) {
			pmesg(1, __FILE__, __LINE__, "Error allocating evaluator\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		evaluator_get_variables(param->f[3], &names, &count);
		if (count > 1) {
			pmesg(1, __FILE__, __LINE__, "Too variables in expression\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		oph_string output_array;
		core_set_type(&output_array, args->args[1], &(args->lengths[1]));
		if (!output_array.type) {
			pmesg(1, __FILE__, __LINE__, "Unable to recognize measures type\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		if (core_set_elemsize(&output_array)) {
			pmesg(1, __FILE__, __LINE__, "Unable to recognize measures type\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}
		param->result_type = output_array.type;
		param->result_elemsize = output_array.elemsize;

		param->length = output_array.elemsize * measure.numelem;
		param->f[0] = malloc(param->length);
		if (!param->f[0]) {
			pmesg(1, __FILE__, __LINE__, "Error allocating result string\n");
			*length = 0;
			*is_null = 0;
			*error = 1;
			return NULL;
		}

		if (args->arg_count > 7) {
			buffer = (char *) malloc(1 + args->lengths[7]);
			core_strncpy(buffer, args->args[7], &(args->lengths[7]));
			if (strcasecmp(buffer, OPH_PREDICATE2_ALL_OCCURRENCE)) {
				if (!strcasecmp(buffer, OPH_PREDICATE2_FIRST_OCCURRENCE) || !strcasecmp(buffer, OPH_PREDICATE2_BEGIN_OCCURRENCE))
					param->occurrence = 1;
				else if (!strcasecmp(buffer, OPH_PREDICATE2_LAST_OCCURRENCE) || !strcasecmp(buffer, OPH_PREDICATE2_END_OCCURRENCE))
					param->occurrence = -1;
				else {
					if (args->arg_type[7] == STRING_RESULT)
						param->occurrence = (long) strtol(buffer, NULL, 10);
					else
						param->occurrence = *((long long *) args->args[7]);
					if (param->occurrence < 1) {
						free(buffer);
						pmesg(1, __FILE__, __LINE__, "Unable to read occurrence\n");
						*length = 0;
						*is_null = 0;
						*error = 1;
						return NULL;
					}
				}
			}
			free(buffer);
		}
	} else
		param = (oph_predicate2_param *) initid->ptr;

	i = core_oph_predicate2(&measure, initid->ptr);
	if (i) {
		pmesg(1, __FILE__, __LINE__, "Unable to compute result\n");
		*length = 0;
		*is_null = 0;
		*error = 1;
		return NULL;
	}
	*length = param->length;
	*error = 0;
	*is_null = 0;
	return (char *) param->f[0];

}
Ejemplo n.º 12
0
char *me_change_string(char *string, int opt, me_t * me)
{
	void *f = NULL, *f_prim = NULL;
	char *sep_ptr;
	char *str = NULL;

	char **names = NULL;
	int count = 0;

	char *f_prim_str = NULL;

	char *vars_str = NULL;

	double res = 0, res_prim = 0;

	debug(NULL, "me_change_string: Entered: %s\n", string);

	if (!me || !string)
		return NULL;

	sep_ptr = str_find_sep(string);
	if (sep_ptr)
		string = sep_ptr;

	if (!strlen(string))
		return NULL;

	f = evaluator_create(string);
	if (!f)
		return NULL;

	evaluator_get_variables(f, &names, &count);

	if (!names)
		goto cleanup;

	vars_str =
	    array_to_str((void **)names, count, (char *(*)(void *))strdup);
	if (!vars_str)
		goto cleanup;

	f_prim = evaluator_derivative_x(f);

	if (!f_prim)
		goto cleanup;

	f_prim_str = evaluator_get_string(f_prim);

	res = evaluator_evaluate_x_y_z(f, me->x, me->y, me->z);
	res_prim = evaluator_evaluate_x_y_z(f_prim, me->x, me->y, me->z);

	str =
	    str_unite("f(x,y,z)=%f ... vars={%s} ::: f'(x)=%s=%f", res,
		      vars_str, f_prim_str, res_prim);

 cleanup:
	if (f_prim)
		evaluator_destroy(f_prim);

	if (f)
		evaluator_destroy(f);

	if (vars_str)
		free(vars_str);

	return str;
}
Ejemplo n.º 13
0
void SwitchingFunction::set(const std::string & definition,std::string& errormsg){
  vector<string> data=Tools::getWords(definition);
  if( data.size()<1 ) errormsg="missing all input for switching function"; 
  string name=data[0];
  data.erase(data.begin());
  invr0=0.0;
  invr0_2=0.0;
  d0=0.0;
  dmax=std::numeric_limits<double>::max();
  dmax_2=std::numeric_limits<double>::max();
  stretch=1.0;
  shift=0.0;
  init=true;

  bool present;

  present=Tools::findKeyword(data,"D_0");
  if(present && !Tools::parse(data,"D_0",d0)) errormsg="could not parse D_0";

  present=Tools::findKeyword(data,"D_MAX");
  if(present && !Tools::parse(data,"D_MAX",dmax)) errormsg="could not parse D_MAX";
  if(dmax<std::sqrt(std::numeric_limits<double>::max())) dmax_2=dmax*dmax;
  bool dostretch=false;
  Tools::parseFlag(data,"STRETCH",dostretch); // this is ignored now
  dostretch=true;
  bool dontstretch=false;
  Tools::parseFlag(data,"NOSTRETCH",dontstretch); // this is ignored now
  if(dontstretch) dostretch=false;
  double r0;
  if(name=="CUBIC"){
     r0 = dmax - d0;
  } else {
     bool found_r0=Tools::parse(data,"R_0",r0);
     if(!found_r0) errormsg="R_0 is required";
  }
  invr0=1.0/r0;
  invr0_2=invr0*invr0;

  if(name=="RATIONAL"){
    type=rational;
    nn=6;
    mm=0;
    present=Tools::findKeyword(data,"NN");
    if(present && !Tools::parse(data,"NN",nn)) errormsg="could not parse NN";
    present=Tools::findKeyword(data,"MM");
    if(present && !Tools::parse(data,"MM",mm)) errormsg="could not parse MM";
    if(mm==0) mm=2*nn;
  } else if(name=="SMAP"){
    type=smap;
    present=Tools::findKeyword(data,"A");
    if(present && !Tools::parse(data,"A",a)) errormsg="could not parse A";
    present=Tools::findKeyword(data,"B");
    if(present && !Tools::parse(data,"B",b)) errormsg="could not parse B";
    c=pow(2., static_cast<double>(a)/static_cast<double>(b) ) - 1; 
    d = -static_cast<double>(b) / static_cast<double>(a);
  } 
  else if(name=="Q") {
    type=nativeq; 
    beta = 50.0;  // nm-1
    lambda = 1.8; // unitless
    present=Tools::findKeyword(data,"BETA");
    if(present && !Tools::parse(data, "BETA", beta)) errormsg="could not parse BETA";
    present=Tools::findKeyword(data,"LAMBDA");
    if(present && !Tools::parse(data, "LAMBDA", lambda)) errormsg="could not parse LAMBDA";
    bool found_ref=Tools::parse(data,"REF",ref); // nm
    if(!found_ref) errormsg="REF (reference disatance) is required for native Q";

  }
  else if(name=="EXP") type=exponential;
  else if(name=="GAUSSIAN") type=gaussian;
  else if(name=="CUBIC") type=cubic;
  else if(name=="TANH") type=tanh;
#ifdef __PLUMED_HAS_MATHEVAL
  else if(name=="MATHEVAL"){
    type=matheval;
    std::string func;
    Tools::parse(data,"FUNC",func);
    evaluator=evaluator_create(const_cast<char*>(func.c_str()));
    char **check_names;
    int    check_count;
    evaluator_get_variables(evaluator,&check_names,&check_count);
    if(check_count!=1){
      errormsg="wrong number of arguments in MATHEVAL switching function";
      return;
    } 
    if(std::string(check_names[0])!="x"){
      errormsg ="argument should be named 'x'";
      return;
    }
    evaluator_deriv=evaluator_derivative(evaluator,const_cast<char*>("x"));
  }
#endif
  else errormsg="cannot understand switching function type '"+name+"'";
  if( !data.empty() ){
      errormsg="found the following rogue keywords in switching function input : ";
      for(unsigned i=0;i<data.size();++i) errormsg = errormsg + data[i] + " "; 
  }

  if(dostretch && dmax!=std::numeric_limits<double>::max()){
    double dummy;
    double s0=calculate(0.0,dummy);
    double sd=calculate(dmax,dummy);
    stretch=1.0/(s0-sd);
    shift=-sd*stretch;
  }
}