Exporting all vertex results to array

Hi,
I am a new user of SfePy and finite element programming in general, and I have started my own finite element project last week. After comparing SfePy with other packages like FEniCS and GetFEM++ I have decided to give SfePy a try. My problem is close to a diffusion problem. I have a fair amount of scientific Python experience in general, including some VTK.
I know I can export the computation result to VTK files in Unstructured Grid format, which is performed at the end of many examples. My question is: how can I access the final result, at each vertex, directly from the Python program itself, saving the result into say a Numpy array or equivalent data structure.
For example, the Time Thermal Interactive example <http://sfepy.org/doc-devel/examples/diffusion/time_poisson_interactive.html> exports domain.01.vtk to domain.02.vtk, containing points, cells, and temperature at each point ("SCALARS T float 1.") Is there a way to access these numbers directly at the end of the computation, from say the Problem variable pb or State variable state0? I feel like this is probably an easy task but I could not figure out on my own just by looking into the State and Problem variables.
I need these values directly in Python as I have other part of my model that takes the temperature values at these vertices. Since the other computation will use the same mesh I would like to have the mesh and all solved values.
Thanks!
Bill

Hi Bill,
On 05/02/2018 03:32 AM, null null wrote:
Hi,
I am a new user of SfePy and finite element programming in general, and I have started my own finite element project last week. After comparing SfePy with other packages like FEniCS and GetFEM++ I have decided to give SfePy a try. My problem is close to a diffusion problem. I have a fair amount of scientific Python experience in general, including some VTK.
I know I can export the computation result to VTK files in Unstructured Grid format, which is performed at the end of many examples. My question is: how can I access the final result, at each vertex, directly from the Python program itself, saving the result into say a Numpy array or equivalent data structure.
In static problems (no time stepping), you can simply call state() or state.get_parts() to get the solution vectors as numpy arrays of DOFs in the FE nodes - those nodes correspond to mesh vertices for linear FEM. In time-dependent problems, the same holds, but the state returned by the solver is the final state - if you need to access the DOFs in all time steps, see below.
For example, the Time Thermal Interactive example <http://sfepy.org/doc-devel/examples/diffusion/time_poisson_interactive.html> exports domain.01.vtk to domain.02.vtk, containing points, cells, and temperature at each point ("SCALARS T float 1.") Is there a way to access these numbers directly at the end of the computation, from say the Problem variable pb or State variable state0? I feel like this is probably an easy task but I could not figure out on my own just by looking into the State and Problem variables.
I need these values directly in Python as I have other part of my model that takes the temperature values at these vertices. Since the other computation will use the same mesh I would like to have the mesh and all solved values.
Check the example [1] to see how to setup a custom hook (or callback) function that is called in each time step, and where you can, for example, store the state. Essentially, you need to:
- get the standard callbacks of a time-stepper:
init_fun, prestep_fun, _poststep_fun = pb.get_tss_functions(state0)
define your own poststep_fun(), where you call the standard _poststep_fun() and then do what you need - you can access the DOF arrays of variables using something like pb.get_variables()'T'.
call the time stepper, as in [1]
tss(state0.get_vec(pb.active_only), init_fun=init_fun, prestep_fun=prestep_fun, poststep_fun=poststep_fun, status=tss_status)
Let me know if you need more help - the current implementation of the time-stepping solvers is very new and not much documented.
r.
[1] http://sfepy.org/doc-devel/examples/diffusion/time_poisson_interactive.html
Thanks!
Bill
SfePy mailing list sfepy@python.org https://mail.python.org/mm3/mailman3/lists/sfepy.python.org/

Hi Robert,
Thanks! This is very helpful. I missed just calling state().
I will explore the time-stepping solver a bit more.
Regards,
Bill
On 2 May 2018 at 09:20, Robert Cimrman <cimrman3@ntc.zcu.cz> wrote:
Hi Bill,
On 05/02/2018 03:32 AM, null null wrote:
Hi,
I am a new user of SfePy and finite element programming in general, and I have started my own finite element project last week. After comparing SfePy with other packages like FEniCS and GetFEM++ I have decided to give SfePy a try. My problem is close to a diffusion problem. I have a fair amount of scientific Python experience in general, including some VTK.
I know I can export the computation result to VTK files in Unstructured Grid format, which is performed at the end of many examples. My question is: how can I access the final result, at each vertex, directly from the Python program itself, saving the result into say a Numpy array or equivalent data structure.
In static problems (no time stepping), you can simply call state() or state.get_parts() to get the solution vectors as numpy arrays of DOFs in the FE nodes - those nodes correspond to mesh vertices for linear FEM. In time-dependent problems, the same holds, but the state returned by the solver is the final state - if you need to access the DOFs in all time steps, see below.
For example, the Time Thermal Interactive example
<http://sfepy.org/doc-devel/examples/diffusion/time_poisson_ interactive.html> exports domain.01.vtk to domain.02.vtk, containing points, cells, and temperature at each point ("SCALARS T float 1.") Is there a way to access these numbers directly at the end of the computation, from say the Problem variable pb or State variable state0? I feel like this is probably an easy task but I could not figure out on my own just by looking into the State and Problem variables.
I need these values directly in Python as I have other part of my model that takes the temperature values at these vertices. Since the other computation will use the same mesh I would like to have the mesh and all solved values.
Check the example [1] to see how to setup a custom hook (or callback) function that is called in each time step, and where you can, for example, store the state. Essentially, you need to:
- get the standard callbacks of a time-stepper:
init_fun, prestep_fun, _poststep_fun = pb.get_tss_functions(state0)
define your own poststep_fun(), where you call the standard _poststep_fun() and then do what you need - you can access the DOF arrays of variables using something like pb.get_variables()'T'.
call the time stepper, as in [1]
tss(state0.get_vec(pb.active_only), init_fun=init_fun, prestep_fun=prestep_fun, poststep_fun=poststep_fun, status=tss_status)
Let me know if you need more help - the current implementation of the time-stepping solvers is very new and not much documented.
r.
[1] http://sfepy.org/doc-devel/examples/diffusion/time_poisson_ interactive.html
Thanks!
Bill
SfePy mailing list sfepy@python.org https://mail.python.org/mm3/mailman3/lists/sfepy.python.org/
SfePy mailing list sfepy@python.org https://mail.python.org/mm3/mailman3/lists/sfepy.python.org/
participants (2)
-
null null
-
Robert Cimrman