Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values for function input variables #1346

Closed
modelica-trac-importer opened this issue Nov 4, 2018 · 10 comments
Closed

Default values for function input variables #1346

modelica-trac-importer opened this issue Nov 4, 2018 · 10 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@modelica-trac-importer
Copy link
Collaborator

Reported by stefanv on 8 Nov 2013 18:35 UTC
Is the default value of a function input variable allowed to be an expression referring to other input variables? Like this:

function F
    input Real x;
    input Real y = 2*x;
    output Real z;
algorithm
    z := x + y;
end F;

I would of course say yes. But can the default value of a function's input variable depend on a later input variable? Like this:

function G
    input Real x;
    input Real y = 2*z;
    input Real z = x;
    output Real z;
algorithm
    z := x + y + z;
end G;

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

@modelica-trac-importer
Copy link
Collaborator Author

Comment by stefanv on 8 Nov 2013 18:36 UTC
EDIT: output "z" in function "G" above should be some other name, since I added an input named "z".

@modelica-trac-importer
Copy link
Collaborator Author

Comment by sjoelund.se on 8 Nov 2013 18:43 UTC
I would say if the first is allowed the of course the second is allowed to (just sort the dependencies and if there is a cycle without any given input, give an error). But I would also say the first isn't good to allow either. There is a third one as well (the bindings would be solved at the same level I assume; before the algorithm section but inside the call):

function G
    input Real x;
    input Real y = 2*z;
    output Real o;
protected
    Real z = x;
algorithm
    o := x + y + z;
end G;

@modelica-trac-importer
Copy link
Collaborator Author

Comment by stefanv on 8 Nov 2013 20:40 UTC
Personally, I would be in favour of only the first being allowed. Unlike other variables, function input (and output) variables do have a defined order.

@modelica-trac-importer
Copy link
Collaborator Author

Comment by perost on 8 Nov 2013 22:53 UTC
Spec 3.2rev2, section 12.4.4, says the following:

A declaration assignment for a non-input component initializes the component to this expression at the start of every function invocation (before executing the algorithm section or calling the external function). These bindings must be executed in an order where a variable is not used before its declaration assignment has been executed; it is an error if no such order exists (i.e. the binding must be acyclic).

So the behaviour seems well-defined for everything that's not inputs. It seems to me like it would be reasonable to also include inputs with default values in this sorting.

@modelica-trac-importer
Copy link
Collaborator Author

Comment by hansolsson on 4 Dec 2013 11:07 UTC
Declaration assignments for function inputs may only depend on other function inputs, (and constants?), and not on local variables of the function.

For each function call the default values are evaluated in an order such that the input is not used before its declaration assignment has been executed, it is an error if no such order exists (i.e. the bindings must be acyclic).

Unclear: Is there still any functions in MSL not satisfying the first paragraph?
Should it include "constants" - or even parameters?

@modelica-trac-importer
Copy link
Collaborator Author

Comment by stefanv on 4 Dec 2013 11:21 UTC
I think it should allow constants and parameters. Scoping-wise, the following is perfectly legal, so I don't see why it should be disallowed:

model M
    constant Real K = 1;

    function F
        parameter Real P = 2;
        input Real x = K + P;
        output Real y;
    algorithm
        y := x^2;
    end F;
end M;

Note that this allows one to generate different versions of functions for different applications:

model M2 = M(F.P=3);

@modelica-trac-importer
Copy link
Collaborator Author

Comment by hansolsson on 14 Mar 2014 14:14 UTC
Gerd - just substitute the argument for that input (in the caller).
Hans:

function foo
  input Real x;
  input Real y=2*x;
  input Real z=3*y;
end foo;

Substitute until nothing left (acyclic dependencies).

Favor: 6
Against: 0
Abstain: 16

@modelica-trac-importer
Copy link
Collaborator Author

Comment by hansolsson on 14 Mar 2014 14:25 UTC
Gerd - additional comment. Can input depend on protected variables?

function foo
  input Real x;
  input Real y=3*z;
protected
  Real z=sin(x);
end foo;

Sven Erik: Hard to understand - should be able to understand interface of foo - without looking at internals.

Solution: Only allow input-defaults to depend on inputs.
Martin Sjölund: The input-default dependencies should be acyclic in themselves, not only for the specific calls.
Sven Erik: Cp. issue for parameter-bindings.
This is (#1320) - and the exception is not applicable since no annotation here.

Input defaults depending on inputs exist in MSL - e.g. Modelica.Math.Matrices.continuousLyapunov

@modelica-trac-importer
Copy link
Collaborator Author

Modified by hansolsson on 22 Mar 2014 10:25 UTC

@modelica-trac-importer
Copy link
Collaborator Author

Comment by hansolsson on 22 Mar 2014 10:25 UTC
Updated in r7444

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests