Beispiel #1
0
void ModelicaInternal_print(_In_z_ const char* string,
    _In_z_ const char* fileName) {
    if ( fileName[0] == '\0' ) {
      /* Write string to terminal */
        ModelicaFormatMessage("%s\n", string);
    }
    return; }
Beispiel #2
0
/* Fetch sha's for the libraries at locations 'libPaths' and print them to the file 'fileName'. */
void tagCommit(const char *fileName, const char **libNames, const char **libPaths, int nLib, int verbose){ 
	/* Buffers */
	char sha[44];
	char buffer[250]; 

	/* Write header */
	FILE *fOut = fopen(fileName, "w");
	sprintf(buffer, "Library: sha\n---------------------------\n"); 
	if (fputs(buffer, fOut)==EOF){
		ModelicaFormatError("In tagcommit.c: failed to write to file %s.", fileName);
 	}

 	/* Fetch library sha's and print to file */
 	int i;
	for (i = 0; i < nLib; ++i)
	{
		getCommit(libPaths[i], sha);
		sprintf(buffer, "%s: %s\n", libNames[i],sha); 
		if (verbose)
			ModelicaFormatMessage("Library %s has sha %s. Writing to file path %s\n", libNames[i], sha, fileName);
		if (fputs(buffer, fOut)==EOF){
			ModelicaFormatError("In tagcommit.c: failed to write to file %s.", fileName);
 		}
	}
	fclose(fOut);
}
Beispiel #3
0
/*
 *	m y P r i n t f
 */
