Is it possible to display coverage of a specific bin within a coverpoint in a logfile using the simvision tool? - system-verilog

I am trying to display coverage (in terms of percentage) for a specific bin within a coverpoint. I am able to display the coverage percentage of a coverpoint but not the coverage percentage for individual bins within the coverpoint.
covergroup cov_a #(posedge clk);
c1:coverpoint signal{
bins a={1};
bins b={0};
}
c2:coverpoint b{
bins c={1};
bins d={0};
}
option.per_instance = 1;
endgroup :cov_a
cov_a cov_a_inst = new();
final begin
$display("Coverage for c1: %d%%", cov_a_inst.c1.get_inst_coverage());
$display("Coverage for c2: %d%%", cov_a_inst.c2.get_inst_coverage());
end
I want to do the below
//$display("Coverage for bin a in c1 coverpoint: %d", $coverage(cov_a_inst.c1.a));
not sure how to give the hierarchy.
Note: I have tried adding a single bin within a coverpoint which also solves the issue, but I want to optimize this.

The SystemVerilog language has no mechanism for accessing individual bins. Naming bins is quite complicated, and sometimes you won't even know if a bin exists until after the covergroup gets constructed.
Many tools have post-simulation analysis tools that can report individual bin hits and even timestamps for when it was hit.
If you need that level of detail while your simulation is still executing, your best option is the coverpoint with the single bin as you mention.

Table 19-5 — Predefined coverage methods in IEEE Std 1800-2017 shows that get_coverage and get_inst_coverage can be called on covergroups, coverpoints and crosses. It does not specify that they can be called on bins within a coverpoint. Thus, you should not expect to be able to get coverage for individual bins or to be able to specify the hierarchy of a bin.
Perhaps the tool you are using provides that custom capability; refer to your user documentation.

Related

Determine the number of a specific component in a Modelica model at translation (using Dymola)

How could you determine the number of instances of a given class within this model at translation. This size is to be used to set the size of a vector.
model ModelToBeCounted
end ModelToBeCounted;
model Example
ModelToBeCounted A;
ModelToBeCounted B;
ModelToBeCounted C;
end Example;
So in this example I would like to like to determine the number instances of ModelToBeCounted. These instances could also be within instances of other classes. The code at the top level could be altered to do this and the ModelToBeCounted could also be altered.
I tried using external objects however I could not find a way to ensure that the external objects are all created before the function used to report the total number of external objects was run.
Any ideas?
Thanks
If you can modify the model ModelToBeCounted you can do this using inner/outer:
connector C
Real totalNum;
flow Real addNum;
end C;
model InnerObjectCounter
C c;
equation
c.totalNum+c.addNum=0 "Correct sign";
annotation(missingInnerMessage="Add inner component to count objects");
end InnerObjectCounter;
model FlowSource
C c;
equation
c.addNum=1;
end FlowSource;
model AddCounter
outer InnerObjectCounter objectCounter;
final Integer totalNum=integer(flowSource.c.totalNum);
FlowSource flowSource;
equation
connect(flowSource.c, objectCounter.c);
end AddCounter;
model ModelToBeCounted
AddCounter c;
end ModelToBeCounted;
All components are automatically connected to the inner object using inner/outer-mechanism, and they have a flow of 1 that is summed together.
If you cannot modify the model ModelToBeCounted, and you are willing to use non-standarized methods you can set the flag:
Hidden.AllowAutomaticInstanceOf=true;
and then use:
final parameter Integer m=sum(1 for m in P.ModelToBeCounted);
From:
https://github.com/modelica/ModelicaSpecification/blob/MCP/0021/RationaleMCP/0021/
The ModelManagement library can do that.
In fact, there is exactly what you need available as a function:
ModelManagement.Structure.Instantiated.CountUsedModels("Example", {"ModelToBeCounted"});
= {3}
The library is installed with Dymola and available with the standard license.
You just have to open it from the Libraries menu.
A problem could be, that this function has to translate your model. I am not sure how well this will work for your requirement:
This size is to be used to set the size of a vector.

