How to use LAMMPS shell command if I want to pass quoted paths into powershell?

in LAMMPS, I have simulated and generated dump files, whose full path is held inside in string type variable. I want to pass that comment directly to poweshell. Suppose, I have

variable dump_name string "C:/path/to/test_dump_000.cfg"
variable program_name string "D:/way/for/program.exe"

While in powershell, I need to manually enter

& "C:/path/to/test_dump_000.cfg" "D:/way/for/program.exe"

to avoid doing that manually, I written on my lammps input file

variable shellcom string '& "${dump_name}" "${program_name}"'
shell shellcom

The problem is, the command sent to powershell is not

& "C:/path/to/test_dump_000.cfg" "D:/way/for/program.exe"

but rather

& ${dump_name} ${program_name}

LAMMPS is obstinately refusing to evaluate if I encase ${…} with single or double quotes. I have used ‘’ instead of ’ or “” instead of " to have literal " or ’ in my string, but both caused error. that is

variable shellcom string '& ""${dump_name}"" ""${program_name}""'
shell shellcom

shows error , because it feeds into powershell

 & ""C:/path/to/test_dump_000.cfg"" ""D:/way/for/program.exe""

and similarly

variable shellcom string "& ''${dump_name}'' ''${program_name}''"
shell shellcom

shows error , because it feeds into powershell

 & ''C:/path/to/test_dump_000.cfg'' ''D:/way/for/program.exe''

I cannot concatenate & followed by space followed by a single ’ or “” (even if I enter as double ‘’ or “”) before dump_name variable to create any other variable, so, this way is also closed for me.

to worsen the matter, my LAMMPS GUI build (29apr2024 i think!) is not built with python, (I have installed from .exe file, and I cannot build a .exe) so I cannot invoke regex of python as well.

Now, what can I do?

FWIW, these are using unix-style path names. The correct version for Windows would be:

variable dump_name string "C:\\path\\to\\test_dump_000.cfg"
variable program_name string "D:\\way\\for\\program.exe"

You need double up on the backslash ‘\’ to escape the backslash in strings.

This is wrong since there is no expansion and it cannot produce the error you are quoting.

This is also incorrect, since “shell” will not use power shell, but cmd.exe and thus it would have to be:

variable shellcom string "${program_name} ${dump_name}"

And then the shell command would be:

shell ${program_name} ${dump_name}

or

shell ${shellcom}

Just run LAMMPS with echo screen to see how the expansion happens.
Also, please have a look at: 5.2. Parsing rules for input scripts — LAMMPS documentation

If you have path names with blanks you have to escape the blank:

variable dump_name string "C:\\path\\with\ blank\\test_dump_000.cfg"

or use two kinds of quotes:

variable dump_name string "'C:\\path\\with blank\\test_dump_000.cfg'"

or:

variable dump_name string '"C:\\path\\with blank\\test_dump_000.cfg"'
1 Like

Well, now I have tried something like in my windows pc

variable        my_prog string "'E:/my/path with space/prog.exe'"
variable        my_file string "'C:/my/another_path/file.txt'"
variable        my_command string '& ${my_prog} ${my_path}'
print "feeding this COMMAND to run ${powershell_command}"
shell         powershell -NoProfile -ExecutionPolicy Bypass -command ${powershell_command}

While I have text output like

feeding this COMMAND to run & ‘E:/my/path with space/prog.exe’ ‘C:/my/another_path/file.txt’

But I am having error message like

Cannot process the command because of a missing parameter. A command must follow -Command.

followed by a great deal of instruction that are of little use to me
and finally

Shell command powershell -NoProfile -ExecutionPolicy Bypass -command & E:/my/path with space/prog.exe C:/my/another_path/file.txt returned with non-zero status (src/input.cpp:1325)

LAMMPS output doesn’t seem to be wrong but it feeds shell unexpectedly. Now what to do?

Why don’t you just follow my (much simpler) suggestion?
All the errors you see are not from LAMMPS. LAMMPS is working as documented.
So what happens is a case of PEBCAK.

OK.

Why don’t you just follow my (much simpler) suggestion?

Because I need to cut-and-paste many paths directly from file-explorer, so escaping blanks with \ or replacing / with \ might be a bit too cumbersome for me.

