LineForceWithTwoMasses

model LineForceWithTwoMasses "General line force component with two optional point masses on the connection line"
    import Modelica.Mechanics.MultiBody.Types;

    extends Interfaces.LineForceBase;

    Modelica.Mechanics.Translational.Interfaces.Flange_a flange_b "1-dim. translational flange (connect force of Translational library between flange_a and flange_b)"
        annotation (Placement(transformation(
            origin = {60, 110},
            extent = {
                {-10, -10}, 
                {10, 10}},
            rotation = 90)));
    Modelica.Mechanics.Translational.Interfaces.Flange_b flange_a "1-dim. translational flange (connect force of Translational library between flange_a and flange_b)"
        annotation (Placement(transformation(
            origin = {-60, 110},
            extent = {
                {-10, -10}, 
                {10, 10}},
            rotation = 90)));
    parameter Boolean animate = true "= true, if animation shall be enabled";
    parameter Boolean animateMasses = true "= true, if point masses shall be visualized provided animate=true and m_a, m_b > 0";
    parameter SI.Mass m_a(min = 0) = 0 "Mass of point mass a on the connection line between the origin of frame_a and the origin of frame_b";
    parameter SI.Mass m_b(min = 0) = 0 "Mass of point mass b on the connection line between the origin of frame_a and the origin of frame_b";
    parameter SI.Position L_a = 0 "Distance between point mass a and frame_a (positive, if in direction of frame_b)";
    parameter SI.Position L_b = L_a "Distance between point mass b and frame_b (positive, if in direction of frame_a)";
    input Types.SpecularCoefficient specularCoefficient = world.defaultSpecularCoefficient "Reflection of ambient light (= 0: light is completely absorbed)"
        annotation (Dialog(
            tab = "Animation",
            enable = animate));
    input SI.Diameter cylinderDiameter_a = world.defaultForceWidth "Diameter of cylinder at frame_a"
        annotation (Dialog(
            tab = "Animation",
            group = "Cylinder at frame_a if animate = true",
            enable = animate));
    parameter SI.Length cylinderLength_a = 2 * L_a "Length of cylinder at frame_a"
        annotation (Dialog(
            tab = "Animation",
            group = "Cylinder at frame_a if animate = true",
            enable = animate));
    input Types.Color color_a = {155, 155, 155} "Color of cylinder at frame_a"
        annotation (Dialog(
            colorSelector = true,
            tab = "Animation",
            group = "Cylinder at frame_a if animate = true",
            enable = animate));
    input Real diameterFraction = 0.8 "Diameter of cylinder at frame_b with respect to diameter of cylinder at frame_a"
        annotation (Dialog(
            tab = "Animation",
            group = "Cylinder at frame_b if animate = true",
            enable = animate));
    parameter SI.Length cylinderLength_b = 2 * L_b "Length of cylinder at frame_b"
        annotation (Dialog(
            tab = "Animation",
            group = "Cylinder at frame_b if animate = true",
            enable = animate));
    input Types.Color color_b = {100, 100, 100} "Color of cylinder at frame_b"
        annotation (Dialog(
            colorSelector = true,
            tab = "Animation",
            group = "Cylinder at frame_b if animate = true",
            enable = animate));
    input Real massDiameterFaction = 1.7 "Diameter of point mass spheres with respect to cylinderDiameter_a"
        annotation (Dialog(
            tab = "Animation",
            group = "if animate = true and animateMasses = true",
            enable = animate and animateMasses));
    input Types.Color massColor = Modelica.Mechanics.MultiBody.Types.Defaults.BodyColor "Color of point masses"
        annotation (Dialog(
            colorSelector = true,
            tab = "Animation",
            group = "if animate = true and animateMasses = true",
            enable = animate and animateMasses));