How does Dymola choose the iteration variables during nonlinear system initialization process?

Assuming I have a very complicated thermal-fluid model built with Dymola. During the initialization process, how would dymola choose the iteration variables for the nonlinear solver? Is there a standard for this issue in dymola? I wanna make this clear, cause sometimes if the start values of the iteration variables are too far away from the right solution, there would be a divergence issue during the initialization process. I think if I could know the choice of iteration variables, I could make sure their values are appropriate instead of checking all the start values.
I think this information is not available to the public. At least I could not find anything in the user manuals. Therefore, you have only limited options.
What you can do is:
List your current iteration variables
Try to influence the selection
Get information on performed iterations
List iteration variables
You can list your current iteration variables by activating the flag
Advanced.LogNonLinearIterationVariables = true;
The iteration variables will be listed in the Translation tab as info message Variables appearing in the nonlinear systems of equations:
Influence selected variables
You can influence (but not control) the variables selected for iteration by setting start values. Take this code for example:
model MyNonLinear
Real x;
Real y;
Real z(start=-1);
equation
0 = y - z;
x = (y + z)^2;
x = (y + z) + time;
end MyNonLinear;
If you translate it with the above flag active, Dymola will give this information in the translation tab of the message window:
Iteration variables:
y
When you additionally set a start value for x, (e.g. start=1) x becomes the iteration variable in this case.
Information on performed iterations
To get more information on the performed iterations, activate the Nonlinear solver diagnostics flags in the Debug tab of the Simulation Setup:
Dymola will then display additional information in the simulation log. Note that some of these flags can generate a log of output.
This is possible via a very lightly documented feature. See the 2019 FD01 release highlights for some limited info: https://www.3ds.com/fileadmin/PRODUCTS/CATIA/DYMOLA/PDF/Dymola-2019FD01-highlights.pdf
Specifically:

Simulink S-Functions - Retrieve Initial Values from another S-Function

I'm trying to model the respective processes of an internal combustion engine. My current modelling approach is to have different sub functions which model the different processes.
Within each sub function is a Level 2 S-Function which solves the ODEs to give the in cylinder state (pressure, temperature, etc).
The problem that I'm having is that each sub function is enabled depending on the current crank angle which is computed from the current timestep in Simulink. The first process works fine as I manually set the initial values, but then I can't pass the latest in-cylinder state (the output from the first sub function) to the second sub function to use as the initial conditions (it insists on using the initial values I set at the beginning of the simulation).
Is there any way round this? Currently I'm going along a path of global data stores, but haven't had any joy so far.
There are a lot of different ways to solve this problem.
I'll show some of them as examples.
You can create additive output with Unit dalay block like this:
So you can get value of your crank angle from previous timestep and USE IT in formula for solving you equations.
Also you can use some code like this:
if (t == 0)
% equations with your initial values
sred = 0;
else
% equations with other values
y = uOld + myCoeef;
end
Another idea: sometimes I use persistent variables in Matlab function to save values of some variable from previous step. But I think it makes calculation slower.
One more idea - if you have Stateflow you can create chart with two states: first for initial moment with your coefficient and second to solve new equations.
If I understood you in wrong way you can show your code and we'll offer some new ideas!
P.S. Example of my using of S-Function:
My S-Function needs 2 values: Q is calculated in simulink at every step, ro is initial I took from big matrix I loaded from workspace in table and took necessary value depending of time.
So there is no any initial values in S-Function - all needed values I transmit into it from simulink!

Creating an uncertain model based on a family of multiple inputs multiple outputs models (MIMOs)

