I want to know how I can visualise the results of a Modelica simulation in the form of an animation.
Imagine I have a simple simulation as below:
model test
//parameters
parameter Real m_1 = 1;
parameter Real m_2 = 10;
parameter Real K_c = 100000;
//variables
Real x_1;
Real v_1;
Real x_2;
Real v_2;
Real f_1;
Real f_12;
initial equation
x_1 = 0;
v_1 = 0;
x_2 = 0.2;
v_2 = 0;
equation
v_1 = der(x_1);
m_1 * der(v_1) = f_1 - f_12;
v_2 = der(x_2);
m_2 * der(v_2) = f_12;
f_12 = if x_2 >= x_1 then 0 else K_c * (x_1 - x_2);
f_1 = 1;
end test;
(it is actually a very simple elastic collision)
and I have part1.obj and part2.obj (or other possible formats rather than .obj), designed in other CAD software, which I want to import and assign to x_1 and x_2 and then animate. OpenModelica 1.11.0 is already installed on Ubuntu using these instructions.
What I have found so far and the problems I have encountered:
From this page I have learned that Modelica3D should also be installed. However I couldn't find this library using apt or aptitude.
I found the installation instructions for windows and mac on OpenModelica official website, but no Ubuntu (or other Linux distro) one.
I have also found this github repo which claims to be a regularly maintained mirror of the original distribution, however the latest commit is for 2 years ago! Not to mention that the original website is dead.
Do I still need to install this library separately or it has already been integrated into the latest stable OpenModelica release? if yes how can I install it? what other libraries/packages might be needed?
Edit: It seems that Modelica3D has been depreciated (same for OMVisualize). Now the ModelicaServices is being used for animation. more information here
This post gives a very simple example of animating using Wolfram SystemModeller. When I open it into the OpenModelica, it does compile and shows the results. I tried exporting the model as FMU and XML and then use animation icon from the top menus to import the model.
but when tried to import the .mat file I got the error:
Scripting Error
Could not find the visual XML file /tmp/OpenModelica_user/OMEdit/SimpleCarModel_visual.xml.
Is this example compatible with OpenModelica? and if yes what is the problem and how I can solve it? (you can also download the example from this Github gist)
Edit: This example is compatible with OpenModelica. I was doing it wring. I had to choose the "simulate with Animation" instead of "Simulate". I still can't assign an external CAD object to the simulation and a cylinder is being shown instead.
I also tried to learn using OMShell scripting to run the Modelica3D example from this page. However it seems that the code is wrong because I get many errors running it in my OMShell.
One of the linked websites has mentioned that it is possible to use blender for animated results however the author hadn't succeed to do so. I would appreciate if you could help learn this in simple and clear steps.
Visualizing CAD-files is currently only possible for .stl and .dxf.
You can specify the URI of your CAD-file in the shapeType-parameter of your shape. Please use the following notation:
"modelica://packageName/relativePath/To/Your/CADfile.stl"
It is also possible to set the absolute path in the *_visual.xml that has been generated with your simulation results(and should be in the same directory if you want to load simulation from OMEdit).
If you want to move your animation shapes, simply assign variables to i.e. the position vector
r = {x,0,0};
Related
I want to use the bayesreg package within the MATLAB environment, which enables flexible Bayesian penalized regression modelling. I am following a paper, which details the software implementation within the MATLAB environment. I am using the exact instructions available in the paper but do not seem to get the code to work.
I have imported the table containing the data that I want to analyze. I then type the following code:
varnames=finale.Properties.VariableNames;
% count regression
X = finale{:,2:9}
y = finale{:,11}
[beta, beta0, retval] = bayesreg(X,y,'poisson','g','nsamples',1e5,'burnin',1e5,'thin',5,'displayor',true,'varnames',varnames(2:9),'display',true);
X and y are simply my dependent and independent variables, and all arguments of bayesreg are in agreement with the software implementation paper instructions.
I get the below error:
Undefined function or variable 'bayesreg'.
Could anyone shed any light?
In my case, the problem was that I didn't have the Neural Network toolbox installed, as well as another 1-2 packages. Installing those solved the issue.
I'm trying to solve a big quadratic optimization problem using CPLEX (cplexqp) in matlab. Unfortunately by H matrix (or Q matrix as some sources call it) is non-convex in nature and thus I want to set the optimality target from 0 (default) to 3 to tell CPLEX to not terminate when finding out Q is non-convex. However, I'm not sure how to do this. I tried to read the manuals and instructions and they all just said set optimality target = 2 or 3 without any actual examples or general command on how to do it. I tried to do it in options but got an error that CPLEX doesn't recognize 'optimalitytarget'.
options = cplexoptimset('Display','on','TolFun',0.0000001,'TolRLPFun',0.0000001,'MaxNodes',50000,'MaxIter',50000, 'optimalitytarget',3);
I also tried:
Cplex = cplexoptimset('cplex')
Cplex.Param.optimalitytarget = 3;
without any sucess. I know it the API is Cplex.Param.optimalitytarget but I cannot seem to figure out where to set this.
Sorry if this is a trivial or dumb question. I feel like this is one of the things that's very simple and either you do know or you don't know and I don't know how to do it. Any help or advice on going about this is greatly appreciated.
You can find examples of using CPLEX from within MATLAB in the distribution. They are located in [installPath]/cplex/examples/src/matlab.
You mentioned that you are using cplexqp, which is the toolbox API. Looking at https://www.ibm.com/support/knowledgecenter/es/SSSA5P_12.7.0/ilog.odms.cplex.help/CPLEX/MATLAB/topics/gs_param.html, I suspect that the issue with your second example is with your using Param. That structure relates to the Cplex Class API, not the toolbox API. I suppose that the following would work better:
options = cplexoptimset('cplex');
options.optimalitytarget=3;
Model = cplex.Cplex("filename.mps")
Model.parameters.optimalitytarget.set(float(3))
This worked for me!
I have a very simple MATLAB program for training and testing a regression tree, I use the same Data carsmall that is in the tutorial examples:
clear all
clc
close all
load carsmall
X = [Cylinders, Weight, Horsepower, Displacement];
Y = MPG;
tree = fitrtree(X,Y, 'PredictorNames',{'Cylinders', 'Weight', 'Horsepower', 'Displacement'},'ResponseName','MPG','MinLeaf',10);
Xtest=[6,4100,150,130];
MPGest = predict(tree, Xtest);
This gives as a result MPGest=14.9167
I want to know how the predict function is arriving at that value, usually to understand I go line by line inside the function. This one is very tricky because uses classes so I arrive at this line
node = findNode(this.Impl,X,this.DataSummary.CategoricalPredictors,subtrees);
and inside that function I arrive to
n = classreg.learning.treeutils.findNode(X,...
subtrees,this.PruneList,...
this.Children',iscat,...
this.CutVar,this.CutPoint,this.CutCategories,...
this.SurrCutFlip,this.SurrCutVar,...
this.SurrCutPoint,this.SurrCutCategories,...
verbose);
when I try to step in at this step it just give me n=10, how is MATLAB arriving at this number? for example, If I wanted to make my own program to calculate this number using the tree object as input without using predict?
Actually, the function you are looking for is defined into a MEX file. If you try to open it using the open function, you will get the following outcome:
open 'classreg.learning.treeutils.findNode'
Error using open (line 145) Cannot edit the MEX-file
'C:...\toolbox\stats\classreg+classreg+learning+treeutils\findNode.mexw64'
Unfortunately, MEX files are compiled from C++ sources into bytecode (better known as assembly language). There are many decompilers out there that you can use in order to rebuild the instructions compiled into the library (this is a nice starting point, if you want a fast overview), and the whole process is feasible especially because the file itself is quite small.
The code you will get back will not be the original source code, but something similar: variables will have a default and meaningless name, there will be pointers all over and it may also contain bugs due to a wrong reversing of some assembly instructions. This will be probably enough to let you understand what's going on, but you will not be able to follow the computations through a step by step debugging session in Matlab.
Now, the only question you have to answer to is: is this worth the effort?
I am solving two coupled time dependent PDE eqations using pde tool box.The simulation box size is x=6 and y=10.Currently I solve it properly and acces the solution data using
results = solvepde(model,tlist); u = results.NodalSolution(:,1,:);
What I need to do is now to stop the code when the solution u is unstable along the y axis.That means I want to monitor solution of u along y axis while running the code and stop it when it meets a criteria that I want.(For example I want to stop the code when solution of u along y equals to 0)..How can I do it using pde tool box?
Here is what I tried so far.Is there easy way to access the results while code is running?.That is I want to access the solution for each time step.
%calculate solutions
n=4000;
tlist = linspace(0,200,n);
partial=zeros(49,1);
for i=1:n
results = solvepde(model,tlist(i:i+1));
u = results.NodalSolution(:,1,1);
v=results.NodalSolution(:,2,1);
u1=results.NodalSolution(113:161,1,1);
u2=results.NodalSolution(1,1,1);
u3=results.NodalSolution(4,1,1);
for j=1:49
partial(j)=u1(j)-0.5*u2-0.5*u3;
end
sigma=sum(partial);
if sigma>1e-4
disp('verified')
return
end
end
There are several ways to stop a code during execution:
keyboard
Using this function stops the execution and gives you access to the keyboard, such that you can see all values.
warning('Solution is unstable')
This won't actually stop the code but will give you a warning stating that the solution is unstable.
error('Solution is unstable')
This will actually stop the code, giving you the error, that the solution is unstable.
break
This will not give you any feedback, but it will stop the for-loop and then continue the rest of the code.
In all cases you include the way of stopping the code in an if-statement, like
if (all(u==0))
*statement from above*
end
I am completely new to matlab and can't seem to get an if loop to work. For example if Ln > k , plot point i(n-1) to i(n). How would I automatically assign the correct row or column vectors to i(n)?
Here is a diagram of what I'm wanting
What I want to achieve is connect i(0) to i(1) to ... i(n-1) to i(n).
I also am a bit confused at which co-ordinate system to use? I thought it would be easy to use a polar co-ordinate system. Defining a distance and angle from point i(o) and then doing the same from point i(1) but from what I could find, it is necessary to convert back to a cartesian co-ordinate system.
Once I am comfortable with this section I am confident I can take on the next steps, and develop a full solution to my problem. If you are interested in what I am trying to achieve, here's a link
[PLEASE NOTE] In that question I linked to, I was told I made a mess of it. I'm sorry if this question is also not clear. I really have spent the time to make it as clear as possible. I find it hard to express myself sometimes.
For coordinate system, you can use complex numbers as a simple way of working within a 1-D matrix. Otherwise, I'm struggling to understand what you are trying to accomplish. You should at least try to show some code as we will be in a better position to guide you.
There are a bunch of ways you can execute your problem. without getting into details, you do the following:
n = 1
L(1) = ...
point(1) = ...
while (L(n) < k)
n = n+1;
L(n) = L(n-1)*sin(alpha)/sin(alpha+theta);
point(n) = ...
end
plot(point(1:n));