diff --git a/contents/verlet_integration/code/java/Verlet.java b/contents/verlet_integration/code/java/Verlet.java new file mode 100644 index 000000000..6d102809b --- /dev/null +++ b/contents/verlet_integration/code/java/Verlet.java @@ -0,0 +1,80 @@ +// Submitted by lolatomroflsinnlos +public class VerletValues { + public double time; + public double vel; + + public VerletValues(double time, double vel) { + this.time = time; + this.vel = vel; + } +} + +public class Verlet { + static double verlet(double pos, double acc, double dt) { + + // Note that we are using a temp variable for the previous position + double prev_pos, temp_pos, time; + prev_pos = pos; + time = 0; + + while (pos > 0) { + time += dt; + temp_pos = pos; + pos = pos*2 - prev_pos + acc * dt * dt; + prev_pos = temp_pos; + } + + return time; + } + + static VerletValues stormer_verlet(double pos, double acc, double dt) { + + // Note that we are using a temp variable for the previous position + double prev_pos, temp_pos, time, vel; + prev_pos = pos; + vel = 0; + time = 0; + while (pos > 0) { + time += dt; + temp_pos = pos; + pos = pos*2 - prev_pos + acc * dt * dt; + prev_pos = temp_pos; + + // The acceleration is constant, so the velocity is straightforward + vel += acc*dt; + } + + VerletValues stormerVerlet = new VerletValues(time, vel); + return stormerVerlet; + } + + static VerletValues velocity_verlet(double pos, double acc, double dt) { + + // Note that we are using a temp variable for the previous position + double time, vel; + vel = 0; + time = 0; + while (pos > 0) { + time += dt; + pos += vel*dt + 0.5*acc * dt * dt; + vel += acc*dt; + } + + VerletValues velocityVerlet = new VerletValues(time, vel); + return velocityVerlet; + } + + public static void main(String[] args) { + + double verletTime = verlet(5.0, -10, 0.01); + System.out.println("Time for Verlet integration is: " + verletTime); + + VerletValues stormerVerlet = stormer_verlet(5.0, -10, 0.01); + System.out.println("Time for Stormer Verlet integration is: " + stormerVerlet.time); + System.out.println("Velocity for Stormer Verlet integration is: " + stormerVerlet.vel); + + VerletValues velocityVerlet = velocity_verlet(5.0, -10, 0.01); + System.out.println("Time for velocity Verlet integration is: " + velocityVerlet.time); + System.out.println("Velocity for velocity Verlet integration is: " + velocityVerlet.vel); + } +} diff --git a/contents/verlet_integration/code/java/verlet.java b/contents/verlet_integration/code/java/verlet.java deleted file mode 100644 index de6459508..000000000 --- a/contents/verlet_integration/code/java/verlet.java +++ /dev/null @@ -1,66 +0,0 @@ -// Submitted by lolatomroflsinnlos -static void verlet(double pos, double acc, double dt){ - - // Note that we are using a temp variable for the previous position - double prev_pos, temp_pos, time; - prev_pos = pos; - time = 0; - - while (pos > 0){ - time += dt; - temp_pos = pos; - pos = pos*2 - prev_pos + acc * dt * dt; - prev_pos = temp_pos; - } - - System.out.println(time); - -} - -// Simple function for stormer-verlet -static void stormer_verlet(double pos, double acc, double dt){ - - // Note that we are using a temp variable for the previous position - double prev_pos, temp_pos, time, vel; - prev_pos = pos; - vel = 0; - time = 0; - while (pos > 0){ - time += dt; - temp_pos = pos; - pos = pos*2 - prev_pos + acc * dt * dt; - prev_pos = temp_pos; - - // The acceleration is constant, so the velocity is straightforward - vel += acc*dt; - } - - System.out.println(time); - -} - -// Simple function for velocity-verlet -static void velocity_verlet(double pos, double acc, double dt){ - - // Note that we are using a temp variable for the previous position - double time, vel; - vel = 0; - time = 0; - while (pos > 0){ - time += dt; - pos += vel*dt + 0.5*acc * dt * dt; - vel += acc*dt; - } - - System.out.println(time); - -} - -public static void main(String[] args) { - - verlet(5.0, -10, 0.01); - stormer_verlet(5.0, -10, 0.01); - velocity_verlet(5.0, -10, 0.01); - -} - diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index da97169a0..a416a4820 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -37,7 +37,7 @@ Here is what it looks like in code: {% sample lang="c" %} [import:3-16, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} -[import:2-18, lang:"java"](code/java/verlet.java) +[import:13-28, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import:28-33, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} @@ -83,7 +83,7 @@ Here's what it looks like in code: {% sample lang="c" %} [import:18-33, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} -[import:21-40, lang:"java"](code/java/verlet.java) +[import:30-49, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import:35-42, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} @@ -140,7 +140,7 @@ Here is the velocity Verlet method in code: {% sample lang="c" %} [import:35-46, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} -[import:43-57, lang:"java"](code/java/verlet.java) +[import:51-65, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import:44-48, lang:"python"](code/python/verlet.py) {% sample lang="hs" %} @@ -176,7 +176,7 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w {% sample lang="c" %} [import, lang:"c_cpp"](code/c/verlet.c) {% sample lang="java" %} -[import, lang:"java"](code/java/verlet.java) +[import, lang:"java"](code/java/Verlet.java) {% sample lang="py" %} [import, lang:"python"](code/python/verlet.py) {% sample lang="hs" %}