VariableSpring

model VariableSpring "1D translational spring with variable stiffness"
    input Modelica.Blocks.Interfaces.RealInput c(unit = "N/m") "Variable spring constant"
        annotation (Placement(
            visible = true,
            transformation(
                origin = {0, 80},
                extent = {
                    {-20, -20}, 
                    {20, 20}},
                rotation = -90),
            iconTransformation(
                origin = {0, 80},
                extent = {
                    {-20, -20}, 
                    {20, 20}},
                rotation = -90)));

    extends Modelica.Mechanics.Translational.Interfaces.PartialCompliant;

    parameter SI.Distance s_rel0 = 0 "Unstretched spring length";
equation
    f = c * (s_rel - s_rel0);

    annotation (
        Documentation(info = "<html>\n                <p>\n                    A <em>linear 1D translational spring</em>. The component can be connected either\n                    between two sliding masses, or between\n                    a sliding mass and the housing (model Fixed), to describe\n                    a coupling of the sliding mass with the housing via a spring.\n                </p></html>"),
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-60, -90}, 
                        {20, -90}},
                    color = {95, 127, 95}), 
                Polygon(
                    lineColor = {95, 127, 95},
                    fillColor = {95, 127, 95},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {50, -90}, 
                        {20, -80}, 
                        {20, -100}, 
                        {50, -90}}), 
                Line(
                    points = {
                        {-98, 0}, 
                        {-60, 0}, 
                        {-44, -30}, 
                        {-16, 30}, 
                        {14, -30}, 
                        {44, 30}, 
                        {60, 0}, 
                        {100, 0}},
                    color = {0, 127, 0})}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-100, 0}, 
                        {-100, 65}},
                    color = {128, 128, 128}), 
                Line(
                    points = {
                        {100, 0}, 
                        {100, 65}},
                    color = {128, 128, 128}), 
                Line(
                    points = {
                        {-100, 60}, 
                        {100, 60}},
                    color = {128, 128, 128}), 
                Polygon(
                    lineColor = {128, 128, 128},
                    fillColor = {128, 128, 128},
                    fillPattern = FillPattern.Solid,
                    points = {
                        {90, 63}, 
                        {100, 60}, 
                        {90, 57}, 
                        {90, 63}}), 
                Text(
                    lineColor = {0, 0, 255},
                    extent = {
                        {-56, 66}, 
                        {36, 81}},
                    textString = "s_rel"), 
                Line(points = {
                    {-86, 0}, 
                    {-60, 0}, 
                    {-44, -30}, 
                    {-16, 30}, 
                    {14, -30}, 
                    {44, 30}, 
                    {60, 0}, 
                    {84, 0}})}));
end VariableSpring;