<commit_before>//!
//! @file   HydraulicCylinderC.hpp
//! @author <NAME> <<EMAIL>>
//! @date   2010-01-20
//!
//! @brief Contains a Hydraulic Cylinder of C type
//! Written by <NAME> 9005617
//! Revised by ??? 910410
//! Translated to C++ by <NAME> 100120
//! Revised by <NAME> 110429
//!
//$Id$

#ifndef HYDRAULICCYLINDERC_HPP_INCLUDED
#define HYDRAULICCYLINDERC_HPP_INCLUDED

#include <sstream>

#include "../../ComponentEssentials.h"
#include "../../ComponentUtilities.h"

namespace hopsan {

//!
//! @brief
//! @ingroup HydraulicComponents
//!
class HydraulicCylinderC : public ComponentC
{

    private:
        double A1,A2,sl,cLeak,bp,me,betae,V01,V02, CxLim, ZxLim, wfak;

        double c1, ci1, cl1, c2, ci2, cl2;  //Members because old value need to be remembered
        double mNum[2];
        double mDen[2];
        FirstOrderFilter mFilter;
        Integrator mInt;

        //Node data pointers
        double *mpND_p1, *mpND_q1, *mpND_c1, *mpND_Zc1, *mpND_p2, *mpND_q2, *mpND_c2, *mpND_Zc2, *mpND_f3, *mpND_x3, *mpND_v3, *mpND_c3, *mpND_Zx3;

        //Ports
        Port *mpP1, *mpP2, *mpP3;

    public:
        static Component *Creator()
        {
            return new HydraulicCylinderC("CylinderC");
        }

        HydraulicCylinderC(const std::string name) : ComponentC(name)
        {
            //Set member attributes
            wfak = 0.1;
            betae = 1000000000.0;
            me = 100.0;
            V01 = 0.0003;
            V02 = 0.0003;
            A1 = 0.001;
            A2 = 0.001;
            sl = 1.0;
            cLeak = 0.00000000001;
            bp = 1000.0;

            //Add ports to the component
            mpP1 = addPowerPort("P1", "NodeHydraulic");
            mpP2 = addPowerPort("P2", "NodeHydraulic");
            mpP3 = addPowerPort("P3", "NodeMechanic");

            //Register changable parameters to the HOPSAN++ core
            registerParameter("A_1", "Piston Area 1", "[m^2]", A1);
            registerParameter("A_2", "Piston Area 2", "[m^2]", A2);
            registerParameter("s_l", "Stroke", "[m]", sl);
            registerParameter("m_e", "Equivalent Load Mass", "[kg]", me);
            registerParameter("V_1", "Dead Volume in Chamber 1", "[m^3]", V01);
            registerParameter("V_2", "Dead Volume in Chamber 2", "[m^3]", V02);
            registerParameter("B_p", "Viscous Friction", "[Ns/m]", bp);
            registerParameter("Beta_e", "Bulk Modulus", "[Pa]", betae);
            registerParameter("c_leak", "Leakage Coefficient", "[]", cLeak);

            setStartValue(mpP1, NodeHydraulic::PRESSURE, 1.0e5);
            setStartValue(mpP2, NodeHydraulic::PRESSURE, 1.0e5);
        }