protected
    SI.Force fa "Force from flange_a";
    SI.Force fb "Force from flange_b";
    SI.Position r_CM1_0[3](each stateSelect = StateSelect.avoid) "Position vector from world frame to point mass 1, resolved in world frame";
    SI.Position r_CM2_0[3](each stateSelect = StateSelect.avoid) "Position vector from world frame to point mass 2, resolved in world frame";
    SI.Velocity v_CM1_0[3](each stateSelect = StateSelect.avoid) "der(r_CM_1_0) - velocity of point mass 1";
    SI.Velocity v_CM2_0[3](each stateSelect = StateSelect.avoid) "der(r_CM_2_0) - velocity of point mass 2";
    SI.Acceleration ag_CM1_0[3] "der(v_CM1_0) - gravityAcceleration(r_CM1_0)";
    SI.Acceleration ag_CM2_0[3] "der(v_CM2_0) - gravityAcceleration(r_CM2_0)";
    SI.Force aux1_0[3] "Auxiliary force 1";
    SI.Force aux2_0[3] "Auxiliary force 2";
    input SI.Length cylinderDiameter_b = cylinderDiameter_a * diameterFraction;
    input SI.Length massDiameter = cylinderDiameter_a * massDiameterFaction;
    parameter Boolean animateMasses2 = world.enableAnimation and animate and animateMasses and 0 < m_a and 0 < m_b;
    Visualizers.Advanced.Shape cylinder_a(shapeType = "cylinder", color = color_a, specularCoefficient = specularCoefficient, length = cylinderLength_a, width = cylinderDiameter_a, height = cylinderDiameter_a, lengthDirection = e_rel_0, widthDirection = {0, 1, 0}, r = frame_a.r_0) if world.enableAnimation and animate;
    Visualizers.Advanced.Shape cylinder_b(shapeType = "cylinder", color = color_b, specularCoefficient = specularCoefficient, length = cylinderLength_b, width = cylinderDiameter_b, height = cylinderDiameter_b, lengthDirection = -e_rel_0, widthDirection = {0, 1, 0}, r = frame_b.r_0) if world.enableAnimation and animate;
    Visualizers.Advanced.Shape sphere_a(shapeType = "sphere", color = massColor, specularCoefficient = specularCoefficient, length = massDiameter, width = massDiameter, height = massDiameter, lengthDirection = e_rel_0, widthDirection = {0, 1, 0}, r_shape = e_rel_0 * (L_a - 0.5 * massDiameter), r = frame_a.r_0) if animateMasses2;
    Visualizers.Advanced.Shape sphere_b(shapeType = "sphere", color = massColor, specularCoefficient = specularCoefficient, length = massDiameter, width = massDiameter, height = massDiameter, lengthDirection = -e_rel_0, widthDirection = {0, 1, 0}, r_shape = -e_rel_0 * (L_b - 0.5 * massDiameter), r = frame_b.r_0) if animateMasses2;
