I would like to run MSC NASTRAN using python. I have seen a similiar function in MATLAB using
system('nastran.exe file_name.bdf') #where file_name.bdf is the input file to Run using nastran.
Hence i tried below using python code, but it did not work,
import os
os.system('nastran.exe file_name.bdf')
Could you tell me where i going wrong?
Also, how to give the command line in NASTRAN thru python? Like for example memory allocation for the run, number of cores need to be used for run etc.
some NASTRAN command lines include,
1. scr=yes delete=f04,log,xdb pause=yes
2. mem=10gb bpool=3gb memorymaximum=14gb sscr=500gb sdball=500gb mode=i8
...etc.
I can't speak directly for MSC Nastran, its been a while since I've used it. But most modern FEA programs have an API (application program interface) to allow you to call commands from a external program like python or matlab.
Without an API, you may be limited to using python to start the program from the command line, which is what your code is trying to do. As for how to launch a program from within python, check out this question/answer:
How to run application with parameters in Python?
Easy way to run a MSC NASTRAN file is to create a .bat file and run it from python.
The format for .bat file is:
<nastran.exe location> <Python script file location> <Nastran command line>
An example can be :
C:\MSC.Software\MSC_Nastran\20141\bin\nastran.exe C:\py_nastran_run\example.bdf scr=yes old=no delete=f04,log,xdb
Then include the below line in python script,
status=subprocess.call("runBatch.bat")
Related
i have matlab script which using few matlab libraries like Map ToolKit. I can run this script on MATLAB without any error. But i need to run this with QGIS and get output files.
Simply, script asks few question about Map Tools and get like .shp files and give output folder.
So to get these output files, i tried 3 ways:
I tried to run this code on Python Console. For that i installed matlab.engine and called script just like in documentation. But these matlab.engine doesn't let python to use matlab's own libraries so error on using shapeinfo function. It just let python to use few variables i think. So after failing on this i tried second way:
I created executable matlab file to run .exe file on python console. In here, there is a two way to get this exe file. First one is let you smaller file and doesn't include matlab runtime so you need to add matlab runtime on your path and i did it. Actually Matlab does that as default and i checked that it was okey. Executable file runs perfectly after clicking or on normal python scripts which is running on through CMD. But if i call Python Script in QGIS, it trying to open but returns Matlab runtime dll missing error. After i tried to compile with matlab runtime selection and results were same.
Lastly, i tried to run matlab.exe -r "try, run SCRPIT_PATH\script.m; end; quit on CMD and it works fine but if i call this on QGIS Python Console with os.system or subprocess.call functions, cmd appearing and closing immediately.
I guess QGIS doesn't let you to open some other applications somehow and i need to let it run. Or i just need to find import matlab libraries to python for work perfectly. I would be pleasure if you give any suggestion. Any solution for these ways or a different way would be great. Thanks.
I'm trying to automate my workflow. So I'm generating a test list for my system in MATLAB. In the same script, I'm then waiting for a new log file which only gets generated when I run a python script in a certain directory through windows powershell.
That python script would take the test list generated and produce a new log file. My code looks for the date of modification of that file, if it has changed, it means a new log file has been produced. The code then extracts certain strings of characters and then plot the result.
My question is how can I invoke powershell from MATLAB to invoke that python script execution command in a specific directory?
Regards
If you really want to commit to this Powershell-in-the-middle approach, you can call it with system
system( 'powershell -command "Some Powershell command"' );
You can collect outputs from this if there are any.
Seems like it would be easier to call the Python script from Matlab though and cut out the call to Powershell, this could also be done with the system command (i.e. without also calling Powershell in between). Or you could invoke Python directly.
Error with Subprocess addition
This is the error I get regarding the abaqus module
I'm quite new to python and so please don't mind if this question might seem silly.
So I have python file that does the function of opening an abaqus viewer and I have another python file that describes the functions I want to do in the abaqus viewer.
I need a piece of code that can automate the second script without me having to manually go into file>run script.
Script to Open Abaqus:
import os
import subprocess
os.startfile('Q:/win_apps/scripts/simulia/Abaqus/6.14-3/Use_these_if_not_working/abq6143_viewer.bat')
And then I have a python script that has the code regarding my output requests from abaqus viewer.
What line can I add to the above file to automatically take the second python script and run it?
When running Abaqus with the typical start up scripts you can pass Abaqus/Viewer a script to run from the command line:
abq6143 viewer noGUI=script.py
where you replace script.py with the name of your Python script. This will start up Abaqus/Viewer with no user interface, run the script, and then quit.
If you want the user interface to come up and automatically run your script you can use the script= command instead of noGUI:
abq6143 viewer script=script.py
I see that you're using a custom batch file to start up Abaqus/Viewer. Without seeing those contents I couldn't say exactly how you would integrate the above, but you will probably need to adjust the relevant line in the batch file with the noGUI or script command.
I have a Python 2.7 script that among others contains the following piece of code:
import spss
columns = []
spss.StartDataStep()
dataset = spss.Dataset()
for column in dataset.varlist:
columns.append(column.name)
spss.EndDataStep()
print columns
When running this code inside a SPSS syntax (so between BEGIN PROGRAM. and END PROGRAM), it runs as expected and I end up with the variables in the active dataset.
However, when running the same code as part of a script (so from Utilities > Run script...) will return me no results.
It looks as if the SPSS session context is not taken into consideration when running a script.
Is there a way around this problem, or am I doing something wrong?
I don't want to run my code as part of Syntax file, I just want to use vanilla Python scripts.
This is, unfortunately, a complicated issue. I don't think Statistics is working as documented. I will take this up with Development.
It appears that in V24, when you run a Python script via Utilities > Run Script (which is the same as issuing the SCRIPT command), your script is connected to the Statistics Viewer process but not to the Statistics backend (the spssengine process), which is where the data live. There are typically three processes running - the stats.exe process, the spssengine process, and, for Python code, the startx process. Your script can issue commands via the spss.Submit api and can use other spss apis, but they go against a new copy of the backend, so the expected backend context is not present.
To get around this, you can run a trivial program like
begin program.
import ascript
end program.
where ascript.py is a Python module on the Python search path. (You could put these lines in an sps file and use INSERT to execute it, too.)
Another way to approach this would be to run Statistics in external mode. In that mode, you run a Python program that uses SPSS apis but the Python program is on top, and no Statistics user interface appears. You can read about this in the Python scripting help.
An advantage of external mode is that you can use your favorite Python IDE to build and debug your code. That's a big advantage if you are basically a Python person. I use Wing IDE, but any Python IDE should work. You can also set up an alternative IDE as your default by editing the clientscriptingcfg.ini file in the Statistics installation directory. See the scripting help for details. With a tool like Wing, this lets you debug your scripts or other Python code even if run within Statistics.
I am trying to get a home made Fiji script to sun inside Python by calling Fiji, but there's little documentation on how to do it.
What I need is something like this:
def myfijiscript:
[CODE]
and then in Python:
fiji(myfijiscript)
is there a way to do this?
Python (or, to be precise, Jython) scripts within Fiji are executed using the org.python.util.PythonInterpreter class (see source code).
It doesn't make much sense to run a Jython script within a Java instance that is started from with Python, but have a look at those two questions concerning how to run external commands in python. You can save your script in a file myscript.py and then do:
call(["./ImageJ-linux64", "myscript.py"])
using the ImageJ launcher from the command line.
The other way is to use ImageJ as a library and just import the classes you need for your script, as others have suggested:
from ij import IJ