Automate jobs by manipulating files in Python

Until now, when performing computer simulations while changing parameters little by little, I had to manually operate one file each time, but as the number of required data increases (about 200), it becomes difficult. So I wrote a simple script to automate it to some extent.

Overview

I was doing it manually -Create a directory for each parameter value -Create an input file and a job command file in the directory -Edit the parameters of the input file with an editor ・ Job submission I will operate the command with python so that it can be executed.

This time, the first-principles calculation package is used to calculate the interaction energy obtained from the interatomic distance of graphite.

Required library

import os
import re
from time import sleep

```osis used for terminal command operations, reis used for regular expressions, and sleep`` is used for spacing job submissions.

Parameter range setting

Create 200 pieces of data with interatomic distances from 3.01 Å to 5.00 Å.

begin = 3.01
end = 5.00
step = 0.01

Reading input files and job files

Create the input file and the file that is the source of the job file in the data directory in advance, and read those files.

#Assign the path to a variable
input_path = 'data/input_seed'
job_path = 'data/qsub_seed'

#Open the file and read it as text
with open(input_path) as f:
  input_file = f.read()
  
with open(job_path) as f:
  job_file = f.read()

Run

Since the parameter is not an integer and the range function cannot be used, we are defining a new drange function.

#drange function definition
def drange(begin, end, step):
    n = begin
    while n < end:
     yield n
     n += step
     
for r in drange(begin,end,step):
  #Å=>Convert to bohr
  ang = ('{:.8f}'.format(r*2/0.529177))

  #Create directory, specify file path
  os.system('mkdir %s' % r)
  input_name = ('%s/nfinp' % r)
  job_name = ('%s/run.sh' % r)
  
  #Replace distance with regular expression
  distance = str(ang)
  input_mod = re.sub('distance',distance,input_file)
   
  #Create a file in the directory
  with open(input_name, mode='w') as f:
    f.write(input_mod)
  
  with open(job_name, mode='w') as f:
    f.write(job_file)

  #Enter the directory and submit a job
  current_dir = os.getcwd()
  os.chdir(current_dir + ('/%s' % r))
  os.system('qsub run.sh')
  os.chdir(current_dir)
  sleep(1)

Contents of input file

The contents of the original input file look like this. The interatomic distance ( distance) is used as a variable, and ** replaced with a regular expression **. I honestly don't understand the other parts lol

data/input_seed


0 0 0 0 0 0                    : graphite
6.00 20.00 1 4 4               : GMAX, GMAXP, NTYP, NATM, NATM2
1 0                            : num_space_group, type of bravis lattice
4.655149797 4.655149797 distance 90.0 90.0 60.0 : a,b,c,alpha,beta,gamma
24 24 2 1 1 1                  : knx,kny,knz, k-point shift
0 0                            : NCORD, NINV
 0.000000000000  0.000000000000  0.250000000000 1 1 1
 0.333333333333  0.333333333333  0.250000000000 1 1 1
 0.000000000000  0.000000000000 -0.250000000000 1 1 1
-0.333333333333 -0.333333333333 -0.250000000000 1 1 1
6 0.1500 1.00794 3 1 0.d0      : TYPE 1IATOMN,ALFA,AMION,ILOC,IVAN
0 0 0 0 0                      : ICOND 0-MD, 1-CONT.MD, 2-WAVE FN,, 3-WAVE FN CONT., iconstpw
0 1                            : IPRE, IPRI
200 1000 0 57200.00 0         : NMD1, NMD2, iter_last, CPUMAX,ifstop
6 1                            : Simple=1,Broyd2=3,Blugel=6, 1:charge, 2:potential mix.
0 30 0.5                       : starting mixing, kbxmix,alpha
0.60 0.50 0.60 0.70 1.00       : DTIM1, DTIM2, DTIM3, DTIM4, dtim_last
100.00 2 1 0.10D-08 1.d-06     : DTIO ,IMDALG, IEXPL, EDELTA
-0.0010 1.00D+03 0             : WIDTH,FORCCR,ISTRESS
rev-vdw-df2 1                       : XCTYPE, nspin
1.00 3                         : destm, n_stm
101                            : NBZTYP 0-SF, 1-BK, 2-SC, 3-BCC, 4-FCC, 5-DIA, 6-HEX
0 0 0                          : NKX, NKY, NKZ
0 0 0                          : NKX2,NKY2,NKZ2
12                             : NEG
1                              : NEXTST(MB)
0                              : 0; random numbers, 1; matrix diagon
2 0 0 0(MB)                    : imsd, i_2lm, i_sd2another, wksz for phase
0                              : evaluation of eko difference.0 = no ,1 = yes
0                              : npdosao
0 0.0                          : SM_N, dopping

What I want to do next

It would be nice if we could output the necessary part of the output file as a database and even draw a graph.

Recommended Posts

Automate jobs by manipulating files in Python
Run automatic jobs in python
Sort by date in python
Transpose CSV files in Python Part 1
Manipulating EAGLE .brd files with Python
Download Google Drive files in Python
Sort large text files in Python
Read files in parallel with Python
Export and output files in Python
Sorting files by Python naming convention
Extract strings from files in Python
Find files like find on linux in Python
Type annotations for Python2 in stub files!
Referencing INI files in Python or Ruby
Read the file line by line in Python
Read the file line by line in Python
Read and write JSON files in Python
Sample for handling eml files in Python
Common mock by moto in Python Unittest
Make cron-like jobs run regularly in Python
Download files in any format using Python
Alignment algorithm by insertion method in Python
Scene recognition by GIST features in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
[Python] Get the files in a folder with Python
Convert FBX files to ASCII <-> BINARY in Python
Meta-analysis in Python
Unittest in python
Summary of how to import files in Python 3
Resolve Japanese write error UnicodeEncodeError in Python files
Epoch in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Reading and writing CSV and JSON files in Python
N-Gram in Python
Programming in python
Handle zip files with Japanese filenames in Python 3
Plink in Python
Constant in python
Character encoding when dealing with files in Python 3
Split files when writing vim plugin in python
How to get the files in the [Python] folder
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
Get files, functions, line numbers running in python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python