-
Notifications
You must be signed in to change notification settings - Fork 1
/
DriverMain.java
100 lines (93 loc) · 3.85 KB
/
DriverMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Runs a particular example of the Double Pendulum Simulator
* The parameters here are completely adjustable according to necessary requirements
* @author Sarannya Bhattacharya
*/
import java.io.*;
public class DriverMain {
/**
* Writes the values of Theta1 and Theta2 into .txt files in /data
* @param Theta1 values of theta1 in an array
* @param Theta2 values of theta2 in an array
*/
public static void writer(float[] Theta1, float[] Theta2) {
try {
File theta1 = new File("data" + File.separator + "Theta1.txt");
File theta2 = new File("data" + File.separator + "Theta2.txt");
FileWriter writer1 = new FileWriter(theta1);
FileWriter writer2 = new FileWriter(theta2);
for(int i = 0; i < Theta1.length; i++) {
if(i % 1 == 0) {
writer1.write(i + " " + Theta1[i] + "\n");
writer2.write(i + " " + Theta2[i] + "\n");
}
}
writer1.close();
writer2.close();
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
float length1 = 1;
float length2 = /*(float)Math.sqrt(2) **/ length1;
float mass1 = 1f;
float mass2 = 1.5f;
DoublePendulumDriver pendulumSystem = new DoublePendulumDriver(mass1, mass2, length1, length2);
pendulumSystem.secularEquationSolver();
float[] Bw1 = pendulumSystem.eigenmodeFinder(pendulumSystem.w[0]);
float[] Bw2 = pendulumSystem.eigenmodeFinder(pendulumSystem.w[1]);
float[] time = new float[1000];
for(int i = 0; i < time.length; i++) {
time[i] = i;
}
float[] n1 = new float[time.length];
float[] n2 = new float[time.length];
/**
* We assume that Theta1 = B11 + B12
* Theta2 = B21 + B22 initially at t = 0
*
* float n10 = 1;
* float n20 = 1;
* n1 = pendulumSystem.normalModes(pendulumSystem.w[0], time, n10, 0);
* n2 = pendulumSystem.normalModes(pendulumSystem.w[1], time, n20, 0);
*
* THIS IS NOT USED IN LIEU OF ISSUES WITH NORMALIZATION OF THE COEFFICIENTS. WE IMPLICITLY ASSUME THAT THE NORMAL MODES
* ARE GIVEN AS COSINE FUNCTIONS RATHER THAN NUMERICALLY SOLVE THE EQUATION.
*
* Numerical solution using ODESolver.java's FDM method causes the solution to blow up, presumably due to the lack of normalization
* of the eigenmodes of this algorithm. Hence we stuck to an analytical solution of using the normal modes as the real part of the
* exp(iwt), which is cos (wt).
*/
for(int i = 0; i < time.length; i++) {
n1[i] = (float) Math.cos(pendulumSystem.w[0] * time[i]);
n2[i] = (float) Math.cos(pendulumSystem.w[1] * time[i]);
}
System.out.println("Mass Tensor: ");
System.out.println(pendulumSystem.massTensor[0] + " " + pendulumSystem.massTensor[1]);
System.out.println(pendulumSystem.massTensor[2] + " " + pendulumSystem.massTensor[3]);
System.out.println();
System.out.println("Response Tensor: ");
System.out.println(pendulumSystem.responseTensor[0] + " " + pendulumSystem.responseTensor[1]);
System.out.println(pendulumSystem.responseTensor[2] + " " + pendulumSystem.responseTensor[3]);
System.out.println();
System.out.println("Eigenfrequencies: ");
System.out.println(pendulumSystem.w[0] + " " + pendulumSystem.w[1]);
System.out.println();
System.out.println("Eigenmodes, each line corresponding to w1 and w2 respectively");
System.out.println(Bw1[0] + " " + Bw1[1]);
System.out.println(Bw2[0] + " " + Bw2[1]);
System.out.println();
System.out.println("Normal modes:");
for(int i = 0; i < n1.length; i++) {
System.out.println(n1[i] + " " + n2[i] + " " + time[i]);
}
float[] Theta1 = new float[time.length];
float[] Theta2 = new float[time.length];
for(int i = 0; i < time.length; i++) {
Theta1[i] = Bw1[0] * n1[i] + Bw2[0] * n2[i];
Theta2[i] = Bw1[1] * n1[i] + Bw2[1] * n2[i];
}
writer(Theta1, Theta2);
}
}