## 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.

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. 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.
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. 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.

### 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. The Settings window for the Initial Stress and Strain feature, wherein the withsol operator is used.

### 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.