/
Local_Simulation.py
71 lines (53 loc) · 2.99 KB
/
Local_Simulation.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import multiprocessing
import logging
from SimulationServer.SimulationServer import SimulationServer
from SimulationClient.SimulationClient import SimulationClient
from SimulationPotential.SimulationPotential import SimulationPotential
def startMdServer(configuration_file, output_filename, logfile, hostname, port, authkey, loglevel):
""" A function to create and start a SimulationServer instance.
"""
md_server = SimulationServer(configuration_file, output_filename, logfile, hostname, port, authkey, loglevel)
md_server.run_simulation()
md_server.save_simulation()
def startMetServer(configuration_file, log_file, log_level, hostname, port, authkey):
""" A function to create and start a SimulationPotential instance.
"""
met_server = SimulationPotential(configuration_file, log_file, log_level, hostname, port, authkey)
met_server.run_potential_server()
def startMdClient(client_id, server_host, server_port, authkey, metric_server_addresses, configuration_file, logfile):
""" A function to create and start a SimulationServer instance.
"""
md_client = SimulationClient(client_id, server_host, server_port, authkey, metric_server_addresses,
configuration_file, logfile)
md_client.start_client()
if __name__ == '__main__':
# Configure distributed system for head machine
NUMBER_OF_METRIC_SERVERS_PER_CLIENT = 2
NUMBER_OF_CLIENTS = 2
IP_ADDRESS = 'localhost'
SERVER_PORT = 5000
BASE_PORT = 5000 # Typically same value as server port
AUTHKEY = 'password'
# Location of the configuration file
CONFIG_FILE = 'Experiment/Example.bkhf'
# Location and name of the output file without a suffix (will be added in runtime)
OUTPUT = 'Experiment/Trajectory'
# Create a local process running a SimulationServer instance
s = multiprocessing.Process(target=startMdServer, args=(CONFIG_FILE, OUTPUT, None, IP_ADDRESS, BASE_PORT,
AUTHKEY, logging.INFO))
s.start()
for i in xrange(NUMBER_OF_CLIENTS):
metric_server_addresses = []
# For the current client create NUMBER_OF_METRIC_SERVERS_PER_CLIENT many SimulationPotential instances
for j in xrange(NUMBER_OF_METRIC_SERVERS_PER_CLIENT):
# Compute a new port number (to avoid local conflicts)
BASE_PORT += 1
metric_server_addresses.append(('localhost', BASE_PORT))
m = multiprocessing.Process(target=startMetServer, args=(CONFIG_FILE, None,
logging.INFO, IP_ADDRESS, BASE_PORT, AUTHKEY, ))
m.start()
# Start a SimulationClient process in a separate thread.
UNIQUE_CLIENT_ID = 'Client_' + str(i)
c = multiprocessing.Process(target=startMdClient, args=(UNIQUE_CLIENT_ID, IP_ADDRESS, SERVER_PORT, AUTHKEY,
metric_server_addresses, CONFIG_FILE, None))
c.start()