Skip to content

Clarify/Allow use of ExternalObjects in equations #1669

@modelica-trac-importer

Description

@modelica-trac-importer

Reported by thiele on 26 Feb 2015 17:38 UTC

Basic Idea

Using external objects in equations is useful and works in some tools, but the rules in "12.9.7 External Objects" in MLS 3.3 Revision 1 are rather restrictive and might not allow that.

Allowing to use external object in (connect) equations allows to propagate external object through a model using a natural data-flow style. An example usage for this feature is the packaging system found in the Modelica_DeviceDrivers library (https://github.com/modelica/Modelica_DeviceDrivers).

Image

The important point is that this requires that external objects can be used in (connect) equations.

Alternatively, one might try to use the inner/outer mechanism to implement something similar. However, this is not so easy if one wants to support several instances of the packaging system and if parameters to the external object constructor depend on parameters/static equations that are not readily available at the place there the inner is defined.

Illustrative Modelica Code Example

The following Modelica code shows the principle of this implementation (using inline C code to have everything at one place). The model to simulate is TestExtObj. TestExtObj instantiates a source block in which an external object is created. In addition it instantiates block ExtObjThrough that takes an external object as input, modifies its state, and returns it as an output. The various dummy variables introduce dependencies that ensure the correct ordering of the function calls.

package ExtObjEqn "Using external objects in (connect) equations"
  class ExtObj
   extends ExternalObject;

  encapsulated function constructor "Claim the memory"
    import ExtObjEqn.ExtObj;
    input Integer n = 1;
    output ExtObj extObj;
    external "C" extObj=  extObjConstructor(n)
    annotation(Include = "
  #ifndef EXTOBJTEST_C_
  #include <stdlib.h>
  
  void* extObjConstructor(int n) {
    double* payload = calloc(n, sizeof(double));
    return (void*) payload;
  }
  
  void extObjDestructor(void* p) {
    double* payload = (double*) p;
    free(payload);
  }
  
  double addPayload(void* p, double newValue, double dummy) {
    double* payload = (double*) p;
    *payload = newValue;
    return dummy;
  }
  
  double getPayload(void* p, double dummy) {
    double* payload = (double*) p;
    double value = *payload;
    return value;
  }
  
  #endif
  ");
  end constructor;

  encapsulated function destructor "Free memory"
    import ExtObjEqn.ExtObj;
    input ExtObj extObj;
    external "C" extObjDestructor(extObj);
  end destructor;

  end ExtObj;

  function addPayload
    input ExtObj extObj;
    input Real newValue;
    input Real dummyIn;
    output Real dummyOut;
    external "C" dummyOut=  addPayload(extObj, newValue, dummyIn);
  end addPayload;

  function getPayload
    input ExtObj extObj;
    input Real dummy;
    output Real payload;
    external "C" payload=  getPayload(extObj,dummy);
  end getPayload;

  connector ExtInCon
     input ExtObj eo;
     input Real dummy;
  end ExtInCon;

  connector ExtOutCon
     output ExtObj eo;
     output Real dummy;
  end ExtOutCon;

  block ExtObjSource
    ExtOutCon extOutCon(eo=ExtObj(12), dummy=time);
  end ExtObjSource;

  block ExtObjThrough
    ExtInCon extInCon;
    ExtOutCon extOutCon;
  equation
    extOutCon.dummy =addPayload(
      extInCon.eo,
      2,
      extInCon.dummy);
    extOutCon.eo = extInCon.eo;
  end ExtObjThrough;

  model TestExtObj
    ExtObjSource ceos;
    ExtObjThrough ceot;
    output Real payload;
  equation
    connect(ceos.extOutCon,ceot.extInCon);
    payload = getPayload(ceot.extOutCon.eo, ceot.extOutCon.dummy);
  end TestExtObj;
end ExtObjEqn;

Migrated-From: https://trac.modelica.org/Modelica/ticket/1669

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions