This notebook addresses a fundamental piping design problem in process engineering: determining the pipe diameter that minimizes total cost for transporting a non-Newtonian power-law fluid. The fluid has a consistency index K = 0.6, flow behavior index n = 0.65 (indicating shear-thinning behavior), and flows at Q = 0.005 m³/s through a 50 m pipe. The total cost balances two competing factors — capital cost, which scales with pipe diameter squared (Cp = 4000 $/m²), and pumping/energy cost, which depends on the pressure drop needed to overcome friction (Ce = 0.02). This creates a trade-off: larger pipes cost more to purchase but require less pumping energy, while smaller pipes are cheaper upfront but suffer higher pressure drops.
The pressure drop is computed using the power-law laminar flow correlation, which generalizes the Hagen-Poiseuille equation to account for shear-dependent viscosity through the flow behavior index n. The first solution method applies Mathematica’s NMinimize function with the Differential Evolution algorithm, searching for the optimal diameter within a bounded range of 0.02 to 0.15 m. This converges to an optimal diameter of 0.138539 m, producing a pressure drop of 2602.46 Pa and a minimum total cost of $128.822 — establishing a benchmark solution for comparison with PSO results.
To verify the flow regime assumption underlying the laminar pressure-drop correlation, one can compute a generalized Reynolds number using the fluid density (ρ = 1050 kg/m³). The resulting Reynolds number of approximately 208.774 confirms the flow is laminar, validating the use of the power-law pressure drop equation throughout the optimization.
The second approach solves the same problem using Particle Swarm Optimization, this time formulated as a simple 1D search problem since diameter is the only decision variable. A custom psoOptimize module is again employed with 30 particles, standard PSO update rules (inertia weight decay, velocity clamping, cognitive and social coefficients c1 = c2 = 1.42), and bounds matching the NMinimize search range. Because the problem is low-dimensional and unconstrained aside from simple bounds, convergence is dramatically faster than in more complex multi-variable problems.
Running PSO for just 20 iterations shows the swarm’s best-known cost collapsing almost immediately from 128.974 to 128.822, matching the NMinimize benchmark within the first few iterations. The final PSO result gives a diameter of 0.138538 m with a cost of $128.822 — in agreement with the Differential Evolution solution. The log-scale convergence plot illustrates this rapid single-variable convergence: a steep initial drop followed by an immediate plateau, demonstrating that for simple, well-behaved 1D optimization landscapes, PSO can match rigorous global optimizers in only a few iterations.
The pressure drop is computed using the power-law laminar flow correlation, which generalizes the Hagen-Poiseuille equation to account for shear-dependent viscosity through the flow behavior index n. The first solution method applies Mathematica’s NMinimize function with the Differential Evolution algorithm, searching for the optimal diameter within a bounded range of 0.02 to 0.15 m. This converges to an optimal diameter of 0.138539 m, producing a pressure drop of 2602.46 Pa and a minimum total cost of $128.822 — establishing a benchmark solution for comparison with PSO results.
To verify the flow regime assumption underlying the laminar pressure-drop correlation, one can compute a generalized Reynolds number using the fluid density (ρ = 1050 kg/m³). The resulting Reynolds number of approximately 208.774 confirms the flow is laminar, validating the use of the power-law pressure drop equation throughout the optimization.
The second approach solves the same problem using Particle Swarm Optimization, this time formulated as a simple 1D search problem since diameter is the only decision variable. A custom psoOptimize module is again employed with 30 particles, standard PSO update rules (inertia weight decay, velocity clamping, cognitive and social coefficients c1 = c2 = 1.42), and bounds matching the NMinimize search range. Because the problem is low-dimensional and unconstrained aside from simple bounds, convergence is dramatically faster than in more complex multi-variable problems.
Running PSO for just 20 iterations shows the swarm’s best-known cost collapsing almost immediately from 128.974 to 128.822, matching the NMinimize benchmark within the first few iterations. The final PSO result gives a diameter of 0.138538 m with a cost of $128.822 — in agreement with the Differential Evolution solution. The log-scale convergence plot illustrates this rapid single-variable convergence: a steep initial drop followed by an immediate plateau, demonstrating that for simple, well-behaved 1D optimization landscapes, PSO can match rigorous global optimizers in only a few iterations.
In[]:=
ClearAll["Global`*"]
Fluid Data
Pipe length
L=50;
Consistency index
K=0.6;
Power-law index less than 1 - shear-thinning fluid
n=0.65;
Volumetric flow rate
Q=0.005;
Economic Parameters
Pipe cost coefficient
Cp=4000;
Pumping cost coefficient
Ce=0.02;
Pressure Drop for Power-law fluids - laminar flow case - D is the pipe diameter
ΔP[D_]:=(4*L*K/D)*((3*n+1)/(4*n)*(8*Q)/(Pi*D^3))^n;
Total Cost
Cost[D_]:=Cp*D^2+Ce*ΔP[D];
Method based on NMinimize
Optimization
solution=NMinimize[{Cost[D],0.02<=D<=0.15},D,Method->"DifferentialEvolution"]
Out[]=
{128.822,{D0.138539}}
Optimal solution
Dopt=D/.Last[solution];Costopt=First[solution];ΔPopt=ΔP[Dopt];Print["Optimal Diameter = ",Dopt," m"];Print["Pressure Drop = ",ΔPopt," Pa"];Print["Minimum Total Cost = ",Costopt];
Optimal Diameter = 0.138539 m
Pressure Drop = 2602.46 Pa
Minimum Total Cost = 128.822
ρ=1050;(*kg/m^3*)
μ=0.8;(*Pa.s(non-Newtonianapparentviscosity)*)
Reynolds number
velocity
v=4Q/(PiDopt^2);
Generalized Reynolds number
Repl=(ρv^(2-n)Dopt^n/K)*((8/(3n+1))^n);
Classification
regime=Which[Repl<2100,"Laminar",Repl<=4000,"Transitional",True,"Turbulent"];{Repl,regime}
Out[]=
{208.774,Laminar}
PSO approach
In[]:=
Fitness[{D_}]:=Cp*D^2+Ce*ΔP[D]
1D problem
SwarmDim=1;
Upper and lower bounds for the search domain
FitnessLB={0.02};FitnessUB={0.5};
PSO parameters
c1=1.42;c2=1.42;w=0.72984;
Clip each column of a matrix against its own scalar bound
clipMatrix[mat_,lbv_,ubv_]:=Transpose[MapThread[Clip[#1,{#2,#3}]&,{Transpose[mat],lbv,ubv}]];clampMatrix[mat_,vmaxv_]:=Transpose[MapThread[Clip[#1,{-#2,#2}]&,{Transpose[mat],vmaxv}]];
PSO Optimizer
psoOptimize[lossFunc_,dim_,lb_,ub_,nParticles_:30,nIter_:100,wMax_:0.9,wMin_:0.4,c1_:1.42,c2_:1.42,vClampFrac_:0.2]:=Module[{particles,velocities,pbest,pbestScore,gbest,gbestScore,convergence={},r1,r2,scores,gbestIdx,vmax,wIter},vmax=vClampFrac*(ub-lb);particles=Transpose@Table[RandomReal[{lb[[i]],ub[[i]]},nParticles],{i,dim}];velocities=ConstantArray[0.,{nParticles,dim}];pbest=particles;pbestScore=lossFunc/@particles;gbestIdx=First@Ordering[pbestScore,1];gbestScore=pbestScore[[gbestIdx]];gbest=pbest[[gbestIdx]];Do[wIter=wMax-(wMax-wMin)*(j-1)/(nIter-1);r1=RandomReal[1,{nParticles,dim}];r2=RandomReal[1,{nParticles,dim}];velocities=wIter*velocities+c1*r1*(pbest-particles)+c2*r2*(ConstantArray[gbest,nParticles]-particles);velocities=clampMatrix[velocities,vmax];particles+=velocities;particles=clipMatrix[particles,lb,ub];scores=lossFunc/@particles;pbest=MapThread[If[#3<#2,#1,#4]&,{particles,pbestScore,scores,pbest}];pbestScore=MapThread[Min,{pbestScore,scores}];gbestIdx=First@Ordering[pbestScore,1];If[pbestScore[[gbestIdx]]<gbestScore,gbestScore=pbestScore[[gbestIdx]];gbest=pbest[[gbestIdx]];];AppendTo[convergence,gbestScore];If[j<10||Mod[j,2]==0,Print["PSO Iter ",j," Loss = ",gbestScore]];,{j,nIter}];{gbest,gbestScore,convergence}];
PSO run
result=psoOptimize[Fitness,SwarmDim,FitnessLB,FitnessUB,10,30];{gbest,gbestScore,convergence}=result;
PSO Iter 1 Loss = 128.974
PSO Iter 2 Loss = 128.896
PSO Iter 3 Loss = 128.842
PSO Iter 4 Loss = 128.822
PSO Iter 5 Loss = 128.822
PSO Iter 6 Loss = 128.822
PSO Iter 7 Loss = 128.822
PSO Iter 8 Loss = 128.822
PSO Iter 9 Loss = 128.822
PSO Iter 10 Loss = 128.822
PSO Iter 12 Loss = 128.822
PSO Iter 14 Loss = 128.822
PSO Iter 16 Loss = 128.822
PSO Iter 18 Loss = 128.822
PSO Iter 20 Loss = 128.822
PSO Iter 22 Loss = 128.822
PSO Iter 24 Loss = 128.822
PSO Iter 26 Loss = 128.822
PSO Iter 28 Loss = 128.822
PSO Iter 30 Loss = 128.822
PSO result vs . NMinimize solution
In[]:=
gbest
Out[]=
{0.138542}
In[]:=
gbestScore
Out[]=
128.822
In[]:=
ΔP[gbest]//First
Out[]=
2602.31
In[]:=
Print["Optimal Diameter = ",Dopt," m"];Print["Pressure Drop = ",ΔPopt," Pa"];Print["Minimum Total Cost = ",Costopt];
Optimal Diameter = 0.138539 m
Pressure Drop = 2602.46 Pa
Minimum Total Cost = 128.822
PSO convergence plot
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Optimal pipe diameter design for power-law fluid flow: a cost-minimization approach
by Housam Binous
Wolfram Community, STAFF PICKS, July 5, 2026
https://community.wolfram.com/groups/-/m/t/3744899
by Housam Binous
Wolfram Community, STAFF PICKS, July 5, 2026
https://community.wolfram.com/groups/-/m/t/3744899