Task: N-body problem




Go: package main

import (
    "fmt"
    "math"
    "os"
)

type vector struct{ x, y, z float64 }

func (v vector) add(w vector) vector {
    return vector{v.x + w.x, v.y + w.y, v.z + w.z}
}

func (v vector) sub(w vector) vector {
    return vector{v.x - w.x, v.y - w.y, v.z - w.z}
}

func (v vector) scale(m float64) vector {
    return vector{v.x * m, v.y * m, v.z * m}
}

func (v vector) mod() float64 {
    return math.Sqrt(v.x*v.x + v.y*v.y + v.z*v.z)
}

var (
    bodies, timeSteps                    int
    masses                               []float64
    gc                                   float64
    positions, velocities, accelerations []vector
)

func initiateSystem(fileName string) error {
    file, err := os.Open(fileName)
    if err != nil {
        return err
    }
    defer file.Close()
    fmt.Fscanf(file, "%f%d%d", &gc, &bodies, &timeSteps)
    masses = make([]float64, bodies)
    positions = make([]vector, bodies)
    velocities = make([]vector, bodies)
    accelerations = make([]vector, bodies)
    for i := 0; i < bodies; i++ {
        fmt.Fscanf(file, "%f", &masses[i])
        fmt.Fscanf(file, "%f%f%f", &positions[i].x, &positions[i].y, &positions[i].z)
        fmt.Fscanf(file, "%f%f%f", &velocities[i].x, &velocities[i].y, &velocities[i].z)
    }
    return nil
}

func resolveCollisions() {
    for i := 0; i < bodies-1; i++ {
        for j := i + 1; j < bodies; j++ {
            if positions[i] == positions[j] {
                velocities[i], velocities[j] = velocities[j], velocities[i]
            }
        }
    }
}

func computeAccelerations() {
    for i := 0; i < bodies; i++ {
        accelerations[i] = vector{0, 0, 0}
        for j := 0; j < bodies; j++ {
            if i != j {
                temp := gc * masses[j] / math.Pow(positions[i].sub(positions[j]).mod(), 3)
                accelerations[i] = accelerations[i].add(positions[j].sub(positions[i]).scale(temp))
            }
        }
    }
}

func computeVelocities() {
    for i := 0; i < bodies; i++ {
        velocities[i] = velocities[i].add(accelerations[i])
    }
}

func computePositions() {
    for i := 0; i < bodies; i++ {
        positions[i] = positions[i].add(velocities[i].add(accelerations[i].scale(0.5)))
    }
}

func simulate() {
    computeAccelerations()
    computePositions()
    computeVelocities()
    resolveCollisions()
}

func printResults() {
    f := "Body %d : % 8.6f  % 8.6f  % 8.6f | % 8.6f  % 8.6f  % 8.6f\n"
    for i := 0; i < bodies; i++ {
        fmt.Printf(
            f, i+1,
            positions[i].x, positions[i].y, positions[i].z,
            velocities[i].x, velocities[i].y, velocities[i].z,
        )
    }
}

func main() {
    if len(os.Args) != 2 {
        fmt.Printf("Usage : %s <file name containing system configuration data>\n", os.Args[0])
    } else {
        err := initiateSystem(os.Args[1])
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Print("Body   :      x          y          z    |")
        fmt.Println("     vx         vy         vz")
        for i := 0; i < timeSteps; i++ {
            fmt.Printf("\nCycle %d\n", i+1)
            simulate()
            printResults()
        }
    }
}


Java: import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

public class NBodySim {
    private static class Vector3D {
        double x, y, z;

        public Vector3D(double x, double y, double z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public Vector3D plus(Vector3D rhs) {
            return new Vector3D(x + rhs.x, y + rhs.y, z + rhs.z);
        }

        public Vector3D minus(Vector3D rhs) {
            return new Vector3D(x - rhs.x, y - rhs.y, z - rhs.z);
        }

        public Vector3D times(double s) {
            return new Vector3D(s * x, s * y, s * z);
        }

        public double mod() {
            return Math.sqrt(x * x + y * y + z * z);
        }
    }