Objective
Currently I am trying to create an uncertain system based on a family of statespace models using ucover. For this I am basing my script on the documentation "Modeling a Family of Responses as an Uncertain System" which shows the technique for creating an uncertain system based on a single-input-single-output system (SISO) explicitly but makes it clear that this is fully useable for MIMO systems as well.
Technical details
Specifically it is stated with the documentation of ucover that it supports MIMO systems:
USYS = ucover(PARRAY,PNOM,ORD1,ORD2,UTYPE) returns an uncertain
system USYS with nominal value PNOM and whose range of behaviors
includes all LTI responses in the LTI array PARRAY. PNOM and PARRAY
can be SS, TF, ZPK, or FRD models. USYS is of class UFRD if PNOM
is an FRD model and of class USS otherwise.
ORD1 and ORD2 specify the order (number of states) of each diagonal
entry of W1 and W2. If PNOM has NU inputs and NY outputs, ORD1 and ORD2
should be vectors of length:
UTYPE ORD1 ORD2
InputMult NU-by-1 NU-by-1
OutputMult NY-by-1 NY-by-1
Additive NY-by-1 NU-by-1
In my case I am using both 2 inputs and 2 outputs so both ORD1 adn ORD2 should be 2 by 1. I am using 8 as the number of states used by W1 and W2 (just because, I will try adjusting that once this issue is sorted).
The Attempt
Based on the SISO example I have attempted to create a MIMO example, this is shown below
noInputs=2;
noOutputs=2;
noOfStates=4;
Anom=rand(noOfStates,noOfStates);
Bnom=rand(noOfStates,noInputs);
Cnom=rand(noOutputs,noOfStates);
Dnom=rand(noOutputs,noInputs);
Pnom=ss(Anom, Bnom, Cnom, Dnom);
p1 = Pnom*tf(1,[.06 1]); % extra lag
p2 = Pnom*tf([-.02 1],[.02 1]); % time delay
p3 = Pnom*tf(50^2,[1 2*.1*50 50^2]);
Parray = stack(1,p1,p2,p3);
Parrayg = frd(Parray,logspace(-1,3,60));
[P,Info] = ucover(Parrayg,Pnom,[8 8]',[8 8]','InputMult');
Wt = Info.W1;
bodemag((Pnom-Parray)/Pnom,'b--',Wt,'r'); grid
title('Relative Gaps vs. Magnitude of Wt')
The problem
Unlike the image in the documentation my uncertain model (when put through a bode plot) only shows a response on the lead diagonal. See the screenshot for what I mean:
Where blue is the individual models and red is the uncertain model
Question
How can I create an uncertain system based on a family of MIMO statespace models that correctly covers responses between all inputs and outputs?
If you use [8,8]' as your uncertainty order structure ord1,ord2, matlab will try to have two diagonal blocks in your uncertainty block each.
However matlab only supports diagonal weighting functions (due to some complications about nonconvex search) and what you are plotting is the diagonal weighting that will multiply the 2x2 full block LTI dynamic uncertainty. W1 affects the rows and W2 affects the columns of the uncertainty.
Hence you should check the samples of that uncertainty multiplied by the weights and then the plant. Then you can compare it with the uncertain model stack. Notice that your off-diagonal entries are practically zero (<1e-10) hence almost decoupled. But W1, W2 search looks for the H-infinity norm hence you don't get to see perfect covering at each block of the Bode plot. It combines the rows/columns of the required minimum uncertainty amount (see the examples on the help file). That's why you see 1 plot per each weight in the demos.
If you would like to model the each uncertainty affecting each block separately then you need to form a new augmented LFT such that the uncertainty is four 1x1(scalar) LTI dynamic uncertainty on the diagonal then you can have four entries in ord1 and ord2.
Since this is a MIMO system, you shouldn't compare things element-by-element. You are using the input-multiplicative form, so the uncertain system being created is of the form
Pnom*(I + W1*Delta*W2), where Delta is any stable (2-by-2, in this case) system, with ||Delta|| <= 1. So, to verify that the produced uncertain model "covers" your array of system, you should think of the equation
Parray = Pnom*(I + W1*Delta*W2)
and solve for Delta. Plot it (with SIGMA, say), and you will see that it is less than 1 in magnitude, for all frequencies. The Matlab code would be (multiply everything listed below, in order - my mulitplication symbol is not showing up in the posted answer...)
sigma(inv(W1)*inv(Pnom)*(Parrayg-Pnom)*inv(W2))
Now, using the syntax you specified, you are using weights W1 and W2 of the following form:
W1 = [W1_11 0;
0 W1_22]
and
W2 = [W2_11 0;
0 W2_22]
where you've specified 8th-order fits for all nonzero entries. Certainly for your example, this is overkill (although on a richer problem, it might be fine).
I would try much simpler, like
ucover(Parrag,Pnom,3,[],'InputMult')
That syntax will make an uncertain model of the form
Pnom*(I + w1*Delta)
where w1 is a scalar, 3rd order system. You could still see the covering by plotting SIGMA(Delta), namely
sigma((1/w1)*inv(Pnom)*(Parrayg-Pnom))
I hope that helps.
In order to create discrete or continuous time uncertain systems you can use uss associated with ureal.
Quick example
Define an uncertain propeller radius
% Propeller radius (m)
rp = ureal('rp',13.4e-2,'Range',[0.08 0.16]);
Define uncertain continuous time system
tenzo_unc = uss(A,Bw,Clocal,D,'statename',states,'inputname',inputs,'outputname',outputsLocal);
Simulate step response:
N = 5;
% Prende alcuni campioni del sistema incerto e calcola bound su incertezze
for i=1:1:N
sys{i} = usample(tenzo_unc);
step(sys{i})
hold on
cprintf('text','.');
end
Complete example
Quadcopter uncertain linearized model control with LQR. Code is available here
Step response
Closed Loop Step response
<script src="https://gist.github.com/GiovanniBalestrieri/f90a20780eb2496e730c8b74cf49dd0f.js"></script>
NB:
If you don't have the utility cprintf, include this script in your folder and use it.

