Is it possible to change a model's parametrization when running a script? - modelica

For example:
in my script I launch a simulation of a model that have, among other things, 10 water volumes and then store all the results in a specified .mat file.
With the final values of certain variables I would like to parametrize the same 10 volumes in another model, all in the same script.
I'm not quite skilled with Modelica languange (yet) but I firmly think that this is possible without the tediousness of doing it by hand.
Thanks.

Related

How to use VTK to efficiently write time-varying field data on a fixed mesh?

I am working on physics simulation research. I have a large fixed grid in one of my projects that does not vary with time. The fields on the grid, on the other hand, vary with time in the simulation. I need to use VTK to record the field data in each step for visualization (Paraview).
The method I am using is to write a separate *.vtu file to disk at each time step. This basically serves the purpose, but actually writes a lot of duplicate data (re-recording the geometry of the mesh at each step), which not only consumes more disk space, but also wastes time on encoding and parsing.
I would like to have a way to write the mesh information only once, and the rest of the time only new field data is written, while being able to guarantee the same visualization. Please let me know if VTK and Paraview provide such an interface and how to implement it.
Using .pvtu and refer to the same .vtu as Piece for each step should do the trick.
See this similar post on the ParaView discourse, and the pvtu doc
EDIT
This seems to be a side effect of the format, this is not supported by the writer.
The correct solution is to use another file format ...
Let me provide my own research findings for reference.
As Nico said, with the combination of pvtu/vtu files, we could theoretically implement a geometry structure stored in a separate vtu file, referenced by a pvtu file. Setting the NumberOfPieces attribute of the ptvu file to 1 would enable the construction of only one separate vtu file.
However, the VTK library does not expose a dedicated operation interface to control the writing process of vtu files. No matter how it is set, as long as the writer's input contains geometry structures, the writer will write geometry information to disk, and this process cannot be skipped through the exposed interface.
However, it is indeed possible to make multiple pvtu files point to the same vtu file by manually editing the piece node in the ptvu file, and paraview can recognize and visualize such a file group properly.
I did not proceed to try adding arrays to the unstructured grid and using pvtu output.
So, I think the conclusion is.
if you don't want to dive into VTK's library code and XML implementation, then this approach doesn't make sense.
if you are willing to write a series of files, delete most of them from the vtu file, and then point all the pvtu's piece nodes to the only surviving vtu file by editing the pvtu file, you can save a lot of disk space, but will not shorten the write, read, and parse times.
If you implement an XML writer by yourself, you can achieve all the requirements in theory, but it requires a lot of coding work.

Modelica command which can select all components of the same type in a model

I am looking for a command in Modelica which can select all the components of the same type.
I want to summarize a volume of an extensive pipe system consisting of over a 100 of single dynamic pipes and save it later as a variable.
Currently Modelica does not have that.
However, there's a proposal that would change that https://github.com/modelica/ModelicaSpecification/tree/MCP/0021/RationaleMCP/0021
It has been test-implemented in Dymola, and possibly other tools.
See also the paper:
https://ep.liu.se/ecp/118/026/ecp15118245.pdf
The other alternative would be to replace the Pipe-model with a specialized pipe-model that communicated the total volume using e.g., inner/outer.

Combining/Merging two SimBIology Models

I am making two separate SimBiology Models with the same compartments but different species. Transport between the compartments are different for each species. I want to combine the two models so I can add an interaction term between the two species and use the simulations to get concentration profiles of both the species. Is there a function to do that in SimBiology? I have not been able to find one. If not, what would be the best approach to code a function to do so?
The most relevant function is copyobj. You can also use the SimBiology Desktop's graphical interface to copy and paste one model into another. I also have a prototype of a function for combining two models that I would be happy to share. If you're interested in that, please contact me via my MathWorks community profile.
Your best bet would be to write a script based on copyobj function provided by Matlab, if you want to copy programmatically. You need to be little cautious here as this function throws an error, IF the object copied and the target model have name conflicts. For example, if you want to copy a compartment by name 'C1' from a source model to a target model that already contains a compartment by the same name ('C1'), then the program throws an error due to name conflicts. In such a case you have to programmatically rename the source 'C1' compartment before you copy it to the target model. The rename command provided by Matlab might be useful.
Even after overcoming the errors due to name conflicts, there is another problem with the above method --- the layout/diagram of the models will not be preserved. If you want to preserve the model layout (that is the diagram of the model), you can use GUI based simbiology model builder. First you manually select the model/submodel that you want to copy, click 'Ctrl+C' (like you would do for copying a text) and paste (Ctrl+V) it in your target model. The name conflicts will be automatically handled by the matlab. That is the source 'C1' compartment will be automatically renamed by matlab (to probably 'C2'), and pasted in the target model.
You may look at this function, that probably helps you to build your custom model merger programmatically. It is an old function that works with older versions of Matlab, and may not be relevant now. But it may provide some guidance on building your own script.