equation
    if 0 < m_a or 0 < m_b then 
        r_CM1_0 = frame_a.r_0 + e_rel_0 * L_a;
        r_CM2_0 = frame_b.r_0 - e_rel_0 * L_b;
        v_CM1_0 = der(r_CM1_0);
        v_CM2_0 = der(r_CM2_0);
        ag_CM1_0 = der(v_CM1_0) - world.gravityAcceleration(r_CM1_0);
        ag_CM2_0 = der(v_CM2_0) - world.gravityAcceleration(r_CM2_0);
        aux1_0 = ag_CM1_0 * (m_a * L_a / length) - ag_CM2_0 * (m_b * L_b / length);
        aux2_0 = aux1_0 - aux1_0 * e_rel_0 * e_rel_0;
        frame_a.f = Frames.resolve2(frame_a.R, m_a * ag_CM1_0 - aux2_0 - e_rel_0 * fa);
        frame_b.f = Frames.resolve2(frame_b.R, m_b * ag_CM2_0 + aux2_0 - e_rel_0 * fb);
    else 
        r_CM1_0 = zeros(3);
        r_CM2_0 = zeros(3);
        v_CM1_0 = zeros(3);
        v_CM2_0 = zeros(3);
        ag_CM1_0 = zeros(3);
        ag_CM2_0 = zeros(3);
        aux1_0 = zeros(3);
        aux2_0 = zeros(3);
        frame_a.f = -Frames.resolve2(frame_a.R, e_rel_0 * fa);
        frame_b.f = -Frames.resolve2(frame_b.R, e_rel_0 * fb);
    end if;
    if 0 < cardinality(flange_a) and 0 < cardinality(flange_b) then 
        fa = flange_a.f;
        fb = flange_b.f;
    elseif 0 < cardinality(flange_a) and cardinality(flange_b) == 0 then 
        fa = flange_a.f;
        fb = -fa;
    elseif cardinality(flange_a) == 0 and 0 < cardinality(flange_b) then 
        fa = -fb;
        fb = flange_b.f;
    else 
        fa = 0;
        fb = 0;
    end if;
    flange_a.s = 0;
    flange_b.s = length;

    annotation (
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Ellipse(
                    extent = {
                        {-100, -40}, 
                        {-20, 40}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Ellipse(
                    extent = {
                        {-90, -30}, 
                        {-30, 30}},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Ellipse(
                    extent = {
                        {20, -40}, 
                        {100, 40}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Ellipse(
                    extent = {
                        {31, -29}, 
                        {91, 30}},
                    lineColor = {128, 128, 128},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-150, -55}, 
                        {150, -95}},
                    textString = "%name",
                    lineColor = {0, 0, 255}), 
                Rectangle(
                    extent = {
                        {-52, 40}, 
                        {48, -40}},
                    lineColor = {255, 255, 255},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Ellipse(
                    extent = {
                        {-74, 15}, 
                        {-45, -13}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Ellipse(
                    extent = {
                        {45, 14}, 
                        {74, -14}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Line(points = {
                    {-60, 0}, 
                    {-60, 23}, 
                    {-30, 23}, 
                    {-30, 70}, 
                    {-60, 70}, 
                    {-60, 101}}), 
                Line(points = {
                    {60, 0}, 
                    {60, 20}, 
                    {30, 20}, 
                    {30, 70}, 
                    {60, 70}, 
                    {60, 100}}), 
                Line(
                    points = {
                        {-23, 0}, 
                        {25, 0}},
                    pattern = LinePattern.Dot), 
                Ellipse(
                    extent = {
                        {23, 8}, 
                        {39, -8}},
                    fillPattern = FillPattern.Solid), 
                Ellipse(
                    extent = {
                        {-39, 8}, 
                        {-23, -8}},
                    fillPattern = FillPattern.Solid), 
                Line(points = {
                    {-60, 0}, 
                    {-29, 0}}), 
                Line(points = {
                    {29, 0}, 
                    {60, 0}}), 
                Ellipse(
                    visible = fixedRotationAtFrame_a,
                    extent = {
                        {-70, 30}, 
                        {-130, -30}},
                    lineColor = {255, 0, 0}), 
                Text(
                    visible = fixedRotationAtFrame_a,
                    extent = {
                        {-62, 50}, 
                        {-140, 30}},
                    lineColor = {255, 0, 0},
                    textString = "R=0"), 
                Ellipse(
                    visible = fixedRotationAtFrame_b,
                    extent = {
                        {70, 30}, 
                        {130, -30}},
                    lineColor = {255, 0, 0}), 
                Text(
                    visible = fixedRotationAtFrame_b,
                    extent = {
                        {62, 50}, 
                        {140, 30}},
                    lineColor = {255, 0, 0},
                    textString = "R=0")}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-60, 80}, 
                        {46, 80}},
                    color = {0, 0, 255}), 
                Polygon(
                    points = {
                        {60, 80}, 
                        {45, 86}, 
                        {45, 74}, 
                        {60, 80}},
                    lineColor = {0, 0, 255},
                    fillColor = {0, 0, 255},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-42, 91}, 
                        {30, 79}},
                    textString = "length",
                    lineColor = {0, 0, 255}), 
                Ellipse(
                    extent = {
                        {-100, -40}, 
                        {-20, 40}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Ellipse(
                    extent = {
                        {-90, -30}, 
                        {-30, 30}},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Ellipse(
                    extent = {
                        {20, -40}, 
                        {100, 40}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Ellipse(
                    extent = {
                        {31, -29}, 
                        {91, 30}},
                    lineColor = {128, 128, 128},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Rectangle(
                    extent = {
                        {-49, 39}, 
                        {51, -41}},
                    lineColor = {255, 255, 255},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Ellipse(
                    extent = {
                        {-74, 15}, 
                        {-45, -13}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Ellipse(
                    extent = {
                        {45, 15}, 
                        {74, -13}},
                    fillPattern = FillPattern.Sphere,
                    fillColor = {192, 192, 192}), 
                Line(points = {
                    {-60, 0}, 
                    {-60, 24}, 
                    {-40, 24}, 
                    {-40, 60}, 
                    {-60, 60}, 
                    {-60, 110}}), 
                Line(points = {
                    {60, 1}, 
                    {60, 21}, 
                    {40, 21}, 
                    {40, 60}, 
                    {60, 60}, 
                    {60, 110}}), 
                Line(
                    points = {
                        {-60, 0}, 
                        {60, 0}},
                    pattern = LinePattern.Dot), 
                Ellipse(
                    extent = {
                        {20, 8}, 
                        {36, -8}},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-18, -18}, 
                        {11, -18}},
                    color = {0, 0, 255}), 
                Polygon(
                    points = {
                        {23, -18}, 
                        {11, -15}, 
                        {11, -21}, 
                        {23, -18}},
                    lineColor = {0, 0, 255}), 
                Line(
                    points = {
                        {-60, 16}, 
                        {-37, 16}},
                    color = {0, 0, 255}), 
                Line(
                    points = {
                        {-25, 0}, 
                        {-25, 20}},
                    color = {0, 0, 255}), 
                Text(
                    extent = {
                        {-38, -20}, 
                        {33, -35}},
                    textString = "e_rel_0"), 
                Polygon(
                    points = {
                        {-25, 16}, 
                        {-37, 19}, 
                        {-37, 13}, 
                        {-25, 16}},
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {-39, 31}, 
                        {-22, 21}},
                    textString = "L_a"), 
                Ellipse(
                    extent = {
                        {-33, 7}, 
                        {-17, -9}},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {29, 3}, 
                        {29, 22}},
                    color = {0, 0, 255}), 
                Line(
                    points = {
                        {29, 16}, 
                        {60, 16}},
                    color = {0, 0, 255}), 
                Polygon(
                    points = {
                        {29, 16}, 
                        {41, 19}, 
                        {41, 13}, 
                        {29, 16}},
                    lineColor = {0, 0, 255}), 
                Text(
                    extent = {
                        {15, 36}, 
                        {32, 26}},
                    textString = "L_b"), 
                Line(
                    points = {
                        {37, 18}, 
                        {30, 27}},
                    pattern = LinePattern.Dot,
                    color = {0, 0, 255})}),
        Documentation(info = "<html>\n<p>\nThis component is used to exert a <strong>line force</strong>\nbetween the origin of frame_a and the origin of frame_b\nby attaching components of the <strong>1-dimensional translational</strong>\nmechanical library of Modelica (Modelica.Mechanics.Translational)\nbetween the two flange connectors <strong>flange_a</strong> and\n<strong>flange_b</strong>. Optionally, there are <strong>two point masses</strong> on the line\nconnecting the origin of frame_a and the origin of frame_b.\nThese point masses approximate the <strong>masses</strong> of the <strong>force element</strong>.\nThe locations of the two point masses are defined by their\n(fixed) distances of L_a relative to frame_a and of L_b relative\nto frame_b, respectively.\n</p>\n<p>\nIn example\n<a href=\"modelica://Modelica.Mechanics.MultiBody.Examples.Elementary.LineForceWithTwoMasses\">\nMultiBody.Examples.Elementary.LineForceWithTwoMasses</a> the usage of this\nline force element is shown and is compared with an alternative\nimplementation using a\n<a href=\"modelica://Modelica.Mechanics.MultiBody.Joints.Assemblies.JointUPS\">\nMultiBody.Joints.Assemblies.JointUPS</a> component.\nThe composition diagram of this example\nis displayed in the figure below.\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Mechanics/MultiBody/Examples/Elementary/LineForceWithTwoMasses1.png\">\n</p>\n\n<p>\nThe animation view at time = 0 is shown in the next figure.\nThe system on the left side in the front is the animation with\nthe LineForceWithTwoMasses component whereas the system on the right\nside in the back is the animation with the JointUPS component.\nBoth implementations yield the same result. However, the implementation\nwith the LineForceWithTwoMasses component is simpler.\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Mechanics/MultiBody/Examples/Elementary/LineForceWithTwoMasses2.png\">\n</p>\n\n<p>\nIn the translational library there is the implicit assumption that\nforces of components that have only one flange connector act with\nopposite sign on the bearings of the component. This assumption\nis also used in the LineForceWithTwoMasses component: If a connection\nis present to only one of the flange connectors, then the force\nin this flange connector acts implicitly with opposite sign also\nin the other flange connector.\n</p>\n</html>"));
end LineForceWithTwoMasses;