Dymola giving different solution compared to OpenModelica - modelica

Below is the code snippet I have tried running in Dymola and OpenModelica to get varying results.
model Test
Real a[2,2](each start=0);
Real b[2,2];
algorithm
b := a;
a := [1,2; 2,4];
if time > 10 then
a := b;
end if;
end Test;
Dymola(2022) is producing the following output/plot for variable b:
whereas, OM (v1.19.2) is giving the following output for variable b:
The experiment is only ran up to 1 second so my expectation is that the if block doesn't get executed.
My intention is to get the value of b={0,0,0,0} according to the sequence of its declaration than b={1,2,2,4}.
I was expecting the code to run in the sequence in which it is declared such that b takes the start value of a until the if block gets executed. a values are also different in Dymola and OM runs:
Dymola:
OM:

Related

Import Fortran unformatted binary

I have an unformatted binary file generated using the Compaq Visual Fortran compiler (big endian).
Here's what the little bit of documentation states about it:
The binary file is written in a general format consisting of data arrays, headed by a descriptor record:
An 8-character keyword which identifies the data in the block.
A 4-byte signed integer defining the number of elements in the block.
A 4-character keyword defining the type of data. (INTE, REAL, LOGI, DOUB, or CHAR)
The header items are read in as a single record. The data follows the descriptor on a new record. Numerical arrays are divided into block of up to 1000 items. The physical record size is the same as the block size.
Attempts to read such data
module modbin
type rectype
character(len=8)::key
integer::data_count
character(len=4)::data_type
logical::is_int
integer, allocatable:: idata(:)
real(kind=8), allocatable::rdata(:)
end type
contains
subroutine rec_read(in_file, out_rec)
integer, intent(in):: in_file
type (rectype), intent(inout):: out_rec
!
! You need to play around with this figure. It may not be
! entirely accurate - 1000 seems to work, 1024 does not
integer, parameter:: bsize = 1000
integer:: bb, ii, iimax
! read the header
out_rec%data_count = 0
out_rec%data_type = ' '
read(in_file, end = 20) out_rec%key, out_rec%data_count,
out_rec%data_type
! what type is it?
select case (out_rec%data_type)
case ('INTE')
out_rec%is_int = .true.
allocate(out_rec%idata(out_rec%data_count))
case ('DOUB')
out_rec%is_int = .false.
allocate(out_rec%rdata(out_rec%data_count))
end select
! read the data in blocks of bsize
bb = 1
do while (bb .lt. out_rec%data_count)
iimax = bb + bsize - 1
if (iimax .gt. out_rec%data_count) iimax = out_rec%data_count
if (out_rec%is_int) then
read(in_file) (out_rec%idata(ii), ii = bb, iimax)
else
read(in_file) (out_rec%rdata(ii), ii = bb, iimax)
end if
bb = iimax + 1
end do
20 continue
end subroutine rec_read
subroutine rec_print(in_recnum, in_rec)
integer, intent(in):: in_recnum
type (rectype), intent(in):: in_rec
print *, in_recnum, in_rec%key, in_rec%data_count, in_rec%data_type
! print out data
open(unit=12, file='reader.data' , status='old')
write(12,*)key
!write(*,'(i5')GEOMINDX
!write(*,'(i5')ID_BEG
!write(*,'(i5')ID_END
!write(*,'(i5')ID_CELL
!write(*,'(i5')TIME_BEG
!write(*,'(i5')SWAT
!format('i5')
!end do
close(12)
end subroutine rec_print
end module modbin
program main
use modbin
integer, parameter:: infile=11
! fixed size for now - should really be allocatable
integer, parameter:: rrmax = 500
type (rectype):: rec(rrmax)
integer:: rr, rlast
open(unit=infile, file='TEST1603.SLN0001', form='UNFORMATTED',
status='OLD', convert='BIG_ENDIAN')
rlast = 0
do rr = 1, rrmax
call rec_read(infile, rec(rr))
if (rec(rr)%data_type .eq. ' ') exit
rlast = rr
call rec_print(rr, rec(rr))
end do
close(infile)
end program main
This code compiles and runs smoothly showing
and produces no errors but this is written in the output file
shows me no useful numerical values
The file in question is available here
And the right WRITE statement should produce a file like this one here
Is my WRITE STATEMENT to output this file type wrong? , and if so, what is the best way?
thank you
The comments above are trying to direct you to one of (at least) two problems in your code. In the subroutine rec_print you have write(12,*)key where you meant to write write(12,*)in_rec%key (at least I think that's what you wanted.)
The other problem I spotted is that rec_print opens reader.data with status='old' and then closes it after writing key. (The use of old here suggests that the file already exists.) Each time rec_print is called, the file is opened, the first record is overwritten, and the file is closed. One solution to this would be to use status='unknown'. position='append', though it would be more efficient to open the file once in the main program and just let the subroutine write to it.
If I make these changes, I get in the data file:
INTEHEAD
GEOMETRY
GEOMINDX
ID_BEG
ID_END
ID_CELL
TIME_BEG
SWAT
A side-comment about CONVERT= and derived types: Your program isn't affected by this, but there are compiler differences with how reading a derived type record with CONVERT= is handled. I think gfortran converts each component according to its type, but I know that Intel Fortran doesn't convert reads (nor writes) of an entire derived type. You are reading individual components, which works in both compilers, so that's fine, but I thought it was worth mentioning.
If you're wondering why Intel Fortran does it this way, it's due to the VAX FORTRAN (where CONVERT= came from) heritage with STRUCTURE/RECORD and the possible use of UNION/MAP (not available in standard Fortran). With unions, there's no way to know how a particular component should be converted, so it just transfers the bytes. I had suggested to the Intel team that this could be relaxed if no UNIONs were present, but that I'm sure is very low priority.

how to stop at an internal Maple proc?

I want to see how Maple determined the type of an ODE. But can't set break point at internal Maple proc:
restart;
ode:=2*sqrt(a*diff(y(x),x))+x*diff(y(x),x)-y(x) = 0;
DEtools:-odeadvisor(ode);
#[[_homogeneous, `class G`], _Clairaut]
But when I do
stopat(DEtools:-odeadvisor);
it gives erorr
Error, invalid input: stopat expects its 1st argument, p, to be of type {`::`, name, string}, but received proc () option `Copyright (c) 1997 Waterloo Maple Inc. All rights reserved.`; `ODEtools/initialized` <> 'true' and `ODEtools/init`() <> 0; `ODEtools/odeadv`(args) end proc
Is it possible to set break point at DEtools:-odeadvisor? showstat does not show much. I thought it was possible to view all Maple library code (other than the builtin ones).
But may be some are not possible to see in addition to the builtin? How does one know then which one can see and which one can't see? How one see the code the DEtools:-odeadvisor?
Maple 2018.1
Try this,
restart;
ode:=2*sqrt(a*diff(y(x),x))+x*diff(y(x),x)-y(x) = 0:
showstat(DEtools[odeadvisor]);
stopat(`ODEtools/odeadv`);
DEtools:-odeadvisor(ode);

OpenModelica updating CombiTable input in real time

I have a model in which I am using a CombiTable1D to retrieve an external input from a .txt file. The file is generated by a Python script at the moment but in the final phase of the project, it is going to be updated each second. For now, simulation takes place without a problem as the .txt file is static. Just read the file and make simulation according to the data written there.
What I want to do is to simulate a model until a certain time, let's say 100s, and then make it wait until a real time event by which the .txt file is updated for the next external input values between 100-200. The simulation should continue by getting these new values for the next 100 seconds.
As I have already been working with OMPython, it is really practical for me to edit the .txt file using Python, let's say for each 10 seconds in real time. I can now simulate the model until the time instance that I define as the refreshing point of the external input. But I couldn't figure out how to keep the state of the simulation and make it read the file once again.
Actually, this sounds like a co-simulation scenario to me. Anyway, what you could do is to extend from CombiTable1D and have something like
block CombiTable1DWithUpdate
extends Modelica.Blocks.Tables.CombiTable1D(final tableOnFile=true);
algorithm
when sample(0, 10) then
readTableData(tableID, /* force update */ true, verboseRead);
end when;
end CombiTable1DWithUpdate;
In addition the the answer I accepted, I want to give another solution which is not so efficient. For a simple model with a capacitor and resistor, I made successfull tests but with more complex models, it doesn't function properly. In a Modelica script, realTimeSimulation.mos:
outputFile := "stepResult.mat";
simulation_step := 1;
start_time := 0;
stop_time := start_time+simulation_step;
loadFile("WhereverTheFileIs.mo");
buildModel(myTestModel);
system("myTestModel-override=startTime="+String(start_time)+",stopTime="+String(stop_time)+" -r="+outputFile);
will build the model and simulate the first step until the simulation time t=1s. Later on, using Python the text file is updated. The new data for the time between t=1s and t=2s are written to the text file where I am getting the input of the model. Then another step of the simulation is made for the time between t=1s and t=2s. As a loop, it continues like forever like: actualize the data, make new simulation for the new time interval. The trick is, reading the output file created at the end of each step and giving all the variable values as the new initial conditions to the simulation, using following script:
valueList := readSimulationResultVars(outputFile);
start_time := start_time+simulation_step;
stop_time := stop_time+simulation_step;
value := val(OpenModelica.Scripting.stringVariableName(valueList[1]),start_time,outputFile);',
variableString := valueList[1] + "=" + String(value);
for i in 2:size(valueList,1) loop
value := val(OpenModelica.Scripting.stringVariableName(valueList[i]),start_time,outputFile);
variableString := variableString + "," + valueList[i] + "=" + String(value);
end for;
system("myTestModel-override startTime="+String(start_time)+",stopTime="+String(stop_time)+",variableString+" -r="+outputFile);

How to reciprocate?

Actually I am coding a Matlab simulation where the AnchorID and SourceID will report to eachother. For example if I take an anchor 30 and source 50 it will collect all the agc values between these anchor and source and calculate rssi_dB and display them.Below mentioned is an example of anchor 30 and source id 50
Note: list of anchors ID's and source ID's are same. for e.g. 30 50 55 58 . These ID are both same for anchor and source.
function A30(BlinkSet)
for i=1:length(BlinkSet)
xAnchorID=30;
xSourceID=50;
a=BlinkSet{i}.AnchorID;
b=BlinkSet{i}.SourceID;
if xAnchorID==a && xSourceID==b
xagc=BlinkSet{i}.agc;
rssi_dB(i)=-(33+xagc*(89-33)/(29-1));
end
end
rssi_dB(rssi_dB==0)=[];
rssi_dBm=sum(rssi_dB(:))/length(rssi_dB);
disp([sprintf('The rssi value is %0.0f',rssi_dBm)]);
When I call the function in Matlab command window I get the rssi value of the above function.
Also my task is when I reciprocate the Anchor ID and source ID say Anchor as 50 and source as 30 like the function I have mentioned below I get an error which is mentioned after the function below.
function A50(BlinkSet)
for i=1:length(BlinkSet)
xAnchorID=50;
xSourceID=30;
a=BlinkSet{i}.AnchorID;
b=BlinkSet{i}.SourceID;
if xAnchorID==a && xSourceID==b
xagc=BlinkSet{i}.agc;
rssi_dB(i)=-(33+xagc*(89-33)/(29-1));
end
end
rssi_dB(rssi_dB==0)=[];
rssi_dBm=sum(rssi_dB(:))/length(rssi_dB);
disp([sprintf('The rssi value is %0.0f',rssi_dBm)]);
When I call this function I get an error as
??? Undefined function or variable "rssi_dB".
Error in ==> A50 at 14
rssi_dB(rssi_dB==0)=[];
Error in ==> main_reduced at 26
A50(BlinkSet);
In main function I have coded like this,
%A30(BlinkSet);
A50(BlinkSet);
Any help is highly appreciated.
In both of these functions, you only create the variable rssi_dB if execution enters the if statement within the loop (i.e., if xAnchorID==a && xSourceID==b is at some point true). Clearly, this code is never executed in your A50 function. Without knowing what is in BlinkSet it's a bit difficult to diagnose the exact problem, but this is the cause at least.
As a side note: it's not a good idea to create two separate functions to do this job when their code is almost identical. You should add an input argument to your function that allows it to do the job of both. In this particular case, all that changes is the value of xAnchorID and xSourceID, so you could just pass these in:
function srcToAnchorRssi(BlinkSet, xSourceID, xAnchorID)
% The rest of the function stays the same!
If you want to provide some defaults for these arguments, you can do, e.g.:
if nargin < 3 || isempty(xAnchorID), xAnchorID = 50; end
if nargin < 2 || isempty(xSourceID), xSourceID = 30; end
It's always a good idea to include an isempty in statements of this sort, so that your function supports syntax like myFunction(myArg1, [], myArg3). Also note that the order of the operands to || is crucial; if you did if isempty(theArgument) || nargin < theArgumentNumber and the user did not pass theArgument, then it would error in the isempty because theArgument would not exist as a local variable. We can get around this by swapping the operands' order because MATLAB is smart enough to know it doesn't have to evaluate the right operand if the left operand is true (note that this is also the case in many other programming languages).

an error in Matlab: Input argument undefined, although variable is existing

I have written this function, and I have already defined values for rg and Lp, but still when I run this function it returns the error : (Input argument "Lr" is undefined.
Error in ==> Bis at 12
if f(Lr,rg,Xo)*f(Lr,rg,Xf)>0)
here is the function :
function[Lp,Xo,Xf]=Bis(Lr,rg)
Xo=0;
Xf=10;
Err=0.01;
syms x;
f=inline('(sqrt((2/3)*(((x*Lr)/3)-(x*x)+((2*x*x*x)/Lr)-((2*x*x*x*x)/(Lr*Lr))+(((2*x*x*x*x)/(Lr*Lr))*exp(-Lr/x))))-rg)');
if f(Lr,rg,Xo)*f(Lr,rg,Xf)>0
disp('The values you entered are not apropriate !')
PlotLpFunction;
Lp='unknown';
elseif f(Lr,rg,Xo)*f(Lr,rg,Xf)==0
if f(Lr,rg,Xo)==0
Lp=Xo;
elseif f(Lr,rg,Xf)==0
Lp=Xf;
end
elseif f(Lr,rg,Xo)*f(Lr,rg,Xf)<0
xi=(Xf-Xo)/2;
while abs(f(Lr,rg,xi))>Err
if f(Lr,rg,xi)*f(Lr,rg,Xf)<0
Xo=xi;
xi=(Xo+Xf)/2;
elseif f(Lr,rg,xi)*f(Lr,rg,Xf)>0
Xf=xi;
xi=(Xo+Xf)/2;
end
end
Lp=xi;
end
The code executes for me on the newest version of Matlab, other than the fact that I don't have the PlotLpFunction.
My initial impression was that you forgot to send the Lr (and all other argument) into you're inlined f function, very easy to fix by adding them as arguments to the inline function. You'll find the full usage in the official documentation.
The relevant part being
inline(expr,arg1,arg2,...) constructs an inline function whose input
arguments are specified by the strings arg1, arg2,.... Multicharacter
symbol names may be used.
but it seems to form the inline just fine by itself on both Matlab 2011b and 2008b, from context presumably. Answer is accepted now, so presumably that was the problem. Can anyone else reproduce his problem? If so please provide your Matlab version or other circumstances.