# Import and update a matrix during runtime

**URL:** <https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887>\
**Category:** Development\
**Created:** [27 July 2023 06:31 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887 "2023-07-27T06:31:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edmund](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Edmund](https://pyfr.discourse.group/u/Edmund)\
**Post date:** [27 July 2023 06:31 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887/1 "2023-07-27T06:31:40Z")

</div>

Hi, everyone

I wonder if I can update some data in the kernel run like time. Because I’m trying to use the actuator line method in PyFR, which requires updating some parameters in every step.I can only transfer individual data like this `q.enqueue(kernels['eles/negdivconf'], t=t, x1=a, x2=b]`.  
However, an error occurs when I try to transfer a matrix. Could you give me some advice?

Best regards

---

<div class="post-metadata">

**Author:** ![WillT](https://avatars.discourse-cdn.com/v4/letter/w/bc79bd/32.png) [@WillT](https://pyfr.discourse.group/u/WillT)\
**Post date:** [28 July 2023 08:33 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887/2 "2023-07-28T08:33:07Z")

</div>

Other than time, are these parameters global or local? By that I mean are there a few of the same parameters needed by all the kernels, or will each element need different parameters?

---

<div class="post-metadata">

**Author:** ![Edmund](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Edmund](https://pyfr.discourse.group/u/Edmund)\
**Post date:** [28 July 2023 08:56 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887/3 "2023-07-28T08:56:04Z")

</div>

Thank you for the reply.  
I think these parameters are local. In fact, I want to add some parameters just like adding the source term a particular kernel. In my case, the difference is that these parameters need to be recalculated at each step.

---

<div class="post-metadata">

**Author:** ![WillT](https://avatars.discourse-cdn.com/v4/letter/w/bc79bd/32.png) [@WillT](https://pyfr.discourse.group/u/WillT)\
**Post date:** [28 July 2023 09:40 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887/4 "2023-07-28T09:40:18Z")

</div>

OK, you are probably best allocating some new memory and then passing that in a similar manner as how memory is passed to other kernels.

You can see some examples of memory allocation here: [https://github.com/PyFR/PyFR/blob/1a8f7fb821024f14eae745c39a58b89e9104f3fa/pyfr/solvers/base/elements.py#L177](https://github.com/PyFR/PyFR/blob/1a8f7fb821024f14eae745c39a58b89e9104f3fa/pyfr/solvers/base/elements.py#L177)

You’ll just want to call the alloc from your solver-specific set\_backend in elements.py

Then you can pass it to the kernel. You’ll want something like this in the mako: [https://github.com/PyFR/PyFR/blob/1a8f7fb821024f14eae745c39a58b89e9104f3fa/pyfr/solvers/baseadvec/kernels/negdivconf.mako#L11](https://github.com/PyFR/PyFR/blob/1a8f7fb821024f14eae745c39a58b89e9104f3fa/pyfr/solvers/baseadvec/kernels/negdivconf.mako#L11)  
and this in the set\_backend of elements.py: [https://github.com/PyFR/PyFR/blob/1a8f7fb821024f14eae745c39a58b89e9104f3fa/pyfr/solvers/baseadvec/elements.py#L113](https://github.com/PyFR/PyFR/blob/1a8f7fb821024f14eae745c39a58b89e9104f3fa/pyfr/solvers/baseadvec/elements.py#L113)

That kernel also shows you how to pass the time to a kernel.

---

<div class="post-metadata">

**Author:** ![Edmund](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@Edmund](https://pyfr.discourse.group/u/Edmund)\
**Post date:** [1 August 2023 13:32 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887/5 "2023-08-01T13:32:01Z")

</div>

Thanks for your advice.  
According to the examples, I allocated memory and passed it to the kernel. But there are still two problems.  
First, only the data in the pointwise matrix can be passed correctly. This means that if I want to pass 100 parameters, I have to create a matrix of 100_number of points. Can I create a 10_10 matrix and pass it to all the points in the kernel  
Second, how can I update the data in the matrix? I can’t update it like time. I tried using the set() function, but the matrix cannot be found in the integrator.

Thanks again for your time.

---

<div class="post-metadata">

**Author:** ![fdw](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@fdw](https://pyfr.discourse.group/u/fdw)\
**Post date:** [3 August 2023 12:57 UTC](https://pyfr.discourse.group/t/import-and-update-a-matrix-during-runtime/887/6 "2023-08-03T12:57:29Z")

</div>

Please see the current turbulent inflow pull request ([https://github.com/PyFR/PyFR/pull/358](https://github.com/PyFR/PyFR/pull/358)) for an example of how an entire matrix can be updated at runtime.

Regards, Freddie.
