Advertisement
Guest User

Untitled

a guest
May 25th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.concurrent.BrokenBarrierException;
  3. import java.util.concurrent.CyclicBarrier;
  4. import java.util.concurrent.TimeUnit;
  5. import java.util.function.Function;
  6.  
  7. public class Simulation {
  8.  
  9.     //    final Problem problem = new HeatTransfer();
  10.     final Exercise problem = new Exercise();
  11. //        final Problem problem = new Exercise2CN();
  12.  
  13.     public void run(int levels, double dt, int steps, Function<Double, Double> initialState) throws Exception {
  14.         Vertex S = new Vertex(null, null, null, "S");
  15.         double h = 1 / Math.pow(2.0, levels - 1);
  16.         try {
  17.             //make a tree
  18.             ArrayList<ArrayList<Production>> productionList = new ArrayList<>();
  19.  
  20.             CyclicBarrier barrier = new CyclicBarrier(1 + 1); //P1 inicjalization
  21.             ArrayList<Production> firstLevel = new ArrayList<>();
  22.             firstLevel.add(new P1(S, barrier));
  23.             productionList.add(firstLevel);
  24.             firstLevel.get(0).start();
  25.             barrier.await();
  26.  
  27.             ArrayList<Production> lastLevel = firstLevel;
  28.             ArrayList<Production> newLevel = new ArrayList<>();
  29.             for (int i = 1; i < levels - 1; i++) { //P2 inicjalization
  30.                 barrier = new CyclicBarrier(lastLevel.size() * 2 + 1);
  31.                 for (Production parent : lastLevel) {
  32.                     newLevel.add(new P2(parent.m_vertex.m_left, barrier));
  33.                     newLevel.add(new P2(parent.m_vertex.m_right, barrier));
  34.                 }
  35.                 for (Production production : newLevel) {
  36.                     production.start();
  37.                 }
  38.                 barrier.await();
  39.                 productionList.add(newLevel);
  40.                 lastLevel = newLevel;
  41.                 newLevel = new ArrayList<>();
  42.             }
  43.  
  44.             barrier = new CyclicBarrier(lastLevel.size() * 2 + 1); //P3 inicjalization
  45.             for (Production parent : lastLevel) {
  46.                 newLevel.add(new P3(parent.m_vertex.m_left, barrier));
  47.                 newLevel.add(new P3(parent.m_vertex.m_right, barrier));
  48.             }
  49.             for (Production production : newLevel) {
  50.                 production.start();
  51.             }
  52.             productionList.add(newLevel);
  53.             barrier.await();
  54.  
  55.             //initial states
  56.             for (int i = 0; i < Math.pow(2.0, levels - 1); i++) {
  57.                 newLevel.get(i).m_vertex.m_x[1] = initialState.apply(i / Math.pow(2.0, levels - 1));
  58.                 newLevel.get(i).m_vertex.m_x[2] = initialState.apply((i + 1) / Math.pow(2.0, levels - 1));
  59.             }
  60.  
  61.  
  62.             // Plot the initial state
  63.             int n = newLevel.size();
  64.             double[] init = new double[n + 1];
  65.  
  66.             for (int i = 0; i < n; i++) {
  67.                 init[i] = newLevel.get(i).m_vertex.m_x[1];
  68.             }
  69.             init[n] = newLevel.get(newLevel.size() - 1).m_vertex.m_x[2];
  70.             plotSolution(init);
  71.  
  72.             for (int j = 0; j < steps; ++j) {
  73.                 // Run the solver
  74.                 ArrayList<Production> currentPLevel = newLevel; //A1-AN inicjalization
  75.                 ArrayList<Production> temporaryLevel = new ArrayList<>();
  76.                 barrier = new CyclicBarrier(currentPLevel.size() + 1);
  77.                 temporaryLevel.add(problem.makeA1(currentPLevel.get(0).m_vertex, 1 / Math.pow(2.0, levels - 1), dt, j * dt, j, barrier)); //A1
  78.                 for (int i = 1; i < currentPLevel.size() - 1; i++) { //A
  79.                     Production production = currentPLevel.get(i);
  80.                     temporaryLevel.add(problem.makeA(production.m_vertex, 1 / Math.pow(2.0, levels - 1), dt, j * dt, j, barrier));
  81.                 }
  82.                 temporaryLevel.add(problem.makeAN(currentPLevel.get(currentPLevel.size() - 1).m_vertex, 1 / Math.pow(2.0, levels - 1), dt, j * dt, j, barrier)); //AN
  83.                 for (Production production : temporaryLevel) {
  84.                     production.start();
  85.                 }
  86.                 barrier.await();
  87.  
  88.                 for (int i = levels - 1; i > 1; i--) { //A2 E2 inicjalization
  89.                     currentPLevel = productionList.get(i - 1);
  90.                     temporaryLevel = new ArrayList<>(); //A2
  91.                     barrier = new CyclicBarrier(currentPLevel.size() + 1);
  92.                     for (Production production : currentPLevel) {
  93.                         temporaryLevel.add(new A2(production.m_vertex, barrier));
  94.                     }
  95.                     for (Production production : temporaryLevel) {
  96.                         production.start();
  97.                     }
  98.                     barrier.await();
  99.                     temporaryLevel = new ArrayList<>(); //E2
  100.                     barrier = new CyclicBarrier(currentPLevel.size() + 1);
  101.                     for (Production production : currentPLevel) {
  102.                         temporaryLevel.add(new E2(production.m_vertex, barrier));
  103.                     }
  104.                     for (Production production : temporaryLevel) {
  105.                         production.start();
  106.                     }
  107.                     barrier.await();
  108.                 }
  109.  
  110.                 barrier = new CyclicBarrier(1 + 1); //Aroot inicjalization
  111.                 Aroot aroot = new Aroot(productionList.get(0).get(0).m_vertex, barrier);
  112.                 aroot.start();
  113.                 barrier.await();
  114.  
  115.                 barrier = new CyclicBarrier(1 + 1); //Eroot inicjalization
  116.                 Eroot eroot = new Eroot(productionList.get(0).get(0).m_vertex, barrier);
  117.                 eroot.start();
  118.                 barrier.await();
  119.  
  120.                 if (levels > 2) {
  121.                     for (int i = 1; i < levels - 1; i++) { //BS inicjalization
  122.                         currentPLevel = productionList.get(i - 1);
  123.                         temporaryLevel = new ArrayList<>();
  124.                         barrier = new CyclicBarrier(currentPLevel.size() + 1);
  125.                         for (Production production : currentPLevel) {
  126.                             temporaryLevel.add(new BS(production.m_vertex, barrier));
  127.                         }
  128.                         for (Production production : temporaryLevel) {
  129.                             production.start();
  130.                         }
  131.                         barrier.await();
  132.                     }
  133.  
  134.                     currentPLevel = productionList.get(levels - 2); //BSA
  135.                     temporaryLevel = new ArrayList<>();
  136.                     barrier = new CyclicBarrier(currentPLevel.size() + 1);
  137.                     for (Production production : currentPLevel) {
  138.                         temporaryLevel.add(new BSA(production.m_vertex, barrier));
  139.                     }
  140.                     for (Production production : temporaryLevel) {
  141.                         production.start();
  142.                     }
  143.                     barrier.await();
  144.                 } else {
  145.                     barrier = new CyclicBarrier(1 + 1); //BSA inicjalization
  146.                     BSA bsa = new BSA(productionList.get(0).get(0).m_vertex, barrier);
  147.                     bsa.start();
  148.                     barrier.await();
  149.                 }
  150.  
  151.                 // Get the solution from the leaves and plot it
  152.                 double[] solution = new double[n + 1];
  153.                 for (int i = 0; i < n; i++) {
  154.                     solution[i] = newLevel.get(i).m_vertex.m_x[1];
  155.                 }
  156.                 solution[n] = newLevel.get(newLevel.size() - 1).m_vertex.m_x[2];
  157.                 plotSolution(solution);
  158.             }
  159.         } catch (InterruptedException | BrokenBarrierException e) {
  160.             throw new RuntimeException(e);
  161.         }
  162.     }
  163.  
  164.     private void plotSolution(double[] values) throws InterruptedException {
  165.         int delay = 200;
  166.         ResultPrinter.printResult(values);
  167.         ResultPrinter.plot.setFixedBounds(1, 0, 2);
  168.         TimeUnit.MILLISECONDS.sleep(delay);
  169.     }
  170.  
  171.     public static void main(String[] args) throws Exception {
  172.         Simulation s = new Simulation();
  173.  
  174.         int k = 8;
  175.         double dt = 0.01;
  176.         int steps =4;
  177.         s.run(k, dt, steps, x -> x);
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement