Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.
Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.
state space export
Posted 31.01.2011, 19:54 GMT-5 Interfacing Version 4.3b 21 Replies
Please login with a confirmed email address before reporting spam
I'm trying to export a state space model for the microresistor beam example. Can anyone tell me how to export a state space model of the displacement as a function of temperature? A tutorial in 4.1 would also be helpful.
Thanks
Hello Andrew Wang
Your Discussion has gone 30 days without a reply. If you still need help with COMSOL and have an on-subscription license, please visit our Support Center for help.
If you do not hold an on-subscription license, you may find an answer in another Discussion or in the Knowledge Base.
Please login with a confirmed email address before reporting spam
I am also trying to export state-space matrices using COMSOL GUI for use in MATLAB/ Simulink. Can anybody suggest proper references or documented tutorial examples for the same? I am stuck in the part where I need to input an expression for exporting the matrices (MA, MB, C, D) that I was able to access by navigating through Study-> Solver-> Other-> State Space. But I am unable to export these matrices without entering anything in the expression text box.
Thanks for your time,
Madu
Please login with a confirmed email address before reporting spam
try a search on the forum, there has been a couple of discussion already + some examples
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
I will keep looking in the forum anyways and let you know if I find anything useful.
Thanks
Madu
Please login with a confirmed email address before reporting spam
you might want to get Livelink for Matlab, a software option for COMSOL, that exports the state space matrices for you. If you look in your comsol folder, you can find a documentation for Matlab livelink.
As for extracting the state space matrices from the COMSOL GUI and a programming environment like Eclipse and then importing them into Matlab to solve for a solution, I haven't successfully done that yet. I'm able to export the matrices but they don't give me the correct answer when I calculate the output using the state space matrices in Matlab. I'm currently working with Remi on this problem.
What I've done so far is: define an output variable in the COMSOL GUI and set it equal to the output that you're interested in (in my case it was solid.disp). Then check the MA,MB,C,D (even though you normally don't use D). Set the input equal to whatever the input is (in my case it was Voltage V). Then run the model as usual. Save the model as a .java file. Create a new java project in your environemnt (mine was Eclipse). Import your .java file into a new java project. Insert the code:
System.out.println("11a");
model.sol("sol1").feature().create("sp1", "StateSpace");
model.sol("sol1").feature("sp1").set("input", new String[]{"Vin"});
model.sol("sol1").feature("sp1").set("output",new String[]{"Vin"});//{"mod1.intop1(mod1.outvar)"});
model.sol("sol1").feature("sp1").set("C",true);
model.sol("sol1").feature("sp1").set("D",true);
model.sol("sol1").feature("sp1").set("MA",true);
model.sol("sol1").feature("sp1").set("MB",true);
model.sol("sol1").feature("sp1").set("Mc",true);
//model.sol("sol1").feature("sp1").set("static", "on");
System.out.println("11b");
model.sol("sol1").run("sp1");
System.out.println("11c");
//get the MA, MB, C, D matrices
double[] matrixMA, matrixMB, matrixC = null;
int[] rowMA, rowMB, rowC = null;
int[] colMA, colMB, colC = null;
System.out.println("12a");
matrixMA = model.sol("sol1").feature("sp1").getSparseMatrixVal("MA");
rowMA = model.sol("sol1").feature("sp1").getSparseMatrixRow("MA");
colMA = model.sol("sol1").feature("sp1").getSparseMatrixCol("MA");
int M = model.sol("sol1").feature("sp1").getM("MA");
int N = model.sol("sol1").feature("sp1").getN("MA");
System.out.println("M of MA=" + M);
System.out.println("N of MA=" + N);
matrixMB = model.sol("sol1").feature("sp1").getSparseMatrixVal("MB");
rowMB = model.sol("sol1").feature("sp1").getSparseMatrixRow("MB");
colMB = model.sol("sol1").feature("sp1").getSparseMatrixCol("MB");
M = model.sol("sol1").feature("sp1").getM("MB");
N = model.sol("sol1").feature("sp1").getN("MB");
System.out.println("M of MB=" + M);
System.out.println("N of MB=" + N);
matrixC = model.sol("sol1").feature("sp1").getSparseMatrixVal("C");
rowC = model.sol("sol1").feature("sp1").getSparseMatrixRow("C");
colC = model.sol("sol1").feature("sp1").getSparseMatrixCol("C");
M = model.sol("sol1").feature("sp1").getM("C");
N = model.sol("sol1").feature("sp1").getN("C");
System.out.println("M of C=" + M);
System.out.println("N of C=" + N);
System.out.println("12b");
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_MA.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(matrixMA));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_MA_row.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(rowMA));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_MA_col.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(colMA));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_MB.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(matrixMB));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_MB_row.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(rowMB));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_MB_col.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(colMB));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_C.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(matrixC));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_C_row.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(rowC));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
FileOutputStream fo = new FileOutputStream("C:\\TextFile_C_col.txt");
PrintStream ps = new PrintStream(fo);
ps.println(Arrays.toString(colC));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This prints the state space matrices to a text file which I imported into Matlab. In Matlab, I reconstructed the matrices using
matr = sparse(double(row)+1, double(col)+1, out, M, N);
Once I got all the sparse matrices I calculated the output using normal state space equations. However, I don't get the correct answer when I did this. There's some error in extracting state space matrices in my model. My matlab code is correct since I checked it for a simpler case.
Please login with a confirmed email address before reporting spam
Actually I did find this reply in other discussion topic but when I try to do this in COMSOL GUI, it asks me to enter an output expression and my only issue is what to enter into this box. Based on your code snippet, should I put here as
"my_var_output = solid.disp "
if the output variable that I created is "my_var_output". I already tried this but still get an error saying "Syntax error in expression. - Expression: temp=solid.disp"
In fact, I also have a livelink to MATLAB license with my COMSOL but no proper documentation for this process makes it very difficult.
Even with valid license, it would not allow me to create state space or simulink blocks. I remember in COMSOL 3.5 or earlier, there was an option under File menu to export our COMSOL model into Simulink blocks or as statespace matrices easily. So, it is my intuition that it should not be that difficult to do the same with newer version of COMSOL 4.0a. My first aim was only to generate simulink blocks, since I did not find a way to do this I thought I will validate the model with the state-space export option for now but still am not able to work out my way with that as well.
Let me know if I am making any mistake in variable declaration or syntax expression given above.
Thanks again.
Madu
Please login with a confirmed email address before reporting spam
If you have livelink, then you can use a point probe to extract the state space matrices. Ivar talks about it here:
www.comsol.com/community/forums/general/thread/15482/
Regards,
Andrew
Please login with a confirmed email address before reporting spam
Thanks for the link. I will go through that and let you know if there were any issues.
Apparently, I just now got a reply from COMSOL tech support that in COMSOL 4.1 one cannot convert the models directly into simulink blocks. The only way to do is to use the state space export and use the state-space block in simulink to simulate the model. So, I will have to follow state-space export method for this purpose.
Thanks everyone for replying to my queries.
Madu
Please login with a confirmed email address before reporting spam
I can confirm that I'm also missing the Simulink link that existed in 3.5, so Ihope it will come back once. But the state space way is also possible, expect improvements there too. In particular for structural, I have no mode reduction option today
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
Did you manage to get the same answer using the state space matrices as you did using the COMSOL GUI? If so, what model did you use for that? Was it a single physics problem or a coupled multi physics problem with moving mesh?
Regards,
Andrew
Please login with a confirmed email address before reporting spam
no I was in pure structural, you can get the full matrices, but for complete models these are so sparse and big that I cannot do much with them, apart for simple "academic" examples, I need a mode reduction, being able to select a few interesting modes and get reduced matrices
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
I managed to export state-space matrices of a stationary cantilever beam system and using which I was able to obtain beam deflections upto an accuracy of 8-digits after decimal point by simulating the same in MATLAB. In my case, however, I deal with a stationary system and the solution turns out to be simple due to steady-state state-space matrices. I used the following equation directly to obtain the output vector:
% After loading the COMSOL mph model, I extract the state-space matrices using mphstate command
% After matrix extraction, I use the following relationships (commented in MATLAB to explain the sequence)
% MATLAB state-space simulation:
A= full(M.MA);
B= full(M.MB);
C= full(M.C);
D= full(M.D);
% State space equation
% x_dot_Vec = [A]*xVec + [B]*uVec => 0=[A]*xVec + [B]*uVec (for steady state solutions, x_dot_vec = [0])
% y_Vec = [C]*xVec + [D]*uVec => y_Vec = [-C*inv(A)*B + D]*uVec -- I use this final equation
% finally, we get:
% y_Vec =(-C*inv(A)*B + D)*uVec => which we can use to determine the states of the system for different uVec-s
% however, in my case, the cantilever system has a single input -- point load in y-direction at the free end of the
%beam and multi-output-- deflections at different points along the length of the beam, so size(uVec) = 1,
% size(yVec)= 4)
% for a point load of 1000 N in -ve y-direction:
uVec = -1000;
% corresponding deflection:
y_Vec = (-C*inv(A)*B + D)*uVec
% executing this, MATLAB gave the following result:
> [yVec' uVec]
0 0.000173657548 0.001367040350 0.003172897623 -1000.000000000000
------
yVec: 4 x 1--> deflections-- 1st-col: at fixed end, 4th-col: at free end, with 2 & 3 probes located at intermediate locations along the length of the beam
---------------------------------------------
In COMSOL, the deflection at free end was computed to be:
Total displacement (m), Point: (1, 0)
0.0031728899855153684
--------------------------------------------
I suspect the discrepancy is mainly due to the default-precision of calculations carried out in MATLAB but am not completely sure. I will explore further with this simple system and then let you know if I could ever come up finally with the reason for such discrepancies.
Madu
Please login with a confirmed email address before reporting spam
interesting, how many Dof did you have for your model ?
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
Again, for now, I implemented for a simple beam with well-defined coarse square mesh elements (guess, the entire length only had about 10 elements) and the problem domain is 2D (not 3D). But atleast, with that I was able to get some results which seems to be sufficiently accurate to me-- I may be wrong here. However, I would like to validate this for a more complex model (may be 3D or nonlinear systems) and analyze the results.
Will keep you updated.
Madu
Please login with a confirmed email address before reporting spam
thanks for the info, coearse mesh examples are possible (and correct from my tests, but I'm interested in reducing a 1-2 MDoF model and then I get "out of memeory" issues, for the moment ;)
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
May I know what do you mean by "reducing a 1-2 MDof models"? You mean conducting a Modal analysis? I am sorry, just failed to understand what you meant.
Madu
Please login with a confirmed email address before reporting spam
for me, modal reduction (in structural) means analysing the eigenmodes, decide to keep only a few
w.r.t. input/output pair hence you end up with only a few rows an columns and not >1Mdof ;)
for that you must be able to use a criteria (ie. modal mass w.r.t. u,v,w but also Rx,Ry,Rz ...) to select the modes desired
See aso the excellent book: "Vibration Simulations Using Matlab and ANSYS" by M.R. Hatch (it could/should have been COMSOL and not ANSYS ;).
--
Good luck
Ivar
Please login with a confirmed email address before reporting spam
That's really interesting. Can you post your model here so that I can look at it? I'd like to know what I did wrong.
Regards,
Andrew
Please login with a confirmed email address before reporting spam
The model that I used is a very simple planar cantilever beam system. Pl find the files attached with this post -- MATLAB and COMSOL model.
Regards
Madu
Attachments:
Please login with a confirmed email address before reporting spam
I'm also trying to get state space matrices for models with a lot of dofs. Mine has 20907 dofs. I also had this 'out of memory' message when trying to get the state space matrices with the mphstate function. I just find a way on the support page, which seems to solve the problem. See: www.comsol.com/support/knowledgebase/1092/.
Now I'm getting the matrices without problem (I am using a 64bit machine, I guess it can help as well). I just have to figure out how I want to manipulate them now since the matrices are huge...
Regards
Vincent
Please login with a confirmed email address before reporting spam
Hi
interesting, how many Dof did you have for your model ?
--
Good luck
Ivar
I run this command in Matlab but it has error
M = mphstate(model,'sol1','out',{'MA' 'MB' 'C' 'D'},...
'input', {'Heat1','Heat2'}, 'output', {'mod1.ppb1','mod1.ppb2','mod1.ppb3'});
Error:
Error using full
Out of memory. Type HELP MEMORY for your options.
Error in C:\Program
Files\COMSOL\COMSOL43b\mli\mphstate.p>mphstate (line 171)
Error in StateSpaceExport (line 7)
M = mphstate(model,'sol1','out',{'MA' 'MB' 'C' 'D'},...
Can you help me to solve this problem?
Thank you very much.
Please login with a confirmed email address before reporting spam
I am trying to extract the state space matrices of a cantilever beam subjected to 1 input force, and solve for for the frequency response in 4 points. I am quite ok with programming in Matlab but not really familiar with the command lines using livelink. If it's not too much to ask, could you please post or send me the part of your code on how you extracted the matrix system? . So that I can see where im having troubles with.
Thanks for your help !!
Jorge
Hello Andrew
I managed to export state-space matrices of a stationary cantilever beam system and using which I was able to obtain beam deflections upto an accuracy of 8-digits after decimal point by simulating the same in MATLAB. In my case, however, I deal with a stationary system and the solution turns out to be simple due to steady-state state-space matrices. I used the following equation directly to obtain the output vector:
% After loading the COMSOL mph model, I extract the state-space matrices using mphstate command
% After matrix extraction, I use the following relationships (commented in MATLAB to explain the sequence)
% MATLAB state-space simulation:
A= full(M.MA);
B= full(M.MB);
C= full(M.C);
D= full(M.D);
% State space equation
% x_dot_Vec = [A]*xVec + [B]*uVec => 0=[A]*xVec + [B]*uVec (for steady state solutions, x_dot_vec = [0])
% y_Vec = [C]*xVec + [D]*uVec => y_Vec = [-C*inv(A)*B + D]*uVec -- I use this final equation
% finally, we get:
% y_Vec =(-C*inv(A)*B + D)*uVec => which we can use to determine the states of the system for different uVec-s
% however, in my case, the cantilever system has a single input -- point load in y-direction at the free end of the
%beam and multi-output-- deflections at different points along the length of the beam, so size(uVec) = 1,
% size(yVec)= 4)
% for a point load of 1000 N in -ve y-direction:
uVec = -1000;
% corresponding deflection:
y_Vec = (-C*inv(A)*B + D)*uVec
% executing this, MATLAB gave the following result:
> [yVec' uVec]
0 0.000173657548 0.001367040350 0.003172897623 -1000.000000000000
------
yVec: 4 x 1--> deflections-- 1st-col: at fixed end, 4th-col: at free end, with 2 & 3 probes located at intermediate locations along the length of the beam
---------------------------------------------
In COMSOL, the deflection at free end was computed to be:
Total displacement (m), Point: (1, 0)
0.0031728899855153684
--------------------------------------------
I suspect the discrepancy is mainly due to the default-precision of calculations carried out in MATLAB but am not completely sure. I will explore further with this simple system and then let you know if I could ever come up finally with the reason for such discrepancies.
Madu
Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.
Suggested Content
- FORUM State-Space model for surface to surface radiation
- BLOG Combining Parallel Slices to Create an Animation
- FORUM How to perform Eigenfrequency study with temperature varied from rubbery state to glassy state in COMSOL?
- FORUM Recovery files overrunning disk space
- KNOWLEDGE BASE How can I Migrate my Local Model Manager to the Database of a Model Manager Server