Live interaction with running python script

Post Reply
StavrosP
Posts: 14
Joined: Sat Oct 22, 2022 2:48 pm

Live interaction with running python script

Post by StavrosP » Fri Jun 23, 2023 1:53 pm

Hello,

I am wondering if there is a way via STKO's UI to stop the execution of a Python script. This is needed if the script includes iterations for example (eg updating models sequentially) but it's already obvious that there is something wrong while running. What is the way to do this? If not possible, what is a suggested workaround?

Thanks in advance,
Stavros

StavrosP
Posts: 14
Joined: Sat Oct 22, 2022 2:48 pm

Re: Live interaction with running python script

Post by StavrosP » Sat Jun 24, 2023 8:33 am

A workaround that I found meanwhile is to modify/break the script in real-time (while running) and then "confirm" it's reloading via the STKO UI.

But curious to read if you have other suggestions... :)

Stavros

STKO Team
Posts: 1979
Joined: Tue Oct 29, 2019 8:45 am

Re: Live interaction with running python script

Post by STKO Team » Mon Jun 26, 2023 9:08 am

It's not so easy, you need to use working threads in order to stop the execution, and you need to check for the termination flag in your own code.

Based on the files shared with the PythonAPI webinar,
I updated this file in .\shared\utils:
ThreadUtils.zip
(1.56 KiB) Downloaded 77 times
to add a "Cancel" button


and this is an example of how to use it in a simple for-loop:

StavrosP
Posts: 14
Joined: Sat Oct 22, 2022 2:48 pm

Re: Live interaction with running python script

Post by StavrosP » Thu Jul 13, 2023 9:55 am

Hello,

I am replying to this sequence of posts for a related matter to this automation process that I am after.
I am using the minimize algorithm of Scipy to optimize a boundary value problem that I am after.
So I am changing some geometrical properties of the system, deleting the entire model, re-generating it, calculating (modal analysis), and repeating the same until convergence is reached... Note: I don't save the models (cae scd file), but only write the tcl files and results (with mpco) each time.

If the problem is not solved within 131 iterations (so 131 geometry generations and calculations), consistently, STKO crashes and terminates immediately (without any error/warning being displayed or anything like that). It halts for 2-3 seconds and then terminates... This is not related to the number of iterations as at heavier models it happens for a lesser number.

How can I debug and investigate further what is going on? Could it be related to any automated command (e.g. autosave) that tries to get executed but it cannot?

Stavros

STKO Team
Posts: 1979
Joined: Tue Oct 29, 2019 8:45 am

Re: Live interaction with running python script

Post by STKO Team » Mon Jul 17, 2023 10:18 am

It's hard to tell. There can be many issues. One is probably not cleaning properly memory of deleted geometries (STKO keeps them in memory for the undo-redo operations).

Can you try to monitor your system resources (in terms of RAM) and see if that is the case?

StavrosP
Posts: 14
Joined: Sat Oct 22, 2022 2:48 pm

Re: Live interaction with running python script

Post by StavrosP » Wed Jul 19, 2023 8:20 am

Indeed this could be a reason, the memory usage gradually increases with every new model that I generate, reaching from 450MB to ~800MB before crashing (however this is not critical for the system, I have 128GB available).
What is the best way to do a "wipe out" of all elements in memory, after I delete them from the preprocess document? If not a single command exists for this, could start a new project each time do the trick (App.runCommand('NewPreDocument'))? If yes, could you maybe let me know how I can "Save as" a project so that I don't have to interact with the popup window?

Btw, this is how I delete the geometry each time.

Code: Select all

def clear_model():
    """
    Define geometry deletion.
    """
    doc_pre.scene.unselectAll()
    # existing_beam_geom = doc_pre.getGeometry(doc_pre.geometries.getlastkey(0))
    existing_beam_geom = doc_pre.geometries.getlastkey(0)
    existing_physical_props = doc_pre.physicalProperties.getlastkey(0)
    existing_element_props = doc_pre.elementProperties.getlastkey(0)
    existing_conditions = doc_pre.conditions.getlastkey(0)
    existing_analyses_steps = doc_pre.analysisSteps.getlastkey(0)
    if existing_beam_geom is not None:
        for existing_beam_geom_id in range(existing_beam_geom):
            existing_beam_geom_selected = doc_pre.getGeometry(doc_pre.geometries.getlastkey(0))
            shape = existing_beam_geom_selected.shape
            for edge_id in range(shape.getNumberOfSubshapes(MpcSubshapeType.Edge)):
                doc_pre.scene.select(existing_beam_geom_selected, edge_id, MpcSubshapeType.Edge)
            App.runCommand("DeleteGeometry")
    if existing_physical_props is not None:
        for physical_prop_id in range(existing_physical_props):
            physical_prop = str(doc_pre.physicalProperties.getlastkey(0))
            App.runCommand("DeletePhysicalProperty", physical_prop)
    if existing_element_props is not None:
        for element_prop_id in range(existing_element_props):
            element_prop = str(doc_pre.elementProperties.getlastkey(0))
            App.runCommand("DeleteElementProperty", element_prop)
    if existing_conditions is not None:
        for condition_id in range(existing_conditions):
            condition = str(doc_pre.conditions.getlastkey(0))
            App.runCommand("DeleteCondition", condition)
    if existing_analyses_steps is not None:
        for analyses_step_id in range(existing_analyses_steps):
            analysis_step = str(doc_pre.analysisSteps.getlastkey(0))
            App.runCommand("DeleteAnalysisStep", analysis_step)
    doc_pre.commitChanges()
    doc_pre.dirty = True
    App.runCommand("Regenerate")
    App.processEvents()

