Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Can anyone figure out what I'm doing wrong?

Can anyone figure out what I'm doing wrong?

Code:


model Mortar3D "Point Mass Mortar Shell Trajectory in Three Dimensions"
  parameter Modelica.SIunits.Acceleration g = 9.8 "gravity";
  parameter Modelica.SIunits.Density p = 1.2041 "air density";
  parameter Modelica.SIunits.Mass m = 6.35 "mass of shell";
  parameter Real Cd = 0.9 "coefficient of drag";
  parameter Modelica.SIunits.Length d = 0.06 "diameter of shell";
  parameter Modelica.SIunits.Velocity Wx = 0.0 "velocity of wind in the X direction";
  parameter Modelica.SIunits.Velocity Wz = 0.0 "velocity of wind in the Z direction";
  parameter Modelica.SIunits.Velocity Wy = 0.0 "velocity of wind in the Y direction";
  parameter Modelica.SIunits.Angle declination = 1000.0 "angle from the ground in mils";
  parameter Modelica.SIunits.Angle azimuth = 0 "angle from North in mils (clockwise)";
  parameter Modelica.SIunits.Length targetAltitude = 0.0 "elevation of target";
  discrete Boolean shellHasImpacted "whether the shell has hit";
  discrete Modelica.SIunits.Time timeOfImpact "time when y == targetY";
  discrete Modelica.SIunits.Length impactN "distance North from launch to impact";
  discrete Modelica.SIunits.Length impactE "distance East from launch to impact";
  Modelica.SIunits.Length x(start = 0) "Meters the shell has traveled in the X direction";
  Modelica.SIunits.Length z(start = 0) "Meters the shell has traveled in the Z direction";
  Modelica.SIunits.Length y(start = 0) "Altitude of the shell relative to the canon";
  Modelica.SIunits.Velocity v(start = 220) "scalar velocity of the shell";
  Modelica.SIunits.Length n(start = 0) "Meters the shell has traveled North";
  Modelica.SIunits.Length e(start = 0) "Meters the shell has traveled East";
  //Modelica.SIunits.Position a(start=200) "Meters the shell has traveled Vertically";
  Modelica.SIunits.Velocity Vx(start = 185) "velocity of shell in the X direction";
  Modelica.SIunits.Velocity Vz(start = 0) "velocity of shell in the Z direction";
  Modelica.SIunits.Velocity Vy(start = 118) "velocity of shell in the Y direction";
  Real Drag(start = 0) "drag force opposing velocity";
//initial equation
//    Vx = v * Modelica.Math.cos(declination / 1000);
//    Vy = v * Modelica.Math.sin(declination / 1000);
equation
  Drag = ((p * Modelica.Constants.pi) / 8) * (Cd / (m / d ^ 2));
  n = x * Modelica.Math.cos(azimuth / 1000) - z * Modelica.Math.sin(azimuth / 1000);
  e = x * Modelica.Math.sin(azimuth / 1000) + z * Modelica.Math.cos(azimuth / 1000);
  der(x) = Vx;
  der(y) = Vy;
  der(z) = Vz;
  v = sqrt((Vx - Wx) ^ 2 + (Vz - Wz) ^ 2 + (Vy - Wy) ^ 2);
  der(Vx) = -Drag * v * (Vx - Wx);
  der(Vz) = -Drag * v * (Vz - Wz);
  der(Vy) = -Drag * v * (Vy - Wy) - g;
algorithm
  when y < targetAltitude and not shellHasImpacted then
      timeOfImpact:=time;
    impactN:=n;
    impactE:=e;
    shellHasImpacted:=true; 
  end when;
end Mortar3D;

The main problem is that the start values don't seem to be holding after the initial timestep 0.

My .csv output (First few lines) is:

"time","Vy","Vz","Vx","y","z","x","der(Vy)","der(Vz)","der(Vx)","der(y)","der(z)","der(x)","e","n","v","impactE","impactN","timeOfImpact","Drag","shellHasImpacted",

0,118,0,185,0,0,0,-482029.0344807348,0,-755708.1218553894,118,0,185,0,0,219.4288039433292,0,0,0,18.616100955647,0,

0.1,-0.1917142436531548,0,0.4946236127748513,0.1504328022527183,0,0.2754381554269071,-7.906739153869569,0,-4.884621516867643,-0.1917142436531548,0,0.4946236127748513,0,0.2754381554269071,0.5304779632877762,0,0,0,18.616100955647,0,

0.2,-0.6464381605952849,0,0.16650213084609,0.1032450377464333,0,0.3063460609543107,-1.766757758910391,0,-2.069110445943552,-0.6464381605952849,0,0.16650213084609,0,0.3063460609543107,0.667536706893414,0,0,0,18.616100955647,0,


If there's a better way to format this, let me know.  But to extract the information I think is relevant, here is the scalar velocity and timestep data:

time     v
0           140.23...
0.1        0.3623...
0.2        0.6307...
0.3       0.7169...

I would expect the velocity to remain in line with the first step (around 140 m/s), but it drops off abruptly.  Am I doing something wrong in my Modelica code?

Anything I'm leaving out, or that I should format better?

Thanks to whoever takes the time to help me with this issue, hopefully it's something obvious.

Re: Can anyone figure out what I'm doing wrong?

For what it's worth, I think the problem was the two conversion equations

Code:


  n = x * Modelica.Math.cos(azimuth / 1000) - z * Modelica.Math.sin(azimuth / 1000);
  e = x * Modelica.Math.sin(azimuth / 1000) + z * Modelica.Math.cos(azimuth / 1000);

should only happen once, as part of the impact algorithm.  Moving this seems to have fixed the problem.

Is there some way to define an equation to determine the "start" attribute of a time variable?

For example, I want Vx.start = v*cos(angle) and Vz.start = v*sin(angle).

Is the initial algorithm/equation used for this?  Is it the same thing?

Thanks.

There are 0 guests and 0 other users also viewing this topic
You are here: