Evaluating: loadModel(Modelica, {"3.2"})
true
Evaluating: getErrorString()
""
Evaluating: instantiateModel(Modelica.Mechanics.Rotational.Examples.LossyGearDemo1)
"function Modelica.Math.Matrices.isEqual \"Compare whether two Real matrices are identical\"
  input Real[:, :] M1 \"First matrix\";
  input Real[:, :] M2 \"Second matrix (may have different size as M1\";
  input Real eps(min = 0.0) = 0.0 \"Two elements e1 and e2 of the two matrices are identical if abs(e1-e2) <= eps\";
  output Boolean result \"= true, if matrices have the same size and the same elements\";
  protected Integer nrow = size(M1, 1) \"Number of rows of matrix M1\";
  protected Integer ncol = size(M1, 2) \"Number of columns of matrix M1\";
  protected Integer i = 1;
  protected Integer j;
algorithm
  result := false;
  if size(M2, 1) == nrow and size(M2, 2) == ncol then
    result := true;
    while i <= nrow loop
      j := 1;
      while j <= ncol loop
        if abs(M1[i, j] - M2[i, j]) > eps then
          result := false;
          i := nrow;
          j := ncol;
        end if;
        j := 1 + j;
      end while;
      i := 1 + i;
    end while;
  end if;
end Modelica.Math.Matrices.isEqual;

function Modelica.Math.isEqual \"Inline before index reduction\" \"Determine if two Real scalars are numerically identical\"
  input Real s1 \"First scalar\";
  input Real s2 \"Second scalar\";
  input Real eps(min = 0.0) = 0.0 \"The two scalars are identical if abs(s1-s2) <= eps\";
  output Boolean result \"= true, if scalars are identical\";
algorithm
  result := abs(s1 - s2) <= eps;
end Modelica.Math.isEqual;

function Modelica.Math.tempInterpol2 \"Temporary function for vectorized linear interpolation (will be removed)\"
  input Real u \"input value (first column of table)\";
  input Real[:, :] table \"table to be interpolated\";
  input Integer[:] icol \"column(s) of table to be interpolated\";
  output Real[1, size(icol, 1)] y \"interpolated input value(s) (column(s) icol of table)\";
  protected Integer i;
  protected Integer n \"number of rows of table\";
  protected Real u1;
  protected Real u2;
  protected Real[1, size(icol, 1)] y1;
  protected Real[1, size(icol, 1)] y2;
