UnityMol  0.9.6-875
UnityMol viewer / In developement
ArtemisManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 
4 using Molecule.Model;
5 
6 using ArtemisClientPointer = System.IntPtr;
7 using System.Collections.Generic;
8 using UI;
9 using System.Threading;
10 
11 public class ArtemisThreadedClient{
12  GameObject artemisGo;
13 
14  private float[] pos2;
15 
16  private bool initialized = false;
17  private bool run = false;
18  private bool kill_simulation = false;
19 
21  {
22  kill_simulation = true;
23  }
24 
25  public void disconnect(ArtemisClientPointer client)
26  {
27  if (!initialized)
28  return;
29 
31 
32  initialized = false;
33  }
34 
35  public void mainProc(ArtemisClientPointer client, int capacity)
36  {
39 
40  kill_simulation = false;
41 
42  pos2 = new float[capacity * 3];
43  run = true;
44  while(run)
45  {
46  int ready = ArtemisWrapper.artemis_client_receive(client);
47  if (ready != 0)
48  {
49  // Get the IMD header
50  ArtemisWrapper.artemis_client_read_header(client, ref header);
51 
52  switch(header.type)
53  {
54  case ArtemisWrapper.artemis_imd_type_e.IMD_ENERGIES:
56  PlotManager.Instance.PlotAdd("Total Energy", e.Etot);
57  PlotManager.Instance.PlotAdd("Hydrogen Bonds Energy", e.Evdw);
58  PlotManager.Instance.PlotAdd("Stacking Energy", e.Eelec);
59 
60  ArtemisManager.Etot = e.Etot;
61  ArtemisManager.Evdw = e.Evdw;
62  ArtemisManager.Eelec = e.Eelec;
63  break;
64  case ArtemisWrapper.artemis_imd_type_e.IMD_FCOORDS:
65 
66  if (ArtemisWrapper.artemis_client_read_coords(client, pos2, capacity) == 1)
67  {
68  List<Vector3> lst = new List<Vector3>();
69  for (int i = 0; i < capacity; i++)
70  {
71  lst.Add(new Vector3(pos2[i*3],pos2[i*3+1],pos2[i*3+2]));
72  }
73 
75  }
76 
77  break;
78  default:
79  break;
80  }
81  }
82  }
83 
84  disconnect (client);
85 
86  if (kill_simulation) {
88  }
89  }
90 
91  public void send_forces(ArtemisClientPointer client, int nb_forces, int[] indices, float[] coordinates)
92  {
93  ArtemisWrapper.artemis_client_send_forces(client, nb_forces, indices, coordinates);
94  }
95 
96  public void stop()
97  {
98  run = false;
99  }
100 }
101 
102 public class ArtemisManager{
104 
106 
107  Thread oThread;
108  GameObject artemisGo;
109 
110  bool initialized = false;
111  private bool connected = false;
112 
113  public static float Etot = 0.0f;
114  public static float Evdw = 0.0f;
115  public static float Eelec = 0.0f;
116 
117  public bool isConnected()
118  {
119  return connected;
120  }
121 
122  public ArtemisManager()
123  {
124  threadedClient = new ArtemisThreadedClient();
125  }
126 
127  public void connect()
128  {
129  Debug.Log("ArtemisManager::Connect");
130 
131  ArtemisOldGUI.artemisNotificationMessage = "Connecting...";
132 
133  int capacity = (int) MoleculeModel.atomsnumber;
134  MoleculeModel.atomsIMDSimulationLocationlist = new List<Vector3>();
135  for (int i = 0; i < capacity; i++)
136  {
137  MoleculeModel.atomsIMDSimulationLocationlist.Add(new Vector3(0.0f, 0.0f, 0.0f));
138  }
139 
140  if(!initialized)
141  {
142  client = ArtemisWrapper.artemis_client_create(capacity);
143  initialized = true;
144  }
145 
146  int portAsInt;
147 
148  if(!int.TryParse(ArtemisOldGUI.portString, out portAsInt))
149  {
151  return;
152  }
153 
154  Debug.Log ("Artemis Threaded Client connect");
155  int res = ArtemisWrapper.artemis_client_connect(client, ArtemisOldGUI.host, portAsInt);
156  Debug.Log(res);
157  if(res == 0)
158  {
160 
161  artemisGo = new GameObject("Artemis");
162  ArtemisClientLoop s = artemisGo.AddComponent<ArtemisClientLoop>();
163 
165  atomManager.CreateMouseOversIMDSimulation();
167 
168  oThread = new Thread(() => threadedClient.mainProc(client, capacity));
169  oThread.Start();
170  while (!oThread.IsAlive);
171 
172  connected = true;
173  }
174  else
175  {
177  }
178  }
179 
180  public void stop_simulation()
181  {
182  if (connected)
183  {
184  threadedClient.stop_simulation(client);
185  disconnect();
186  }
187  }
188 
189  public void disconnect()
190  {
191  if (connected)
192  {
193  connected = false;
194 
196  atomManager.DestroyMouseOversIMDSimulation();
198 
199  Debug.Log("Destroying GO");
200  GameObject.DestroyImmediate(artemisGo);
201  Debug.Log("Destroyed");
202 
203  threadedClient.stop();
204 
205  oThread.Join();
206 
208 
209  initialized = false;
210 
211  Debug.Log ("Thread joined");
212  }
213  }
214 
215  public void send_forces(int nb_forces, int[] indices, float[] coordinates)
216  {
217  threadedClient.send_forces(client, nb_forces, indices, coordinates);
218  }
219 
220 }
void stop_simulation(ArtemisClientPointer client)
static float Evdw
static int artemis_client_destroy(ArtemisClientPointer client)
static string artemisNotificationMessage
ArtemisThreadedClient threadedClient
void PlotAdd(String plotName, float value)
Add a value to plot graph
Definition: PlotManager.cs:53
static ArtemisClientPointer artemis_client_create(int nb_atoms)
static int artemis_client_connect(ArtemisClientPointer client, [In] string hostname, [In] int port)
static float Eelec
GameObject artemisGo
static float Etot
void send_forces(ArtemisClientPointer client, int nb_forces, int[] indices, float[] coordinates)
static PlotManager Instance
Instance of object
Definition: PlotManager.cs:18
static List< Vector3 > atomsIMDSimulationLocationlist
The coordinates of each atom, simulated through an IMD simulation.
void disconnect(ArtemisClientPointer client)
static int artemis_client_send_forces(ArtemisClientPointer client, int nb_forces, [In] int[] indexes, [In] float[] forces)
static string host
Definition: ArtemisOldGUI.cs:7
static int artemis_client_disconnect(ArtemisClientPointer client)
void mainProc(ArtemisClientPointer client, int capacity)
static int artemis_client_read_energies(ArtemisClientPointer client, ref ArtemisImdEnergies energies)
static int artemis_client_read_header(ArtemisClientPointer client, ref ArtemisHeader header)
ArtemisClientPointer client
void stop_simulation()
static int artemis_client_read_coords(ArtemisClientPointer client, [In, Out] float[] coords, int nb_atoms)
static int artemis_client_receive(ArtemisClientPointer client)
static int artemis_client_send_kill(ArtemisClientPointer client)
void DestroyMouseOversIMDSimulation()
void send_forces(int nb_forces, int[] indices, float[] coordinates)
static string portString
Definition: ArtemisOldGUI.cs:8
Definition: GUIDisplay.cs:66
static GenericManager getCurrentAtomManager()
Definition: UnityMolMain.cs:50