Constraints in Dynamic Systems

Updated: 2026-05-30

What is a constraint?

In system dynamics, a constraint is a condition that must be met at all times during the simulation. Unlike an expression derived from the natural behavior of a component, a constraint does not change over time (it does not depend on the evolution of variables), but rather imposes a fixed relationship between them.

This is also known as an algebraic constraint and turns the system into a system of Differential-Algebraic Equations (DAE).

Example: Two masses joined by a rigid rod

Suppose two masses are connected by springs and dampers, but also joined by a rigid bar that maintains a constant distance between them.

This condition can be expressed as:

X2 - X1 = L

Where:

  • X1 and X2 are the positions of the masses
  • L is the length of the bar

This equality must be maintained throughout the simulation. To achieve this, a constraint force is introduced, calculated using a Lagrange multiplier (Lambda) which is added to the system's equations.

System Configuration

INITIAL mech_config, {
  EXPRESSIONS: [

    ; Newton's laws for each mass
    "F1 - M1 * (V1 - V1_prev) / DT",
    "F2 - M2 * (V2 - V2_prev) / DT",

    ; Internal forces: spring and damper
    "FD1 - C1 * V1",
    "F_spring1 - K1 * X1",
    "FD2 - C2 * V2",
    "F_spring2 - K2 * (X2 - X1)",

    ; Constraint force imposed by Lagrange
    "F_restriction + Lambda",

    ; Sum of forces on each mass
    "F1 + F_spring1 + FD1 - F_spring2 - F_restriction",
    "F2 + F_spring2 + FD2 + F_restriction",

    ; Position integration
    "X1 - X1_prev - DT * V1",
    "X2 - X2_prev - DT * V2",

    ; ***** Geometric constraint *****
    "X2 - X1 - L"
  ],

  STATES: [
    "X1", "X2",
    "V1", "V2",
    "F1", "F2",
    "FD1", "FD2",
    "F_spring1", "F_spring2",
    "F_restriction",
    "Lambda"  ; Lagrange variable
  ],

  VARIABLES: [
    "M1", "M2",
    "K1", "K2",
    "C1", "C2",
    "L",
    "X1_prev", "X2_prev",
    "V1_prev", "V2_prev"
  ]
}

How the constraint acts

The equation "X2 - X1 - L" is a constraint equation.

The system must solve all equations simultaneously (including the constraint). To do this, it introduces Lambda, an internal variable representing the necessary force to maintain that condition at every instant.

This force is then added (or subtracted) to the total force equations for each mass:

"F1 + ... - F_restriction"
"F2 + ... + F_restriction"

This ensures that if the system tends to violate the constraint (due to acceleration, for example), the constraint force reacts immediately to maintain it.

What does GPSS-Plus solve?

All of this is resolved within the DYNAMIC block, through the internal use of the Newton-Raphson method and Jacobian matrices. These allow for solving both dynamic variables (X1, V1, etc.) and algebraic variables (Lambda) in the same system.

Conclusion

Constraints allow for the simulation of realistic and complex behaviors, such as:

  • Rigid bars

  • Hydraulic pistons with constant volume

  • Geometric relationships in mechanisms

  • Conservation of energy or momentum

The use of Lambda as a Lagrange multiplier is a key tool for introducing them explicitly and in a controlled manner into your dynamic systems.

 

 

Example

/*

 Constraints in Dynamic Systems
 
*/

SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
SYSTEM {type:OPTIONS, TIME_DECIMALS:1, SPEED:5}     

PLOTTER {NAME:positions, Y_0:X1,  Y_1:X2,  X:TIME_AC1}   

Graphic {NAME:tText,Type:TEXT,X:327,Y:486,Text:"tText"}



INITIAL mech_config, {
  EXPRESSIONS: [
    "F1 - M1 * (V1 - V1_PREV) / DT",
    "FD1 - C1 * V1",
    "F_spring1 - K1 * X1",

    "F_spring2 - K2 * (X2 - X1)",
    "FD2 - C2 * V2",
    "F2 - M2 * (V2 - V2_PREV) / DT",

    "F_restriction + Lambda",  ; constraint force (with sign)

    "F1 + F_spring1 + FD1 - F_spring2 - F_restriction",
    "F2 + F_spring2 + FD2 + F_restriction",

    "X1 - X1_PREV - DT * V1",
    "X2 - X2_PREV - DT * V2",

    "X2 - X1 - L"  ; geometric constraint
  ],

  STATES: [
    "X1", "X2",
    "V1", "V2",
    "F1", "F2",
    "FD1", "FD2",
    "F_spring1", "F_spring2",
    "F_restriction",
    "Lambda"
  ],

  VARIABLES: [
    "M1", "M2",
    "K1", "K2",
    "C1", "C2",
    "L",
    "X1_PREV", "X2_PREV",
    "V1_PREV", "V2_PREV"
  ]
}




INITIAL mech_values, {
  M1: 1.5,
  M2: 1.5,

  K1: 5,
  K2: 8,

  C1: 0.1,
  C2: 0.1,

  X1_PREV: 1.5,
  V1_PREV: 2,

  X2_PREV: 3.5,
  V2_PREV: -1,

  L: 2    ; distance constraint between masses
}






DYNAMIC {
  name: sys,
  CONFIG: V$mech_config,
  VALUES: V$mech_values,
  X: 300,
  Y: 300,
  TOLERANCE: 1e-6,
  MAX_ITER: 10
}


START 1000 

include ./library_graphics/speedometer.lib

;==============================================================

PROCEDURE agent.init

    
while (1==1)

    SOLVE {  name:"sys",  DT: 0.1,  SAVEVALUE: "result"}

    SAVEVALUE X1, X$(result.X1)
    SAVEVALUE X2, X$(result.X2)
    SAVEVALUE V1, X$(result.V1)

    PLOT positions, X$X1, X$X2

	assign rX1,round(X$X1,5)
	assign rX2,round(X$X2,5)
    
	CALL tank1.speedometer.set, P$rX1
	CALL tank2.speedometer.set, P$rX2

    IF (ABS(X$V1) < 0.00001)
        stop
    ENDIF

advance 0.1,0

endwhile

stop
ENDPROCEDURE
;====================================================================

PROCEDURE PRE_RUN

    CALL create_indicators
    
    TIMEOUT agent.init,0

	TERMINATE_VE 
ENDPROCEDURE
;=================================================
PROCEDURE create_indicators

	assign config,{title:"X1"
		,x:100,y:50 
            ,width:50 ,height:180
            ,value:0
            ,min_value:-5
            ,max_value:5
            ,"color":"#ff0000"}
            
	call tank1.speedometer.init,V$config

	assign config,{title:"X2"
		,x:400,y:50 
            ,width:50 ,height:180
            ,value:0
            ,min_value:-5
            ,max_value:5
            ,"color":"purple"}
            
	call tank2.speedometer.init,V$config

	
ENDPROCEDURE

;=================================================