Run FEASST as server with Python client

In this example, run feasst in server mode and use a Python client to send text interface commands.

[1]:
import multiprocessing
import subprocess
import time
import socket

PORT = 54321
BUFFER_SIZE = 1000

def server():
    subprocess.call('echo "Server port '+str(PORT)+' buffer_size '+str(BUFFER_SIZE)+'" | ../../../build/bin/fst', shell=True, executable='/bin/bash')

def client():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(("localhost", PORT))
    for line in ['MonteCarlo',
                 'Configuration cubic_side_length=8 particle_type=fluid:/feasst/particle/lj.txt',
                 'Potential Model=LennardJones',
                 'ThermoParams beta=1 chemical_potential=1',
                 'Metropolis',
                 'TrialTranslate',
                 'TrialAdd particle_type=fluid',
                 'Run until_num_particles=20',
                 'Remove name=TrialAdd',
                 'Log output_file=lj.csv clear_file=true',
                 'Run num_trials=10']:
        sock.send(bytes(line, 'utf-8'))
        message = sock.recv(BUFFER_SIZE)
        #print(str(message))

if __name__ == '__main__':
    proc1 = multiprocessing.Process(target=server, args=())
    proc2 = multiprocessing.Process(target=client, args=())
    proc1.start()
    time.sleep(1)
    proc2.start()
    proc1.join()
    proc2.join()
    import pandas as pd
    df = pd.read_csv('lj.csv')
    assert len(df)==10  # Check if simulation printed output
# Usage: /home/user/feasst/build/bin/fst < file.txt
FEASST version 0.25.13
# initializing server on localhost:54321
# Initializing random number generator with seed: 1749046013

Did this tutorial work as expected? Did you find any inconsistencies or have any comments? Please contact us. Any feedback is appreciated!