<commit_before>/* nbody_reductions + triangle_iterator
 *
 * The Great Computer Language Shootout
 * http://shootout.alioth.debian.org/
 *
 * contributed by <NAME>
 *
*/

config var n = 10000;
param PI = 3.141592653589793;
const solar_mass = (4 * PI * PI);
param days_per_year = 365.24;
const vecLen = 0..2;
const NBODIES = 0..4;

class Planet {
  var coord_vector : [vecLen] real; // x,y,z
  var vel_vector : [vecLen] real;   // vx,vy,vz
  var mass : real;
}

iter TriangleIter(B: [] Planet) {
  for (b1,i) in zip(B,NBODIES) do {
    for b2 in B[i+1..] do {
      yield (b1,b2);
    }
  }
}

proc advance(B: [] Planet, dt: real) {
  for (b1,b2) in TriangleIter(B) do {
    var d : [vecLen] real = b1.coord_vector - b2.coord_vector;
    var distance = sqrt(+ reduce d**2);
    var mag = dt / (distance**3);
    b1.vel_vector -= d * b2.mass * mag;
    b2.vel_vector += d * b1.mass * mag;
  }
  for b in B do {
    b.coord_vector += dt * b.vel_vector;
  }
}

proc energy(B : [] Planet) : real {
  var e : real;
  for (b1,i) in zip(B,NBODIES) do {
    e += 0.5 * b1.mass * (+ reduce b1.vel_vector**2);
    for b2 in B[i+1..] do {
      var d : [vecLen] real = b1.coord_vector - b2.coord_vector;
      var distance = sqrt(+ reduce d**2);
      e -= (b1.mass * b2.mass) / distance;
    }
  }
  return e;
}

proc offset_momentum(B : [] Planet) {
  var p : [vecLen] real;
  for b in B do
    p += b.vel_vector * b.mass;
  B(0).vel_vector = -p / solar_mass;
}

proc main() {
  var bodies : [NBODIES] Planet;
  
  var p0,v0 : [vecLen] real = (0,0,0);
  bodies(0) = new Planet(p0,v0, solar_mass);
  var p1 : [vecLen] real = 	(4.84143144246472090e+00,
                                 -1.16032004402742839e+00,
                                 -1.03622044471123109e-01);
  var v1 : [vecLen] real = 	(1.66007664274403694e-03 * days_per_year,
                                 7.69901118419740425e-03 * days_per_year,
                                 -6.90460016972063023e-05 * days_per_year);
  bodies(1) = new Planet(p1, v1, 9.54791938424326609e-04 * solar_mass);
  var p2 : [vecLen] real = (8.34336671824457987e+00,
                            4.12479856412430479e+00,<commit_msg>Made a few simple changes that sped this up significantly<commit_after>/* nbody_reductions + triangle_iterator
 *
 * The Great Computer Language Shootout
 * http://shootout.alioth.debian.org/
 *
 * contributed by <NAME>
 *
*/

config var n = 10000;
param PI = 3.141592653589793;
const solar_mass = (4 * PI * PI);
param days_per_year = 365.24;
const vecLen = {0..2};
const numBodies = 4;
const NBODIES = 0..numBodies;

class Planet {
  var coord_vector : [vecLen] real; // x,y,z
  var vel_vector : [vecLen] real;   // vx,vy,vz
  var mass : real;
}

iter TriangleIter(B: [] Planet) {
  for i in NBODIES {
    refvar b1 = B[i];
    for j in i+1..numBodies {
      yield (b1,B[j]);
    }
  }
}

proc advance(B: [] Planet, dt: real) {
  for (b1,b2) in TriangleIter(B) do {
    var d : [vecLen] real = b1.coord_vector - b2.coord_vector;
    var distance = sqrt(+ reduce d**2);
    var mag = dt / (distance**3);
    b1.vel_vector -= d * b2.mass * mag;
    b2.vel_vector += d * b1.mass * mag;
  }
  for b in B do {
    b.coord_vector += dt * b.vel_vector;
  }
}

proc energy(B : [] Planet) : real {
  var e : real;
  for (b1,i) in zip(B,NBODIES) do {
    e += 0.5 * b1.mass * (+ reduce b1.vel_vector**2);
    for b2 in B[i+1..] do {
      var d : [vecLen] real = b1.coord_vector - b2.coord_vector;
      var distance = sqrt(+ reduce d**2);
      e -= (b1.mass * b2.mass) / distance;
    }
  }
  return e;
}

proc offset_momentum(B : [] Planet) {
  var p : [vecLen] real;
  for b in B do
    p += b.vel_vector * b.mass;
  B(0).vel_vector = -p / solar_mass;
}

proc main() {
  var bodies : [NBODIES] Planet;
  
  var p0,v0 : [vecLen] real = (0,0,0);
  bodies(0) = new Planet(p0,v0, solar_mass);
  var p1 : [vecLen] real =     (4.84143144246472090e+00,
                                -1.16032004402742839e+00,
                                -1.03622044471123109e-01);
  var v1 : [vecLen] real =     (1.66007664274403694e-03 * days_per_year,
                                7.69901118419740425e-03 * days_per_year,
                                -6.90460016972063023e-05 * days_per_year);
  bodies(1) = new Planet(p1, v1, 9.54791938424326609e-04 * solar_mass);
  var p2 : [vecLen] real = (8.34336671824457987e+00,
                            4.12479856412430479e+00,
{'fraction_non_alphanumeric': 0.1455038409399006, 'fraction_numerical': 0.11432444645277903, 'mean_word_length': 2.279259259259259, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7247361', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1792, 'n_words': 422}