d67103c8eaa7661516ead0fa3913e899ce158f45
[IRC.git] / Robust / src / Benchmarks / oooJava / DelaunayRefinement-orig-algo / SerialDelaunayRefinement.java
1 /*
2 Lonestar Benchmark Suite for irregular applications that exhibit 
3 amorphous data-parallelism.
4
5 Center for Grid and Distributed Computing
6 The University of Texas at Austin
7
8 Copyright (C) 2007, 2008, 2009 The University of Texas at Austin
9
10 Licensed under the Eclipse Public License, Version 1.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.eclipse.org/legal/epl-v10.html
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 File: UndirectedEdgeGraph.java 
23
24 */
25
26 public class SerialDelaunayRefinement {  
27   public boolean isFirstRun;
28   public SerialDelaunayRefinement() {
29     isFirstRun = true;
30   }
31
32   public static void main(String args[]) {
33     SerialDelaunayRefinement sdr = new SerialDelaunayRefinement();
34     sdr.runMain(args);
35   }
36   
37   public void runMain(String args[]) {
38     long runtime = 0;
39     //Numbers below are Long.Max_Value
40     long lasttime = 0x7fffffffffffffffL;
41     long mintime = 0x7fffffffffffffffL;
42     for (long run = 0; ((run < 3) || Math.abs(lasttime - runtime) * 64 > Math.min(lasttime, runtime)) && run < 7; run++) {
43       runtime = run(args);
44       if (runtime < mintime) {
45         mintime = runtime;
46       }
47       System.gc();
48     }
49
50     System.out.println("minimum runtime: " + mintime + " ms");
51     System.out.println("");
52   }
53   
54
55   public long run(String args[]) {
56     if (isFirstRun) {
57       System.out.println();
58       System.out.println("Lonestar Benchmark Suite v2.1");
59       System.out.println("Copyright (C) 2007, 2008, 2009 The University of Texas at Austin");
60       System.out.println("http://iss.ices.utexas.edu/lonestar/");
61       System.out.println();
62       System.out.println("application: Delaunay Mesh Refinement (serial version)");
63       System.out.println("Refines a Delaunay triangulation mesh such that no angle");
64       System.out.println("in the mesh is less than 30 degrees");
65       System.out.println("http://iss.ices.utexas.edu/lonestar/delaunayrefinement.html");
66       System.out.println();
67     }
68     if (args.length < 1) {
69       System.out.println("Arguments: <input file> [verify]");
70       System.exit(-1);
71     }
72
73     EdgeGraph mesh = new UndirectedEdgeGraph();
74
75     Mesh m = new Mesh();
76     m.read(mesh, args[0]);
77
78     //treat LinkedList as a stack
79     Stack worklist = new Stack();
80     //    LinkedList worklist = new LinkedList();
81
82     // worklist.addAll(Mesh.getBad(mesh));
83     HashMapIterator it = m.getBad(mesh).iterator();
84     while (it.hasNext()) {
85       worklist.push(it.next());
86     }
87
88     Cavity cavity = new Cavity(mesh);
89     if (isFirstRun) {
90       System.out.println("configuration: " + mesh.getNumNodes() + " total triangles, " + worklist.size() + " bad triangles");
91       System.out.println();
92     }
93
94     System.gc();
95
96 //    long id = Time.getNewTimeId();
97     long startTime = System.currentTimeMillis();
98     while (!worklist.isEmpty()) {
99       Node bad_element = (Node) worklist.pop();
100 //      System.out.println("Bad Node"+ ((Element)mesh.getNodeData(bad_element)).toString());
101       if (bad_element != null && mesh.containsNode(bad_element)) {
102         cavity.initialize(bad_element);
103         cavity.build();
104         cavity.update();
105         
106         //remove old data
107         Node node;
108         for (Iterator iterator = cavity.getPre().getNodes().iterator(); iterator.hasNext();) {
109           node = (Node) iterator.next();
110           mesh.removeNode(node);
111         }
112
113         //add new data
114         for (Iterator iterator1 = cavity.getPost().getNodes().iterator(); iterator1.hasNext();) {
115           node = (Node) iterator1.next();
116           mesh.addNode(node);
117         }
118
119         Edge_d edge;
120         for (Iterator iterator2 = cavity.getPost().getEdges().iterator(); iterator2.hasNext();) {
121           edge = (Edge_d) iterator2.next();
122           mesh.addEdge(edge);
123         }
124
125         // worklist.addAll(cavity.getPost().newBad(mesh));
126         it = cavity.getPost().newBad(mesh).iterator();
127         while (it.hasNext()) {
128           worklist.push((Node)it.next());
129         }
130
131         if (mesh.containsNode(bad_element)) {
132           worklist.push((Node) bad_element);
133         }
134       }
135     }
136     long time = System.currentTimeMillis() - startTime;
137     System.out.println("runtime: " + time + " ms");
138     //TODO note how we only verify on first run...
139     if (isFirstRun && args.length > 1) {
140       verify(mesh);
141     }
142     isFirstRun = false;
143     return time;
144   }
145
146   public void verify(EdgeGraph result) {
147     //Put in cuz of static issues.
148     Mesh m = new Mesh();
149     if (!m.verify(result)) {
150 //      throw new IllegalStateException("refinement failed.");
151       System.out.println("Refinement Failed.");
152       System.exit(-1);
153     }
154     
155     int size = m.getBad(result).size();
156     if (size != 0) {
157       System.out.println("refinement failed\nstill have "+size+" bad triangles left.\n");
158       System.exit(-1);
159     } else {
160       System.out.println("OK");
161       return;
162     }
163   }
164 }