diff --git a/run.py b/run.py index 9e1d059..72230d7 100644 --- a/run.py +++ b/run.py @@ -1,5 +1,64 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- +import os, sys +from collections import namedtuple + +ROOT = os.getcwd() +sys.path.append(ROOT) + +import src.utils +import src.salome_utils + +if __name__ == "__main__": + + nprocs = os.cpu_count() + port = 2810 + salomeServers = [] + salomeServer = namedtuple("salomeServer", ["process", "port"]) + + for n in range(nprocs): + while not utils.portIsFree: + port += 1 + + s = salomeServer(salome_utils.startServer(port), port) + salomeServers.append(s) + + var = [] + cmd = "python " + + # TODO: pass it to samples in namedtuples + # get sample.directions and etc .. + structures = [ + "simpleCubic", + "bodyCenteredCubic", + "faceCenteredCubic" + ] + directions = [ + [1, 0, 0], + [0, 0, 1], + [1, 1, 1] + ] + + cmd += "-m " + var.append((salomeServer[n].port, cmd)) + + utils.parallel(nprocs, var, salome_utils.execute) + + + + + + + + + + + + + + + + + + if __name__ == "__main__": ### diff --git a/src/samples.py b/samples.py similarity index 84% rename from src/samples.py rename to samples.py index 01f9cdb..d6e0a71 100644 --- a/src/samples.py +++ b/samples.py @@ -1,13 +1,28 @@ -from . import geometry_utils +from .src import geometry_utils import GEOM -from . import mesh_utils +from .src import mesh_utils import SMESH -from . import anisotropeCubic +from .src import anisotropeCubic import salome import math +def genMesh(ctype, theta, flowdirection): + _G = globals() + + for g in _G: + func = g.get(ctype) + + if func: + salome.salome_init() + func(theta, flowdirection) + salome.salome_close() + + else: + raise Exception("Unknown type of the sample function") + + def simpleCubic(theta, flowdirection): radius = 1 stackAngle = [0.5 * math.pi, 0.5 * math.pi, 0.5 * math.pi] @@ -62,8 +77,3 @@ def faceCenteredCubic(theta, flowdirection): mesh_utils.meshCompute(mesh) -def genMesh(ctype, theta, flowdirection): - salome.salome_init() - - - salome.salome_close() diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/salome_utils.py b/src/salome_utils.py index 7afd2e1..a9a9d20 100644 --- a/src/salome_utils.py +++ b/src/salome_utils.py @@ -1,6 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - import salome import subprocess import logging @@ -8,45 +5,37 @@ import logging def hasDesktop() -> bool: return salome.sg.hasDesktop() -def startServer(port, logPath): +def startServer(port): - log = open("{}/salome.log".format(logPath), "a") logging.info("Starting SALOME on port {} ...".format(port)) p = subprocess.Popen(["salome", "start", "--port", str(port), "-t"], - shell = True, - stdout = log, - stderr = log) - - log.close() + shell = False, + stdout = subprocess.PIPE, + stderr = subprocess.PIPE) return p -def killServer(port, logPath): +def killServer(port): - log = open("{}/salome.log".format(logPath), "a") logging.info("Terminating SALOME on port {} ...".format(port)) p = subprocess.Popen(["salome", "kill", str(port)], shell = True, - stdout = log, - stderr = log) - - log.close() + stdout = subprocess.PIPE, + stderr = subprocess.PIPE) return p -def execute(port, cmd, logPath): +def execute(port, cmd): - log = open("{}/salome.log".format(logPath), "a") logging.info("Executing command in the SALOME on port {} ...".format(port)) - - p = subprocess.Popen(["salome", "connect", "-p", str(port), str(cmd)], + + # cmd = "python -m"; = "python -c" + p = subprocess.Popen(["salome", "remote", "-p", str(port), "--", str(cmd)], shell = True, - stdout = log, - stderr = log) - - log.close() + stdout = subprocess.PIPE, + stderr = subprocess.PIPE) return p diff --git a/src/utils.py b/src/utils.py index 60e52fb..76e6d37 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,4 +1,5 @@ from multiprocessing import Queue, Process, cpu_count +import socket def queue(cmd, qin, qout, *args): @@ -64,4 +65,8 @@ def parallel(np, var, cmd): return results +def portIsFree(address, port): + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + return s.connect_ex((address, port)) == 0