Simscape global parameter set and change
4 views (last 30 days)
Show older comments
I am working with simulink and simscape inside of it. The gas domain in Simscape is what I use but I kind of need a extra parameter (D) in the gas domain that I can set with the gas properties block. What I did was create a custom gas properties block that has the new extra parameter, but I now want it accessible by other (custom) simcape blocks. I could add an output to the custom gas properties block, which is not really the problem, but I will use it in many simscape components scattered throughout the system/network as it is a global parameter. That is why I want a way to just use the parameter in other block 'inputs'/parameter sections like a value, so instead of 1 or 10 input D and then it resolves to D from gas properties.
I could theoretically make a whole new domain but this means copying all other unchanged components to match that domain instead of foundation.gas.gas which is overkill if you ask me. I also tried using memory blocks in all kind of ways but this does not work either in the easy way I imagined.
Currently the best workaround way seems to have a subsystem with a memory read. Which is coupled with a memory block in the main/top system and a dictionary which then needs to imported to every model using this which holds the original parameter D with not value given. Then set it as initial value in the memory block and adjust using memory write. This seems very unnecessary to me but nowhere can I find any documentation or references that show a better approach. And to match with what my use case is, I need to output the D parameter from the gas properties block into the memory write. So in total 3 blocks and a dictionary, together with many links are necessary to just use a 'global' parameter. I know you could theoretically set values in Matlab, but I want it accessible within Simulink/Simscape as that is where you work and simulate so it is just inconvenient to have multiple places that manage one model.
If anyone knows how to do this better please let me know.
2 Comments
Yifeng Tang
on 24 Feb 2025
From your description, it feels like this D is a variable (a function of time) for the whole domain, not a parameter (NOT a function of time). Is that right?
Do you mind sharing a bit more on the physics side of things why you'd like to add this D or what the physical meaning of this D is? This could help the community understand your question better and propose a solution.
Answers (1)
Yifeng Tang
on 25 Feb 2025
Moved: Sabin
on 11 Aug 2025
This makes sense. Yes, it's indeed a constant, hence a parameter, then. I guess now the question is whether to treat it as a "domain parameter" and customize the whole domain, or treat it as a parameter added when needed to blocks in the gas domain.
Your current workaround is closer to the latter and it does sounds a bit inconvenient. Use a workspace variable would be simpler, but it sounds like you'd like to avoid having to manage the workspace at multiple places? What about using the Simulink Model Workspace? It's visible only to the model. Will that be an acceptable solution?
Now, about the first approach, if you plan to customize quite a few components in future, investing in a custom domain may worth the effort. I think it's going to be simpler than you thought :p You'll need to:
(1) make a copy of all files in C:\Program Files\MATLAB\R2024b\toolbox\physmod\simscape\library\m\+foundation\+gas\ to another folder, with a proper name (see https://www.mathworks.com/help/simscape/lang/building-custom-block-libraries-from-simscape-component-files.html)
(2) modify gas.ssc to add a domain parameter. change the file name hence domain name if you like.
(3) open all .ssc files and search for "foundation.gas", replace it with your new domain name. Start with one file and see if you get the name right, by running ssc_build.
I think that's about it.
And, if you'd like to use components from the Simscape Fluids library, whose source code isn't open, you can write a Gas-NewDomain interface block, like these: https://www.mathworks.com/help/releases/R2024b/hydro/fluid-fluid-interfaces.html?s_tid=CRUX_lftnav . The source code is available. Because your domain is almost the same as the gas domain, the equations will be super simple.
Hope this helps.
2 Comments
Yifeng Tang
on 26 Feb 2025
Moved: Sabin
on 11 Aug 2025
It's easy to define a "Model Workspace" variable as part of your model. Whenever the model is opened, it's available to every block in the model, but not visible in MATLAB base workspace. This variable can have its value defined as part of the model, or using a script embedded in the model (so you can do some calculation instead of just a value). See this page: https://www.mathworks.com/help/simulink/ug/specify-source-for-data-in-model-workspace.html
Basically you define it once in the model, and it's available ... forever. Will this work?
See Also
Categories
Find more on Gas Models in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!