I have now tried writing the command to a file and then calling shell to run the command, that is

variable        my_prog string "'E:/my/path with space/prog.exe'"
variable        my_file string "'C:/my/another_path/file.txt'"
variable        script string "run_raw.ps1"
variable        temp_cmd string "storecom.txt"
variable        my_command string '& ${my_prog} ${my_path}'
print "${my_command}" file ${temp_cmd}
shell powershell -NoProfile -ExecutionPolicy Bypass -File ${script} -CmdFilePath ${temp_cmd}
shell del ${temp_cmd}

Here run_raw.ps1 is simply reading the temporary file and executing the command

[string]$CmdFilePath = "storecom.txt"
$rawCommand = Get-Content -Path $CmdFilePath -Raw
Invoke-Expression $rawCommand

Sorry if it’s bothersome to you, but as I equilibriate on one part of code, then call shell , then deform on next part of code, and then again call shell, inside my whole script; while running CLI directly, it is OK, but when I store the cli data into a log file and open it, for example,

lmp -in test.lmp *> lmpout.log; type lmpout.log
the equilibiriation, deofrmation and shell calling texts are all mixed up. Any way to improve this keeping this very order of works? I have no idea about how to increase powershell buffer size. I am using powershell 5.1, Lammps 18 Jun19 (CLI only) and 29Aug24 (CLI and GUI) versions

thanks for being with me!

This makes no sense to me. Cut-n-paste is rarely a useful and reproducible(!) strategy. I cannot recall how many times I selected files that I didn’t intend to select this way. I would rather use some scripting to collect the files to process beforehand. Please note that LAMMPS has file style variables that you can read and iterate with the “next” command.

I fail to understand what you are trying to do here. Also, I have zero knowledge of powershell scripting. If necessary, I rather use tools that are multi-platform, like python.

So perhaps, you should use the python scripting for doing the file selection and iterations and then use the LAMMPS python module to create a LAMMPS instance from python (which is supported by any reasonably recent LAMMPS installer for Windows) and then alternate between Python scripting and feeding commands to LAMMPS (instead of the other way around and issuing command from within LAMMPS).

No idea. Windows is my primary platform. I know how to launch and use Windows programs and I (obviously) know how to build programs on Windows, but my primary platform is Linux and even the Windows packages are built on Linux (not Windows) because it is easier for me to compile software on Linux (just need to use a cross-compiler for Windows executables) and some parts of LAMMPS cannot be compiled on Windows (except using WSL2 which is in essence a Linux container for Windows) due to their build systems assuming a Unix environment.

I would not want to even touch such an old version of LAMMPS, let alone the Windows version of it. Too many known issues and lacking features.

This is my last post on this topic. As far as I can tell, there is still nothing wrong with what LAMMPS is doing, it is more a question of what you are trying to do and doing to LAMMPS. That makes it your problem and not ours.

I would only suggest that you should add whatever directory holds your post-processing executable to your (Windows) PATH, so that you do not need an absolute path to invoke the executable.

After all, if you want people to replicate your work, you need them to be able to run your scripts. And for them to run their scripts they can’t be hand-changing a hundred executable paths. Setting up a PATH folder for calling executables is a standard abstraction across OSes and will make your work that much more easy to distribute.

OK. I would try. But I am also very naive in powershell coding, so I cannot make sure that programs situated in faraway folders would appear in my directory where i am storing my simulations and potentials. And also, I do not have knowledge of python coding (except for twakings of code from chatgpt/gemini) nor I have python installed.(I am effectively, sometimes calling poweshell from MATLAB, on which i have some preliminary knowledge, that is calling powershell, which in in turn calling lammps script. The script is calling powershell again and then powershell is calling my target function!- it’s all too complicated!). I have to call LAMMPS from CLIs, and having two versions of LAMMPS installed means while I can tweak code in GUI and run, I can also parallely run same code on CLI version. (LAMMPS simultaneous paralel script execution is still a long way for me!) . And at this stage, I am far, far away from publishability or replicability of my results (I am not even thinking of paper publication in such an early stage). Once I have enough preparation, i would proceed if possible.

Anyways, thanks for your patience and guidance, and if similar problems appear, I would contact you (the community!) further.