returnValue myPrintf( const char* s )
{
	#ifdef __MATLAB__
	mexPrintf( s );
        #elif defined __MODELICA__
	ModelicaFormatMessage("%s",s);
	#else
	FILE* outputfile = getGlobalMessageHandler( )->getOutputFile( );
	if ( outputfile == 0 )
		return THROWERROR( RET_NO_GLOBAL_MESSAGE_OUTPUTFILE );

	fprintf( outputfile, "%s", s );
	#endif

	return SUCCESSFUL_RETURN;
}
double
interpolateVectorTable(void *object, double x) {
  VectorTable *table = (VectorTable *)object;
  size_t i = table->lastIndex;
  double p;
  
  ModelicaFormatMessage("Request to compute value of y at %g\n", x);
  if (x<table->x[0])
    ModelicaFormatError("Requested value of x=%g is below the lower bound of %g\n", x, table->x[0]);
  if (x>table->x[table->npoints-1])
    ModelicaFormatError("Requested value of x=%g is above the upper bound of %g\n", x, table->x[table->npoints-1]);
	
  while(x>=table->x[i+1]) i = i + 1;
  while(x<table->x[i]) i = i - 1;
  
  p = (x-table->x[i])/(table->x[i+1]-table->x[i]);
  table->lastIndex = i;
  return p*table->y[i+1]+(1-p)*table->y[i];
}
void *
createVectorTable(const double *data, size_t np) {
  VectorTable *table = (VectorTable *)malloc(sizeof(VectorTable));
  size_t i;
  
  /* Allocate memory for data */
  table->x = (double *)malloc(sizeof(double)*np);
  table->y = (double *)malloc(sizeof(double)*np);
  /* Copy data into our local array */
  for(i=0;i<np;i++) {
    table->x[i] = data[2*i];
	table->y[i] = data[2*i+1];
  }
  for(i=0;i<2*np;i++) ModelicaFormatMessage("Value at %d is %g\n", i, data[i]);
  /* Initialize the rest of the table object */
  table->npoints = np;
  table->lastIndex = 0;
  return table;
}
void* FMI2ModelExchangeConstructor_OMC(int fmi_log_level, char* working_directory, char* instanceName, int debugLogging)
{
  FMI2ModelExchange* FMI2ME = malloc(sizeof(FMI2ModelExchange));
  jm_status_enu_t status, instantiateModelStatus;
  FMI2ME->FMILogLevel = fmi_log_level;
  /* JM callbacks */
  FMI2ME->JMCallbacks.malloc = malloc;
  FMI2ME->JMCallbacks.calloc = calloc;
  FMI2ME->JMCallbacks.realloc = realloc;
  FMI2ME->JMCallbacks.free = free;
  FMI2ME->JMCallbacks.logger = importlogger;
  FMI2ME->JMCallbacks.log_level = FMI2ME->FMILogLevel;
  FMI2ME->JMCallbacks.context = 0;
  FMI2ME->FMIImportContext = fmi_import_allocate_context(&FMI2ME->JMCallbacks);
  /* parse the xml file */
  FMI2ME->FMIWorkingDirectory = (char*) malloc(strlen(working_directory)+1);
  strcpy(FMI2ME->FMIWorkingDirectory, working_directory);
  FMI2ME->FMIImportInstance = fmi2_import_parse_xml(FMI2ME->FMIImportContext, FMI2ME->FMIWorkingDirectory, NULL);
  if(!FMI2ME->FMIImportInstance) {
    FMI2ME->FMISolvingMode = fmi2_none_mode;
    ModelicaFormatError("Error parsing the XML file contained in %s\n", FMI2ME->FMIWorkingDirectory);
    return 0;
  }
  /* FMI callback functions */
  FMI2ME->FMICallbackFunctions.logger = fmi2logger;
  FMI2ME->FMICallbackFunctions.allocateMemory = calloc;
  FMI2ME->FMICallbackFunctions.freeMemory = free;
  FMI2ME->FMICallbackFunctions.componentEnvironment = FMI2ME->FMIImportInstance;
  /* Load the binary (dll/so) */
  status = fmi2_import_create_dllfmu(FMI2ME->FMIImportInstance, fmi2_import_get_fmu_kind(FMI2ME->FMIImportInstance), &FMI2ME->FMICallbackFunctions);
  if (status == jm_status_error) {
    FMI2ME->FMISolvingMode = fmi2_none_mode;
    ModelicaFormatError("Loading of FMU dynamic link library failed with status : %s\n", jm_log_level_to_string(status));
    return 0;
  }
  FMI2ME->FMIInstanceName = (char*) malloc(strlen(instanceName)+1);
  strcpy(FMI2ME->FMIInstanceName, instanceName);
  FMI2ME->FMIDebugLogging = debugLogging;
  instantiateModelStatus = fmi2_import_instantiate(FMI2ME->FMIImportInstance, FMI2ME->FMIInstanceName, fmi2_model_exchange, NULL, fmi2_false);
  if (instantiateModelStatus == jm_status_error) {
    FMI2ME->FMISolvingMode = fmi2_none_mode;
    ModelicaFormatError("fmi2InstantiateModel failed with status : %s\n", jm_log_level_to_string(instantiateModelStatus));
    return 0;
  }
  /* Only call fmi2SetDebugLogging if debugLogging is true */
  if (FMI2ME->FMIDebugLogging) {
    int i;
    size_t categoriesSize = 0;
    fmi2_status_t debugLoggingStatus;
    fmi2_string_t *categories;
    /* Read the log categories size */
    categoriesSize = fmi2_import_get_log_categories_num(FMI2ME->FMIImportInstance);
    categories = (fmi2_string_t*)malloc(categoriesSize*sizeof(fmi2_string_t));
    for (i = 0 ; i < categoriesSize ; i++) {
      categories[i] = fmi2_import_get_log_category(FMI2ME->FMIImportInstance, i);
    }
    debugLoggingStatus = fmi2_import_set_debug_logging(FMI2ME->FMIImportInstance, FMI2ME->FMIDebugLogging, categoriesSize, categories);
    if (debugLoggingStatus != fmi2_status_ok && debugLoggingStatus != fmi2_status_warning) {
      ModelicaFormatMessage("fmi2SetDebugLogging failed with status : %s\n", fmi1_status_to_string(debugLoggingStatus));
    }
  }
  FMI2ME->FMIToleranceControlled = fmi2_true;
  FMI2ME->FMIRelativeTolerance = 0.001;
  FMI2ME->FMIEventInfo = malloc(sizeof(fmi2_event_info_t));
  FMI2ME->FMISolvingMode = fmi2_instantiated_mode;
  return FMI2ME;
}
Beispiel #7
0
int acadoVFPrintf ( FILE * stream, const char * format, va_list arg )
{
#ifdef __MATLAB__
#ifdef WIN32
	if(stream==stdout)   // workaround
#else
  if(stream==stdout || stream==stderr)   // workaround
#endif
	    {
	      const int buffersize = 256;
	      char buffer[buffersize];

	      // check length of buffer
	      int count = vsnprintf(NULL, 0, format, arg );

	      if(count < buffersize)
		{
		  // if string fits in buffer
		  vsprintf(buffer,format,arg);
		  mexPrintf( "%s", buffer );
		}
	      else
		{
		  // else dynamically allocate a larger buffer
		  char *newbuffer = new char[count];
		  vsprintf( newbuffer, format, arg );
		  mexPrintf( "%s", newbuffer );
		  delete [] newbuffer;
		}
	      return count;
	    }
	  else // not stdout or stderr -> really print to file
	    {
	      return vfprintf( stream, format, arg );
	    }
#elif defined __MODELICA__
  if(stream==stdout || stream==stderr)   // workaround
    {
      const int buffersize = 256;
      char buffer[buffersize];

      // check length of buffer
      int count = vsnprintf(NULL, 0, format, arg );

      // if string fits in buffer
      if(count < buffersize)
	{
	  vsprintf(buffer,format,arg);
	  ModelicaFormatMessage("%s",buffer);
	}
      else // dynamically allocate a larger buffer
	{
	  char *newbuffer = new char[count];
	  vsprintf( newbuffer, format, arg );
	  ModelicaFormatMessage("%s",newbuffer);
	  delete [] newbuffer;
	}
      return count;
    }
  else // not stdout or stderr -> really print to file
    {
      return vfprintf( stream, format, arg );
    }

#else
  return vfprintf( stream, format, arg );
#endif

}