0

I am trying to solve a physical problem by coupling a simulation software to Python. Basically, I need to find the values of length and diameter of each of the pipe sections in the picture below (line segment between any 2 black dots is a pipe section) such that fluid flow from point 0 reaches points 1-5 at the same time instant. I give some starting values for the length and diameter of each of the pipe sections and the simulation software solves to check if the fluid reaches the points 1-5 at the same time instant. If not, the lengths and diameters of the pipe section(s) need to be changed to ensure this. Flow not reaching points 1-5 at the same instant is known as flow imbalance, and ideally I need to reduce this imbalance to zero.

enter image description here

Now my question is - can I couple Python to the simulation software to suggest values of the length and diameter of the various pipe sections to ensure that flow reaches points 1-5 at the same time instant? I already know how to run the simulation software through a python script, and how to extract the flow imbalance result from the software. All I want to know is does a library/ function exist in Python that can iteratively suggest values for the length and diameter of pipe section(s) such that flow imbalance reduces after every iteration?

Please know that it is not possible to frame an objective function that will consider the length and diameter of the pipe section(s) and try to minimize or maximize it to eliminate flow imbalance. Running the software simulation is the only way to actually check this flow imbalance. I know that optimization libraries exist such as scipy.optimize, but AFAIK they work on an objective function. I could not find anything that would suggest values for the length and diameter of pipe sections depending on how large the flow imbalance is after every iteration.

  • Perhaps you could use [MeshGrid](https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html) to generate points to try as described by [What is the purpose of meshgrid in Python / NumPy?](https://stackoverflow.com/questions/36013063/what-is-the-purpose-of-meshgrid-in-python-numpy). The idea is to use your simulator to evaluate each point and choose the point with the best value in the grid. – DarrylG Jun 12 '22 at 14:52
  • @DarrylG yeah but this can get really tedious and time consuming right? Cos it essentially means to actually solve all possible combinations of lengths and diameters for every pipe section. I am looking for something that suggests the values depending on the current iteration's imbalance, and the values used in that iteration. – Digvijay Rawat Jun 12 '22 at 15:42
  • 1
    Is it possible to create a Python function that calls the simulator and obtains results? – DarrylG Jun 12 '22 at 15:57
  • @DarrylG Yes, that is how I will extract the imbalance results after every iteration. The simulator will be run, and its results will also be extracted through python calls. Further, I also know how to give the new values of diameter and lengths of the pipe sections as input to the simulator through Python. The only thing I do not know is how to get the suggestions from python on the values themselves. – Digvijay Rawat Jun 12 '22 at 16:02
  • 2
    Could you use [scipy.optimize.minimize](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) as described by [Optimization with SciPy and application ideas to machine learning](https://towardsdatascience.com/optimization-with-scipy-and-application-ideas-to-machine-learning-81d39c7938b8)? – DarrylG Jun 12 '22 at 16:17

1 Answers1

1

So you can write a function

def imbalance(pipe_diameters):
  times = get_pipe_times(pipe_diameters)
  return times - np.mean(times)

Then you can use

from scipy.optimize import leastsq
x0 = uniform_diameter_pipes()
diameters = leastsq(imbalance, x0)

If the number of parameters is more than the number of outputs then you may have to use minimize as mentioned in the comments. In that case your imbalance must return a scalar.

def imbalance(pipe_diameters):
  times = get_pipe_times(pipe_diameters)
  return np.var(times) # calculate variance, could be other metric as well
Bob
  • 13,867
  • 1
  • 5
  • 27