SciML / SciML/DataDrivenDiffEq.jl

Understanding controls

Open
#447 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
430
Forks
58
Avg merge
6h 29m
Merged PRs (30d)
28

Description

Dear package authors! I have much fun playing with this tool (and diffeqflux), this will be a game changer in my field also (ecology). I was wondering if I could get some advice on how to deal with controls, or independent (or external) variables. My concrete example is data from a lake, measuring nutrients, algae and zooplankton. I also have measurements of temperature and influx of nutrients. The later two are external to the system. I think I have managed to set up a system to make the data driven problem identification. I am not sure though, How to use the resulting system and plugging the external variables back in. I did a MWE to generate similar but simpler data:

function T(t)
		10*(1+sin(t/200)*0.4+0.4)
	end
	function Inp(t)
		0.05+0.1*(sin(t/100)*0.4+0.4)
	end
	function system(u,p,t)
		N=u[1]
		A=u[2]
		Z=u[3]
		inp=Inp(t)

		TA=exp(-(T(t)-10.0).^2/200)
		TZ=exp(-(T(t)-15.0).^2/200)
		dN=inp-N*0.05-0.4*N/(0.2+N)*A*TA
		dA=0.4*N/(0.2+N)*A*TA-0.1*A/(0.2+A)*Z*TZ-0.05*A
		dZ=0.1*A/(0.2+A)*Z*TZ-0.05*Z
		return [dN;dA;dZ]
	end
	z0 = [0.1; 0.1;0.1]
	ztspan = (0.0, 500.0) # this gets really slow for endtime>50....
	zprob = ODEProblem(system, z0, ztspan)
	zsol = solve(zprob, Tsit5(), saveat = 0.1);

	X = zsol[:, :]  .* (1.0.+ 0.02.*randn(rng, size(zsol)));
	ts = zsol.t;
	controls=vcat(Inp.(ts)',T.(ts)')

	prob = ContinuousDataDrivenProblem(X, ts, GaussianKernel(),U = controls)

	#Three variables: N,A,Z and two controls: Inp and T
	@variables u[1:3] c[1:2]
	u = collect(u)
	c = collect(c)

	# just a start for basis exploration
	h = Num[polynomial_basis(u, 3); polynomial_basis(c, 3);exp(c[2])]
	
	basis = Basis(h, u, controls = c);
	println(basis) # hide
	
	sampler = DataProcessing(split = 0.8, shuffle = true, batchsize = 30, rng = rng)
	λs = exp10.(-10:0.1:0)
	opt = STLSQ(λs)
	res = solve(prob, basis, opt,
	            options = DataDrivenCommonOptions(data_processing = sampler, digits = 1))
       sys = get_basis(res)

This gives me a system of equations. I am not entirely sure what the best way to get the parameters as an array is but I use:

P=ModelingToolkit.varmap_to_vars(get_parameter_map(get_basis(res)),parameters(get_basis(res)))

If I do eqns=sys(z0,P,0) I get

Screenshot 2023-01-23 at 16 47 11

My Question: How would I be able to get the controls, c1 and c2 into the equations as external variables so that I can run it as an ODE directly, assuming the controls are measured variables, controls and not the functions T(t) and Inc(t) at the top of the MWE.

I would want to run:

ODEProblem(eqns,z0,ztspan,P)

but driven by the variables in the matrix controls.

I hope this is reasonably clear as a question? Any recommendations for improvements are greatly appreciated!

Cheers, J

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no repository files or tests; begin by reproducing the Julia MWE and tracing the controls-related API calls it uses. Done would be a documented, verifiable path for using measured controls with the generated ODE system, including an example based on the supplied matrix.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
machine-learning
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.