Examples of the withsol Operator


The withsol operator allows you to extract the solution from any solver sequence in the current model and use it in other studies, or during postprocessing.

Comparing the Results Between Two Studies

Let us consider a simple thermal example. A steady-state simulation of a square with a temperature of 20ºC on the left-hand side. We run two studies in the model. One where the temperature on the right-hand side is 100ºC (Study 1) and one where the temperature is 200ºC (Study 2). To evaluate the temperature difference between the two studies, we can use the withsol operator.

The basic form of the withsol operator is: withsol('tag',expr), where 'tag' is the tag of the solver sequence we want to extract the solution from, and expr is the expression we want to evaluate. To be able to see the tag, make sure that tag is selected from the Model Tree Node Text menu at the top of the Model Builder window. The tag for each node appears in curly brackets {} after the node name.

A screenshot of the Model Tree Node Text menu, with the Name and Tag options selected.
The Model Tree Node Text menu, with the Name and Tag options enabled.

To see the difference between the temperatures in Study 2 and Study 1, we create a plot for the Study 2 dataset, and use the expression T-withsol('sol1',T).

A screenshot of the model tree, with the Solution 1 and Surface 1 nodes highlighted, and the Surface Settings window with the withsol Expression highlighted. A screenshot of the model tree, with the Solution 1 and Surface 1 nodes highlighted, and the Surface Settings window with the withsol Expression highlighted. The operator used in the expression for a Surface plot node, which defines the temperature difference between the solution from the second study and solution from the first study.

In this expression, the variable T outside of the withsol operator is taken from Study 2 (with the tag 'sol2'), whereas the withsol operator picks up the temperature from Study 1, which has the tag 'sol1'. The resulting plot shows the temperature difference across the domain, with the difference being 0 at the end where the temperature is the same in both studies.

A screenshot of the Surface Settings window on the left and the Graphics window with a temperature plot on the right. A screenshot of the Surface Settings window on the left and the Graphics window with a temperature plot on the right.
The settings for one of the Surface plot nodes, wherein the operator and utilized, and the resulting plot in the Graphics window.

Comparing the Results Between Two Parameter Values in the Same Study

It is also possible to compare results between two parameters in the same study or between two times in a transient study. If we, instead of using two studies in our example, use only one study with a parameterized temperature on the right-hand side, we can similarly find the difference between the two temperatures. However, we need to use an expanded version of the withsol operator to not only specify which solution we want to extract results from, but also which parameter value. This time, the operator will look like this:

withsol('tag',expr, setval(par,value))

Here, 'tag', and expr are the same as before. What is new is setval(par,value). The operator setval is an operator commonly used together with withsol to specify which value the parameter par should have.

A screenshot of the model tree on the left, with the Solution and Surface nodes selected, and the Surface Settings window on the right, with the Expression highlighted and including the withsol and setval operators. A screenshot of the model tree on the left, with the Solution and Surface nodes selected, and the Surface Settings window on the right, with the Expression highlighted and including the withsol and setval operators. The Settings window for one of the Surface plot nodes, wherein the withsol and setval operators are used in the expression. The solution for Study 3 is included in the expression and is highlighted in the Model Builder window.


Here, T is taken from the setting for our plot, so Study 3 and a parameter value of 200ºC. The withsol operator picks up the values of the variable T from the same study (with the tag 'sol3'), but when the parameter Temp is 100ºC. This is done by setting the value of the parameter Temp to '100[degC]' with the setval operator. This plot gives the same result as the plot above.

Please note that while this can be accomplished with the expanded use of the withsol operator outlined above, there are other more efficient options also available. When comparing results inside the same solution, the at operator using the expression at( and with operator using the expression with(,expr) are preferred. Since they don’t need to map solutions from one solution/xmesh to another and just read from another position in the same solution, this method can be expected to be faster. Additionally, the syntax involved is simplified since you don't need to know the solution tag and don't need to use the setval operator.

Using Results from One Study as Input in Another

In the model Slope Stability in an Embankment Dam, the in situ stresses from pore pressure and gravity are calculated, and the results from that study are used as an initial stress for the safety factor calculation. The in situ stresses are accessed using the withsol operator.

A screenshot of the model tree and Settings window for the Initial Stress and Strain feature. A screenshot of the model tree and Settings window for the Initial Stress and Strain feature. The Settings window for the Initial Stress and Strain feature, wherein the withsol operator is used.

It is additionally possible to use the setind and setval operators as additional arguments to the withsol operator. These operators work with results from a time-dependent solver, an eigenvalue solver, a frequency sweep, or an Auxiliary sweep over one or more more parameters.

The setind operator indexes into the time-domain solution, or the index of the list of eigenvalues, or the index into a sweep over frequency or any other parameter. Positive indices start from the beginning, and negative from the end of the list.

For example, when working with time-domain solutions:

withsol('sol1',expr,setind(t,1)) will return the first timestep. Alternatively, use withsol('sol1',expr,setind(t,'first'))

withsol('sol1',expr,setind(t,2)) will return the second timestep.

withsol('sol1',expr,setind(t,-2)) will return the second-to-last timestep.

withsol('sol1',expr,setind(t,-1)) will return the last timestep. Alternatively, use withsol('sol1',expr,setind(t,'last'))

For eigenvalue solutions, the index variable is lambda, and for frequency sweep the frequency variable is freq.

The setval operator instead extracts the solution at a user-specified value, and interpolates between stored time-domain solutions. For example:

withsol('sol1',expr,setval(t,0.5)) will return the solution at time of 0.5 seconds. If there is no stored data at this timestep, then a solution is interpolated from the nearest stored timesteps. This interpolation will make usage of the time derivatives, if they are stored as shown in the screenshot below of the Time-Dependent Solver. A Hermite interpolation is used if time derivatives are available, otherwise linear interpolation is used. If a value outside of the time range is specified then the nearest timestep (first or last) is used.

The Settings window for the Time-Dependent Solver. The Output section of the Settings window is expanded to display the options available.
Within the Time-Dependent Solver settings, the default option is to store time derivatives.

When used in conjunction with eigenvalue solutions, the setval operator will return a solution that is within a tolerance of the actual eigenvalue. For example:
withsol('sol1',expr, setval(lambda,-100i)) will return the solution even if the eigenvalue is actually -100.01i.

When used with a frequency sweep or auxiliary sweep, the exact numeric value of the frequency or parameter must be specified. If sweeping over multiple frequencies and/or parameters, it is possible to pass multiple arguments to setval, as well as setind, to make the syntax more compact. For example:
withsol('sol1',expr, setval(freq,60,param,5)) is equivalent to withsol('sol1',expr, setval(freq,60),setval(param,5)).

More Advanced Usage of the withsol Operator

For an example of more advanced usage of the withsol operator, see the model Bracket - General Periodic Dynamic Analysis. You can find it among the tutorials for the Structural Mechanics Module in the Application Library in the following files:

  • bracket_general_periodic.mph
  • models.sme.bracket_general_periodic.pdf

In that model, the withsol operator is used to extract frequency-dependent loads from one study and use them as input for the corresponding frequencies in a second study.


Submit feedback about this page or contact support here.