1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.ui;
39
40 import ffx.potential.bonded.Atom;
41 import ffx.ui.commands.FFXClient;
42 import ffx.ui.commands.SimulationFilter;
43 import ffx.ui.commands.SimulationUpdate;
44 import ffx.utilities.Keyword;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.io.File;
48 import java.net.InetSocketAddress;
49 import java.util.List;
50 import javax.swing.Timer;
51
52
53
54
55
56
57
58 public class SimulationLoader implements ActionListener {
59
60
61
62 private FFXClient client;
63 private SimulationFilter simulationFilter;
64 private InetSocketAddress address;
65
66
67 private Thread job = null;
68
69 private boolean finished = false;
70 private MainPanel mainPanel;
71 private FFXSystem system;
72 private SimulationUpdate ffxUpdate = null;
73 private boolean firstUpdate = true;
74 private Timer timer;
75 private double time = 0.0;
76 private int step = 0;
77
78
79
80
81
82
83
84
85
86
87
88 public SimulationLoader(FFXSystem s, Thread j, MainPanel f, InetSocketAddress a) {
89 system = s;
90 job = j;
91 mainPanel = f;
92 address = a;
93 if (address == null) {
94 finished = true;
95 }
96 }
97
98
99 @Override
100 public void actionPerformed(ActionEvent e) {
101
102 if (!connect()) {
103 return;
104 }
105
106 if (system == null) {
107 ffx.ui.commands.SimulationDefinition sys = client.getSystem();
108 if (sys != null) {
109 if (simulationFilter == null) {
110 if (system == null) {
111 system =
112 new FFXSystem(new File("Simulation"), "Simulation", Keyword.loadProperties(null));
113 }
114 simulationFilter = new SimulationFilter(sys, system);
115 UIFileOpener openFile = new UIFileOpener(simulationFilter, mainPanel);
116
117
118 Thread reader = new Thread(openFile);
119 reader.start();
120 } else if (simulationFilter.fileRead()) {
121 system = (FFXSystem) simulationFilter.getActiveMolecularSystem();
122 simulationFilter = null;
123 }
124 }
125 }
126 else {
127 if (ffxUpdate == null || ffxUpdate.read) {
128 ffxUpdate = client.getUpdate();
129 }
130 if (ffxUpdate != null
131 && !ffxUpdate.read
132 && !mainPanel.getGraphics3D().isSceneRendering()) {
133 update();
134 }
135 }
136 }
137
138
139
140
141
142
143 public boolean isConnected() {
144 return client != null && client.isConnected();
145 }
146
147
148 public void release() {
149 finished = true;
150 if (timer != null) {
151 timer.stop();
152 }
153 if (client != null) {
154 client.release();
155 }
156 mainPanel.getMainMenu().setConnect(true);
157 }
158
159
160
161
162
163
164 boolean connect() {
165 if (isFinished()) {
166 return false;
167 }
168 if (isConnected()) {
169 return true;
170 }
171
172 int delay = 10;
173 if (timer == null) {
174 timer = new Timer(delay, this);
175 timer.setCoalesce(true);
176 timer.setDelay(10);
177 timer.start();
178 }
179
180 if (client == null) {
181 client = new FFXClient(address);
182 }
183
184 client.connect();
185
186 if (client.isConnected()) {
187 timer.setDelay(delay);
188 return true;
189 }
190
191
192
193
194 return false;
195 }
196
197
198
199
200
201
202 FFXSystem getFSystem() {
203 return system;
204 }
205
206
207
208
209
210
211
212
213 boolean isFinished() {
214 if (client != null && client.isClosed()) {
215 finished = true;
216 }
217 if (job != null && !job.isAlive()) {
218 finished = true;
219 }
220 if (finished) {
221 if (timer != null) {
222 timer.stop();
223 }
224 update();
225 release();
226 }
227 return finished;
228 }
229
230 private void update() {
231 if (system.isStale()) {
232 return;
233 }
234 if (ffxUpdate == null || ffxUpdate.read) {
235 return;
236 }
237
238 List<Atom> atoms = system.getAtomList();
239 int n = atoms.size();
240 if (ffxUpdate.numatoms != n) {
241 finished = true;
242 return;
243 }
244
245 if (ffxUpdate.type == ffx.ui.commands.SimulationUpdate.SIMULATION) {
246 if (ffxUpdate.time == time) {
247 ffxUpdate.read = true;
248 return;
249 }
250 time = ffxUpdate.time;
251 } else if (ffxUpdate.type == ffx.ui.commands.SimulationUpdate.OPTIMIZATION) {
252 if (ffxUpdate.step == step) {
253 ffxUpdate.read = true;
254 return;
255 }
256 step = ffxUpdate.step;
257 }
258
259
260 double[] d = new double[3];
261 for (Atom a : atoms) {
262 int index = a.getIndex() - 1;
263 d[0] = ffxUpdate.coordinates[0][index];
264 d[1] = ffxUpdate.coordinates[1][index];
265 d[2] = ffxUpdate.coordinates[2][index];
266 a.moveTo(d);
267 }
268 if (firstUpdate) {
269 system.center();
270 firstUpdate = false;
271 }
272 mainPanel.getGraphics3D().updateScene(system, true, false, null, false, null);
273 mainPanel.getHierarchy().updateStatus();
274 mainPanel.getHierarchy().repaint();
275 ffxUpdate.read = true;
276 ffxUpdate = client.getUpdate();
277 }
278 }