fmiComponent fmiInstantiateSlave(fmiString  instanceName, fmiString  GUID,
            fmiString  fmuLocation, fmiString  mimeType, fmiReal timeout, fmiBoolean visible,
            fmiBoolean interactive, fmiCallbackFunctions functions, fmiBoolean loggingOn) {
    ModelInstance* component;

    // Perform checks.
    // FIXME: Boilerplate below is shared among all test FMUs. Consolidate to one file.
    // Logger callback is required.
    if (!functions.logger) {
        return NULL;
    }
    // Functions to allocate and free memory are required.
    if (!functions.allocateMemory || !functions.freeMemory) {
        functions.logger(NULL, instanceName, fmiError, "error",
                "fmiInstantiateSlave: Missing callback function: freeMemory");
        return NULL;
    }
    if (!instanceName || strlen(instanceName)==0) {
        functions.logger(NULL, instanceName, fmiError, "error",
                "fmiInstantiateSlave: Missing instance name.");
        return NULL;
    }
    if (strcmp(GUID, MODEL_GUID)) {
        functions.logger(NULL, instanceName, fmiError, "error",
                "fmiInstantiateSlave: Wrong GUID %s. Expected %s.", GUID, MODEL_GUID);
        return NULL;
    }
    component = (ModelInstance *)functions.allocateMemory(1, sizeof(ModelInstance));
    // Allocate memory for the pointer holding the values.
    component->r = functions.allocateMemory(4, sizeof(fmiReal));
    component->r[0] = 0.0;
    component->r[1] = 1.0;
    component->r[2] = -1.0;   // Last successful firing time.
    component->r[3] = 0.0;    // Flag counting firings at breakpoints.
    component->functions = functions;
    component->instanceName = instanceName;

    functions.logger(component, instanceName, fmiOK, "message",
                     "Invoked fmiInstantiateSlave for instance %s.", instanceName);

    return component;
}
示例#2
0
// fname is fmiInstantiateModel or fmiInstantiateSlave
static fmiComponent instantiateModel(char* fname, fmiString instanceName, fmiString GUID,
        fmiCallbackFunctions functions, fmiBoolean loggingOn) {
    ModelInstance* comp;
    if (!functions.logger)
        return NULL; // we cannot even log this problem
    if (!instanceName || strlen(instanceName)==0) {
        functions.logger(NULL, "?", fmiError, "error",
                "%s: Missing instance name.", fname);
        return NULL;
    }
    if (!GUID || strlen(GUID)==0) {
        functions.logger(NULL, instanceName, fmiError, "error",
                "%s: Missing GUID.", fname);
        return NULL;
    }
    if (!functions.allocateMemory || !functions.freeMemory){
        functions.logger(NULL, instanceName, fmiError, "error",
                "%s: Missing callback function.", fname);
        return NULL;
    }
    if (strcmp(GUID, MODEL_GUID)) {
        functions.logger(NULL, instanceName, fmiError, "error",
                "%s: Wrong GUID %s. Expected %s.", fname, GUID, MODEL_GUID);
        return NULL;
    }
    comp = (ModelInstance *)functions.allocateMemory(1, sizeof(ModelInstance));
    if (comp) {
        comp->r = functions.allocateMemory(NUMBER_OF_REALS,    sizeof(fmiReal));
        comp->i = functions.allocateMemory(NUMBER_OF_INTEGERS, sizeof(fmiInteger));
        comp->b = functions.allocateMemory(NUMBER_OF_BOOLEANS, sizeof(fmiBoolean));
        comp->s = functions.allocateMemory(NUMBER_OF_STRINGS,  sizeof(fmiString));
        comp->isPositive = functions.allocateMemory(NUMBER_OF_EVENT_INDICATORS, sizeof(fmiBoolean));
        comp->instanceName = functions.allocateMemory(1 + strlen(instanceName), sizeof(char));
        comp->GUID = functions.allocateMemory(1 + strlen(GUID), sizeof(char));
    }
    if (!comp || !comp->r || !comp->i || !comp->b || !comp->s || !comp->isPositive
        || !comp->instanceName || !comp->GUID) {
        functions.logger(NULL, instanceName, fmiError, "error",
                "%s: Out of memory.", fname);
        return NULL;
    }
    if (loggingOn) functions.logger(NULL, instanceName, fmiOK, "log",
            "%s: GUID=%s", fname, GUID);

    strcpy((char *)comp->instanceName, (char *)instanceName);
    strcpy((char *)comp->GUID, (char *)GUID);
    comp->functions = functions;
    comp->loggingOn = loggingOn;
    comp->state = modelInstantiated;
    setStartValues(comp); // to be implemented by the includer of this file
    return comp;
}
示例#3
0
/***************************************************
Functions for FMI for Model Exchange
****************************************************/
fmiComponent fmiInstantiateModel(fmiString instanceName, fmiString GUID, fmiCallbackFunctions functions, fmiBoolean loggingOn)
{
  ModelInstance* comp;
  if (!functions.logger)
    return NULL;
  if (!functions.allocateMemory || !functions.freeMemory){
    functions.logger(NULL, instanceName, fmiError, "error",
        "fmiInstantiateModel: Missing callback function.");
    return NULL;
  }
  if (!instanceName || strlen(instanceName)==0) {
    functions.logger(NULL, instanceName, fmiWarning, "Warning",
        "fmiInstantiateModel: Missing instance name.");
  }
  if (strcmp(GUID, MODEL_GUID) != 0) {
    functions.logger(NULL, instanceName, fmiError, "error",
        "fmiInstantiateModel: Wrong GUID %s. Expected %s.", GUID, MODEL_GUID);
    return NULL;
  }
  comp = (ModelInstance *)functions.allocateMemory(1, sizeof(ModelInstance));
  if (comp) {
    DATA* fmudata = NULL;
	MODEL_DATA* modelData = NULL;
	SIMULATION_INFO* simInfo = NULL;
	threadData_t *threadData = NULL;

    comp->functions = functions;
    comp->loggingOn = loggingOn;
    comp->state = modelInstantiated;
    comp->instanceName = functions.allocateMemory(1 + strlen(instanceName), sizeof(char));
    comp->GUID = functions.allocateMemory(1 + strlen(GUID), sizeof(char));
    /* Cannot use functions.allocateMemory since the pointer might not be stored on the stack of the parent */
    fmudata = (DATA *)functions.allocateMemory(1, sizeof(DATA));
    modelData = (MODEL_DATA *)functions.allocateMemory(1, sizeof(MODEL_DATA));
    simInfo = (SIMULATION_INFO *)functions.allocateMemory(1, sizeof(SIMULATION_INFO));
    fmudata->modelData = modelData;
    fmudata->simulationInfo = simInfo;


    threadData = (threadData_t *)functions.allocateMemory(1, sizeof(threadData_t));
    memset(threadData, 0, sizeof(threadData_t));
    /*
    pthread_key_create(&fmu1_thread_data_key,NULL);
    pthread_setspecific(fmu1_thread_data_key, threadData);
    */

    comp->threadData = threadData;
    comp->fmuData = fmudata;

    if (!comp->fmuData) {
      functions.logger(NULL, instanceName, fmiError, "error",
          "fmiInstantiateModel: Error: Could not initialize the global data structure file.");
      return NULL;
    }
  }
  if (!comp || !comp->instanceName || !comp->GUID) {
    functions.logger(NULL, instanceName, fmiError, "error", "fmiInstantiateModel: Out of memory.");
    return NULL;
  }
  /* intialize modelData */
  fmu1_model_interface_setupDataStruc(comp->fmuData);
  useStream[LOG_STDOUT] = 1;
  useStream[LOG_ASSERT] = 1;
  initializeDataStruc(comp->fmuData, comp->threadData);
  /* setup model data with default start data */
  setDefaultStartValues(comp);
  setAllVarsToStart(comp->fmuData);
  setAllParamsToStart(comp->fmuData);
  comp->fmuData->callback->read_input_fmu(comp->fmuData->modelData, comp->fmuData->simulationInfo);
  modelInfoInit(&(comp->fmuData->modelData->modelDataXml));

  strcpy((char*)comp->instanceName, (const char*)instanceName);
  strcpy((char*)comp->GUID, (const char*)GUID);

  /* read input vars */
  //input_function(comp->fmuData);
  /* initial sample and delay before initial the system */
  comp->fmuData->callback->callExternalObjectConstructors(comp->fmuData, comp->threadData);
  /* allocate memory for non-linear system solvers */
  initializeNonlinearSystems(comp->fmuData, comp->threadData);
  /* allocate memory for non-linear system solvers */
  initializeLinearSystems(comp->fmuData, comp->threadData);
  /* allocate memory for mixed system solvers */
  initializeMixedSystems(comp->fmuData, comp->threadData);
  /* allocate memory for state selection */
  initializeStateSetJacobians(comp->fmuData, comp->threadData);

  return comp;
}
/*
 Inherits from : -
======================================================================*/
fmiComponent fmiInstantiateModel(fmiString instanceName,
                                 fmiString GUID,
                                 fmiCallbackFunctions memory,
                                 fmiBoolean loggingOn)
{
   /* Declaration of internal variables */
   fmiComponentStructure* component; /* Internal data structure */
   init_struct dim_values; /* Structure containing size of variables */
   size_t i; /* Loop variable */

   size_t dim_string[] = {7};
   dim_values.nb_boolSVar = 0;
   dim_values.nb_intSVar = 11;
   dim_values.nb_realSVar = 29;
   dim_values.nb_stringSVar = 1;
   dim_values.nb_state_var = 2;
   dim_values.dim_string = dim_string;
   dim_values.GUID = GUID;
   dim_values.ref_GUID = "5922B96112515EF2";
   dim_values.ModelName = "SHUNTDCMOTORWITHSTARTINGRESISTOR";
   dim_values.memory = memory;
   dim_values.loggingOn = loggingOn;
   dim_values.nb_integer_stores = 0;
   dim_values.nb_real_stores = 0;
   dim_values.nb_pointer_stores = 1;
   component = NULL; /* Initialize pointer to fmiComponentStructure */
   if ((component = commonInit(&dim_values)) == 0) /* Initiate variables */
   {
      if (loggingOn)
      {
         memory.logger(component, instanceName, fmiFatal, "initialization", "Unable to initialize component structure.");
      }
      return NULL;
   }

   /* State variables */
   component->idx_of_state_var[0] = 7;
   component->deriv_idx[7] = 0;
   component->idx_of_state_var[1] = 8;
   component->deriv_idx[8] = 1;

   /* Initialize var types */
   component->real_var_type = (FMITransition*)memory.allocateMemory(29,sizeof(FMITransition));
   for (i = 0 ; i < 29 ; ++i)
   {
      component->real_var_type[i] = T_unknown;
   }
   component->integer_var_type = (FMITransition*)memory.allocateMemory(19,sizeof(FMITransition));
   for (i = 0 ; i < 19 ; ++i)
   {
      component->integer_var_type[i] = T_unknown;
   }
   component->boolean_var_type = NULL;
   component->string_var_type = (FMITransition*)memory.allocateMemory(1,sizeof(FMITransition));
   for (i = 0 ; i < 1 ; ++i)
   {
      component->string_var_type[i] = T_unknown;
   }
   component->real_var_type[0] = T_fmiSetINI;
   component->real_var_type[1] = T_fmiSetINI;
   component->real_var_type[2] = T_fmiSetINI;
   component->real_var_type[3] = T_fmiSetINI;
   component->real_var_type[4] = T_fmiSetINI;
   component->real_var_type[5] = T_fmiSetINI;
   component->real_var_type[6] = T_fmiSetINI;
   component->real_var_type[7] = T_fmiSetINI;
   component->real_var_type[8] = T_fmiSetINI;
   component->integer_var_type[4] = T_fmiSetINI;
   component->integer_var_type[5] = T_fmiSetINI;
   component->integer_var_type[6] = T_fmiSetINI;
   component->string_var_type[0] = T_fmiSetINI;
   component->integer_var_type[0] = T_fmiSetINI;
   component->integer_var_type[1] = T_fmiSetINI;
   component->integer_var_type[2] = T_fmiSetINI;
   component->integer_var_type[3] = T_fmiSetINI;
   component->integer_var_type[7] = T_fmiSetINI;
   component->integer_var_type[8] = T_fmiSetINI;
   component->integer_var_type[9] = T_fmiSetINI;
   component->integer_var_type[10] = T_fmiSetINI;

   /* Initialize integer variables to their start values */
   component->intSVar[0] = 1; /* x3 (event iteration type) */
   component->intSVar[1] = 1; /* x6 (linear system solver logging) */
   component->intSVar[2] = 3; /* x5 (logged events) */
   component->intSVar[3] = 1; /* systemsettingssteploglevel (logged submodel calls) */
   component->intSVar[4] = 10; /* x2 (maximum number of event iterations) */
   component->intSVar[5] = 10; /* x14 (maximum number of mixed system solver iterations) */
   component->intSVar[6] = 100; /* x11 (maximum number of nonlinear system solver iterations) */
   component->intSVar[7] = 1; /* x15 (mixed system solver logging) */
   component->intSVar[8] = 2; /* systemsettingsevalloglevel (model evaluation logging) */
   component->intSVar[9] = 1; /* x13 (nonlinear system solver logging) */
   component->intSVar[10] = 1; /* x12 (use backtraking in nonlinear system solver) */

   /* Initialize enumeration variables to their start values */
   component->intSVar[0] = 1; /* x3 (event iteration type) */
   component->intSVar[1] = 1; /* x6 (linear system solver logging) */
   component->intSVar[2] = 3; /* x5 (logged events) */
   component->intSVar[3] = 1; /* systemsettingssteploglevel (logged submodel calls) */
   component->intSVar[7] = 1; /* x15 (mixed system solver logging) */
   component->intSVar[8] = 2; /* systemsettingsevalloglevel (model evaluation logging) */
   component->intSVar[9] = 1; /* x13 (nonlinear system solver logging) */
   component->intSVar[10] = 1; /* x12 (use backtraking in nonlinear system solver) */

   /* Initialize real variables to their start values */
   component->realSVar[0] =    0.00000000000000e+000; /* ifieldstart (ifield - fixed start value) */
   component->realSVar[1] =    1.00000000000000e-005; /* x7 (nonlinear system solver function evaluation accuracy) */
   component->realSVar[2] =    1.00000000000000e-005; /* x10 (nonlinear system solver gradient tolerance) */
   component->realSVar[3] =    1.00000000000000e-014; /* x9 (nonlinear system solver step tolerance) */
   component->realSVar[4] =    1.00000000000000e-012; /* x8 (nonlinear system solver value tolerance) */
   component->realSVar[5] =    0.00000000000000e+000; /* speedstart (speed - fixed start value) */
   component->realSVar[6] =    1.00000000000000e-015; /* x4 (zero crossing hysteresis) */
   component->realSVar[7] =    0.00000000000000e+000; /* ifield (ifield - Field current) */
   component->realSVar[8] =    0.00000000000000e+000; /* speed (speed - Rotary speed) */

   /* Initialize string variables to their start values */
   assignString(component, 0, "amo.log"); /* systemsettingslogfilename (log file name) */
   component->verbose_level = VERBOSE_LEVEL;
   component->instanceName = component->memory.allocateMemory(strlen(instanceName)+1, sizeof(char));
   strcpy(component->instanceName, instanceName);
   if ((component->status == fmiFatal) || (component->status == fmiError)) /* Update status if necessary */
   {
      AME_status = fmiError;
   }
   return component;
}