Call Python Script with command line arguments within .dat file

Hi,

Is there anyway for us to call a python script within a dat file that includes a command line argument?

I have a python script that requires a command line argument and I want to iteratively call this script so I used the program call command but it does not recognize command line arguments so is there any alternative solution to that?

Thank you for your time.

Hi,
I’m not sure to understand what you need to do exactly. Call a python script from a classical data file ? Can you please clarify ?
Thanks,

Rima

Hello,
It is possible to call a python file using the program call command (ex : program call “py_script.py”) but you need to establish the connection between python and fish arguments. To do so you might use :
python_argument = itasca.fish.get(“fish_argument”).
Hope it helps you.

Hi @yopiprabowo !

I agree with @Rima and @Zakaria: further explications would be great.
An exemple file would be greater, even a very simplified one :wink: .

Regards.

Théophile

Dear @Rima , @Zakaria , and @Theophile ,

Thank you for your responses. I figured out a way to do this in python without having to resort to FISH variables.

Let’s say i have a main function in a script (solve.py) where model solve command is called:

def main():
    #Apply gravity load and solve to equilibrium
    num_inc = 10 #set total number of increments
    inc_size = 0.1 # 0.1 x 10 = 1.0 (the full application of loads)
    p_1 = 0.0
    p_2 = 0.0
    for count in range(0,num_inc,1):
        mult = inc_size
        p1,p2 = main_loop_v2.main(mult,count) #Call the function that needs two arguments, where loads were incrementally applied
        p_1 += p1
        p_2 += p2
        print("solving for p1 : {} N/m2 and p2 : {} N/m2".format(p_1,p_2))
        dmax,vmax = getmaxdisp()
        it.fish.set("maxdisp",dmax)
        it.command("model solve mech ratio-local 1e-4")
        it.command("model save")

Notice that I called main_loop_v2.py where the main function requires two arguments from
solve.py.

Thank you all for the help.

Regards,

YP

Hi @yopiprabowo !

First of all : sorry for the late response, I was away for a while.

So, I would indeed recommand driving directly from Python since you can easily run Fish commands from there and get access to 3DEC internal variables.

In my opinion, your script is a good solution.

By the way, the range(n) natively iterates from 0 to n with a step of 1, you do not need to provide range(0,n,1) :wink: .

>>> num_inc = 5
>>> for i in range(num_inc): print(i)
0
1
2
3
4
>>> for i in range(0, num_inc, 1): print(i)
0
1
2
3
4

Regards!

Théophile

Dear Théophile,

Thank you for your reply and your suggestion! Indeed typing range(n) instead of range(0,n,1) is a good practice! :smile:

Would you kindly explain to me on how does the "Program Call’ function works? I tried to solve the same data file provided in the FLAC 3D example problems but it shows error in the same line where “Program call” is provided. I am not able to see the link of the program call anywhere. Please see the attached photo.

Hi!

What was the error message shown in the Console pane again?

If the message were something along the lines of “File not found”, I would suggest checking the location where the project file for this example is saved. Please see the snapshot below.

image

Because I’m using 3DEC, the directory on my computer is:
C:\Program Files\Itasca\3DEC700\DataFiles3D\FLAC\ExampleApplications\EmbankmentLoad

Yours would probably be:
C:\Program Files\Itasca\FLAC3D700\DataFiles3D\FLAC\ExampleApplications\EmbankmentLoad

Thanks. But my error is like this. what could be the reason for this? I am running the same data file of the built in example but yet it is not running.

Whenever I apply ‘program call’ for other task as well, error would still be the same. (i.e. error opening file).

Hi. Would you mind taking a screenshot of what the directory looks like in
C:\Users\samee\OneDrive\Documents\Itasca\flac3d900\My Projects\

Thanks

I solved the issue. Thank you very much.