5
$\begingroup$

Let's suppose that I prepare an input file, writing certain parameters defining some molecular structure into to run in Quantum Espresso. For simplicity and convenience I have decided to create an user's variable, for instance, QE_PSEUDO to save a path of a directory with pseudopotentials files. Having passed such a variable into an input file parameter I get an error, see an example of an input file part below.

example.in:

&CONTROL
    ...,
    pseudo_dir='$QE_PSEUDO',
    ...,
/

But if to use a path without variables "as is" it's okay then. Whether no way to make Quantum Espresso understand an user's variable inside an input file?


Addition 1: CRASH file:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     task #         0
     from readpp : error #         1
     file $QE_PSEUDO/Si.bhs not found
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

output file:

     Program PWSCF v.7.2 starts on  2Apr2023 at  0:25:40 

     This program is part of the open-source Quantum ESPRESSO suite
     for quantum simulation of materials; please cite
         "P. Giannozzi et al., J. Phys.:Condens. Matter 21 395502 (2009);
         "P. Giannozzi et al., J. Phys.:Condens. Matter 29 465901 (2017);
         "P. Giannozzi et al., J. Chem. Phys. 152 154105 (2020);
          URL http://www.quantum-espresso.org", 
     in publications or presentations arising from this work. More details at
     http://www.quantum-espresso.org/quote

     Parallel version (MPI), running on     1 processors

     MPI processes distributed on     1 nodes
     7028 MiB available memory on the printing compute node when the environment starts

     Waiting for input...
     Reading input from standard input

     Current dimensions of program PWSCF are:
     Max number of different atomic species (ntypx) = 10
     Max number of k-points (npk) =  40000
     Max angular momentum in pseudopotentials (lmaxx) =  4

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     Error in routine readpp (1):
     file $QE_PSEUDO/Si.bhs not found
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

     stopping ...

Addition 2: CRASH file:

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     task #         0
     from  read_namelists : error #         1
      bad line in namelist &control: "    pseudo_dir=$QE_PSEUDO," (error could be in the previous line)
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

output file:

     Program PWSCF v.7.2 starts on  2Apr2023 at  0:52: 9 

     This program is part of the open-source Quantum ESPRESSO suite
     for quantum simulation of materials; please cite
         "P. Giannozzi et al., J. Phys.:Condens. Matter 21 395502 (2009);
         "P. Giannozzi et al., J. Phys.:Condens. Matter 29 465901 (2017);
         "P. Giannozzi et al., J. Chem. Phys. 152 154105 (2020);
          URL http://www.quantum-espresso.org", 
     in publications or presentations arising from this work. More details at
     http://www.quantum-espresso.org/quote

     Parallel version (MPI), running on     1 processors

     MPI processes distributed on     1 nodes
     6602 MiB available memory on the printing compute node when the environment starts

     Waiting for input...
     Reading input from standard input

 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     Error in routine  read_namelists (1):
      bad line in namelist &control: "    pseudo_dir=$QE_PSEUDO," (error could be in the previous line)
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

     stopping ...
$\endgroup$
10
  • $\begingroup$ What operating system do you use? and what error did you receive? Could you add the output file and/or CRASH file content as a code block in your question? $\endgroup$ Apr 1, 2023 at 15:15
  • $\begingroup$ @AbdulMuhaymin Thanks for the comment! The operating system I use is Linux Mint 21.1. The CRASH and output file I'll add in the Addition 1. $\endgroup$
    – SFriendly
    Apr 1, 2023 at 15:35
  • $\begingroup$ Can you remove the inverted commas around the variable name amd try again? $\endgroup$ Apr 1, 2023 at 15:43
  • $\begingroup$ @HemanthHaridas Thanks for the comment! The same result is. The files contents I'll attach in the Addition 2. $\endgroup$
    – SFriendly
    Apr 1, 2023 at 15:56
  • $\begingroup$ Did you export your environment variable before running QE configuration? Could you confirm if $PSEUDO_DIR is working instead of $QE_PSEUDO? and lastly, how did you define the environment variables? $\endgroup$ Apr 1, 2023 at 15:59

1 Answer 1

8
$\begingroup$

The first thing I should point out is that this particular use of an environment variable is not necessary, because Quantum Espresso already uses an environment variable to determine where it looks for pseudopotentials: PSEUDO_DIR. See the Quantum Espresso user guide at quantum-espresso.org for more details.

Having said that, there are certainly times when it would be useful to use your own environment variables, so let's look at the problem you're having and see how we might solve it.

The reason your current approach doesn't work is because environment variables are set and read by the Linux shell you're using (e.g. bash; there are equivalent ones for Windows etc), but the Quantum Espresso input file is not processed by the shell, it's used by Quantum Espresso.

You can achieve a similar result by writing a shell script (or other program) to create your Quantum Espresso input file. Unlike your Quantum Espresso input files, these shell scripts are interpreted by the shell, so environment variables will be replaced by their values. The simplest script to write just uses "echo" to write a line of text, e.g. create a text file, let's call it make_qe_input.sh, and then put this text in it:

#!/bin/bash
echo \&CONTROL
echo    pseudo_dir=$QE_PSEUDO,
echo /

NB you must put a "\" before any ampersands ("&"), and you must not have quotation marks around the environment variables.

Now we need to make sure Linux knows that this file contains executable commands, so we change its mode using the chmod command, to add execute permission for the user (us); at the command-line, type:

chmod u+x make_qe_input.sh

Now we set the environment variable to what we want, and run our script. For example, at the command-line we might type:

export QE_PSEUDO=/home/me/my_potentials/
./make_qe_input.sh

and you should see the text output:

&CONTROL
pseudo_dir=/home/me/my_potentials/,
/

All we have to do now is to add the other Quantum Espresso input lines to our script, and when we run it we should redirect the output (using ">") to a Quantum Espresso input file instead of the text terminal, e.g.

./make_qe_input.sh > example.in

If you look in the file example.in you will see all the Quantum Espresso input we "echoed" in our shell script, with any environment variables replaced by their appropriate values. If you ever write an environment variable which you haven't actually defined, it won't report an error, it will just write nothing at that point.

This isn't the most elegant solution, but if you haven't used the shell much this is probably the most straightforward. There are many other ways you could achieve this, for example using a text stream editor such as sed, but I recommend starting with my example and getting comfortable with that way first.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .