'Including a causal relation in a Modelica simulation leads to translation Error while flattening model

I want to simulate a controller for a mass-spring model which works based on energy:

model model
//parameters
    parameter Real m = 1;
    parameter Real k = 1;
    parameter Real Fmax = 3;
    parameter Real x0 = 1;
    parameter Real x1 = 2;
    parameter Real t1 = 1;
    
//variables
    Real x, v, a, xy, vm;
    
initial equation
    x = x0;
    v = 2;
    
equation
    v = der(x);
    a = der(v);
    m * a + k * x = F;
    
algorithm
    vm := sign(xy - x)*sqrt(2 * (Fmax * abs(xy - x) + k * (xy^2 - x^2) / 2) / m);

    // step signal
    if time  < t1 then
        xy := x0;
    else 
        xy := x1;
    end if;
    
    if xy == x then
        F := k * x;
    else
        F := sign(vm - v) * Fmax;
    end if;
end model;

But it leads to the error message:

Translation Error

Error occurred while flattening model

I would appreciate it if you could help me know what is the problem and how I can fix it.

P.S.1. SIMULINK is also not able to finish!

P.S.2. New version of the code can be seen here.

P.S.3. According to this discussion on Discord, the algorithm section was not really meant for casual relations. More information about the keyword is here.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source