(Quasi) Minimal script to create sweeping inputs
The script below creates several directories called ngridk_xxx-rgkmax_yyy and copies in them a given input.xml file with different values for the input variables ngridk (variable ngk in the script) and rgkmax (variable rgkm in the script). So that it works, you need to save it to a file (e.g. create_inputs_sweepying.py) and then to run it from terminal with
$ python create_inputs_sweepying.py
In the same directory where you run this, it is necessary that a file called input.xml (like this one) exists.
Remember that in Python there are no braces {}, and the blocks (like a loop) are determined by the indentation.
from lxml import etree as ET import os # This instruccion gives access to functions of the operating system fileobj=open("input.xml","r") # Definition of the object fileobj as the file input.xml doc = ET.parse(fileobj) # Creates representation of the contents (necessary for xml files) # This loads the entire XML document # (input.xml) into an ElementTree instance #Get instance of root element: root = doc.getroot() # This is essential: it stores the reference to the root file you work # with the doc. prefix is a reference to the variable doc defined above # The "root" of a .xml file is a unique identificator for such .xml file atoms = doc.xpath('//atom') # Get list of elements named "atom" #Loop over all "atom" elements and set a new "coord" attribute: groundstate = doc.xpath('//groundstate') # xpath selects concrete fields, like elements or # attributes, in a .xml file. Actually xpath is a # rather complex programming language. The # double bar means it refers to all the nodes # of the tree that are below groundstate in the # tree that is the .xml file for ngk in range(3, 21): # sweeping in variables ngk and rgkm for rgkm in [6.00, 6.25, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00, 8.25, 8.50, 8.75, 9.00]: if not os.path.exists("ngridk_"+str(ngk)+"-rgkmax_"+str(rgkm)): # If the directories do not exist, os.makedirs("ngridk_"+str(ngk)+"-rgkmax_"+str(rgkm)) #creates them groundstate[0].set('ngridk',str(ngk)+" "+str(ngk)+" "+str(ngk)) # Give different values to the variable ngridk, which belongs to the groundstate field. # The symbol + means concatenation of strings groundstate[0].set('rgkmax',str(rgkm)) fileobj2=open("ngridk_"+str(ngk)+"-rgkmax_"+str(rgkm)+"/input.xml","w") #Write changes to input.xml fileobj2.write(ET.tostring(root, pretty_print=True, xml_declaration=True, encoding='UTF-8')) # The line above assigns the modified variable (groundstate) to the field groundstate # of the input.xml file. ET.tostring means element tree to string in xml fileobj2.close()