# Error in restoring the model inside the loop

**URL:** <https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077>\
**Category:** PFC\
**Tags:** fish\
**Created:** [May 12, 2025, 9:42am UTC](https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077 "2025-05-12T09:42:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SahilW](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@SahilW](https://forum.itascainternational.com/u/SahilW)\
**Post date:** [May 12, 2025, 9:42am UTC](https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077/1 "2025-05-12T09:42:41Z")

</div>

Dear all,  
I want to restore the model inside the loop so I can automate the extraction of the results. But when I try the following code, it gives me the error :"  
fish define dataextract  
loop local i(0,15)  
local e0 = i \* 0.01  
local suf = string.build(“%0.2f”, e0)  
command  
model restore(“HostSoilShearingModel/Shearing\_e” + suf + “.p3sav”);  
@output\_balldata(“HostSoilShearingData2/balldata\_Shearing\_e” + suf)  
@output\_contactdata(“HostSoilShearingData2/contactdata\_Shearing\_e” + suf)  
endcommand  
endloop  
end

; execute batch  
@dataextract

Error:  
\*\*\* You cannot RESTORE a model from this input source (Probably a FISH command/end\_command).  
While processing line 137 of source GUI Console.  
While executing line 136 columns 9 to 16 of source GUI Console.  
While executing line 144 columns 2 to 13 of source GUI Console.  
While processing line 144 of source GUI Console.

---

<div class="post-metadata">

**Author:** ![dblanksma](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@dblanksma](https://forum.itascainternational.com/u/dblanksma)\
**Post date:** [May 12, 2025, 8:24pm UTC](https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077/2 "2025-05-12T20:24:49Z")

</div>

Hello @SahilW,  
You cannot restore a save file in a FISH function. If you want to do batch modeling I suggest using Python like in this [parameter studies](https://docs.itascacg.com/itasca930/common/docproject/source/manual/scripting/python/doc/python_3dec.py.html?node8266#running-parameter-studies) example. The example is for 3DEC, but the same logic will work for PFC.

---

<div class="post-metadata">

**Author:** ![SahilW](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@SahilW](https://forum.itascainternational.com/u/SahilW)\
**Post date:** [May 16, 2025, 12:35pm UTC](https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077/3 "2025-05-16T12:35:01Z")

</div>

I’m facing an issue using IPython. I wrote the following lines, but an error appears. Can you help me to fix it?  
import itasca as it

for i in [0.01, 0.02, 0.03, 0.04]:  
filename = f"Sh25/Shearing\_e{str(i)}.p3sav"  
it.command(“”"  
model restore model restore ‘{filename}’)

---

<div class="post-metadata">

**Author:** ![dblanksma](https://avatars.discourse-cdn.com/v4/letter/d/858c86/32.png) [@dblanksma](https://forum.itascainternational.com/u/dblanksma)\
**Post date:** [May 19, 2025, 3:43pm UTC](https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077/4 "2025-05-19T15:43:40Z")

</div>

Hello @SahilW,  
Can you post the python file you wrote? Remember python syntax is space sensitive when writing for loops. Just a guess but it could be something entirely different.

---

<div class="post-metadata">

**Author:** ![jmswdmn](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@jmswdmn](https://forum.itascainternational.com/u/jmswdmn)\
**Post date:** [July 31, 2026, 11:01am UTC](https://forum.itascainternational.com/t/error-in-restoring-the-model-inside-the-loop/2077/5 "2026-07-31T11:01:20Z")

</div>

similar problem solution for UDEC rather than PFC without using python, responding here in case the logic is useful for others…

i have previously used python to do batch exports of data and/or plots from FLAC3D/3DEC as suggested above for PFC, but just hit this problem of trying to use model restore within a FISH **command** / **endcommand** block whilst wanting to do some batch exports from UDEC where python isnt available. I combatted it by using FISH to generate a .dat file of my required **model restore** and **plot export** commands (two different lists of plots first created for different models), and then can run that .dat outside of the FISH function. See below:

```auto
; config
local output_root = 'output'
local size_x = 1920
local size_y = 1080

; scan the working folder for files
local lines = system.directory.list('.', false, false)

; build up the generated command-file contents as a list of lines
local out_lines = list

loop foreach local fname lines

    local flen = string.len(fname)

    ; only process entries that end in ".sav"
    if flen > 4 then
        if string.sub(fname, flen - 3, 4) = '.sav' then

            local basename = string.sub(fname, 1, flen - 4)
            local outdir = output_root + '/' + basename

            ; create the output folder now
            local dir_ok = system.directory.create(outdir)

            out_lines = list.append(out_lines, "model restore '" + fname + "'")

            if string.find(fname, '_initial') # 0 then

                ; initial state - use the initial plot set
                loop foreach local pname initial_plots
                    local pline = "plot '" + pname + "' export bitmap filename '" + outdir + "/" + pname + ".png' size " + string(size_x) + " " + string(size_y)
                    out_lines = list.append(out_lines, pline)
                endloop

            else

                ; all other states - use the other plot set
                loop foreach pname other_plots
                    local pline2 = "plot '" + pname + "' export bitmap filename '" + outdir + "/" + pname + ".png' size " + string(size_x) + " " + string(size_y)
                    out_lines = list.append(out_lines, pline2)
                endloop

            endif

        endif
    endif

endloop

; write the generated command file
local out = file.open('batch_generated.dat', 'write', 'text')
file.write(out, out_lines)
file.close(out)

```
