LoginSignup
0
1

More than 5 years have passed since last update.

ADDA > tool > loop_beta_gamma_171216.py > v0.1-v0.3 > run sequentially ADDA for different values of [beta] and [gamma] for orientations by Euler angles ([alpha] is fixed as 0.0)

Last updated at Posted at 2017-12-16

Environment:

GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 16.04 LTS desktop amd64
TensorFlow v1.2.1
cuDNN v5.1 for Linux
CUDA v8.0
Python 3.5.2
IPython 6.0.0 -- An enhanced Interactive Python.
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
scipy v0.19.1
geopandas v0.3.0
MATLAB R2017b (Home Edition)
ADDA v.1.3b6

This article is related to ADDA (light scattering simulator based on the discrete dipole approximation).

About

A Python code named 'loop_beta_gamma_171216.py' is used to run ADDA for different values of [beta] and [gamma] for orientations by the Euler angle.
The [alpha] is fixed as 0.0 in this version (v0.1).

Link

The code will

  • delete unrelated result directories at first
    • e.g. runXXX_YYY_ZZZ_...
    • with confirmation to continue before deleting
  • read [beta_gamma.tbl] to obtain list of [beta] and [gamma]
  • call ./adda with the runtime argument '-orient '

Beta and gamma table: beta_gamma.tbl

Here is the exmaple of the [beta_gamma.tbl].

beta_gamma.tbl
#beta(deg),gamma(deg)
30.0,10.0
40.0,10.0
50.0,90.0
20.0,60.0
50.0,30.0

The 1st line is the comment, which is not used in the batch run.
The lines from the 2nd are used for the batch run.

The beta and gamma should be separated using a comma (',').

code v0.1

loop_beta_gamma_171216.py
import numpy as np
import subprocess as sb
import sys
import os
import glob
import shutil

'''
v0.1 Dec. 16, 2017
  - add delete_directories()
  - add confirm_delete()
  - add loop_beta_gamma()
  - read runtime arguement
'''

# coding rule: PEP8
# tested on: Python v3.5.2

# be executed at "adda/src/seq"

kAlpha_deg = 0.0  # <alpha> for -orient


def delete_directories():
    res = glob.glob("run*")
    for elem in res:
        shutil.rmtree(elem)
        print("Deleted:", elem)


def confirm_delete():
    print("All the result directories are deleted before the batch run.")
    res = input("Continue: [y/N]?")
    if len(res) > 0 and res in "Yy":
        return True
    return False


def loop_beta_gamma(infile):
    if not os.path.exists(infile):
        msg = "ERROR: file [{0}] not found".format(infile)
        print(msg)
        return

    btgm = np.genfromtxt(infile, delimiter=',')
    for beta, gamma in btgm:
        fmt = "./adda -orient {0} {1} {2}"
        cmd = fmt.format(kAlpha_deg, beta, gamma)
        print(cmd)
        sb.Popen(cmd.split(), stdout=sb.DEVNULL)


if __name__ == '__main__':
    argvs = sys.argv
    argc = len(argvs)

    if argc < 2:
        print("ERROR: [beta_gamma.tbl] is not specified")
        print("    [cmd] [beta_gamma.tbl]")
        sys.exit()

    if confirm_delete():
        delete_directories()
        loop_beta_gamma(argvs[1])

Run example

The code should be executed at the directory, where the code './adda' is found (e.g. 'adda/src/seq/').

Example1:

run
$ python3 CODE_171216_Euler/loop_beta_gamma_171216.py 
ERROR: [beta_gamma.tbl] is not specified
    [cmd] [beta_gamma.tbl]

Example2:

run
$ python3 CODE_171216_Euler/loop_beta_gamma_171216.py CODE_171216_Euler/beta_gamma.tbl 
All the result directories are deleted before the batch run.
Continue: [y/N]?y
Deleted: run6141_sphere_g16_m1.5
Deleted: run6143_sphere_g16_m1.5
Deleted: run6140_sphere_g16_m1.5
Deleted: run6142_sphere_g16_m1.5
Deleted: run6139_sphere_g16_m1.5
./adda -orient 0.0 30.0 10.0
./adda -orient 0.0 40.0 10.0
./adda -orient 0.0 50.0 90.0
./adda -orient 0.0 20.0 60.0
./adda -orient 0.0 50.0 30.0
run
$ grep orient run*/log
run6144_sphere_g16_m1.5/log:command: './adda -orient 0.0 30.0 10.0 '
run6144_sphere_g16_m1.5/log:Particle orientation (deg): alpha=0, beta=30, gamma=10
run6145_sphere_g16_m1.5/log:command: './adda -orient 0.0 40.0 10.0 '
run6145_sphere_g16_m1.5/log:Particle orientation (deg): alpha=0, beta=40, gamma=10
run6146_sphere_g16_m1.5/log:command: './adda -orient 0.0 50.0 90.0 '
run6146_sphere_g16_m1.5/log:Particle orientation (deg): alpha=0, beta=50, gamma=90
run6147_sphere_g16_m1.5/log:command: './adda -orient 0.0 20.0 60.0 '
run6147_sphere_g16_m1.5/log:Particle orientation (deg): alpha=0, beta=20, gamma=60
run6148_sphere_g16_m1.5/log:command: './adda -orient 0.0 50.0 30.0 '
run6148_sphere_g16_m1.5/log:Particle orientation (deg): alpha=0, beta=50, gamma=30

code v0.2 > run with parameters of ADDA

In this version, the code is modified to take parameters of the ADDA (e.g. -shape chebyshev and all the other parameters).

code

loop_beta_gamma_171216.py
import numpy as np
import subprocess as sb
import sys
import os
import glob
import shutil