Also, this is the Error details that I get from the Windows Event Viewer:

Faulting application name: STKO.exe, version: 0.0.0.0, time stamp: 0x641976ba
Faulting module name: Qt5Core.dll, version: 5.13.2.0, time stamp: 0x5db2b3bb
Exception code: 0xc0000409
Fault offset: 0x0000000000022d28
Faulting process id: 0x2398
Faulting application start time: 0x01d9ba13ca64b80e
Faulting application path: C:\Program Files\STKO\STKO.exe
Faulting module path: C:\Program Files\STKO\Qt5Core.dll
Report Id: 360123b1-3b29-4741-90fe-c3bf43804e92
Faulting package full name:
Faulting package-relative application ID:

- <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
- <System>
<Provider Name="Application Error" />
<EventID Qualifiers="0">1000</EventID>
<Version>0</Version>
<Level>2</Level>
<Task>100</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2023-07-19T08:32:00.8029283Z" />
<EventRecordID>38537</EventRecordID>
<Correlation />
<Execution ProcessID="0" ThreadID="0" />
<Channel>Application</Channel>
<Computer>TUD1005139.tudelft.net</Computer>
<Security />
</System>
- <EventData>
<Data>STKO.exe</Data>
<Data>0.0.0.0</Data>
<Data>641976ba</Data>
<Data>Qt5Core.dll</Data>
<Data>5.13.2.0</Data>
<Data>5db2b3bb</Data>
<Data>c0000409</Data>
<Data>0000000000022d28</Data>
<Data>2398</Data>
<Data>01d9ba13ca64b80e</Data>
<Data>C:\Program Files\STKO\STKO.exe</Data>
<Data>C:\Program Files\STKO\Qt5Core.dll</Data>
<Data>360123b1-3b29-4741-90fe-c3bf43804e92</Data>
<Data />
<Data />
</EventData>
</Event>

STKO Team
Posts: 1979
Joined: Tue Oct 29, 2019 8:45 am

Re: Live interaction with running python script

Post by STKO Team » Thu Jul 20, 2023 8:40 am

Regarding the crash issue. It seems like it's happening in the QtCore library, which is a library we use for GUI.
Unfortunately, I need your exact file and full script to replicate this error under a debugger to see where the problem is.
What is the best way to do a "wipe out" of all elements in memory, after I delete them from the preprocess document? If not a single command exists for this, could start a new project each time do the trick (App.runCommand('NewPreDocument'))? If yes, could you maybe let me know how I can "Save as" a project so that I don't have to interact with the popup window?
It would be to close the document and open a new one. However, now, there is no way to close a document from the App.runCommand without showing the window that asks for user permission.
We are going to implement this function in the next version.

Now you can simply proceed by clearing the model as you are doing. However, I've seen a conceptual mistake:
existing_beam_geom = doc_pre.geometries.getlastkey(0)
for existing_beam_geom_id in range(existing_beam_geom):
...
This assumes that all your geometries have a continuous ID from 0 to lastkey-1

You can just do something like this:

Code: Select all

all_ids = list(doc.geometries.keys())
for id in all_ids:
	doc.scene.select(doc.getGeometry(id))
App.runCommand('DeleteGeometry')

StavrosP
Posts: 14
Joined: Sat Oct 22, 2022 2:48 pm

Re: Live interaction with running python script

Post by StavrosP » Thu Aug 03, 2023 9:45 am

Hello,

thanks for the help so far! The issue still persists and if I don't overcome this I cannot finalize the exercise that I am after using STKO. :(

Unfortunately, I cannot share the entire code publically due to IP restrictions. Can I share it somehow privately with the STKO Team only?

Best,
Stavros

STKO Team
Posts: 1979
Joined: Tue Oct 29, 2019 8:45 am

Re: Live interaction with running python script

Post by STKO Team » Mon Aug 07, 2023 8:02 am

You can share it privately at this e-mail: info@asdeasoft.net

Post Reply