# Initialize some functions at every remake in PDEProblem

**URL:** <https://discourse.julialang.org/t/initialize-some-functions-at-every-remake-in-pdeproblem/102078>\
**Category:** Modelling & Simulations\
**Tags:** performance, sciml, modelingtoolkit, methodoflines\
**Created:** [July 25, 2023, 12:42pm UTC](https://discourse.julialang.org/t/initialize-some-functions-at-every-remake-in-pdeproblem/102078 "2023-07-25T12:42:07Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![johnb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnb/32/44115_2.png) [@johnb](https://discourse.julialang.org/u/johnb)\
**Post date:** [July 31, 2023, 5:59pm UTC](https://discourse.julialang.org/t/initialize-some-functions-at-every-remake-in-pdeproblem/102078/3 "2023-07-31T17:59:54Z")

</div>

I looked into this earlier this year, and while it is a known missing feature, it seems to still be in progress.

> [@Passing vectors of data into MTK/MoL problem as parameter](https://discourse.julialang.org/t/passing-vectors-of-data-into-mtk-mol-problem-as-parameter/96578):
>
> I have been able to use vectors of data that I’ve passed through DataInterpolations as inputs/boundary conditions to a ModelingToolkit/MethodOfLines problem, but I would like to use the data vectors as MTK parameters to automate changing my system conditions. I’ve been struggling for a while now and I can’t find any documentation on how to do this. Is there something I’m missing or should I look for a different way to adjust my system inputs? I’ve adapted one of the tutorials from MethodOfLines…

While your problem may be different than mine, since I only ever needed on the order of 10 boundary condition sets at a time, my solution was to initialize all runs with different datasets at once in a multithreaded loop with the interpolation defined within the loop and to save the discretized problems in a vector for easy access. I could then modify non-BC parameters across all problems without re-discretizing. Some generic code similar to what I used is below and it works well enough, though it’s not very elegant.

```julia
#define some input_data matrix or data structure such that it can be indexed for each BC set.
probvec = []
solvec = []
Threads.@threads for i = 1:n_sims
     #just need to somehow index with loop such that t_input, x_input change with i
     t_input = input_data[i,1,:] 
     x_input = input_data[i,2,:]
     x_BC = LinearInterpolation(x_input,t_input)

     #rest of code
     #relevant BC
     x(t,0) ~ x_BC(t)
     

     #more code, discretization
      prob = ModelingToolkit.discretize(pdesys,discretization);
     push!(probvec, (i, prob)) #saved as tuple for indexing since multithreading means code can finish in random orders

     sol = solve(prob) #done to run compilation and speed up future solves with different non-BC parameters
     push!(solvec, (i, sol)) #saved to check whether solve ran properly, but not really necessary
end

#strip probs out of tuple and put in correct order
#definitely could be done more elegantly, but I really just pretend to be a programmer
ordered_prob_tuples = sort(probvec, rev=false)
ordered_probs = []
for i = 1:size(ordered_prob_tuples,1) 
    push!(ordered_probs, ordered_prob_tuples[i][2])
end

#remake for non-BC parameters is fast, uses i to find correct problem 
#from problem vector and remake with some set of parameters from param_vector
newprob = remake(ordered_probs[i], p = param_vector)
sol = solve(newprob)

```

I still hope to figure out a better way to pass in BCs since I will eventually want to modify those parametrically, but I haven’t had much luck yet beyond some extremely low-quality experiments with series approximations. If you end up figuring something better out, please post about it, I’d be very interested to hear!

---

_[View the full topic](https://discourse.julialang.org/t/initialize-some-functions-at-every-remake-in-pdeproblem/102078)._