'''
v0.2 Dec. 16, 2017
  - can take parameters of ADDA
v0.1 Dec. 16, 2017
  - add delete_directories()
  - add confirm_delete()
  - add loop_beta_gamma()
  - read runtime arguement
'''

# coding rule: PEP8
# tested on: Python v3.5.2

# be executed at "adda/src/seq"

kAlpha_deg = 0.0  # <alpha> for -orient


def delete_directories():
    res = glob.glob("run*")
    for elem in res:
        shutil.rmtree(elem)
        print("Deleted:", elem)


def confirm_delete():
    print("All the result directories are deleted before the batch run.")
    res = input("Continue: [y/N]?")
    if len(res) > 0 and res in "Yy":
        return True
    return False


def loop_beta_gamma(infile, params):
    if not os.path.exists(infile):
        msg = "ERROR: file [{0}] not found".format(infile)
        print(msg)
        return

    btgm = np.genfromtxt(infile, delimiter=',')
    for beta, gamma in btgm:
        fmt = "./adda -orient {0} {1} {2} {3}"
        cmd = fmt.format(kAlpha_deg, beta, gamma, params)
        print(cmd)
        sb.Popen(cmd.split(), stdout=sb.DEVNULL)


if __name__ == '__main__':
    argvs = sys.argv
    argc = len(argvs)

    if argc < 2:
        print("ERROR: [beta_gamma.tbl] is not specified")
        print("    [cmd] [beta_gamma.tbl] (ADDA parameters)")
        print("    where () is optional (e.g. -shape chebyshev 0.7 12")
        sys.exit()

    params = ' '.join(argvs[2:])

    if confirm_delete():
        delete_directories()
        loop_beta_gamma(argvs[1], params)

Run example

Without additional parameters:

$ python3 loop_beta_gamma_171216.py beta_gamma.tbl
All the result directories are deleted before the batch run.
Continue: [y/N]?y
Deleted: run6162_chebyshev_g16_m1.5
Deleted: run6163_chebyshev_g16_m1.5
Deleted: run6165_chebyshev_g16_m1.5
Deleted: run6161_chebyshev_g16_m1.5
Deleted: run6164_chebyshev_g16_m1.5
./adda -orient 0.0 30.0 10.0 
./adda -orient 0.0 40.0 10.0 
./adda -orient 0.0 50.0 90.0 
./adda -orient 0.0 20.0 60.0 
./adda -orient 0.0 50.0 30.0 

With additional parameters (e.g. -shape chebyshev 0.7 12)

$ python3 loop_beta_gamma_171216.py beta_gamma.tbl -shape chebyshev 0.7 12
All the result directories are deleted before the batch run.
Continue: [y/N]?y
Deleted: run6167_sphere_g16_m1.5
Deleted: run6170_sphere_g16_m1.5
Deleted: run6166_sphere_g16_m1.5
Deleted: run6168_sphere_g16_m1.5
Deleted: run6169_sphere_g16_m1.5
./adda -orient 0.0 30.0 10.0 -shape chebyshev 0.7 12
./adda -orient 0.0 40.0 10.0 -shape chebyshev 0.7 12
./adda -orient 0.0 50.0 90.0 -shape chebyshev 0.7 12
./adda -orient 0.0 20.0 60.0 -shape chebyshev 0.7 12
./adda -orient 0.0 50.0 30.0 -shape chebyshev 0.7 12

code v0.3 > fix bug for calling ./adda

The sb.Popen() was used to call ./adda without 'shell=True`, resulting the simultaneous execution of ./adda for different parameters.

The sb.popen() was called without parameters of the ADDA.

Following is the fixed code for the sb.Popen().

code

loop_beta_gamma_171216.py
import numpy as np
import subprocess as sb
import sys
import os
import glob
import shutil

'''
v0.3 Dec. 17, 2017
  - fix bug: ./adda was executed without the parameters
  - fix bug: ./adda was called simulaneously not sequentially
    + use subprocess.call() instead of Popen()
v0.2 Dec. 16, 2017
  - can take parameters of ADDA
v0.1 Dec. 16, 2017
  - add delete_directories()
  - add confirm_delete()
  - add loop_beta_gamma()
  - read runtime arguement
'''

# coding rule: PEP8
# tested on: Python v3.5.2

# be executed at "adda/src/seq"

kAlpha_deg = 0.0  # <alpha> for -orient


def delete_directories():
    res = glob.glob("run*")
    for elem in res:
        shutil.rmtree(elem)
        print("Deleted:", elem)


def confirm_delete():
    print("All the result directories are deleted before the batch run.")
    res = input("Continue: [y/N]?")
    if len(res) > 0 and res in "Yy":
        return True
    return False


def loop_beta_gamma(infile, params):
    if not os.path.exists(infile):
        msg = "ERROR: file [{0}] not found".format(infile)
        print(msg)
        return

    btgm = np.genfromtxt(infile, delimiter=',')
    for beta, gamma in btgm:
        fmt = "./adda -orient {0} {1} {2} {3}"
        cmd = fmt.format(kAlpha_deg, beta, gamma, params)
        print(cmd)
        # sb.Popen(cmd, stdout=sb.DEVNULL, shell=True)
        sb.call(cmd, stdout=sb.DEVNULL, shell=True)


if __name__ == '__main__':
    argvs = sys.argv
    argc = len(argvs)

    if argc < 2:
        print("ERROR: [beta_gamma.tbl] is not specified")
        print("    [cmd] [beta_gamma.tbl] (ADDA parameters)")
        print("    where () is optional (e.g. -shape chebyshev 0.7 12")
        sys.exit()

    params = ' '.join(argvs[2:])

    if confirm_delete():
        delete_directories()
        loop_beta_gamma(argvs[1], params)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1