I want to enable warnings about unit mismatches (dimentionality) on modelica code.
Here is the test code I mis-formulated on purpose:
class test
Real v(final unit="V");
Real i(final unit="A");
parameter Real r(start = 120, final unit="Ohm");
equation
v = i * i * r;
v = sin(time);
end test;
Here is the script I used for compiling:
loadModel(Modelica);
loadFile("test.mo");
simulate(test, stopTime=10);
plot(i);
I compile the code above with the following command:
omc t.mos --debug=dumpEqInUC --debug=dumpEqUCStruct --debug=dumpUnits
But there is no warning about units. How to enable unit check?
Thanks.
omc version: 1.9.3+dev (r25940)
Add the following line to script,
setCommandLineOptions("--preOptModules+=unitChecking");
Related
I would like to do
M_type(redeclare package L=L2) m2_instance;
but does not work. Instead I can write
model M2_type
extends M_type(redeclare package L=L2);
end M2_type;
M2_type m2_instance;
Is here not a shorter way to do this and avoid declaring M2_type?
The redeclare modifier must be moved to the instance name.
M_type(redeclare package L=L2) m2_instance; // wrong syntax
M_type m2_instance(redeclare package L=L2); // correct
Below is a small package, which demonstrates everything and simulates perfectly fine in Dymola 2021x and OpenModelica 1.16.2.
package Redeclaration
package L1
constant Real x = 1;
end L1;
package L2
constant Real x = 2;
end L2;
model M_type
replaceable package L = L1;
Real x = L.x;
end M_type;
model Example
M_type m_instance(redeclare package L=L2);
end Example;
end Redeclaration;
I created a model in OpenModelica and would like to create a FMU from it.
Inside OpenModelica, I can initialize the parameters the following:
model r_ctrl
parameter Real startTime(start = 0.1);
parameter SI.Resistance u_ref(start = 230);
parameter SI.Power p_ref(start = 1000);
parameter Real r_start(start = u_ref*u_ref/p_ref);
...
This works without any problems, during the simulation, all parameters have the values they are supposed to have
When I create the FMU, the following error appears in the terminal:
[CodegenUtil.tpl:178:14-178:14:writable] Error: Template Fehler: initial value of unknown type: r_ctrl.u_ref ^ 2.0 / r_ctrl.p_ref.
Is there a way to set the parameters dependent of each other, but without getting errors in the FMU generation process?
The following should work (I suppose the above should as well, but requires OpenModelica to automatically translate it into something like the following):
model r_ctrl
parameter Real startTime = 0.1;
parameter SI.Resistance u_ref = 230;
parameter SI.Power p_ref = 1000;
parameter Real r_start(fixed=false);
initial equation
r_start = u_ref*u_ref/p_ref;
I am trying to run the following code in R2016a
import matlab.unittest.qualifications.Assertable;
assertTrue(true, 'wrong');
But am getting this error.
Undefined function or variable 'assertTrue'.
I did not notice any changes in assertTrue of Matlab 2016a.
I also tried unsuccessfully the import of matlab.unittest.qualifications.*.
I also tried unsuccessfully testcase.assertTrue(true, 'wrong');.
How can you I use assertTrue in Matlab?
assertTrue is not a vanilla MATLAB function, it is a method of matlab.unittest.TestCase.
Notice in the documentation, that the first input (assertable) is a matlab.unittest.TestCase instance.
It is used like this in a class-based unit test
classdef test < matlab.unittest.TestCase
methods (Test)
function doTest(testCase)
testCase.assertTrue(true);
% or
assertTrue(testCase, true);
end
end
end
and this in a function-based unit test
function testFunctionOne(testCase)
testCase.assertTrue(true)
assertTrue(testCase, true)
end
If you don't want unit tests but simply want to assert that something is true, then use assert.
value = false;
assert(value, 'These aren''t the droids you''re looking for.')
I am using openmodelica and I am trying to loop through an array in order to find the maximum value. I was able to reduce my code to a very simple test case that still gives the error. Is this something that I am doing wrong, or is this a bug in openmodelica? Here is a very simple case that does give the error:
package TestLoop
model ItemA
Real p;
end ItemA;
model ItemB
ItemA a[n];
parameter Integer n = 5;
Real p;
equation
for i in 1:n loop
a[i].p = time;
end for;
algorithm
for i in 1:n loop
p := a[i].p;
end for;
end ItemB;
end TestLoop;
The problem is in my algorithm section. Here is the error that I am getting:
TestLoop.ItemB.c:155:13: warning: implicit declaration of function '$Pa$lB' is invalid in C99 [-Wimplicit-function-declaration]
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
TestLoop.ItemB.c:155:20: error: unexpected type name 'modelica_integer': expected expression
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
1 warning and 1 error generated.
Any suggestions for why this might be, or how I can work around it? If I replace the assignment with a fixed value, p:=a[1].p;, the code does run (although that is not useful to me). What I ultimately want to do in the algorithm section is find the largest value of a[n].p, where I do have an equation section that does useful calculations into the array of items.
Yes, the code generation is an error of OpenModelica (it does not like unknown array indexes). Your problem is very easy to solve in a single line though (one of the following):
p = max(r for r in a.p);
p = max(a.p);
So, I am about to train a classifier and needed to save the result from the classifier, in mexopencv.
hello = cv.SVM;
hello.save('foo.xml')
My Matlab compiler crashes due to segmentation fault. As far as I know this is supposed to be the way in OpenCV. Is there any other way to save files trained via SVM in mexopencv ; or is this something to do with Matlab file writing option. Thanks.
This is in fact a bug in OpenCV itself, not mexopencv. You should report it...
Here is a minimal C++ program to reproduce the bug:
#include "opencv2/opencv.hpp"
#include "opencv2/ml/ml.hpp"
using namespace cv;
int main()
{
Ptr<SVM> svm = new SVM();
svm->save("svm.xml");
return 0;
}
Running this under a debugger, I located the offending code:
df_count = class_count > 1 ? class_count*(class_count-1)/2 : 1;
df = decision_func;
cvStartWriteStruct( fs, "decision_functions", CV_NODE_SEQ );
for( i = 0; i < df_count; i++ )
{
int sv_count = df[i].sv_count;
...
}
At this point, the SVM model is untrained, and df is an uninitialized pointer. class_count equals 0, but df_count gets set to 1, hence df[i] (with i=0) causes an access violation...
I think this could be fixed as:
df_count = class_count > 1 ? class_count*(class_count-1)/2 : 1;
if (class_count == 0) df_count = 0;
Changing df_count to 0 during debugging, the program runs correctly and I get the following XML file:
svm.xml
<?xml version="1.0"?>
<opencv_storage>
<my_svm type_id="opencv-ml-svm">
<svm_type>C_SVC</svm_type>
<kernel>
<type>RBF</type>
<gamma>1.</gamma>
</kernel>
<C>1.</C>
<term_criteria>
<epsilon>1.1920928955078125e-007</epsilon>
<iterations>1000</iterations>
</term_criteria>
<var_all>0</var_all>
<var_count>0</var_count>
<sv_total>0</sv_total>
<support_vectors></support_vectors>
<decision_functions></decision_functions>
</my_svm>
</opencv_storage>
For now you could avoid the bug by training the model first before saving it to file :)
You can try libsvm which has a Matlab interface. It comes two functions to read and write in svm (libsvmwrite and libsvmread).