Topic: Plot the solution results during analysis

Hi

I want to plot the solution results while the analysis is being carried out. I wrote the attached python code. Unfortunately, the FEM results of this code are not identical to obtained results of the example.

Post's attachments

TwoStepCasting.py 1.37 kb, 8 downloads since 2023-06-02 

You don't have the permssions to download the attachments of this post.

2

Re: Plot the solution results during analysis

Hi,
The python code looks good (although I did not tested it). Perhaps the tt array should have size equal to number of time steps and not the number of nodes and can be updated outside inner loop over nodes.

There are several options to achieve what you want:
- develop own export module, that can incrementaly produce csv file, for example, and use suitable plotting sw
- develop own monitor, which can listen to user controlled events, such as solution step termination, etc (see src/oofemlib/monitor.h). Depending on your preference, it can save data to a file or even directly show the plot.
- python option is always there. Can you describe what is the difference between raw and python-obtained results? Can you provide testing in file to demonstrate the problem?

Re: Plot the solution results during analysis

Hi

Thank you for your reply and your suggestions. I prefer to adopt Python.
I created two following different tests:


-------------------------test1----------------------------------------------------

import oofempy
dr=oofempy.OOFEMTXTDataReader("TwoStepCasting_01.in")
problem=oofempy.InstanciateProblem(dr, oofempy.problemMode.processor, False, None, False)
problem.init()
problem.solveYourself()
problem.terminateAnalysis()

-------------------------test2------------------------------------------------------
import oofempy
dr=oofempy.OOFEMTXTDataReader("TwoStepCasting_01.in")
problem=oofempy.InstanciateProblem(dr, oofempy.problemMode.processor, False, None, False)

problem.init()
problem.postInitialize()

activeMStep = problem.giveMetaStep(1)
problem.initMetaStepAttributes(activeMStep)
domain = problem.giveDomain(1)

num_steps = problem.giveNumberOfSteps()


for timeStep in range(num_steps):
    problem.preInitializeNextStep()
    problem.giveNextStep()
    currentStep = problem.giveCurrentStep()
    problem.initializeYourself(currentStep)
    problem.solveYourselfAt(currentStep)
    problem.updateYourself(currentStep)
    problem.terminate(currentStep)


problem.terminateAnalysis()

----------------------------------------------------

test1 is run without any warnings and errors while test2 encounters the following warnings and errors:

Warning:Unread token(s) detected in the following record
"transienttransport nsteps 10 deltat 7200...":
[miniter][5]
In oofem::OOFEMTXTInputRecord::finish (D:\oofem\oofem\src\oofemlib\oofemtxtinputrecord.C:536)




_______________________________________________________
Warning:Check failed in TwoStepCasting_01.out: tstep 10, node 1, dof 10, mode 1:
value is 5.82552904e+01, but should be 5.72302290e+01 ( error is 1.025061e+00 but tolerance is 1.000000e-06 )
In oofem::NodeErrorCheckingRule::check (D:\oofem\oofem\src\oofemlib\errorcheckingexportmodule.C:138)
_______________________________________________________
_______________________________________________________
Warning:Check failed in TwoStepCasting_01.out: tstep 10, node 6, dof 10, mode 1:
value is 1.00000000e+01, but should be 3.96247063e+01 ( error is 2.962471e+01 but tolerance is 1.000000e-06 )
In oofem::NodeErrorCheckingRule::check (D:\oofem\oofem\src\oofemlib\errorcheckingexportmodule.C:138)
_______________________________________________________
_______________________________________________________
Warning:Check failed in TwoStepCasting_01.out: tstep 10, element 1, gpnum 3, ist 39, component 1:
value is 5.06685768e-01, but should be 5.05586458e-01 ( error is 1.099310e-03 but tolerance is 1.000000e-06 )
In oofem::ElementErrorCheckingRule::check (D:\oofem\oofem\src\oofemlib\errorcheckingexportmodule.C:206)
_______________________________________________________
_______________________________________________________
Warning:Check failed in TwoStepCasting_01.out: tstep 10, element 2, gpnum 3, ist 39, component 1:
value is 3.24560198e-01, but should be 3.50943937e-01 ( error is 2.638374e-02 but tolerance is 1.000000e-06 )
In oofem::ElementErrorCheckingRule::check (D:\oofem\oofem\src\oofemlib\errorcheckingexportmodule.C:206)
_______________________________________________________
Traceback (most recent call last):
  File "D:\OOFEM_thermal\test2.py", line 22, in <module>
    problem.terminate(currentStep)
RuntimeError: Rule not passed, exiting with error
In oofem::ErrorCheckingExportModule::doOutput (D:\oofem\oofem\src\oofemlib\errorcheckingexportmodule.C:485)


--------------------------------------------------------------------------------------


I have chosen an example (TwoStepCasting) in which there are two elements and the second element is activated at time step 10. I think in test2 something goes wrong at time step 10.

I attached the files.

Post's attachments

TwoStepCasting_01.zip 1.92 kb, 6 downloads since 2023-06-14 

You don't have the permssions to download the attachments of this post.

Re: Plot the solution results during analysis

I think that before using solveYourselfAt(currentStep), the equations need to be renumbered, as the second element is activated during simulation in TwoStepCasting_01.in. Now the following script runs smoothly:

import oofempy
from matplotlib import pyplot as plt

dr=oofempy.OOFEMTXTDataReader("TwoStepCasting_01.in")
problem=oofempy.InstanciateProblem(dr, oofempy.problemMode.processor, False, None, False)

problem.init()
problem.postInitialize()
problem.giveTimer().startTimer(oofempy.EngngModelTimerType.EMTT_AnalysisTimer)

activeMStep = problem.giveMetaStep(1)
problem.initMetaStepAttributes(activeMStep)
domain = problem.giveDomain(1)

num_steps = problem.giveNumberOfSteps()

nodes = [1,5]

aaa = [[] for i in range(len(nodes))]
tt =[[] for i in range(len(nodes))]



fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim(0,10)
ax.set_ylim(0,80)

lines = []
for item in nodes:
    lines.append(ax.plot(tt[0],aaa[0]))


for timeStep in range(num_steps):
    problem.preInitializeNextStep()
    problem.giveNextStep()
    currentStep = problem.giveCurrentStep()
    problem.initializeYourself(currentStep)
    if problem.requiresEquationRenumbering(currentStep):
        problem.forceEquationNumbering()
    problem.solveYourselfAt(currentStep)
    problem.updateYourself(currentStep)
    problem.terminate(currentStep)

    for i in range(len(nodes)):
        temp = problem.giveUnknownComponent(oofempy.ValueModeType.VM_Total, currentStep,domain,
                                            domain.giveDofManager(nodes[i]).giveDofWithID(oofempy.DofIDItem.T_f))

        aaa[i].append(temp)
        tt[i].append(timeStep)
        lines[i][0].set_xdata(tt[i])
        lines[i][0].set_ydata(aaa[i])
    plt.draw()
    plt.pause(0.01)



problem.terminateAnalysis()

plt.show()