algorithm
  n := size(table, 1);
  if n <= 1 then
    y := transpose(promote(table[1,icol], 1));
  else
    if u <= table[1,1] then
      i := 1;
    else
      i := 2;
      while i < n and u >= table[i, 1] loop
        i := 1 + i;
      end while;
      i := i + -1;
    end if;
    u1 := table[i, 1];
    u2 := table[1 + i, 1];
    y1 := transpose(promote(table[i,icol], 1));
    y2 := transpose(promote(table[1 + i,icol], 1));
    assert(u2 > u1, \"Table index must be increasing\");
    y := y1 + (y2 - y1) * (u - u1) / (u2 - u1);
  end if;
end Modelica.Math.tempInterpol2;

class Modelica.Mechanics.Rotational.Examples.LossyGearDemo1
  Real PowerLoss(quantity = \"Power\", unit = \"W\") = gear.flange_a.tau * der(gear.flange_a.phi) + gear.flange_b.tau * der(gear.flange_b.phi) \"Power lost in the gear\";
  parameter Boolean gear.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real gear.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real gear.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real gear.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real gear.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real gear.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  parameter Boolean gear.useHeatPort = false \"=true, if heatPort is enabled\";
  Real gear.lossPower(quantity = \"Power\", unit = \"W\") \"Loss power leaving component via heatPort (> 0, if heat is flowing out of component)\";
  parameter Real gear.ratio(start = 1.0) = 2.0 \"Transmission ratio (flange_a.phi/flange_b.phi)\";
  parameter Real gear.lossTable[1,1] = 0.0 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,2] = 0.5 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,3] = 0.5 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,4] = 0.0 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,5] = 0.0 \"Array for mesh efficiencies and bearing friction depending on speed\";
  Real gear.phi_a(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Angle between left shaft flange and support\";
  Real gear.phi_b(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Angle between right shaft flange and support\";
  Real gear.sa(unit = \"1\") \"Path parameter for acceleration and torque loss\";
  Real gear.w_a(quantity = \"AngularVelocity\", unit = \"rad/s\") \"Angular velocity of flange_a with respect to support\";
  Real gear.a_a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Angular acceleration of flange_a with respect to support\";
  Real gear.eta_mf1;
  Real gear.eta_mf2;
  Real gear.tau_bf_a \"Bearing friction torque on flange_a side\";
  Real gear.tau_eta \"Torque that determines the driving side (= if forwardSliding then flange_a.tau-tau_bf_a else if backwardSliding then flange_a.tau+tau_bf_a else flange_a.tau)\";
  Real gear.tau_bf1;
  Real gear.tau_bf2;
  Real gear.quadrant1;
  Real gear.quadrant2;
  Real gear.quadrant3;
  Real gear.quadrant4;
  Real gear.quadrant1_p;
  Real gear.quadrant2_p;
  Real gear.quadrant3_m;
  Real gear.quadrant4_m;
  Real gear.tauLoss(quantity = \"Torque\", unit = \"N.m\") \"Torque loss due to friction in the gear teeth and in the bearings\";
  Real gear.tauLossMax(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for positive speed\";
  Real gear.tauLossMin(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for negative speed\";
  Real gear.tauLossMax_p(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for positive speed\";
  Real gear.tauLossMin_m(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for negative speed\";
  Boolean gear.tau_aPos(start = true) \"Only for backwards compatibility (was previously: true, if torque of flange_a is not negative)\";
  Boolean gear.tau_etaPos(start = true) \"true, if torque tau_eta is not negative\";
  Boolean gear.startForward(start = false) \"true, if starting to roll forward\";
  Boolean gear.startBackward(start = false) \"true, if starting to roll backward\";
  Boolean gear.locked(start = false) \"true, if gear is locked\";
  Boolean gear.ideal \"true, if losses are neglected\";
  constant Integer gear.Unknown = 3 \"Value of mode is not known\";
  constant Integer gear.Free = 2 \"Element is not active\";
  constant Integer gear.Forward = 1 \"w_a > 0 (forward rolling)\";
  constant Integer gear.Stuck = 0 \"w_a = 0 (forward rolling, locked or backward rolling)\";
  constant Integer gear.Backward = -1 \"w_a < 0 (backward rolling)\";
  Real gear.tau_eta_p(quantity = \"Torque\", unit = \"N.m\") \"tau_eta assuming positive omega\";
  Real gear.tau_eta_m(quantity = \"Torque\", unit = \"N.m\") \"tau_eta assuming negative omega\";
  protected constant Real gear.unitAngularAcceleration(quantity = \"AngularAcceleration\", unit = \"rad/s2\") = 1.0;
  protected constant Real gear.unitTorque(quantity = \"Torque\", unit = \"N.m\") = 1.0;
  protected parameter Real gear.tau_bf_a_0 = if Modelica.Math.isEqual(gear.eta_mf1_0, 1.0, 1e-15) and Modelica.Math.isEqual(gear.eta_mf2_0, 1.0, 1e-15) then gear.tau_bf1_0 / 2.0 else (gear.tau_bf1_0 - gear.tau_bf2_0) / (gear.eta_mf1_0 + -1.0 / gear.eta_mf2_0);
  Real gear.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = gear.phi_support \"Absolute rotation angle of the support/housing\";
  Real gear.support.tau(quantity = \"Torque\", unit = \"N.m\") = (-gear.flange_a.tau) - gear.flange_b.tau \"Reaction torque in the support/housing\";
  Real gear.interpolation_result[1,1];
  Real gear.interpolation_result[1,2];
  Real gear.interpolation_result[1,3];
  Real gear.interpolation_result[1,4];
  Integer gear.mode(min = -1, max = 3, start = 2, fixed = true);
  protected parameter Real gear.interpolation_result_0[1,1] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 1];
  protected parameter Real gear.interpolation_result_0[1,2] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 2];
  protected parameter Real gear.interpolation_result_0[1,3] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 3];
  protected parameter Real gear.interpolation_result_0[1,4] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 4];
  protected parameter Real gear.eta_mf1_0 = gear.interpolation_result_0[1,1];
  protected parameter Real gear.eta_mf2_0 = gear.interpolation_result_0[1,2];
  protected parameter Real gear.tau_bf1_0 = abs(gear.interpolation_result_0[1,3]);
  protected parameter Real gear.tau_bf2_0 = abs(gear.interpolation_result_0[1,4]);
  Real Inertia1.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia1.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real Inertia1.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia1.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  parameter Real Inertia1.J(quantity = \"MomentOfInertia\", unit = \"kg.m2\", min = 0.0, start = 1.0) = 1.0 \"Moment of inertia\";
  parameter enumeration(never, avoid, default, prefer, always) Inertia1.stateSelect = StateSelect.default \"Priority to use phi and w as states\";
  Real Inertia1.a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Absolute angular acceleration of component (= der(w))\";
  Real Inertia1.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\", StateSelect = StateSelect.default) \"Absolute rotation angle of component\";
  Real Inertia1.w(quantity = \"AngularVelocity\", unit = \"rad/s\", StateSelect = StateSelect.default) \"Absolute angular velocity of component (= der(phi))\";
  Real Inertia2.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia2.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real Inertia2.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia2.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  parameter Real Inertia2.J(quantity = \"MomentOfInertia\", unit = \"kg.m2\", min = 0.0, start = 1.0) = 1.5 \"Moment of inertia\";
  parameter enumeration(never, avoid, default, prefer, always) Inertia2.stateSelect = StateSelect.default \"Priority to use phi and w as states\";
  Real Inertia2.a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Absolute angular acceleration of component (= der(w))\";
  Real Inertia2.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\", start = 0.0, fixed = true, StateSelect = StateSelect.default) \"Absolute rotation angle of component\";
  Real Inertia2.w(quantity = \"AngularVelocity\", unit = \"rad/s\", fixed = true, StateSelect = StateSelect.default) \"Absolute angular velocity of component (= der(phi))\";
  parameter Boolean torque1.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real torque1.flange.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real torque1.flange.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real torque1.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  input Real torque1.tau \"Accelerating torque acting at flange (= -flange.tau)\";
  Real torque1.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = torque1.phi_support \"Absolute rotation angle of the support/housing\";
  Real torque1.support.tau(quantity = \"Torque\", unit = \"N.m\") = -torque1.flange.tau \"Reaction torque in the support/housing\";
  parameter Boolean torque2.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real torque2.flange.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real torque2.flange.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real torque2.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  input Real torque2.tau \"Accelerating torque acting at flange (= -flange.tau)\";
  Real torque2.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = torque2.phi_support \"Absolute rotation angle of the support/housing\";
  Real torque2.support.tau(quantity = \"Torque\", unit = \"N.m\") = -torque2.flange.tau \"Reaction torque in the support/housing\";
  output Real DriveSine.y \"Connector of Real output signal\";
  parameter Real DriveSine.amplitude = 10.0 \"Amplitude of sine wave\";
  parameter Real DriveSine.freqHz(quantity = \"Frequency\", unit = \"Hz\", start = 1.0) = 1.0 \"Frequency of sine wave\";
  parameter Real DriveSine.phase(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = 0.0 \"Phase of sine wave\";
  parameter Real DriveSine.offset = 0.0 \"Offset of output signal\";
  parameter Real DriveSine.startTime(quantity = \"Time\", unit = \"s\") = 0.0 \"Output = offset for time < startTime\";
  protected constant Real DriveSine.pi = 3.14159265358979;
  output Real load.y \"Connector of Real output signal\";
  parameter Real load.height = 5.0 \"Height of ramps\";
  parameter Real load.duration(quantity = \"Time\", unit = \"s\", min = 1e-60, start = 2.0) = 2.0 \"Durations of ramp\";
  parameter Real load.offset = -10.0 \"Offset of output signal\";
  parameter Real load.startTime(quantity = \"Time\", unit = \"s\") = 0.0 \"Output = offset for time < startTime\";
  parameter Real fixed.phi0(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = 0.0 \"Fixed offset angle of housing\";
  Real fixed.flange.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real fixed.flange.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
equation
  assert(abs(gear.ratio) > 0.0,\"Error in initialization of LossyGear: ratio may not be zero\");
  gear.ideal = Modelica.Math.Matrices.isEqual({{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {{0.0, 1.0, 1.0, 0.0, 0.0}}, 1e-15);
  gear.interpolation_result = if gear.ideal then {{1.0, 1.0, 0.0, 0.0}} else Modelica.Math.tempInterpol2(abs(gear.w_a), {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5});
  gear.eta_mf1 = gear.interpolation_result[1,1];
  gear.eta_mf2 = gear.interpolation_result[1,2];
  gear.tau_bf1 = abs(gear.interpolation_result[1,3]);
  gear.tau_bf2 = abs(gear.interpolation_result[1,4]);
  if Modelica.Math.isEqual(gear.eta_mf1, 1.0, 1e-15) and Modelica.Math.isEqual(gear.eta_mf2, 1.0, 1e-15) then
  gear.tau_bf_a = gear.tau_bf1 / 2.0;
  else
  gear.tau_bf_a = (gear.tau_bf1 - gear.tau_bf2) / (gear.eta_mf1 + -1.0 / gear.eta_mf2);
  end if;
  gear.phi_a = gear.flange_a.phi - gear.phi_support;
  gear.phi_b = gear.flange_b.phi - gear.phi_support;
  gear.phi_a = gear.ratio * gear.phi_b;
  0.0 = gear.flange_b.tau + gear.ratio * (gear.flange_a.tau - gear.tauLoss);
  gear.w_a = der(gear.phi_a);
  gear.a_a = der(gear.w_a);
  gear.tau_eta_p = gear.flange_a.tau - gear.tau_bf_a_0;
  gear.tau_eta_m = gear.flange_a.tau + gear.tau_bf_a_0;
  gear.quadrant1_p = (1.0 - gear.eta_mf1_0) * gear.flange_a.tau + gear.tau_bf1_0;
  gear.quadrant2_p = (1.0 + -1.0 / gear.eta_mf2_0) * gear.flange_a.tau + gear.tau_bf2_0;
  gear.tauLossMax_p = if noEvent(gear.tau_eta_p > 0.0) then gear.quadrant1_p else gear.quadrant2_p;
  gear.quadrant4_m = (1.0 + -1.0 / gear.eta_mf2_0) * gear.flange_a.tau - gear.tau_bf2_0;
  gear.quadrant3_m = (1.0 - gear.eta_mf1_0) * gear.flange_a.tau - gear.tau_bf1_0;
  gear.tauLossMin_m = if noEvent(gear.tau_eta_m > 0.0) then gear.quadrant4_m else gear.quadrant3_m;
  gear.quadrant1 = (1.0 - gear.eta_mf1) * gear.flange_a.tau + gear.tau_bf1;
  gear.quadrant2 = (1.0 + -1.0 / gear.eta_mf2) * gear.flange_a.tau + gear.tau_bf2;
  gear.quadrant4 = (1.0 + -1.0 / gear.eta_mf2) * gear.flange_a.tau - gear.tau_bf2;
  gear.quadrant3 = (1.0 - gear.eta_mf1) * gear.flange_a.tau - gear.tau_bf1;
  gear.tau_eta = if gear.ideal then gear.flange_a.tau else if gear.locked then gear.flange_a.tau else if gear.startForward or pre(gear.mode) == 1 then gear.flange_a.tau - gear.tau_bf_a else gear.flange_a.tau + gear.tau_bf_a;
  gear.tau_etaPos = gear.tau_eta >= 0.0;
  gear.tau_aPos = gear.tau_etaPos;
  gear.tauLossMax = if gear.tau_etaPos then gear.quadrant1 else gear.quadrant2;
  gear.tauLossMin = if gear.tau_etaPos then gear.quadrant4 else gear.quadrant3;
  gear.startForward = pre(gear.mode) == 0 and gear.sa > gear.tauLossMax_p or initial() and gear.w_a > 0.0;
  gear.startBackward = pre(gear.mode) == 0 and gear.sa < gear.tauLossMin_m or initial() and gear.w_a < 0.0;
  gear.locked = not (gear.ideal or pre(gear.mode) == 1 or gear.startForward or pre(gear.mode) == -1 or gear.startBackward);
  gear.tauLoss = if gear.ideal then 0.0 else if gear.locked then gear.sa else if gear.startForward or pre(gear.mode) == 1 then gear.tauLossMax else gear.tauLossMin;
  gear.a_a = if gear.locked then 0.0 else gear.sa - gear.tauLoss;
  gear.mode = if gear.ideal then 2 else if (pre(gear.mode) == 1 or gear.startForward) and gear.w_a > 0.0 then 1 else if (pre(gear.mode) == -1 or gear.startBackward) and gear.w_a < 0.0 then -1 else 0;
  gear.lossPower = gear.tauLoss * gear.w_a;
  Inertia1.phi = Inertia1.flange_a.phi;
  Inertia1.phi = Inertia1.flange_b.phi;
  Inertia1.w = der(Inertia1.phi);
  Inertia1.a = der(Inertia1.w);
  Inertia1.J * Inertia1.a = Inertia1.flange_a.tau + Inertia1.flange_b.tau;
  Inertia2.phi = Inertia2.flange_a.phi;
  Inertia2.phi = Inertia2.flange_b.phi;
  Inertia2.w = der(Inertia2.phi);
  Inertia2.a = der(Inertia2.w);
  Inertia2.J * Inertia2.a = Inertia2.flange_a.tau + Inertia2.flange_b.tau;
  torque1.flange.tau = -torque1.tau;
  torque2.flange.tau = -torque2.tau;
  DriveSine.y = DriveSine.offset + (if time < DriveSine.startTime then 0.0 else DriveSine.amplitude * sin(6.28318530717959 * DriveSine.freqHz * (time - DriveSine.startTime) + DriveSine.phase));
  load.y = load.offset + (if time < load.startTime then 0.0 else if time < load.startTime + load.duration then (time - load.startTime) * load.height / load.duration else load.height);
  fixed.flange.phi = fixed.phi0;
  gear.flange_a.tau + Inertia1.flange_b.tau = 0.0;
  gear.flange_b.tau + Inertia2.flange_a.tau = 0.0;
  gear.support.tau + torque1.support.tau + torque2.support.tau + fixed.flange.tau = 0.0;
  Inertia1.flange_a.tau + torque1.flange.tau = 0.0;
  Inertia2.flange_b.tau + torque2.flange.tau = 0.0;
  Inertia1.flange_b.phi = gear.flange_a.phi;
  Inertia2.flange_a.phi = gear.flange_b.phi;
  Inertia1.flange_a.phi = torque1.flange.phi;
  Inertia2.flange_b.phi = torque2.flange.phi;
  DriveSine.y = torque1.tau;
  load.y = torque2.tau;
  fixed.flange.phi = gear.support.phi;
  fixed.flange.phi = torque1.support.phi;
  fixed.flange.phi = torque2.support.phi;
end Modelica.Mechanics.Rotational.Examples.LossyGearDemo1;
"
Evaluating: getErrorString()
""
Evaluating: instantiateModel(Modelica.Mechanics.Rotational.Examples.LossyGearDemo2)
"function Modelica.Math.Matrices.isEqual \"Compare whether two Real matrices are identical\"
  input Real[:, :] M1 \"First matrix\";
  input Real[:, :] M2 \"Second matrix (may have different size as M1\";
  input Real eps(min = 0.0) = 0.0 \"Two elements e1 and e2 of the two matrices are identical if abs(e1-e2) <= eps\";
  output Boolean result \"= true, if matrices have the same size and the same elements\";
  protected Integer nrow = size(M1, 1) \"Number of rows of matrix M1\";
  protected Integer ncol = size(M1, 2) \"Number of columns of matrix M1\";
  protected Integer i = 1;
  protected Integer j;
algorithm
  result := false;
  if size(M2, 1) == nrow and size(M2, 2) == ncol then
    result := true;
    while i <= nrow loop
      j := 1;
      while j <= ncol loop
        if abs(M1[i, j] - M2[i, j]) > eps then
          result := false;
          i := nrow;
          j := ncol;
        end if;
        j := 1 + j;
      end while;
      i := 1 + i;
    end while;
  end if;
end Modelica.Math.Matrices.isEqual;

function Modelica.Math.isEqual \"Inline before index reduction\" \"Determine if two Real scalars are numerically identical\"
  input Real s1 \"First scalar\";
  input Real s2 \"Second scalar\";
  input Real eps(min = 0.0) = 0.0 \"The two scalars are identical if abs(s1-s2) <= eps\";
  output Boolean result \"= true, if scalars are identical\";
algorithm
  result := abs(s1 - s2) <= eps;
end Modelica.Math.isEqual;

function Modelica.Math.tempInterpol1 \"Temporary function for linear interpolation (will be removed)\"
  input Real u \"input value (first column of table)\";
  input Real[:, :] table \"table to be interpolated\";
  input Integer icol \"column of table to be interpolated\";
  output Real y \"interpolated input value (icol column of table)\";
  protected Integer i;
  protected Integer n \"number of rows of table\";
  protected Real u1;
  protected Real u2;
  protected Real y1;
  protected Real y2;
algorithm
  n := size(table, 1);
  if n <= 1 then
    y := table[1, icol];
  else
    if u <= table[1,1] then
      i := 1;
    else
      i := 2;
      while i < n and u >= table[i, 1] loop
        i := 1 + i;
      end while;
      i := i + -1;
    end if;
    u1 := table[i, 1];
    u2 := table[1 + i, 1];
    y1 := table[i, icol];
    y2 := table[1 + i, icol];
    assert(u2 > u1, \"Table index must be increasing\");
    y := y1 + (y2 - y1) * (u - u1) / (u2 - u1);
  end if;
end Modelica.Math.tempInterpol1;

function Modelica.Math.tempInterpol2 \"Temporary function for vectorized linear interpolation (will be removed)\"
  input Real u \"input value (first column of table)\";
  input Real[:, :] table \"table to be interpolated\";
  input Integer[:] icol \"column(s) of table to be interpolated\";
  output Real[1, size(icol, 1)] y \"interpolated input value(s) (column(s) icol of table)\";
  protected Integer i;
  protected Integer n \"number of rows of table\";
  protected Real u1;
  protected Real u2;
  protected Real[1, size(icol, 1)] y1;
  protected Real[1, size(icol, 1)] y2;
algorithm
  n := size(table, 1);
  if n <= 1 then
    y := transpose(promote(table[1,icol], 1));
  else
    if u <= table[1,1] then
      i := 1;
    else
      i := 2;
      while i < n and u >= table[i, 1] loop
        i := 1 + i;
      end while;
      i := i + -1;
    end if;
    u1 := table[i, 1];
    u2 := table[1 + i, 1];
    y1 := transpose(promote(table[i,icol], 1));
    y2 := transpose(promote(table[1 + i,icol], 1));
    assert(u2 > u1, \"Table index must be increasing\");
    y := y1 + (y2 - y1) * (u - u1) / (u2 - u1);
  end if;
end Modelica.Math.tempInterpol2;

class Modelica.Mechanics.Rotational.Examples.LossyGearDemo2
  Real PowerLoss(quantity = \"Power\", unit = \"W\") = gear.flange_a.tau * der(gear.flange_a.phi) + gear.flange_b.tau * der(gear.flange_b.phi) \"Power lost in the gear\";
  parameter Boolean gear.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real gear.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real gear.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real gear.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real gear.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real gear.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  parameter Boolean gear.useHeatPort = false \"=true, if heatPort is enabled\";
  Real gear.lossPower(quantity = \"Power\", unit = \"W\") \"Loss power leaving component via heatPort (> 0, if heat is flowing out of component)\";
  parameter Real gear.ratio(start = 1.0) = 2.0 \"Transmission ratio (flange_a.phi/flange_b.phi)\";
  parameter Real gear.lossTable[1,1] = 0.0 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,2] = 0.5 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,3] = 0.5 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,4] = 0.0 \"Array for mesh efficiencies and bearing friction depending on speed\";
  parameter Real gear.lossTable[1,5] = 0.0 \"Array for mesh efficiencies and bearing friction depending on speed\";
  Real gear.phi_a(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Angle between left shaft flange and support\";
  Real gear.phi_b(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Angle between right shaft flange and support\";
  Real gear.sa(unit = \"1\") \"Path parameter for acceleration and torque loss\";
  Real gear.w_a(quantity = \"AngularVelocity\", unit = \"rad/s\") \"Angular velocity of flange_a with respect to support\";
  Real gear.a_a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Angular acceleration of flange_a with respect to support\";
  Real gear.eta_mf1;
  Real gear.eta_mf2;
  Real gear.tau_bf_a \"Bearing friction torque on flange_a side\";
  Real gear.tau_eta \"Torque that determines the driving side (= if forwardSliding then flange_a.tau-tau_bf_a else if backwardSliding then flange_a.tau+tau_bf_a else flange_a.tau)\";
  Real gear.tau_bf1;
  Real gear.tau_bf2;
  Real gear.quadrant1;
  Real gear.quadrant2;
  Real gear.quadrant3;
  Real gear.quadrant4;
  Real gear.quadrant1_p;
  Real gear.quadrant2_p;
  Real gear.quadrant3_m;
  Real gear.quadrant4_m;
  Real gear.tauLoss(quantity = \"Torque\", unit = \"N.m\") \"Torque loss due to friction in the gear teeth and in the bearings\";
  Real gear.tauLossMax(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for positive speed\";
  Real gear.tauLossMin(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for negative speed\";
  Real gear.tauLossMax_p(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for positive speed\";
  Real gear.tauLossMin_m(quantity = \"Torque\", unit = \"N.m\") \"Torque loss for negative speed\";
  Boolean gear.tau_aPos(start = true) \"Only for backwards compatibility (was previously: true, if torque of flange_a is not negative)\";
  Boolean gear.tau_etaPos(start = true) \"true, if torque tau_eta is not negative\";
  Boolean gear.startForward(start = false) \"true, if starting to roll forward\";
  Boolean gear.startBackward(start = false) \"true, if starting to roll backward\";
  Boolean gear.locked(start = false) \"true, if gear is locked\";
  Boolean gear.ideal \"true, if losses are neglected\";
  constant Integer gear.Unknown = 3 \"Value of mode is not known\";
  constant Integer gear.Free = 2 \"Element is not active\";
  constant Integer gear.Forward = 1 \"w_a > 0 (forward rolling)\";
  constant Integer gear.Stuck = 0 \"w_a = 0 (forward rolling, locked or backward rolling)\";
  constant Integer gear.Backward = -1 \"w_a < 0 (backward rolling)\";
  Real gear.tau_eta_p(quantity = \"Torque\", unit = \"N.m\") \"tau_eta assuming positive omega\";
  Real gear.tau_eta_m(quantity = \"Torque\", unit = \"N.m\") \"tau_eta assuming negative omega\";
  protected constant Real gear.unitAngularAcceleration(quantity = \"AngularAcceleration\", unit = \"rad/s2\") = 1.0;
  protected constant Real gear.unitTorque(quantity = \"Torque\", unit = \"N.m\") = 1.0;
  protected parameter Real gear.tau_bf_a_0 = if Modelica.Math.isEqual(gear.eta_mf1_0, 1.0, 1e-15) and Modelica.Math.isEqual(gear.eta_mf2_0, 1.0, 1e-15) then gear.tau_bf1_0 / 2.0 else (gear.tau_bf1_0 - gear.tau_bf2_0) / (gear.eta_mf1_0 + -1.0 / gear.eta_mf2_0);
  Real gear.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = gear.phi_support \"Absolute rotation angle of the support/housing\";
  Real gear.support.tau(quantity = \"Torque\", unit = \"N.m\") = (-gear.flange_a.tau) - gear.flange_b.tau \"Reaction torque in the support/housing\";
  Real gear.interpolation_result[1,1];
  Real gear.interpolation_result[1,2];
  Real gear.interpolation_result[1,3];
  Real gear.interpolation_result[1,4];
  Integer gear.mode(min = -1, max = 3, start = 2, fixed = true);
  protected parameter Real gear.interpolation_result_0[1,1] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 1];
  protected parameter Real gear.interpolation_result_0[1,2] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 2];
  protected parameter Real gear.interpolation_result_0[1,3] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 3];
  protected parameter Real gear.interpolation_result_0[1,4] = Modelica.Math.tempInterpol2(0.0, {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5})[1, 4];
  protected parameter Real gear.eta_mf1_0 = gear.interpolation_result_0[1,1];
  protected parameter Real gear.eta_mf2_0 = gear.interpolation_result_0[1,2];
  protected parameter Real gear.tau_bf1_0 = abs(gear.interpolation_result_0[1,3]);
  protected parameter Real gear.tau_bf2_0 = abs(gear.interpolation_result_0[1,4]);
  Real Inertia1.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia1.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real Inertia1.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia1.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  parameter Real Inertia1.J(quantity = \"MomentOfInertia\", unit = \"kg.m2\", min = 0.0, start = 1.0) = 1.0 \"Moment of inertia\";
  parameter enumeration(never, avoid, default, prefer, always) Inertia1.stateSelect = StateSelect.default \"Priority to use phi and w as states\";
  Real Inertia1.a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Absolute angular acceleration of component (= der(w))\";
  Real Inertia1.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\", StateSelect = StateSelect.default) \"Absolute rotation angle of component\";
  Real Inertia1.w(quantity = \"AngularVelocity\", unit = \"rad/s\", StateSelect = StateSelect.default) \"Absolute angular velocity of component (= der(phi))\";
  Real Inertia2.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia2.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real Inertia2.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real Inertia2.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  parameter Real Inertia2.J(quantity = \"MomentOfInertia\", unit = \"kg.m2\", min = 0.0, start = 1.0) = 1.5 \"Moment of inertia\";
  parameter enumeration(never, avoid, default, prefer, always) Inertia2.stateSelect = StateSelect.default \"Priority to use phi and w as states\";
  Real Inertia2.a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Absolute angular acceleration of component (= der(w))\";
  Real Inertia2.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\", start = 0.0, fixed = true, StateSelect = StateSelect.default) \"Absolute rotation angle of component\";
  Real Inertia2.w(quantity = \"AngularVelocity\", unit = \"rad/s\", fixed = true, StateSelect = StateSelect.default) \"Absolute angular velocity of component (= der(phi))\";
  parameter Boolean torque1.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real torque1.flange.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real torque1.flange.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real torque1.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  input Real torque1.tau \"Accelerating torque acting at flange (= -flange.tau)\";
  Real torque1.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = torque1.phi_support \"Absolute rotation angle of the support/housing\";
  Real torque1.support.tau(quantity = \"Torque\", unit = \"N.m\") = -torque1.flange.tau \"Reaction torque in the support/housing\";
  parameter Boolean torque2.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real torque2.flange.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real torque2.flange.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real torque2.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  input Real torque2.tau \"Accelerating torque acting at flange (= -flange.tau)\";
  Real torque2.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = torque2.phi_support \"Absolute rotation angle of the support/housing\";
  Real torque2.support.tau(quantity = \"Torque\", unit = \"N.m\") = -torque2.flange.tau \"Reaction torque in the support/housing\";
  output Real DriveSine.y \"Connector of Real output signal\";
  parameter Real DriveSine.amplitude = 10.0 \"Amplitude of sine wave\";
  parameter Real DriveSine.freqHz(quantity = \"Frequency\", unit = \"Hz\", start = 1.0) = 1.0 \"Frequency of sine wave\";
  parameter Real DriveSine.phase(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = 0.0 \"Phase of sine wave\";
  parameter Real DriveSine.offset = 0.0 \"Offset of output signal\";
  parameter Real DriveSine.startTime(quantity = \"Time\", unit = \"s\") = 0.0 \"Output = offset for time < startTime\";
  protected constant Real DriveSine.pi = 3.14159265358979;
  output Real load.y \"Connector of Real output signal\";
  parameter Real load.height = 5.0 \"Height of ramps\";
  parameter Real load.duration(quantity = \"Time\", unit = \"s\", min = 1e-60, start = 2.0) = 2.0 \"Durations of ramp\";
  parameter Real load.offset = -10.0 \"Offset of output signal\";
  parameter Real load.startTime(quantity = \"Time\", unit = \"s\") = 0.0 \"Output = offset for time < startTime\";
  parameter Boolean bearingFriction.useSupport = true \"= true, if support flange enabled, otherwise implicitly grounded\";
  Real bearingFriction.flange_a.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real bearingFriction.flange_a.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  Real bearingFriction.flange_b.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real bearingFriction.flange_b.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
  protected Real bearingFriction.phi_support(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute angle of support flange\";
  parameter Real bearingFriction.w_small(quantity = \"AngularVelocity\", unit = \"rad/s\") = 10000000000.0 \"Relative angular velocity near to zero if jumps due to a reinit(..) of the velocity can occur (set to low value only if such impulses can occur)\";
  Real bearingFriction.w_relfric(quantity = \"AngularVelocity\", unit = \"rad/s\") \"Relative angular velocity between frictional surfaces\";
  Real bearingFriction.a_relfric(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Relative angular acceleration between frictional surfaces\";
  Real bearingFriction.tau0(quantity = \"Torque\", unit = \"N.m\") \"Friction torque for w=0 and forward sliding\";
  Real bearingFriction.tau0_max(quantity = \"Torque\", unit = \"N.m\") \"Maximum friction torque for w=0 and locked\";
  Boolean bearingFriction.free \"true, if frictional element is not active\";
  Real bearingFriction.sa(unit = \"1\") \"Path parameter of friction characteristic tau = f(a_relfric)\";
  Boolean bearingFriction.startForward(start = false, fixed = true) \"true, if w_rel=0 and start of forward sliding\";
  Boolean bearingFriction.startBackward(start = false, fixed = true) \"true, if w_rel=0 and start of backward sliding\";
  Boolean bearingFriction.locked(start = false) \"true, if w_rel=0 and not sliding\";
  constant Integer bearingFriction.Unknown = 3 \"Value of mode is not known\";
  constant Integer bearingFriction.Free = 2 \"Element is not active\";
  constant Integer bearingFriction.Forward = 1 \"w_rel > 0 (forward sliding)\";
  constant Integer bearingFriction.Stuck = 0 \"w_rel = 0 (forward sliding, locked or backward sliding)\";
  constant Integer bearingFriction.Backward = -1 \"w_rel < 0 (backward sliding)\";
  protected constant Real bearingFriction.unitAngularAcceleration(quantity = \"AngularAcceleration\", unit = \"rad/s2\") = 1.0;
  protected constant Real bearingFriction.unitTorque(quantity = \"Torque\", unit = \"N.m\") = 1.0;
  parameter Boolean bearingFriction.useHeatPort = false \"=true, if heatPort is enabled\";
  Real bearingFriction.lossPower(quantity = \"Power\", unit = \"W\") \"Loss power leaving component via heatPort (> 0, if heat is flowing out of component)\";
  parameter Real bearingFriction.tau_pos[1,1] = 0.0 \"[w,tau] Positive sliding friction characteristic (w>=0)\";
  parameter Real bearingFriction.tau_pos[1,2] = 0.5 \"[w,tau] Positive sliding friction characteristic (w>=0)\";
  parameter Real bearingFriction.tau_pos[2,1] = 1.0 \"[w,tau] Positive sliding friction characteristic (w>=0)\";
  parameter Real bearingFriction.tau_pos[2,2] = 1.0 \"[w,tau] Positive sliding friction characteristic (w>=0)\";
  parameter Real bearingFriction.peak(min = 1.0) = 1.0 \"peak*tau_pos[1,2] = Maximum friction torque for w==0\";
  Real bearingFriction.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Angle between shaft flanges (flange_a, flange_b) and support\";
  Real bearingFriction.tau(quantity = \"Torque\", unit = \"N.m\") \"Friction torque\";
  Real bearingFriction.w(quantity = \"AngularVelocity\", unit = \"rad/s\") \"Absolute angular velocity of flange_a and flange_b\";
  Real bearingFriction.a(quantity = \"AngularAcceleration\", unit = \"rad/s2\") \"Absolute angular acceleration of flange_a and flange_b\";
  Real bearingFriction.support.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = bearingFriction.phi_support \"Absolute rotation angle of the support/housing\";
  Real bearingFriction.support.tau(quantity = \"Torque\", unit = \"N.m\") = (-bearingFriction.flange_a.tau) - bearingFriction.flange_b.tau \"Reaction torque in the support/housing\";
  Integer bearingFriction.mode(min = -1, max = 3, start = 3, fixed = true);
  parameter Real fixed.phi0(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") = 0.0 \"Fixed offset angle of housing\";
  Real fixed.flange.phi(quantity = \"Angle\", unit = \"rad\", displayUnit = \"deg\") \"Absolute rotation angle of flange\";
  Real fixed.flange.tau(quantity = \"Torque\", unit = \"N.m\") \"Cut torque in the flange\";
equation
  assert(abs(gear.ratio) > 0.0,\"Error in initialization of LossyGear: ratio may not be zero\");
  gear.ideal = Modelica.Math.Matrices.isEqual({{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {{0.0, 1.0, 1.0, 0.0, 0.0}}, 1e-15);
  gear.interpolation_result = if gear.ideal then {{1.0, 1.0, 0.0, 0.0}} else Modelica.Math.tempInterpol2(abs(gear.w_a), {{gear.lossTable[1,1], gear.lossTable[1,2], gear.lossTable[1,3], gear.lossTable[1,4], gear.lossTable[1,5]}}, {2, 3, 4, 5});
  gear.eta_mf1 = gear.interpolation_result[1,1];
  gear.eta_mf2 = gear.interpolation_result[1,2];
  gear.tau_bf1 = abs(gear.interpolation_result[1,3]);
  gear.tau_bf2 = abs(gear.interpolation_result[1,4]);
  if Modelica.Math.isEqual(gear.eta_mf1, 1.0, 1e-15) and Modelica.Math.isEqual(gear.eta_mf2, 1.0, 1e-15) then
  gear.tau_bf_a = gear.tau_bf1 / 2.0;
  else
  gear.tau_bf_a = (gear.tau_bf1 - gear.tau_bf2) / (gear.eta_mf1 + -1.0 / gear.eta_mf2);
  end if;
  gear.phi_a = gear.flange_a.phi - gear.phi_support;
  gear.phi_b = gear.flange_b.phi - gear.phi_support;
  gear.phi_a = gear.ratio * gear.phi_b;
  0.0 = gear.flange_b.tau + gear.ratio * (gear.flange_a.tau - gear.tauLoss);
  gear.w_a = der(gear.phi_a);
  gear.a_a = der(gear.w_a);
  gear.tau_eta_p = gear.flange_a.tau - gear.tau_bf_a_0;
  gear.tau_eta_m = gear.flange_a.tau + gear.tau_bf_a_0;
  gear.quadrant1_p = (1.0 - gear.eta_mf1_0) * gear.flange_a.tau + gear.tau_bf1_0;
  gear.quadrant2_p = (1.0 + -1.0 / gear.eta_mf2_0) * gear.flange_a.tau + gear.tau_bf2_0;
  gear.tauLossMax_p = if noEvent(gear.tau_eta_p > 0.0) then gear.quadrant1_p else gear.quadrant2_p;
  gear.quadrant4_m = (1.0 + -1.0 / gear.eta_mf2_0) * gear.flange_a.tau - gear.tau_bf2_0;
  gear.quadrant3_m = (1.0 - gear.eta_mf1_0) * gear.flange_a.tau - gear.tau_bf1_0;
  gear.tauLossMin_m = if noEvent(gear.tau_eta_m > 0.0) then gear.quadrant4_m else gear.quadrant3_m;
  gear.quadrant1 = (1.0 - gear.eta_mf1) * gear.flange_a.tau + gear.tau_bf1;
  gear.quadrant2 = (1.0 + -1.0 / gear.eta_mf2) * gear.flange_a.tau + gear.tau_bf2;
  gear.quadrant4 = (1.0 + -1.0 / gear.eta_mf2) * gear.flange_a.tau - gear.tau_bf2;
  gear.quadrant3 = (1.0 - gear.eta_mf1) * gear.flange_a.tau - gear.tau_bf1;
  gear.tau_eta = if gear.ideal then gear.flange_a.tau else if gear.locked then gear.flange_a.tau else if gear.startForward or pre(gear.mode) == 1 then gear.flange_a.tau - gear.tau_bf_a else gear.flange_a.tau + gear.tau_bf_a;
  gear.tau_etaPos = gear.tau_eta >= 0.0;
  gear.tau_aPos = gear.tau_etaPos;
  gear.tauLossMax = if gear.tau_etaPos then gear.quadrant1 else gear.quadrant2;
  gear.tauLossMin = if gear.tau_etaPos then gear.quadrant4 else gear.quadrant3;
  gear.startForward = pre(gear.mode) == 0 and gear.sa > gear.tauLossMax_p or initial() and gear.w_a > 0.0;
  gear.startBackward = pre(gear.mode) == 0 and gear.sa < gear.tauLossMin_m or initial() and gear.w_a < 0.0;
  gear.locked = not (gear.ideal or pre(gear.mode) == 1 or gear.startForward or pre(gear.mode) == -1 or gear.startBackward);
  gear.tauLoss = if gear.ideal then 0.0 else if gear.locked then gear.sa else if gear.startForward or pre(gear.mode) == 1 then gear.tauLossMax else gear.tauLossMin;
  gear.a_a = if gear.locked then 0.0 else gear.sa - gear.tauLoss;
  gear.mode = if gear.ideal then 2 else if (pre(gear.mode) == 1 or gear.startForward) and gear.w_a > 0.0 then 1 else if (pre(gear.mode) == -1 or gear.startBackward) and gear.w_a < 0.0 then -1 else 0;
  gear.lossPower = gear.tauLoss * gear.w_a;
  Inertia1.phi = Inertia1.flange_a.phi;
  Inertia1.phi = Inertia1.flange_b.phi;
  Inertia1.w = der(Inertia1.phi);
  Inertia1.a = der(Inertia1.w);
  Inertia1.J * Inertia1.a = Inertia1.flange_a.tau + Inertia1.flange_b.tau;
  Inertia2.phi = Inertia2.flange_a.phi;
  Inertia2.phi = Inertia2.flange_b.phi;
  Inertia2.w = der(Inertia2.phi);
  Inertia2.a = der(Inertia2.w);
  Inertia2.J * Inertia2.a = Inertia2.flange_a.tau + Inertia2.flange_b.tau;
  torque1.flange.tau = -torque1.tau;
  torque2.flange.tau = -torque2.tau;
  DriveSine.y = DriveSine.offset + (if time < DriveSine.startTime then 0.0 else DriveSine.amplitude * sin(6.28318530717959 * DriveSine.freqHz * (time - DriveSine.startTime) + DriveSine.phase));
  load.y = load.offset + (if time < load.startTime then 0.0 else if time < load.startTime + load.duration then (time - load.startTime) * load.height / load.duration else load.height);
  bearingFriction.tau0 = Modelica.Math.tempInterpol1(0.0, {{bearingFriction.tau_pos[1,1], bearingFriction.tau_pos[1,2]}, {bearingFriction.tau_pos[2,1], bearingFriction.tau_pos[2,2]}}, 2);
  bearingFriction.tau0_max = bearingFriction.peak * bearingFriction.tau0;
  bearingFriction.free = false;
  bearingFriction.phi = bearingFriction.flange_a.phi - bearingFriction.phi_support;
  bearingFriction.flange_b.phi = bearingFriction.flange_a.phi;
  bearingFriction.w = der(bearingFriction.phi);
  bearingFriction.a = der(bearingFriction.w);
  bearingFriction.w_relfric = bearingFriction.w;
  bearingFriction.a_relfric = bearingFriction.a;
  bearingFriction.flange_a.tau + bearingFriction.flange_b.tau - bearingFriction.tau = 0.0;
  bearingFriction.tau = if bearingFriction.locked then bearingFriction.sa else if bearingFriction.startForward then Modelica.Math.tempInterpol1(bearingFriction.w, {{bearingFriction.tau_pos[1,1], bearingFriction.tau_pos[1,2]}, {bearingFriction.tau_pos[2,1], bearingFriction.tau_pos[2,2]}}, 2) else if bearingFriction.startBackward then -Modelica.Math.tempInterpol1(-bearingFriction.w, {{bearingFriction.tau_pos[1,1], bearingFriction.tau_pos[1,2]}, {bearingFriction.tau_pos[2,1], bearingFriction.tau_pos[2,2]}}, 2) else if pre(bearingFriction.mode) == 1 then Modelica.Math.tempInterpol1(bearingFriction.w, {{bearingFriction.tau_pos[1,1], bearingFriction.tau_pos[1,2]}, {bearingFriction.tau_pos[2,1], bearingFriction.tau_pos[2,2]}}, 2) else -Modelica.Math.tempInterpol1(-bearingFriction.w, {{bearingFriction.tau_pos[1,1], bearingFriction.tau_pos[1,2]}, {bearingFriction.tau_pos[2,1], bearingFriction.tau_pos[2,2]}}, 2);
  bearingFriction.lossPower = bearingFriction.tau * bearingFriction.w_relfric;
  bearingFriction.startForward = pre(bearingFriction.mode) == 0 and (bearingFriction.sa > bearingFriction.tau0_max or pre(bearingFriction.startForward) and bearingFriction.sa > bearingFriction.tau0) or pre(bearingFriction.mode) == -1 and bearingFriction.w_relfric > bearingFriction.w_small or initial() and bearingFriction.w_relfric > 0.0;
  bearingFriction.startBackward = pre(bearingFriction.mode) == 0 and (bearingFriction.sa < (-bearingFriction.tau0_max) or pre(bearingFriction.startBackward) and bearingFriction.sa < (-bearingFriction.tau0)) or pre(bearingFriction.mode) == 1 and bearingFriction.w_relfric < (-bearingFriction.w_small) or initial() and bearingFriction.w_relfric < 0.0;
  bearingFriction.locked = not bearingFriction.free and not (pre(bearingFriction.mode) == 1 or bearingFriction.startForward or pre(bearingFriction.mode) == -1 or bearingFriction.startBackward);
  bearingFriction.a_relfric = if bearingFriction.locked then 0.0 else if bearingFriction.free then bearingFriction.sa else if bearingFriction.startForward then bearingFriction.sa - bearingFriction.tau0_max else if bearingFriction.startBackward then bearingFriction.sa + bearingFriction.tau0_max else if pre(bearingFriction.mode) == 1 then bearingFriction.sa - bearingFriction.tau0_max else bearingFriction.sa + bearingFriction.tau0_max;
  bearingFriction.mode = if bearingFriction.free then 2 else if (pre(bearingFriction.mode) == 1 or pre(bearingFriction.mode) == 2 or bearingFriction.startForward) and bearingFriction.w_relfric > 0.0 then 1 else if (pre(bearingFriction.mode) == -1 or pre(bearingFriction.mode) == 2 or bearingFriction.startBackward) and bearingFriction.w_relfric < 0.0 then -1 else 0;
  fixed.flange.phi = fixed.phi0;
  gear.flange_a.tau + Inertia1.flange_b.tau = 0.0;
  gear.flange_b.tau + Inertia2.flange_a.tau = 0.0;
  gear.support.tau + torque1.support.tau + torque2.support.tau + bearingFriction.support.tau + fixed.flange.tau = 0.0;
  Inertia1.flange_a.tau + bearingFriction.flange_b.tau = 0.0;
  Inertia2.flange_b.tau + torque2.flange.tau = 0.0;
  torque1.flange.tau + bearingFriction.flange_a.tau = 0.0;
  Inertia2.flange_b.phi = torque2.flange.phi;
  Inertia2.flange_a.phi = gear.flange_b.phi;
  Inertia1.flange_b.phi = gear.flange_a.phi;
  Inertia1.flange_a.phi = bearingFriction.flange_b.phi;
  bearingFriction.flange_a.phi = torque1.flange.phi;
  DriveSine.y = torque1.tau;
  load.y = torque2.tau;
  bearingFriction.support.phi = fixed.flange.phi;
  bearingFriction.support.phi = gear.support.phi;
  bearingFriction.support.phi = torque1.support.phi;
  bearingFriction.support.phi = torque2.support.phi;
end Modelica.Mechanics.Rotational.Examples.LossyGearDemo2;
"
Evaluating: getErrorString()
""
