|
|
Differential Equations in Verilog: model of a spring-mass system
This example works on FinSim 10_07_01 and subsequent versions.
This example models a spring-mass system that is excited with an
external force Fe, which is a sinusoid with ramps in both frequency
and amplitude for 1 sec and a sinusoid with constant frequency and
amplitude for the second second. The total time considered is three seconds.
module top;
parameter omega = 7;
parameter total_time = 3;/* seconds */
parameter nr_samples_per_cycle = 50;
parameter integer Size = total_time*nr_samples_per_cycle*omega;
parameter real fmax = 35000;/* N */
parameter nrEq = 1;
parameter order = 2;
real Fe[0:nrEq-1][0 : Size], x[0:nrEq-1][0 : Size],
y[0:order-1][0:nrEq-1][0 : Size];
integer i;
reg [0 : 3199] ressymb[0 : nrEq-1];
real k, h, r_size, r_omega, r_nr_samples_per_cycle, ramp_fmax;
real m = 170000;/*Kg*/
real c = 400000;/*N*s/m*/
real coef[0 : nrEq-1][0 : (order+1)*nrEq-1];
initial begin
#1;
/* convert integers to reals to be used in divisions*/
r_size = Size;
r_nr_samples_per_cycle = nr_samples_per_cycle;
r_omega = omega;
ramp_fmax = fmax/(r_nr_samples_per_cycle*r_omega);
/* set double of sampling period */
h = 2*total_time/r_size;
/* set strength of spring */
k = 2*1.7*10**9;
/* provide values of external force Fe at each sampling time */
$InitM(Fe, ($I2 < nr_samples_per_cycle*omega) ?
(ramp_fmax*$I2 * $VpSin(((2*$Pi*$I2*$I2*total_time)/nr_samples_per_cycle)/
(omega*r_size))) : ($I2 > Size - nr_samples_per_cycle*omega) ? 0.0 :
fmax * $VpSin(2*$Pi*omega*total_time*$I2/Size));
/* provide the coeficients of the differential eq:
m*xdd(t) + c*xd[t] + k*x[t] = Fe[t]; */
coef[0][0] = m;
coef[0][1] = c;
coef[0][2] = k;
/* provide initial conditions, where y = xd */
x[0][0] = 0;
y[0][0][0] = 0;
/* call dif eq solver. The array x will contain the solution and the
array y will contain the first derivative of x.
Note that the initial conditions have been already placed in x[0]
and y[0].
$VpLODE arguments:
1) order of differential equation(s)
2) number of differential equations in the system
3) double of the sampling period
4) number of samples
5) matrix, where each row is and array containing the sampled
values of the corresponding solution.
6) a matrix representing the concatenation of the coefficients
matrices. Each row of this matrix corresponds to one equation.
7) matrix of external forces. Each row represents the sampled
values of the force in the corresponding equation.
8) three dimensional array with ranges [0:order-1], [0:nrEq-1], and
[0:Size-1]. The first dimension indicates the
order of the derivative, the second dimension indicates the
particular function for which a solution has been found and the
third dimension indicates the particular sampling point.
9) this is an inout argument and it is an array. The input is the
symbolic representation of the external force for each equation.
The output is the symbolic representation of the solutions in case
the solver can figure them out. If not the returned value is the
null string.
*/
$VpLODE(order, nrEq, h, Size, x, coef, Fe, y, ressymb);
// Print values of x
$PrintM(x,"%e");
/* $VpPtPlot arguments:
1) file name that must be copied into standalonePlotMLSample.txt in
the PtPlot directory, before invoking netscape plot.html (or
another browser) in order to display the values of x.
2) number of curves to be plotted
3) double of the sampling period
4) title
5) total number of units on the horizontal axis
6) label on the horizontal axis
7) label on the vertical axis
8) first element of each row of x to be displayed
9) last element of each row of x to be displayed
10) matrix containing the elements of x: each row is plotted as one
curve.
*/
$VpPtPlot("standalonePlotMLSample.txt", nrEq, h,
"OLE with ramp in Amplitude and frequence",total_time,
"Time (sec)", "Amplitude(.01 mm)", 0, Size-1, x);
end
endmodule
|