[RFC PATCH] Make fake profile average depth come out right

Anton Lundin glance at acc.umu.se
Sun Nov 3 14:44:38 UTC 2013


On 15 October, 2013 - Linus Torvalds wrote:

> 
> Ok, so this is very much a request-for-discussion rather than meant to 
> actually be applied, because it doesn't really cover all cases. If we need 
> to generate a profile for some fairly insane cases, the stupid thing just 
> gives up rather than try to search for a solution that satisfies the 
> constraints. 
> 
> For example, if you have a maxdepth=10m and an average depth of 9.9m, it 
> will try two different (roughly sane) dive profiles, fail to get anything 
> that matches that kind of dive and that average depth, and then just say 
> "screw it, I'll match an insane profile instead" and basically generate a 
> rectangular dive.
> 
> It does seem to handle the few cases I tried. 
> 
> Comments? Somebody should double-check my math, and I think we might want 
> to make the "default profiles" a bit saner. Right now the preferred 
> default profile simply says "ascent/descent rate of 5m/min, with two 
> levels at max_depth and 1/3rd maxdepth". 
> 
> That profile might not work for the simple reason that maybe we did a 
> quick dive to 40m, but then came up and spent a lot of time in shallower 
> waters, so our average depth was just 10m or so. Obviously we can *not* 
> make a dive that has the two depths 40m and 13m come out to an average of 
> 10m deep.
> 
> So it has a fallback for things like that, where we try to generate a 
> profile with a faster ascent/descent rate, and a shallower second depth.
> 
> And then there's the fallback with an insane 10m/s ascent/descent rate and 
> a second depth that is basically at the surface, and if we can't generate 
> a dive profile with those parameters, either my math is wrong, or somebody 
> was playing with rockets under-water. So we just give up.
> 
> There's a few other heuristics too (if there is no average depth, we 
> create a random one just to make the profile look reasonable).
> 
> I *think* this is all perfectly fine, but I do want others to take a look. 
> It might be doing something stupid.
> 
> So I'll do:
> 
>   Conditionally-signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
> 
> so you can apply this if you agree with the math.
> 
>                  Linus

I read this on a flight on my cell and i thought it looked a bit
fishy, but then i tested it and produced nice looking profiles.

Now i read it a bit closer and i at least have some questions =)

