From f8c35fa36917d4e90c4a45ca6612979273e333e3 Mon Sep 17 00:00:00 2001 From: L-Nafaryus Date: Mon, 22 Mar 2021 15:44:22 +0500 Subject: [PATCH] Samples --- src/foam.py => run.py | 106 +++++++++++++++++++++++------------------- src/foam_utils.py | 53 +++++++++++++++++++++ src/salome_utils.py | 2 + src/samples.py | 18 +++---- src/utils.py | 2 +- 5 files changed, 123 insertions(+), 58 deletions(-) rename src/foam.py => run.py (66%) create mode 100644 src/foam_utils.py diff --git a/src/foam.py b/run.py similarity index 66% rename from src/foam.py rename to run.py index e6d5f71..9e1d059 100644 --- a/src/foam.py +++ b/run.py @@ -1,56 +1,64 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import os, shutil -import subprocess -import logging -import time -from datetime import timedelta - -def application(name, case, log=False, args=[], parallel=False): - logging.info("Running '{}'.".format(name)) - - if log: - logfile = open("{}/{}.log".format(case, name), "a") - - mpirun = [] - if parallel: - mpirun = ["mpirun", "-np", "4", "--oversubscribe"] - - subprocess.run(mpirun + [name, "-case", case] + args, - stdout=logfile if log else subprocess.STDOUT, - stderr=logfile if log else subprocess.STDOUT) - - if log: - logfile.close() - -def ideasUnvToFoam(case, mesh): - application("ideasUnvToFoam", case, True, [mesh]) - -def createPatch(case): - application("createPatch", case, True, ["-overwrite"]) - -def transformPoints(case, vector): - application("transformPoints", case, True, ["-scale", vector]) - -def checkMesh(case): - application("checkMesh", case, True, ["-allGeometry", "-allTopology"]) - -def foamDictionaryGet(case, foamFile, entry): - application("foamDictionary", case, True, [foamFile, "-entry", entry]) - -def foamDictionarySet(case, foamFile, entry, value): - application("foamDictionary", case, True, [foamFile, "-entry", entry, "-set", value]) - -def decomposePar(case): - application("decomposePar", case, True) - -def potentialFoam(case): - application("potentialFoam", case, True, ["-parallel"], True) - -def simpleFoam(case): - application("simpleFoam", case, True, ["-parallel"], True) if __name__ == "__main__": + ### + # SALOME + # + # Get main paths + project = os.getcwd() + src = os.path.join(project, "src") + build = os.path.join(project, "build") + + if not os.path.exists(build): + os.makedirs(build) + + # Logger + logging.basicConfig( + level=logging.INFO, + format="%(levelname)s: %(message)s", + handlers = [ + logging.StreamHandler(), + logging.FileHandler("{}/genmesh.log".format(build)) + ]) + start_time = time.monotonic() + + # Start in parallel + processes = [] + structures = ["simpleCubic", "faceCenteredCubic", "bodyCenteredCubic"] + directions = ["001"] #, "100", "111"] + coefficients = [0.1] #[ alpha * 0.01 for alpha in range(1, 13 + 1) ] + port = 2810 + + for structure in structures: + for direction in directions: + for coefficient in coefficients: + src_path = os.path.join(src, "{}.py".format(structure)) + build_path = os.path.join(build, + structure, + "direction-{}".format(direction), + "alpha-{}".format(coefficient)) + + if not os.path.exists(build_path): + os.makedirs(build_path) + + p = multiprocessing.Process(target = salome, + args = (port, src_path, build_path, coefficient, direction)) + processes.append(p) + p.start() + logging.info("{} on port {}.".format(p, port)) + port += 1 + + for process in processes: + process.join() + + end_time = time.monotonic() + logging.info("Elapsed time: {}".format(timedelta(seconds=end_time - start_time))) + + + ### + # FOAM + # # Get main paths project = os.getcwd() src = os.path.join(project, "src") diff --git a/src/foam_utils.py b/src/foam_utils.py new file mode 100644 index 0000000..c111e0e --- /dev/null +++ b/src/foam_utils.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os, shutil +import subprocess +import logging +import time +from datetime import timedelta + +def application(name, case, log=False, args=[], parallel=False): + logging.info("Running '{}'.".format(name)) + + if log: + logfile = open("{}/{}.log".format(case, name), "a") + + mpirun = [] + if parallel: + mpirun = ["mpirun", "-np", "4", "--oversubscribe"] + + subprocess.run(mpirun + [name, "-case", case] + args, + stdout=logfile if log else subprocess.STDOUT, + stderr=logfile if log else subprocess.STDOUT) + + if log: + logfile.close() + +def ideasUnvToFoam(case, mesh): + application("ideasUnvToFoam", case, True, [mesh]) + +def createPatch(case): + application("createPatch", case, True, ["-overwrite"]) + +def transformPoints(case, vector): + application("transformPoints", case, True, ["-scale", vector]) + +def checkMesh(case): + application("checkMesh", case, True, ["-allGeometry", "-allTopology"]) + +def foamDictionaryGet(case, foamFile, entry): + application("foamDictionary", case, True, [foamFile, "-entry", entry]) + +def foamDictionarySet(case, foamFile, entry, value): + application("foamDictionary", case, True, [foamFile, "-entry", entry, "-set", value]) + +def decomposePar(case): + application("decomposePar", case, True) + +def potentialFoam(case): + application("potentialFoam", case, True, ["-parallel"], True) + +def simpleFoam(case): + application("simpleFoam", case, True, ["-parallel"], True) + + diff --git a/src/salome_utils.py b/src/salome_utils.py index f2f1fa8..7afd2e1 100644 --- a/src/salome_utils.py +++ b/src/salome_utils.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- import salome +import subprocess +import logging def hasDesktop() -> bool: return salome.sg.hasDesktop() diff --git a/src/samples.py b/src/samples.py index eddbb90..01f9cdb 100644 --- a/src/samples.py +++ b/src/samples.py @@ -1,6 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - from . import geometry_utils import GEOM @@ -8,6 +5,8 @@ from . import mesh_utils import SMESH from . import anisotropeCubic +import salome +import math def simpleCubic(theta, flowdirection): radius = 1 @@ -16,7 +15,7 @@ def simpleCubic(theta, flowdirection): layers = [3, 3, 3] grains = anisotropeCubic.StructuredGrains(radius, stackAngle, theta, layers) - scale = [1, 1, 1] + scale = [2 * math.sqrt(2), 2 * math.sqrt(2), 2] flowdirection = flowdirection if flowdirection else [1, 0, 0] style = 0 cubic = anisotropeCubic.AnisotropeCubic(scale, grains, style) @@ -34,7 +33,7 @@ def bodyCenteredCubic(theta, flowdirection): layers = [3, 3, 3] grains = anisotropeCubic.StructuredGrains(radius, stackAngle, theta, layers) - scale = [1, 1, 1] + scale = [2 / math.sqrt(2), 2 / math.sqrt(2), 1] flowdirection = flowdirection if flowdirection else [1, 0, 0] style = 0 cubic = anisotropeCubic.AnisotropeCubic(scale, grains, style) @@ -52,7 +51,7 @@ def faceCenteredCubic(theta, flowdirection): layers = [3, 3, 3] grains = anisotropeCubic.StructuredGrains(radius, stackAngle, theta, layers) - scale = [1, 1, 1] + scale = [1 / math.sqrt(2), 1 / math.sqrt(2), 1] flowdirection = flowdirection if flowdirection else [1, 0, 0] style = 0 cubic = anisotropeCubic.AnisotropeCubic(scale, grains, style) @@ -63,5 +62,8 @@ def faceCenteredCubic(theta, flowdirection): mesh_utils.meshCompute(mesh) -def genMesh(): - pass +def genMesh(ctype, theta, flowdirection): + salome.salome_init() + + + salome.salome_close() diff --git a/src/utils.py b/src/utils.py index fa9d0a4..60e52fb 100644 --- a/src/utils.py +++ b/src/utils.py @@ -6,7 +6,7 @@ def queue(cmd, qin, qout, *args): # Get item from the queue pos, var = qin.get() - # Exit if last item is None + # Exit point if pos is None: break