How to use test_job_log method in avocado

Best Python code snippet using avocado_python

relax.py

Source:relax.py Github

copy

Full Screen

1#!/usr/local/bin/python3.82"""relax.py3This test code gets Relax density matrix (1-body) by computing 4expectation values of perturbed Hamiltonians.5This program relies on the Quket log format where6the final converged energy appears as7Final: E[uccsd] = -7.882391426828 ... 8 ~~~~~~~~~~~~~~~9 10It also requires a modified version of PySCF 11(installed only on Ravel) to add perturbation 12to a certain one-body term in the atomic orbital basis. 13HOW TO USE:14 Simply run this code with a Quket input.15 e.g.) python3.8 relax.py uccsd_LiH > rdm &16 Then, this program will create ***_dm.inp file and 17 submit to perform several jobs. Computing NBasis*(NBasis+1)/218 jobs may take some time, but if you already have ***_dm.log 19 finished in your directory, you can bypass the re-calculation20 by setting the option "bypass".21"""22### CHANGE ACCORDINGLY ###23delta = 0.000000124nprocs = 2825nthread = 226QDIR = "/home/tsuchimochi/Quket/master"27bypass = False28### YOU MAY MODIFY BELOW TO GET WHAT YOU WANT ###29import os30import re31import sys32import time33import sh34import glob35import numpy as np36np.set_printoptions(precision=10, suppress=True)37np.set_printoptions(threshold=np.inf)38np.set_printoptions(linewidth=2000)39def Submit(jobname, nprocs=1, nthread=1):40 """Function41 Submit job. Track the submitted job until its finished.42 """43 job = '.job'44 job_script = f"{input_dir}/{jobname+job}"45 ### Make job script46 with open(job_script, "w") as f:47 print("#!/bin/bash", file=f)48 print("#PBS -N ", jobname, file=f)49 print("#PBS -l select=1:ncpus={}:mpiprocs={}".format(nprocs, nprocs), file=f)50 print("", file=f)51 print("cd ${PBS_O_WORKDIR}", file=f)52 print("", file=f)53 print("export OMP_NUM_THREADS={}".format(nthread), file=f)54 print("mpirun -np {} -genv I_MPI_FABRICS tcp /usr/local/bin/python3.8 -m mpi4py {}/main.py {} -nt {}".format(nprocs, QDIR, jobname, nthread), file=f)55 56 57 sh.chmod("755", job_script)58 job_id = sh.qsub(job_script)59 job_id = job_id.strip()60 61 qstat = True62 test = 063 while qstat:64 log = sh.qstat().split()65 if job_id not in log:66 # Job done67 qstat = False68 time.sleep(1)69def ReadMatrix(NDim, Mat_name, filename):70 """Function71 Get Matrix(NDim, NDim) from file.72 """73 Nlines = ((NDim//10) + 1) * (NDim + 2)74 log = str(sh.grep("-A", Nlines, Mat_name, filename)).split("\n")75 Mat = np.zeros((NDim, NDim), dtype=float)76 77 for p in range(NDim):78 for qq in range(NDim//10):79 row = (NDim+2)*qq + 380 log_line = log[p+row].split()81 for q in range(10):82 # print(log_line)83 # print('({}, {}) = {}'.format(p,qq*10+q,log_line[q+1]))84 Mat[p, qq*10 + q] = float(log_line[q+1])85 qq = NDim//10 86 row = (NDim+2)*qq + 387 log_line = log[p+row].split()88 for q in range(NDim%10):89 #print(log_line)90 # print('({}, {}) = {}'.format(p,q+qq*10,log_line[q+1]))91 Mat[p, qq*10 + q] = float(log_line[q+1])92 return Mat 93################################################################94# Setting for input and output #95################################################################96len_argv = len(sys.argv)97if len_argv == 1:98 print("Error! No input loaded.")99 exit()100# First argument = Input file, either "***.inp" or "***" is allowed.101input_file=sys.argv[1]102input_dir, base_name = os.path.split(input_file)103input_dir = "." if input_dir == "" else input_dir104input_name, ext = os.path.splitext(base_name)105if ext == "":106 ext = ".inp"107elif ext not in ".inp":108 input_name += ext109 ext = ".inp"110input_file = f"{input_dir}/{input_name+ext}"111test = "_test"112dm = "_dm"113tmp = "tmpfile"114job = ".job"115log = ".log"116test_job = f"{input_name+test}" 117dm_job = f"{input_name+dm}"118dm_job_path = f"{input_dir}/{input_name+dm+ext}"119dm_job_log = f"{input_dir}/{input_name+dm+log}"120test_job_path = f"{input_dir}/{test_job+ext}"121test_job_log = f"{input_dir}/{test_job+log}"122tmpfile = f"{input_dir}/{tmp}"123try:124 if sys.argv[2] == 'bypass' and os.path.isfile(test_job_log):125 bypass = True126except:127 bypass = False128# Prepare input files and run calculations... 129### Get Input file130num_lines = sum(131 1 for line in open(input_file)132) # number of lines in the input file133f = open(input_file)134lines = f.readlines()135if bypass:136 pass137else:138 print('Running test job to get NBasis')139 with open(test_job_path, "w") as f:140 for i in lines:141 print(i.replace('\n',''), file=f)142 print('MaxIter = 0', file=f)143 print('debug = True', file=f)144 #print('pyscf_guess = minao', file=f)145 print('pyscf_guess = read', file=f)146 print('theta_guess = zero', file=f)147 print('kappa_guess = zero', file=f)148 Submit(test_job, nprocs=nprocs, nthread=nthread)149 print('Test job Done')150### Open test.log and get NBasis151num_lines = sum(152 1 for line in open(test_job_log)153) # number of lines in the input file154f = open(test_job_log)155test_lines = f.readlines()156iline = 0157NBasis = None158nf = None159nc = None160nact = None161while iline < num_lines or NBasis is None or nf is None or nc is None or nact is None:162 line = test_lines[iline].replace("=", " ")163 print(line)164 words = [x.strip() for x in line.split() if not line.strip() == ""]165 print('word ',words)166 len_words = len(words)167 if len_words > 0 and words[0][0] not in {"!", "#"}:168 if words[0].lower() == "nbasis":169 NBasis = int(words[1])170 elif words[0].lower() == "n_frozen_orbitals":171 nf = int(words[1])172 elif words[0].lower() == "n_core_orbitals":173 nc = int(words[1])174 elif words[0].lower() == "n_active_orbitals":175 nact = int(words[1])176 iline += 1177### Get MO_coeff and Overlap matrices178MO_coeff = ReadMatrix(NBasis, "MO coeff", test_job_log)179Overlap = ReadMatrix(NBasis, "Overlap", test_job_log)180print("MO_coeff = \n",MO_coeff)181print("Overlap = \n",Overlap)182MO_coeff_act = MO_coeff[:,(nf+nc):(nf+nc+nact)] 183print("MO_coeff = \n",MO_coeff_act)184#Delete files...185wildcard = "*"186sh.cp(test_job_log, tmpfile) 187sh.rm(sh.glob(f"{test_job+wildcard}"))188sh.cp(tmpfile, test_job_log) 189sh.rm(sh.glob(tmpfile))190print("NBasis = ",NBasis)191print('Creating {}'.format(dm_job_path))192if bypass:193 print(f"Will bypass computing {dm_job}")194else:195 pq = 0196 for p in range(NBasis):197 for q in range(p+1):198 for s in range(2):199 if s == 0:200 sign = '+'201 else:202 sign = '-'203 str_pq = str(pq)204 dm_job = f"{input_name+dm+sign+str_pq}"205 dm_job_path = f"{input_dir}/{input_name+dm+sign+str_pq+ext}"206 with open(dm_job_path, "w") as f:207 for i in lines:208 print(i.replace('\n',''), file=f)209 print('theta_guess = zero', file=f)210 print('kappa_guess = zero', file=f)211 print('IntPQ = 0 0 ', delta, file=f)212 print('debug = False', file=f)213 print('1RDM = False', file=f)214 print('ftol = 1e-14', file=f)215 print('gtol = 1e-6', file=f)216 if sign == '+':217 print('IntPQ = {} {} {} '.format(p, q, delta), file=f)218 elif sign == '-':219 print('IntPQ = {} {} {} '.format(p, q, -delta), file=f)220 221 Submit(dm_job, nprocs=nprocs)222 dm_job_log = f"{input_dir}/{input_name+dm+sign+str_pq+log}"223 sh.cp(dm_job_log, tmpfile) 224 sh.rm(sh.glob(f"{dm_job+wildcard}"))225 sh.cp(tmpfile, dm_job_log) 226 sh.rm(sh.glob(tmpfile))227 pq += 1228 print('end of job: now compute relax density matrix')229DM_AO = np.zeros((NBasis, NBasis), dtype=float)230pq = 0231for p in range(NBasis):232 for q in range(p+1):233 str_pq = str(pq)234 dm_job_log = f"{input_name+dm+'+'+str_pq+log}"235 final = str(sh.grep("Final:", dm_job_log)).split("\n")236 Ep = float(final[0].split()[3])237 dm_job_log = f"{input_name+dm+'-'+str_pq+log}"238 final = str(sh.grep("Final:", dm_job_log)).split("\n")239 Em = float(final[0].split()[3])240 print(f"{p} {q} {Ep} {Em}")241 DM_AO[p,q] = (Ep - Em) / (4*delta)242 DM_AO[q,p] = DM_AO[p,q]243 pq += 1244print("Relaxed DM in AO (alpha+beta) \n",DM_AO)245### Compute DM in MO 246DM_MO_act = MO_coeff_act.T @ Overlap @ DM_AO @ Overlap @ MO_coeff_act247print("Relaxed DM in MO (alpha+beta): active\n",DM_MO_act)248print("Relaxed DM in MO alpha: active\n",DM_MO_act/2)249### Compute DM in MO 250DM_MO = MO_coeff.T @ Overlap @ DM_AO @ Overlap @ MO_coeff251print("Relaxed DM in MO (alpha+beta): whole\n",DM_MO)...

Full Screen

Full Screen

test_scrapydApi.py

Source:test_scrapydApi.py Github

copy

Full Screen

...31 def test_logs(self):32 print(self.api.logs())33 def test_spider_logs(self):34 print(self.api.spider_logs(self.project, 'baidu'))35 def test_job_log(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful