Tag Archives: renewable-energy

MATLAB vs Julia: Best Programming Language for Renewable Energy Simulations

By: Great Lakes Consulting

Re-posted from: https://blog.glcs.io/matlab-vs-julia-renewable-energy

This post was written by Steven Whitaker.

At GLCS, we’re proud to have delivered innovative projects across top industries, including renewable energy, aerospace, and biomedical engineering.Our comprehensive Modeling and Simulation Servicesempower clients to elevate their designs,whether rewriting models in Juliafor greater efficiency or unlocking cutting-edge featuresto solve complex problems.With deep expertise spanning several engineering and scientific domains,including computational fluid dynamics,thermodynamics,controls,biomedical engineering,and chemistry,we are your trusted partner in pushing modeling boundariesand achieving breakthrough results.

In this post,we’ll focus on systems modelingfor renewable energy.

Renewable energy systems,from wind farms to wave power,require precise modeling and simulation.Selecting the optimal computational tools is crucial;it can significantly acceleratedevelopment, reduce costs, and drive the transitionto a sustainable future.

Both MATLAB and Julia are widely used in engineering,particularly for solving differential equations.This post compares how each handlesa renewable energy modeling scenario,the steady axisymmetric turbulent wakebehind a wind turbine,ultimately showing why Julia has the edge.Maximize efficiency and energy outputwith cutting-edge technologiesdesigned for tomorrows energy.

Steady Axisymmetric Turbulent Wake

When air flows past a wind turbine,a wake forms downstream.The wake velocity deficit impacts turbine spacing,efficiency,and power output.

A simplified steady axisymmetric turbulent wake equation is:

\[\frac{\partial U}{\partial x} = \nu_t \cdot \left( \frac{\partial^2 U}{\partial r^2} + \frac{1}{r} \frac{\partial U}{\partial r} \right)\]

where:

  • \( U \) is the axial velocity.
  • \( x \) is the downstream distance.
  • \( r \) is the radial coordinate.
  • \( \nu_t \) is the turbulent viscosity.

This is a reduced form of the momentum equation,capturing diffusion of momentumdue to turbulence.

MATLAB vs Julia: ODE/PDE System Set-up

Let’s see how to convert the math into codeand solve this PDE.

  • MATLAB:

    function dUdx = wake_eq(x, U, p)    % Radial step size    dr = p.r(2) - p.r(1);    % First derivative (central difference)    dUdr = (U(3:end) - U(1:end-2)) / (2 * dr);    % Second derivative (central difference)    d2Udr2 = (U(3:end) - 2 * U(2:end-1) + U(1:end-2)) / dr^2;    dUdx = zeros(size(U));    dUdx(2:end-1) = p.nu_t * (d2Udr2 + dUdr ./ p.r(2:end-1));endU0 = initial_profile();xspan = [0 100];p.nu_t = 0.05;p.r = linspace(0, 1, numel(U0));prob = ode;prob.ODEFcn = @dUdx;prob.InitialTime = xspan(1);prob.InitialValue = U0;prob.Parameters = p;prob.Solver = "ode45";sol = solve(prob, xspan(1), xspan(2));
  • Julia:

    using DifferentialEquations@kwdef mutable struct WakeParams{T}    _t::Float64    const r::Tendfunction wake_eq!(dU, U, p, x)    # Radial step size    dr = p.r[2] - p.r[1]    # First derivative (central difference)    dUdr = (U[3:end] .- U[1:end-2]) ./ (2 * dr)    # Second derivative (central difference)    d2Udr2 = (U[3:end] .- 2 .* U[2:end-1] .+ U[1:end-2]) ./ dr^2    dU[1] = dU[end] = 0    dU[2:end-1] .= p._t .* (d2Udr2 .+ dUdr ./ p.r[2:end-1])endU0 = initial_profile()xspan = (0.0, 100.0)p = WakeParams(; _t = 0.05, r = range(0.0, 1.0, length(U0)))prob = ODEProblem(wake_eq!, U0, xspan, p)solver = Tsit5()sol = solve(prob, solver)

(Note that, in practice,the boundary conditionsfor \( r = 0 \) and \( r = 1 \)would need to be handled with more care.)

As you can see,the syntax for Julia and MATLABis quite similar.However,there are some key differencesbetween the two approaches:

  • Julia’s broadcasting (.=, .*, etc.) is explicit and fast.
  • Julia can use in-place functionsto minimize memory allocations,boosting performance.
  • The Julia code for the dynamics above was writtento look more like the MATLAB code.However,additional easy performance optimizations are possibleto reduce memory allocations.
  • Julia’s DifferentialEquations.jl supports many solverswith a unified interface.MATLAB supports only a limited set of solvers.

Event Handling and Callbacks: Wind Gusts

Now let’s add a gust at \( x = 50 \)that will change \( \nu_t \)for \( x \ge 50 \).

  • MATLAB:

    function v = events_func(x, U, p, gust_position)    % Event occurs when `x == gust_position`.    v = x - gust_position;endfunction [stop, U, p] = callbacks_func(x, U, ie, p)    stop = false;    % Check if the event occurred.    if ismember(1, ie)        p.nu_t = 0.08;    endendgust_position = 50;event = odeEvent;event.EventFcn = @(x, U, p) events_func(x, U, p, gust_position);event.Response = "callback";event.CallbackFcn = @callbacks_func;prob.EventDefinition = event;sol = solve(prob);
  • Julia:

    function gust_affect!(integrator)    integrator.p._t = 0.08endgust_position = 50.0;callback = PresetTimeCallback([gust_position], gust_affect!)sol = solve(prob, solver; callback)

When it comes to events and callbacks,Julia’s approach is better:

  • Julia’s callbacks are better organized;events and corresponding callbackscan be defined next to each otherin the code,instead of separated across filesas is typical in MATLAB.
  • Multiple events can be combined cleanlywith CallbackSet,no need to try to cram multiple eventsinto a single functionlike you have to do in MATLAB.
  • Julia keeps track of what events are triggered.In MATLAB,you have to checkwhat events were triggered manually.
  • Julia provides many pre-defined callbacks(in DiffEqCallbacks.jl)that often have to be manually implemented in MATLAB.

Other Considerations

In addition to the differencesin solving differential equations,here are some other key differencesbetween Julia and MATLAB:

  • Performance:Julia’s JIT-compilation and method specializationyield C-like speeds.Large-scale renewable energy simulationsrun faster and scale better,especially for parameter sweeps.You can expect to see 50–150 times faster code with Julia.
  • Workflow:MATLAB offers a polished GUI and plotting out of the box.However,Julia offers open-source freedom,easy integration with Python/C,and a thriving ecosystem.
  • Licensing:MATLAB requires a paid license;Julia is entirely free and open-source.

Summary

In this post,we saw how Julia and MATLAB comparefor defining and solvingsteady axisymmetric turbulent wake.Both languages can model renewable energy systems effectively.However, Julia offers:

  • Performance: JIT speed for large systems.
  • Flexibility: Powerful callbacks and open integrations.
  • Cost: No license fees.
  • Modern syntax: Designed for productivity and clarity.

Ready to revolutionize your renewable energy simulations? Transition your MATLAB models to Juliaand experience unparalleled speed, flexibility, and long-term maintainability.Reach out today and let’s accelerateyour green energy innovations together!

Worried about the technical hurdles and costsof switching from MATLAB to Julia?Discover our cutting-edgeJulia-MATLAB Integration.We develop high-performance Julia modelsthat seamlessly connect withyour existing MATLAB codebase,minimizing risks while maximizingyour return on investment.Transition smarter, faster, and more cost-effectivelywith our expert solutions!

Additional Links

MATLAB is a registered trademarkof The MathWorks, Inc.

]]>