> 
> ---
>  device.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
>  1 file changed, 132 insertions(+), 21 deletions(-)
> 
> diff --git a/device.c b/device.c
> index 6244e41176bc..4084c4edb1fc 100644
> --- a/device.c
> +++ b/device.c
> @@ -2,30 +2,141 @@
>  #include "dive.h"
>  #include "device.h"
>  
> +/*
> + * Good fake dive profiles are hard.
> + *
> + * "depthtime" is the integral of the dive depth over
> + * time ("area" of the dive profile). We want that
> + * area to match the average depth (avg_d*max_t).
> + *
> + * To do that, we generate a 6-point profile:
> + *
> + *  (0, 0)
> + *  (t0, max_d)
> + *  (t1, max_d)
> + *  (t2, d)
> + *  (t3, d)
> + *  (max_t, 0)

Hmm.. These ones should be t1 to t4, not t0 to t3?

> + *
> + * with the same ascent/descent rates between the
> + * different depths.
> + *
> + * NOTE: avg_d, max_d and max_t are given constants.
> + * The rest we can/should play around with to get a
> + * good-looking profile.
> + *
> + * That six-point profile gives a total area of:
> + *
> + *   (max_d*max_t) - (max_d*t1) - (max_d-d)*(t4-t3)

Why do you care about the max_d*t1 part? Isn't that just the accent
slope? There should be more factors there if we should care about all
the accent slopes in that formula.

Shouldn't the whole thing look something more like:
(max_d*max_t) - (max_d*t1*0.5) - (max_d-d)*(t3-t2)*0.5 
- (max_d-d)*(t4-t3) - d*(max_t-t4)*0.5 - (max_d-d)*(max_t-t4)

Or if you just throw away all the slopes:
(max_d*max_t) - (max_d-d)*(t4-t3)


The rest looks sane based on those assumptions, but i can't really
figure out why you care about the (max_d*t1) part.


//Anton


> + *
> + * And the "same ascent/descent rates" requirement
> + * gives us (time per depth must be same):
> + *
> + *   t1 / max_d = (t3-t2) / (max_d-d)
> + *   t1 / max_d = (max_t-t4) / d
> + *
> + * We also obviously require:
> + *
> + *   0 <= t1 <= t2 <= t3 <= t4 <= max_t
> + *
> + * Let us call 'd_frac = d / max_d', and we get:
> + *
> + * Total area must match average depth-time:
> + *
> + *   (max_d*max_t) - (max_d*t1) - (max_d-d)*(t4-t3) = avg_d*max_t
> + *      max_d*(max_t-t1-(1-d_frac)*(t4-t3)) = avg_d*max_t
> + *             max_t-t1-(1-d_frac)*(t4-t3) = avg_d*max_t/max_d
> + *                   t1+(1-d_frac)*(t4-t3) = max_t*(1-avg_d/max_d)
> + *
> + * and descent slope must match ascent slopes:
> + *
> + *   t1 / max_d = (t3-t2) / (max_d*(1-d_frac))
> + *           t1 = (t3-t2)/(1-d_frac)
> + *
> + * and
> + *
> + *   t1 / max_d = (max_t-t4) / (max_d*d_frac)
> + *           t1 = (max_t-t4)/d_frac
> + *
> + * In general, we have more free variables than we have constraints,
> + * but we can aim for certain basics, like a good ascent slope.
> + */
> +static int fill_samples(struct sample *s, int max_d, int avg_d, int max_t, double slope, double d_frac)
> +{
> +	double t_frac = max_t*(1-avg_d/(double)max_d);
> +	int t1 = max_d / slope;
> +	int t4 = max_t - t1*d_frac;
> +	int t3 = t4-(t_frac-t1)/(1-d_frac);
> +	int t2 = t3-t1*(1-d_frac);
> +
> +	if (t1 < 0 || t1 > t2 || t2 > t3 || t3 > t4 || t4 > max_t)
> +		return 0;
> +
> +	s[1].time.seconds = t1; s[1].depth.mm = max_d;
> +	s[2].time.seconds = t2; s[2].depth.mm = max_d;
> +	s[3].time.seconds = t3; s[3].depth.mm = max_d * d_frac;
> +	s[4].time.seconds = t4; s[4].depth.mm = max_d * d_frac;
> +
> +	return 1;
> +}
> +
>  struct divecomputer* fake_dc(struct divecomputer* dc)
>  {
> -	static struct sample fake[4];
> +	static struct sample fake[6];
>  	static struct divecomputer fakedc;
> -	static bool initialized = 0;
> -	if (!initialized){
> -		fakedc = (*dc);
> -		fakedc.sample = fake;
> -		fakedc.samples = 4;
> -
> -		/* The dive has no samples, so create a few fake ones.  This assumes an
> -		ascent/descent rate of 9 m/min, which is just below the limit for FAST. */
> -		int duration = dc->duration.seconds;
> -		int maxdepth = dc->maxdepth.mm;
> -		int asc_desc_time = dc->maxdepth.mm*60/9000;
> -		if (asc_desc_time * 2 >= duration)
> -			asc_desc_time = duration / 2;
> -		fake[1].time.seconds = asc_desc_time;
> -		fake[1].depth.mm = maxdepth;
> -		fake[2].time.seconds = duration - asc_desc_time;
> -		fake[2].depth.mm = maxdepth;
> -		fake[3].time.seconds = duration * 1.00;
> -		fakedc.events = dc->events;
> +
> +	fakedc = (*dc);
> +	fakedc.sample = fake;
> +	fakedc.samples = 6;
> +
> +	/* The dive has no samples, so create a few fake ones */
> +	int max_t = dc->duration.seconds;
> +	int max_d = dc->maxdepth.mm;
> +	int avg_d = dc->meandepth.mm;
> +
> +	memset(fake, 0, sizeof(fake));
> +	fake[5].time.seconds = max_t;
> +	if (!max_t || !max_d)
> +		return &fakedc;
> +
> +	/*
> +	 * We want to fake the profile so that the average
> +	 * depth ends up correct. However, in the absense of
> +	 * a reasonable average, let's just make something
> +	 * up. Note that 'avg_d == max_d' is _not_ a reasonable
> +	 * average.
> +	 */
> +	if (avg_d < max_d / 10 || avg_d >= max_d) {
> +		avg_d = (max_d+10000)/3;
> +		if (avg_d > max_d)
> +			avg_d = max_d * 2 / 3;
>  	}
> +	if (!avg_d)
> +		avg_d = 1;
> +
> +	/*
> +	 * Ok, first we try a basic profile with a specific ascent
> +	 * rate (5 meters per minute) and d_frac (1/3).
> +	 */
> +	if (fill_samples(fake, max_d, avg_d, max_t, 5000.0 / 60, 0.33))
> +		return &fakedc;
> +
> +	/*
> +	 * Ok, assume that didn't work because we cannot make the
> +	 * average come out right because it was a quick deep dive
> +	 * followed by a much shallower region
> +	 */
> +	if (fill_samples(fake, max_d, avg_d, max_t, 10000.0 / 60, 0.10))
> +		return &fakedc;
> +
> +	/*
> +	 * Uhhuh. That didn't work. We'd need to find a good combination that
> +	 * satisfies our constraints. Currently, we don't, we just give insane
> +	 * slopes.
> +	 */
> +	if (fill_samples(fake, max_d, avg_d, max_t, 10000.0, 0.01))
> +		return &fakedc;
> +
> +	/* Even that didn't work? Give up, there's something wrong */
>  	return &fakedc;
>  }
> -

-- 
Anton Lundin	+46702-161604


More information about the subsurface mailing list