modelica / ModelicaSpecification

Specification of the Modelica Language
https://specification.modelica.org
Other
95 stars 41 forks source link

Is return 0 from an ExternalObject constructor an error? #3500

Open maltelenz opened 1 month ago

maltelenz commented 1 month ago

Is it an error that should stop simulation if an ExternalObject constructor returns 0?

I couldn't find any support for this interpretation in the specification as I read it, but it could be considered a sensible behavior.

Example:

model FunctionExternalObject
  class ExtObj
    extends ExternalObject;

    function constructor
      input Integer u;
      output ExtObj externalObject;
    external "C"
      externalObject = createExternalObjectX(u)
        annotation(
          Include = "static void* createExternalObjectX(int u) { return 0; }"
        );
    end constructor;

    function destructor
      input ExtObj externalObject;
    external "C"
      destroyExternalObjectX(externalObject)
        annotation(
          Include = "static void destroyExternalObjectX(void* p) {}"
        );
    end destructor;
  end ExtObj;

  parameter ExtObj e = ExtObj(1);
  annotation(experiment(StopTime = 10.0));
end FunctionExternalObject;

The issue touching closest to this is https://github.com/modelica/ModelicaSpecification/issues/1071 .

HansOlsson commented 1 month ago

I would agree that it should be an error(*) and that it isn't specified; so it should be added. The alternative would be that some wanted to use a null-ptr for a singleton external object without any "payload" - but that just seems wrong and easy to improve.

*: Technically it would just be an error - similar as calling ModelicaError not directly stopping the integration; as one could imagine that the external object is inside a function that is called during integration and it is possible to handle it by redoing the step, but that seems very esoteric.