Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

ADDA > tool > average_Qxxx_171216.py > v0.1,v0.2 > average for Qext and Qabs for various values of the ADDA parameters

More than 3 years have passed since last update.
動作環境
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 "average_Qxxx_171216.py" is used to obtain the average values of the ADDA runs, in terms of the [Qext] and [Qabs].

The code is used, for example, to obtain random orientation averaging of Qext and Qabs.
(e.g. ADDA > tool > loop_beta_gamma_171216.py > v0.1 > run sequentially ADDA for different values of [beta] and [gamma] for orientations by Euler angles ([alpha] is fixed as 0.0)

code v0.1

average_Qxxx_171216.py
import numpy as np
import subprocess as sb
import sys
import glob

'''
v0.1 Dec. 16, 2017
  - add get_Qxxx()
  - add average_Qxxx()
'''

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

# be executed at the directory where the ADDA result direcroties
#   are found (e.g. "run6144_sphere_g16_m1.5")


kTargetKeys = 'Qext', 'Qabs'  # 'Qsca' is obtained by 'Qext - Qabs'


def get_Qxxx(infile, atargetKey):
    with open(infile, 'rt') as fp:
        lines = fp.readlines()
        for aline in lines:
            if atargetKey in aline:  # e.g. Qext    = 3.788800597
                # print(aline)
                res = aline.split(" ")
                return np.array(res[1]).astype(float)


def average_Qxxx():
    res = glob.glob("run*/CrossSec-*")
    sum, cnt = 0.0, 0
    for atargetKey in kTargetKeys:
        for elem in res:
            qext = get_Qxxx(elem, atargetKey)
            sum += qext
            cnt += 1
        msg = '{1:.7f}  # {0}(avg)'.format(atargetKey, sum/cnt)
        print(msg)


if __name__ == '__main__':
    average_Qxxx()

code v0.2 > fix bug (incorrect initialization)

(Updated Dec. 24, 2017)

average_Qxxx_171216.py
import numpy as np
import subprocess as sb
import sys
import glob

'''
v0.2 Dec. 24, 2017
  - fix bug: [sum] and [cnt] was initialized incorrectly
v0.1 Dec. 16, 2017
  - add get_Qxxx()
  - add average_Qxxx()
'''

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

# be executed at the directory where the ADDA result direcroties
#   are found (e.g. "run6144_sphere_g16_m1.5")


kTargetKeys = 'Qext', 'Qabs'  # 'Qsca' is obtained by 'Qext - Qabs'


def get_Qxxx(infile, atargetKey):
    with open(infile, 'rt') as fp:
        lines = fp.readlines()
        for aline in lines:
            if atargetKey in aline:  # e.g. Qext    = 3.788800597
                # print(aline)
                res = aline.split(" ")
                return np.array(res[1]).astype(float)


def average_Qxxx():
    res = glob.glob("run*/CrossSec-*")
    for atargetKey in kTargetKeys:
        sum, cnt = 0.0, 0
        for elem in res:
            qext = get_Qxxx(elem, atargetKey)
            sum += qext
            cnt += 1
        msg = '{1:.7f}  # {0}(avg)'.format(atargetKey, sum/cnt)
        print(msg)


if __name__ == '__main__':
    average_Qxxx()

Run example

The code is needed to be executed at the directory, where the ADDA result directories are found as shown below.
(e.g. run6144_sphere_g16_m1)

$ ls -F | grep "/"
...
run6144_sphere_g16_m1.5/
run6145_sphere_g16_m1.5/
run6146_sphere_g16_m1.5/
run6147_sphere_g16_m1.5/
run6148_sphere_g16_m1.5/

Then, execute the code.

$ python3 CODE_171216_Euler/average_Qxxx_171216.py 
3.7929064  # Qext(avg)
0.0000000  # Qabs(avg)
7of9
セブンオブナインです。Unimatrix 01の第三付属物 9の7という識別番号です。Star trek Voyagerの好きなキャラクターです。まとめ記事は後日タイトルから内容がわからなくなるため、title検索で見つかるよう個々の記事にしてます。いわゆるBorg集合体の有名なセリフから「お前たち(の知識)を吸収する。抵抗は無意味だ」。Thanks in advance.
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away