- Index
- » Developer
- » OpenModelica development
- » Question on how to compare two Integers
Question on how to compare two Integers
Question on how to compare two Integers
Hi, all
I am currently trying to dump some intermediate information about a modelica model using OpenModelica. I want to print equations in the order of BLT and which other equations it depends on. To do that, I need to combine the information from both BLT and incidence .
For example if the BLT is
{2}
{1}
{3}
And incidence is
1: 2
2:
3: 1 2
I want to print out
Block 1: eqn_2 depends on _
Block 2: eqn_1 depends on eqn_2
Block 3: eqn_3 depends on eqn_2, eqn_1
But when I try to look up the incidence matrix to find corresponding dependences for a specific equation, I dont know how to make comparison. I checked the users guide and it seems to suggest using " if ... then.. else". But I cannot use if statement because rmlc will tell me every time"
"/usr/bin/rml" -Eplain -ftrace DAELow.mo
Internal error: FOLToCPS.lookupVar: $11935
Error: Bug
Any kind of help is greatly appreciated. Thanks.
Yun
Re: Question on how to compare two Integers
if ... then ... else ... does not exist in RML MetaModelica. You can use Util.if_(cond, if_exp, else_exp), but it always evaluates both expressions.
- sjoelund.se
- 1700 Posts
Re: Question on how to compare two Integers
ha, it works like a charm! Thanks. I am pretty new to MetaModelica. Thanks a lot for helping me out on this.
Is there any documentation about the grammar of RML? I checked the website but I dont know which one has a list of all statement available to use.
Thanks !
Re: Question on how to compare two Integers
OpenModelicaMetaProgramming.pdf is the only document I know of. It covers the language, but the RML implementation is sometimes a bit different. For example the document states that listGet() is indexed from 1, but in RML MetaModelica we instead have listNth(), which is indexed from 0.
The appendix has both grammar and the builtin functions.
- sjoelund.se
- 1700 Posts
Re: Question on how to compare two Integers
There is an OpenModelica development course here:
http://www.ida.liu.se/~pelab/modelica/O … ersCourse/
You can look at the slides and do the exercises to get more into it.
Cheers,
Adrian Pop/
- adrpo
- 885 Posts
Re: Question on how to compare two Integers
And if you have trouble getting the exercises running, updated ones exist in subversion (trunk/testsuite/meta/MetaModelicaDev/).
- sjoelund.se
- 1700 Posts
Re: Question on how to compare two Integers
Thank you all so much for the quick reply ! I have one more question regarding value comparison. Thanks in advance.
Here is a function I wrote to traverse the incidence matrix and find the list of variables that a specific equation depends on. inInteger is the equation number, and lst is the incidence matrix. counter is for checking whether this is the dependence we are interested. The output should be a list of integer(variable number).
protected function findDep
input Integer inInteger;
input list<list<Integer>> lst;
input Integer counter;
output list<Integer> dep_list;
algorithm
dep_list :=
matchcontinue(inInteger, lst, counter)
local
Integer eqn;
list<list<Integer>> lst_1;
list<Integer> dep;
case (_, {}, _) then {};
case (_, (dep::lst_1), _)
equation
print(" in = ");
print(intString(inInteger));
print(" counter = ");
print(intString(counter));
print("\t");
dep_list = Util.if_((inInteger ==counter), dep, findDep(inInteger, lst_1, counter+1));
then dep_list;
end matchcontinue;
end findDep;
The problem is, I dont know if Util.if_ is working as I expected, because the output is :
in = 10 counter = 1
in = 10 counter = 2
in = 10 counter = 3
in = 10 counter = 4
in = 10 counter = 5
in = 10 counter = 6
in = 10 counter = 7
in = 10 counter = 8
in = 10 counter = 9
in = 10 counter = 10
in = 10 counter = 11
in = 10 counter = 12
....
If the conditional branch really works, shouldn't it stop recursion when counter=10? I may have made some stupid mistake here since I am totally new to this. Please please let me know if I am doing anything wrong.
Thanks for your time !
Yun
Re: Question on how to compare two Integers
No. That's what I was referring to earlier. It will always evaluate both branches of the if-expression, which means you can't use it to break loops (it will try the other branch anyway).
Instead, start two cases like this:
Code:
case (_, (dep::lst_1), _)
equation
true = inInteger == counter;
Code:
case (_, (dep::lst_1), _)
equation
false = inInteger == counter;
- sjoelund.se
- 1700 Posts
Re: Question on how to compare two Integers
I see. That explains a lot. Sorry I didn't understand what you meant by "evaluating both expressions".
In this case, if I want to break loops, shall I write program like:
case (_, (dep::lst_1), _)
equation
true = inInteger == counter;
dep_list = Util.if_(true, dep, {});
then dep_list;
case (_, (dep::lst_1), _)
equation
false = inInteger == counter;
dep_list = Util.if_(false, {}, findDep(inInteger, lst_1, counter+1));
then dep_list;
Re: Question on how to compare two Integers
Yes. Since you cannot match with guards in MetaModelica, you need to break the loop like that.
- sjoelund.se
- 1700 Posts
Re: Question on how to compare two Integers
Thanks, I dont know if I am doing anything else wrong: after I applied the code above, the program always goes to the "false"branch in the first case. The reason is that the fist case is taken, then the If in the fist case is evaluated. Given that the condition is false, the function always returns {}.....
In another word, instead of breaking the loop , the code above breaks the loop in its first iteration always because the first If is always evaluated before the second.
I am bit confused at this point...... How do I break the loop when inIntger==counter?
Here is the output I got:
{10}
in = 10 counter = 1
finished
{8}
in = 8 counter = 1
finished
Re: Question on how to compare two Integers
Code:
matchcontinue(inInteger, lst, counter)
local
Integer eqn;
list<list<Integer>> lst_1;
list<Integer> dep;
case (_, {}, _) then {};
case (_, (dep::lst_1), _)
equation
true = inInteger == counter;
print(...);
then dep;
case (_, (dep::lst_1), _)
equation
false = inInteger == counter;
print(...);
then findDep(inInteger, lst_1, counter+1));
end matchcontinue;
- sjoelund.se
- 1700 Posts
Re: Question on how to compare two Integers
Hi
There is actually a document for RML
look under
http://www.ida.liu.se/~pelab/rml/rml-ft … 060314.pdf
or
http://www.ida.liu.se/~pelab/rml/
Best Regards
Mohsen
- Index
- » Developer
- » OpenModelica development
- » Question on how to compare two Integers