Make A step in a SAS macro timeout after a set interval

I'm on SAS 9.1.3 (on a server) and have a macro looping over an array to feed a computationally intensive set of modelling steps which are appended out to a table. I'm wondering if it is possible to set a maximum time to run for each element of the array. This is so that any element which takes longer than 3 minutes to run is skipped and the next item fed in.
Say for example I'm using a proc nlin with a by statement to build separate models per class on a large data set, and one class is failing to converge; how do I skip over that class?
Bit of a niche requirement, hope someone can assist!
The only approach I can think of here would be to rewrite your code so that it runs each by group separately from the rest, in one or more SAS/CONNECT sessions, have the parent session kill each one after a set timeout, and then recombine the surviving output.
As Dom and Joe have pointed out, this is not a trivial task, but it's possible if you're sufficiently keen on learning about that aspect of SAS. A good place to get started for this sort of thing would be this page:
http://support.sas.com/rnd/scalability/tricks/connect.html
I was able to use the examples there and elsewhere as the basis of a simple parallel processing framework (in SAS 9.1.3, coincidentally!), but there are many details you will need to consider. To give you an idea of the sorts of adventures in store if you go down this route:
Learning how to sign on to your server via SAS/CONNECT within whatever infrastructure you're using (will the usual autoexec file work? What invocation options do you need to use?)
Explaining to your sysadmin/colleagues why you need to run multiple processes in parallel
Managing asynchronous sessions
Syncing macro variables, macro definitions, libraries and formats between sessions
Obscure bugs (I wasn't able to use the usual option for syncing libraries and had to roll my own via call execute...)
One could write a (lengthy) SUGI paper on this topic, and I'm sure there are plenty of them out there if you look around.
In general, SAS is running in a linear manner. So you cannot write a step to monitor another step in the same program. What you could do is run your code in a SAS/CONNECT session and monitor it with the process that started the session. That's not trivial and the how to is beyond the scope of Stack Overflow.
For a data step, use the datetime() function to get the current system date and time. This is measured in seconds. You can check the time inside your data step. Stop a data step with the stop; statement.
Now you specifically asked about breaking a specific step inside a PROC. That must be implemented in the PROC by the SAS developer. If it is possible, it will be documented in the procedure's documentation. View SAS documentation at http://support.sas.com/documentation/.
For PROC NLIN, I do not think there is a "break after X" parameter. You can use the trace parameters to track model execution to see what it hanging up. You can then work on changing the convergence parameters to attempt to speed up slow, badly converging, models.

Generating sub-systems based on user input (MATLAB/SimMechanics)

The user in this webinar;
http://www.mathworks.com.au/videos/parameterizing-bodies-68850.html?form_seq=conf1134
can create new levels of links for the scissor lift by copy pasting the sub systems.
I was wondering if there was any way the number of subsystems and the joints could be automated via user input.
i.e a gui which allows the user to input the number of levels in the scissor lift and that number of levels (subsystems) is generated in SimMechanics.
If someone could provide a solution I could adapt it to the problem I'm trying to solve.
Thanks in advance!
Yes, you can automate it, as long as you know what susbsytems and what joints you want to add. The functions of interest are:
add_block(path_to_your_subsystem,path_to_destination_subsystem) (I assume your susbsystem is stored in a library). You probably want to specify the 'Position` parameter so that all blocks don't end up on top of each other. It will take some experimenting to find coordinates that work for your model and that are parameterised based on the number of susbystems to add.
add_line(path_to_subsystem_of_interest,path_to_output_port,path_to_input_port). You'll need to know which port you want to connect to which and figure out how many times you need to do this based on the number of subsystems to add. Simscape and SimMechanics are a special type of ports, and you need to refer to them correctly otherwise it won't work, see Programatically connect two subsystems for more details (note: this is undocumented as far as I know and is therefore likely to change in a future release).
So in short, yes it's possible (I've done it in the past), but it's by no means easy. See this blog for a very simple introduction.