Evaluating variables at a specific time in Modelica

前端 未结 2 1755
耶瑟儿~
耶瑟儿~ 2021-01-15 23:29

I have a selection of variables in my Modelica simulation (using Dymola) which is running good. Now I want to plot the behaviour of certain of these variables, which are num

相关标签:
2条回答
  • 2021-01-15 23:42

    You can use DymBrowse.m to load variables from the resultfile to Matlab. It should be available in \Program Files\Dymola xx\Mfiles\dymtools. Add the directory \Mfiles... to your matlab paths.

    0 讨论(0)
  • 2021-01-15 23:57

    Maybe I'm misunderstanding your question, but I suspect there is a simple answer here. It sounds like you have an array and you want to populate that array with the values of a specific variable at a specific time and then plot the array. So, for example, let's say you had a variable x and you want to record the time that x crossed certain threshholds. A simple model like this would suffice:

    model RecordVariables
      Real x;
      Real times[10];
    initial equation 
      x = 11;
    equation 
      der(x) = -x;
      when x<=10.0 then
        times[1] = time;
      end when;
      when x<=9.0 then
         times[2] = time;
      end when;
      when x<=8.0 then
        times[3] = time;
      end when;
      when x<=7.0 then
         times[4] = time;
      end when;
      when x<=6.0 then
         times[5] = time;
      end when;
      when x<=5.0 then
         times[6] = time;
      end when;
      when x<=4.0 then
        times[7] = time;
      end when;
      when x<=3.0 then
         times[8] = time;
      end when;
      when x<=2.0 then
        times[9] = time;
      end when;
      when x<=1.0 then
         times[10] = time;
      end when;
    end RecordVariables;
    

    Of course, writing out all those when clauses is pretty tedious. So we can actually create a more compact version like this:

    model RecordVariables2
      Real x;
      Real times[5];
      Integer i;
      Real next_level;
    initial equation 
      next_level = 10.0;
      x = 11;
      i = 1;
    algorithm 
      der(x) :=-x;
      when x<=pre(next_level) then
        times[i] :=time;
        if i<size(times,1) then
          i :=pre(i) + 1;
          next_level :=next_level - 1.0;
        end if;
      end when;
    end RecordVariables2;
    

    A few comments about this approach. First, note the use of the pre operator. This is necessary to distinguish between the values of the variables i and next_level both before and after the events generated by the when clause. Second, you will note the if statement within the when clause that prevents the index i from getting large enough to "overflow" the times buffer. This allows you to set times to have whatever size you want and never risk such an overflow. Note, however, that it is entirely possible in this model to make times so large that some values will never be filled in.

    I hope this helps.

    0 讨论(0)
提交回复
热议问题