    private static final Vector3D origin = new Vector3D(0, 0, 0);

    private static class NBody {
        private double gc;
        private int bodies;
        public final int timeSteps;
        private double[] masses;
        private Vector3D[] positions;
        private Vector3D[] velocities;
        private Vector3D[] accelerations;

        public NBody(String fileName) throws IOException {
            Path path = Paths.get(fileName);
            List<String> lines = Files.readAllLines(path);

            String[] gbt = lines.get(0).split(" ");
            gc = Double.parseDouble(gbt[0]);
            bodies = Integer.parseInt(gbt[1]);
            timeSteps = Integer.parseInt(gbt[2]);
            masses = new double[bodies];
            positions = new Vector3D[bodies];
            Arrays.fill(positions, origin);
            velocities = new Vector3D[bodies];
            Arrays.fill(velocities, origin);
            accelerations = new Vector3D[bodies];
            Arrays.fill(accelerations, origin);
            for (int i = 0; i < bodies; ++i) {
                masses[i] = Double.parseDouble(lines.get(i * 3 + 1));
                positions[i] = decompose(lines.get(i * 3 + 2));
                velocities[i] = decompose(lines.get(i * 3 + 3));
            }
            System.out.printf("Contents of %s\n", fileName);
            for (String line : lines) {
                System.out.println(line);
            }
            System.out.println();
            System.out.print("Body   :      x          y          z    |");
            System.out.println("     vx         vy         vz");
        }

        private Vector3D decompose(String line) {
            String[] xyz = line.split(" ");
            double x = Double.parseDouble(xyz[0]);
            double y = Double.parseDouble(xyz[1]);
            double z = Double.parseDouble(xyz[2]);
            return new Vector3D(x, y, z);
        }

        private void resolveCollisions() {
            for (int i = 0; i < bodies; ++i) {
                for (int j = i + 1; j < bodies; ++j) {
                    if (positions[i].x == positions[j].x
                        && positions[i].y == positions[j].y
                        && positions[i].z == positions[j].z) {
                        Vector3D temp = velocities[i];
                        velocities[i] = velocities[j];
                        velocities[j] = temp;
                    }
                }
            }
        }

        private void computeAccelerations() {
            for (int i = 0; i < bodies; ++i) {
                accelerations[i] = origin;
                for (int j = 0; j < bodies; ++j) {
                    if (i != j) {
                        double temp = gc * masses[j] / Math.pow((positions[i].minus(positions[j])).mod(), 3);
                        accelerations[i] = accelerations[i].plus(positions[j].minus(positions[i]).times(temp));
                    }
                }
            }
        }

        private void computeVelocities() {
            for (int i = 0; i < bodies; ++i) {
                velocities[i] = velocities[i].plus(accelerations[i]);
            }
        }

        private void computePositions() {
            for (int i = 0; i < bodies; ++i) {
                positions[i] = positions[i].plus(velocities[i]).plus(accelerations[i].times(0.5));
            }
        }

        public void simulate() {
            computeAccelerations();
            computePositions();
            computeVelocities();
            resolveCollisions();
        }

        public void printResults() {
            String fmt = "Body %d : % 8.6f  % 8.6f  % 8.6f | % 8.6f  % 8.6f  % 8.6f\n";
            for (int i = 0; i < bodies; ++i) {
                System.out.printf(
                    fmt,
                    i + 1,
                    positions[i].x, positions[i].y, positions[i].z,
                    velocities[i].x, velocities[i].y, velocities[i].z
                );
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String filename = "nbody.txt";
        NBody nb = new NBody(filename);
        for (int i = 0; i < nb.timeSteps; ++i) {
            System.out.printf("\nCycle %s\n", i + 1);
            nb.simulate();
            nb.printResults();
        }
    }
}