How to get level of fitness of data to a distribution by using probplot() in Matlab?

I have 2 sets of data of float numbers, set A and set B. Both of them are matrices of size 40*40. I would like to find out which set is closer to the normal distribution. I know how to use probplot() in matlab to plot the probability of one set. However, I do not know how to find out the level of the fitness of the distribution is.
In python, when people use problot, a parameter ,R^2, shows how good the distribution of the data is against to the normal distribution. The closer the R^2 value to value 1, the better the fitness is. Thus, I can simply use the function to compare two set of data by their R^2 value. However, because of some machine problem, I can not use the python in my current machine. Is there such parameter or function similar to the R^2 value in matlab ?
Thank you very much,
Fitting a curve or surface to data and obtaining the goodness of fit, i.e., sse, rsquare, dfe, adjrsquare, rmse, can be done using the function fit. More info here...
The approach of #nate (+1) is definitely one possible way of going about this problem. However, the statistician in me is compelled to suggest the following alternative (that does, alas, require the statistics toolbox - but you have this if you have the student version):
Given that your data is Normal (not Multivariate normal), consider using the Jarque-Bera test.
Jarque-Bera tests the null hypothesis that a given dataset is generated by a Normal distribution, versus the alternative that it is generated by some other distribution. If the Jarque-Bera test statistic is less than some critical value, then we fail to reject the null hypothesis.
So how does this help with the goodness-of-fit problem? Well, the larger the test statistic, the more "non-Normal" the data is. The smaller the test statistic, the more "Normal" the data is.
So, assuming you have converted your matrices into two vectors, A and B (each should be 1600 by 1 based on the dimensions you provide in the question), you could do the following:
%# Build sample data
A = randn(1600, 1);
B = rand(1600, 1);
%# Perform JB test
[ANormal, ~, AStat] = jbtest(A);
[BNormal, ~, BStat] = jbtest(B);
%# Display result
if AStat < BStat
disp('A is closer to normal');
else
disp('B is closer to normal');
end
As a little bonus of doing things this way, ANormal and BNormal tell you whether you can reject or fail to reject the null hypothesis that the sample in A or B comes from a normal distribution! Specifically, if ANormal is 1, then you fail to reject the null (ie the test statistic indicates that A is probably drawn from a Normal). If ANormal is 0, then the data in A is probably not generated from a Normal distribution.
CAUTION: The approach I've advocated here is only valid if A and B are the same size, but you've indicated in the question that they are :-)