        void initialize()
        {
            //Assign node data pointers
            mpND_p1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::PRESSURE);
            mpND_q1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::FLOW);
            mpND_c1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::WAVEVARIABLE);
            mpND_Zc1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::CHARIMP);
            mpND_p2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::PRESSURE);
            mpND_q2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::FLOW);
            mpND_c2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::WAVEVARIABLE);
            mpND_Zc2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::CHARIMP);
            mpND_f3 = getSafeNodeDataPtr(mpP3, NodeMechanic::FORCE);
            mpND_x3 = getSafeNodeDataPtr(mpP3, NodeMechanic::POSITION);
            mpND_v3 = getSafeNodeDataPtr(mpP3, NodeMechanic::VELOCITY);
            mpND_c3 = getSafeNodeDataPtr(mpP3, NodeMechanic::WAVEVARIABLE);
            mpND_Zx3 = getSafeNodeDataPtr(mpP3, NodeMechanic::CHARIMP);

            //Declare local variables;
            double p1, q1, p2, q2, f3, x3, v3;
            double Zc1, Zc2, c3, Zx3;
            double qi1, qi2, V1, V2, qLeak, V1min, V2min;

            //Read variables from nodes
            p1 = (*mpND_p1);
            q1 = (*mpND_q1);
            p2 = (*mpND_p2);
            q2 = (*mpND_q2);
            f3 = (*mpND_f3);
            x3 = (*mpND_x3);
            v3 = (*mpND_v3);

            //Limit end of stroke limitations
            CxLim = 0;
            ZxLim = 0;

            //Size of volumes
            V1 = V01+A1*(-x3);
            V2 = V01+A2*(sl+x3);
            V1min = betae*mTimestep*mTimestep*A1*A1/(wfak*me);
            V2min = betae*mTimestep*mTimestep*A2*A2/(wfak*me);
            if(V1<V1min) V1 = V1min;
            if(V2<V2min) V2 = V2min;

            //Impedences
            Zc1 = betae*mTimestep/V1;
            Zc2 = betae*mTimestep/V2;
            Zx3 = A1*A1*Zc1 +A2*A2*Zc2 + bp;

            //Internal flows
            qi1 = v3*A1;
            qi2 = -v3*A2;

            //Leakage flow
            qLeak = cLeak*(p1-p2);

            //Wave variables
            c1 = p1 + Zc1*q1;
            ci1 = p1 + Zc1*qi1;
            cl1 = p1 + Zc1*(-qLeak);

            c2 = p2 + Zc2*q2;
            ci2 = p2 + Zc2*qi2;
            cl2 = p2 + Zc2*qLeak;

            c3 = A1*ci1 - A2*ci2;

            //Write to nodes
            (*mpND_c1) = c1;
            (*mpND_Zc1) = Zc1;
            (*mpND_c2) = c2;
            (*mpND_Zc2) = Zc2;
            (*mpND_c3) = c3;
            (*mpND_Zx3) = Zx3;
        }

        void simulateOneTimestep()
        {
            //Declare local variables;
            double p1, q1, p2, q2, f3, x3, v3;
            double Zc1, Zc2, c3, Zx3;

            double V1, V2, qLeak, qi1, qi2, p1mean, p2mean, V1min, V2min;
            double alpha=0.5;

            //Read variables from nodes
            p1 = (*mpND_p1);
            q1 = (*mpND_q1);
            p2 = (*mpND_p2);
            q2 = (*mpND_q2);
            f3 = (*mpND_f3);
            x3 = (*mpND_x3);
            v3 = (*mpND_v3);

            //Leakage flow
            qLeak = cLeak*(p1-p2);

            //Internal flows
            qi1 = v3*A1;
            qi2 = -v3*A2;

            //Size of volumes
            V1 = V01+A1*(-x3);
            V2 = V01+A2*(sl+x3);
            V1min = betae*mTimestep*mTimestep*A1*A1/(0.1*me);       //0.1 was called "WFAK" in old Hopsan
            V2min = betae*mTimestep*mTimestep*A2*A2/(0.1*me);
            if(V1<V1min) V1 = V1min;
            if(V2<V2min) V2 = V2min;

            //Volume equations
            Zc1 = 3 / 2 * betae/V1*mTimestep/(1-alpha);
            p1mean = ((c1 + Zc1*2*q1) + (ci1 + Zc1*2*qi1) + (cl1 + Zc1*2*(-qLeak))) / 3;
            c1 = alpha * c1 + (1.0 - alpha)*(p1mean*2 - c1 - 2*Zc1*q1);
            ci1 = alpha * ci1 + (1.0 - alpha)*(p1mean*2 - ci1 - 2*Zc1*qi1);
            cl1 = alpha * cl1 + (1.0 - alpha)*(p1mean*2 - cl1 - 2*Zc1*(-qLeak));

            Zc2 = 3 / 2 * betae/V2*mTimestep/(1-alpha);
            p2mean = ((c2 + Zc2*2*q2) + (ci2 + Zc2*2*qi2) + (cl2 + Zc2*2*qLeak)) / 3;
            c2 = alpha * c2 + (1.0 - alpha)*(p2mean*2 - c2 - 2*Zc2*q2);
            ci2 = alpha * ci2 + (1.0 - alpha)*(p2mean*2 - ci2 - 2*Zc2*qi2);
            cl2 = alpha * cl2 + (1.0 - alpha)*(p2mean*2 - cl2 - 2*Zc2*qLeak);


            //limitStroke(CxLim, ZxLim, x3, v3, me, sl);
<commit_msg>Minor fix in cylinder c (should not affect simulation results)<commit_after>//!
//! @file   HydraulicCylinderC.hpp
//! @author <NAME> <<EMAIL>>
//! @date   2010-01-20
//!
//! @brief Contains a Hydraulic Cylinder of C type
//! Written by <NAME> 9005617
//! Revised by ??? 910410
//! Translated to C++ by <NAME> 100120
//! Revised by <NAME> 110429
//!
//$Id$

#ifndef HYDRAULICCYLINDERC_HPP_INCLUDED
#define HYDRAULICCYLINDERC_HPP_INCLUDED

#include <sstream>

#include "../../ComponentEssentials.h"
#include "../../ComponentUtilities.h"

namespace hopsan {

//!
//! @brief
//! @ingroup HydraulicComponents
//!
class HydraulicCylinderC : public ComponentC
{

    private:
        double A1,A2,sl,cLeak,bp,me,betae,V01,V02, CxLim, ZxLim, wfak;

        double c1, ci1, cl1, c2, ci2, cl2;  //Members because old value need to be remembered
        double mNum[2];
        double mDen[2];
        FirstOrderFilter mFilter;
        Integrator mInt;

        //Node data pointers
        double *mpND_p1, *mpND_q1, *mpND_c1, *mpND_Zc1, *mpND_p2, *mpND_q2, *mpND_c2, *mpND_Zc2, *mpND_f3, *mpND_x3, *mpND_v3, *mpND_c3, *mpND_Zx3;

        //Ports
        Port *mpP1, *mpP2, *mpP3;

    public:
        static Component *Creator()
        {
            return new HydraulicCylinderC("CylinderC");
        }

        HydraulicCylinderC(const std::string name) : ComponentC(name)
        {
            //Set member attributes
            wfak = 0.1;
            betae = 1000000000.0;
            me = 100.0;
            V01 = 0.0003;
            V02 = 0.0003;
            A1 = 0.001;
            A2 = 0.001;
            sl = 1.0;
            cLeak = 0.00000000001;
            bp = 1000.0;

            //Add ports to the component
            mpP1 = addPowerPort("P1", "NodeHydraulic");
            mpP2 = addPowerPort("P2", "NodeHydraulic");
            mpP3 = addPowerPort("P3", "NodeMechanic");

            //Register changable parameters to the HOPSAN++ core
            registerParameter("A_1", "Piston Area 1", "[m^2]", A1);
            registerParameter("A_2", "Piston Area 2", "[m^2]", A2);
            registerParameter("s_l", "Stroke", "[m]", sl);
            registerParameter("m_e", "Equivalent Load Mass", "[kg]", me);
            registerParameter("V_1", "Dead Volume in Chamber 1", "[m^3]", V01);
            registerParameter("V_2", "Dead Volume in Chamber 2", "[m^3]", V02);
            registerParameter("B_p", "Viscous Friction", "[Ns/m]", bp);
            registerParameter("Beta_e", "Bulk Modulus", "[Pa]", betae);
            registerParameter("c_leak", "Leakage Coefficient", "[]", cLeak);

            setStartValue(mpP1, NodeHydraulic::PRESSURE, 1.0e5);
            setStartValue(mpP2, NodeHydraulic::PRESSURE, 1.0e5);
        }


        void initialize()
        {
            //Assign node data pointers
            mpND_p1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::PRESSURE);
            mpND_q1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::FLOW);
            mpND_c1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::WAVEVARIABLE);
            mpND_Zc1 = getSafeNodeDataPtr(mpP1, NodeHydraulic::CHARIMP);
            mpND_p2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::PRESSURE);
            mpND_q2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::FLOW);
            mpND_c2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::WAVEVARIABLE);
            mpND_Zc2 = getSafeNodeDataPtr(mpP2, NodeHydraulic::CHARIMP);
            mpND_f3 = getSafeNodeDataPtr(mpP3, NodeMechanic::FORCE);
            mpND_x3 = getSafeNodeDataPtr(mpP3, NodeMechanic::POSITION);
            mpND_v3 = getSafeNodeDataPtr(mpP3, NodeMechanic::VELOCITY);
            mpND_c3 = getSafeNodeDataPtr(mpP3, NodeMechanic::WAVEVARIABLE);
            mpND_Zx3 = getSafeNodeDataPtr(mpP3, NodeMechanic::CHARIMP);

            //Declare local variables;
            double p1, q1, p2, q2, f3, x3, v3;
            double Zc1, Zc2, c3, Zx3;
            double qi1, qi2, V1, V2, qLeak, V1min, V2min;

            //Read variables from nodes
            p1 = (*mpND_p1);
            q1 = (*mpND_q1);
            p2 = (*mpND_p2);
            q2 = (*mpND_q2);
            f3 = (*mpND_f3);
            x3 = (*mpND_x3);
            v3 = (*mpND_v3);

            //Limit end of stroke limitations
            CxLim = 0;
            ZxLim = 0;

            //Size of volumes
            V1 = V01+A1*(-x3);
            V2 = V01+A2*(sl+x3);
            V1min = betae*mTimestep*mTimestep*A1*A1/(wfak*me);
            V2min = betae*mTimestep*mTimestep*A2*A2/(wfak*me);
            if(V1<V1min) V1 = V1min;
            if(V2<V2min) V2 = V2min;

            //Impedences
            Zc1 = betae*mTimestep/V1;
            Zc2 = betae*mTimestep/V2;
            Zx3 = A1*A1*Zc1 +A2*A2*Zc2 + bp;

            //Internal flows
            qi1 = v3*A1;
            qi2 = -v3*A2;

            //Leakage flow
            qLeak = cLeak*(p1-p2);

            //Wave variables
            c1 = p1 + Zc1*q1;
            ci1 = p1 + Zc1*qi1;
            cl1 = p1 + Zc1*(-qLeak);

            c2 = p2 + Zc2*q2;
            ci2 = p2 + Zc2*qi2;
            cl2 = p2 + Zc2*qLeak;

            c3 = A1*ci1 - A2*ci2;

            //Write to nodes
            (*mpND_c1) = c1;
            (*mpND_Zc1) = Zc1;
            (*mpND_c2) = c2;
            (*mpND_Zc2) = Zc2;
            (*mpND_c3) = c3;
            (*mpND_Zx3) = Zx3;
        }

        void simulateOneTimestep()
        {
            //Declare local variables;
            double p1, q1, p2, q2, f3, x3, v3;
            double Zc1, Zc2, c3, Zx3;

            double V1, V2, qLeak, qi1, qi2, p1mean, p2mean, V1min, V2min;
            double alpha=0.5;

            //Read variables from nodes
            p1 = (*mpND_p1);
            q1 = (*mpND_q1);
            p2 = (*mpND_p2);
            q2 = (*mpND_q2);
            c1 = (*mpND_c1);
            c2 = (*mpND_c2);
            Zc1 = (*mpND_Zc1);
            Zc2 = (*mpND_Zc2);
            f3 = (*mpND_f3);
            x3 = (*mpND_x3);
            v3 = (*mpND_v3);

            //Leakage flow
            qLeak = cLeak*(c1-c2)/(1+cLeak*(Zc1+Zc2));

            //Internal flows
            qi1 = v3*A1;
            qi2 = -v3*A2;

            //Size of volumes
            V1 = V01+A1*(-x3);
            V2 = V01+A2*(sl+x3);
            V1min = betae*mTimestep*mTimestep*A1*A1/(0.1*me);       //0.1 was called "WFAK" in old Hopsan
            V2min = betae*mTimestep*mTimestep*A2*A2/(0.1*me);
            if(V1<V1min) V1 = V1min;
            if(V2<V2min) V2 = V2min;

            //Volume equations
            Zc1 = 3 / 2 * betae/V1*mTimestep/(1-alpha);
            p1mean = ((c1 + Zc1*2*q1) + (ci1 + Zc1*2*qi1) + (cl1 + Zc1*2*(-qLeak))) / 3;
            c1 = alpha * c1 + (1.0 - alpha)*(p1mean*2 - c1 - 2*Zc1*q1);
            ci1 = alpha * ci1 + (1.0 - alpha)*(p1mean*2 - ci1 - 2*Zc1*qi1);
            cl1 = alpha * cl1 + (1.0 - alpha)*(p1mean*2 - cl1 - 2*Zc1*(-qLeak));

            Zc2 = 3 / 2 * betae/V2*mTimestep/(1-alpha);
            p2mean = ((c2 + Zc2*2*q2) + (ci2 + Zc2*2*qi2) + (cl2 + Zc2*2*qLeak)) / 3;
            c2 = alpha * c2 + (1.0 - alpha)*(p2mean*2 - c2 - 2*Zc2*q2);
            ci2 = alpha * ci2 + (1.0 - alpha)*(p2mean*2 - ci2 - 2*Zc2*qi2);
            cl2 = alpha * cl2 + (1.0 - alpha)*(p2mean*2 - cl2 - 2*Zc2*qLeak);


            //limitStroke(CxLim, ZxLim, x3, v3, me, sl);

{'fraction_non_alphanumeric': 0.14377548904403126, 'fraction_numerical': 0.0650445842261699, 'mean_word_length': 1.7469147522308714, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '416753', 'n_tokens_mistral': 6027, 'n_tokens_neox': 5607, 'n_words': 1103}