-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiscale_runner.R
More file actions
1242 lines (1034 loc) · 46.5 KB
/
multiscale_runner.R
File metadata and controls
1242 lines (1034 loc) · 46.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# R Script: Multiscale Runner Performance Model
# AI Chatbots from various companies were used to help
# generate the code shown below.
# 1. Google AI Studio
# --- 0. Load Libraries ---
library(ggplot2)
library(dplyr)
library(tidyr) # For crossing
# --- 1. Define Runner Parameters (Input Factors) ---
# Physiology
vo2max_ml_kg_min <- 60 # VO2max in ml/kg/min (Good amateur male)
lt1_perc_vo2max <- 65 # LT1 as % of VO2max (Aerobic Threshold)
lt2_perc_vo2max <- 85 # LT2 as % of VO2max (Anaerobic Threshold / MLSS approximation)
base_re_ml_kg_km <- 210 # Base Running Economy in ml/kg/km (Average)
# Biomechanics (Micro -> Meso influence on RE)
# Factor < 1.0 implies better than average efficiency
# Factor > 1.0 implies worse than average efficiency
biomechanics_factor <- 0.98 # Slightly efficient biomechanics
# Equipment (Micro -> Meso influence on RE)
# Factor < 1.0 implies efficient equipment (e.g., super shoes)
# Factor > 1.0 implies less efficient equipment (e.g., heavy trainers)
equipment_factor <- 0.97 # Efficient shoes
# Nutrition (Micro -> Macro influence on performance sustainability)
# Factor > 1.0 implies optimal fueling for the effort
# Factor < 1.0 implies suboptimal fueling
nutrition_factor <- 1.01 # Well-fueled
# --- 2. Define Training Zones based on %VO2max ---
# Common 5-zone model (approximations)
zone_defs <- list(
"Zone 1 (Recovery)" = c(0, 55),
"Zone 2 (Aerobic)" = c(55, 75), # Often overlaps LT1
"Zone 3 (Tempo)" = c(75, 88), # Often spans LT1 to LT2
"Zone 4 (Threshold)"= c(88, 95), # Around or just above LT2
"Zone 5 (VO2max)" = c(95, 110) # Approaching and at VO2max (includes anaerobic)
)
# --- 3. Model Calculations ---
# Create a sequence of effort levels
effort_perc_vo2max <- seq(40, 105, by = 1) # From 40% to 105% VO2max
# Calculate Adjusted Running Economy
adjusted_re <- base_re_ml_kg_km * biomechanics_factor * equipment_factor
# Calculate performance across effort levels
performance_data <- data.frame(Effort_perc_VO2max = effort_perc_vo2max) %>%
mutate(
VO2_ml_kg_min = vo2max_ml_kg_min * (Effort_perc_VO2max / 100),
# Theoretical aerobic velocity based on O2 consumption and economy
Velocity_km_min = VO2_ml_kg_min / adjusted_re,
# Convert to m/s for potential other uses (Velocity_m_s = Velocity_km_min * 1000 / 60)
Velocity_m_s = Velocity_km_min * 1000 / 60,
# Calculate Pace in min/km
Pace_min_km_raw = 1 / Velocity_km_min,
# Apply Nutrition Factor (simplistic application)
Pace_min_km = Pace_min_km_raw / nutrition_factor,
# Assign Training Zone
Zone = cut(Effort_perc_VO2max,
breaks = c(zone_defs[[1]][1], zone_defs[[1]][2], zone_defs[[2]][2], zone_defs[[3]][2], zone_defs[[4]][2], zone_defs[[5]][2]),
labels = names(zone_defs),
right = FALSE, # Intervals are [min, max)
include.lowest = TRUE)
)
# --- 4. Calculate Key Threshold Points ---
# Function to find the pace for a given %VO2max
get_pace_at_perc_vo2max <- function(perc) {
vo2_at_perc <- vo2max_ml_kg_min * (perc / 100)
vel_km_min_at_perc <- vo2_at_perc / adjusted_re
pace_raw <- 1 / vel_km_min_at_perc
pace_final <- pace_raw / nutrition_factor
return(pace_final)
}
lt1_pace <- get_pace_at_perc_vo2max(lt1_perc_vo2max)
lt2_pace <- get_pace_at_perc_vo2max(lt2_perc_vo2max)
vo2max_pace <- get_pace_at_perc_vo2max(100) # Theoretical pace at 100% VO2max
threshold_points <- data.frame(
Threshold = factor(c("LT1", "LT2", "VO2max"), levels = c("LT1", "LT2", "VO2max")),
Effort_perc_VO2max = c(lt1_perc_vo2max, lt2_perc_vo2max, 100),
Pace_min_km = c(lt1_pace, lt2_pace, vo2max_pace)
)
# --- 5. Create Zone Rectangles for Plotting ---
zone_rects <- data.frame(
Zone = factor(names(zone_defs), levels = names(zone_defs)),
xmin = sapply(zone_defs, function(x) x[1]),
xmax = sapply(zone_defs, function(x) x[2]),
ymin = -Inf,
ymax = Inf
)
# Adjust xmax slightly for better visual separation if needed
zone_rects$xmax[1:4] <- zone_rects$xmax[1:4] - 0.1
# Define nice colors for zones
zone_colors <- c(
"Zone 1 (Recovery)" = "#add8e6", # Light Blue
"Zone 2 (Aerobic)" = "#90ee90", # Light Green
"Zone 3 (Tempo)" = "#ffffb3", # Light Yellow
"Zone 4 (Threshold)"= "#ffcc80", # Light Orange
"Zone 5 (VO2max)" = "#ff9999" # Light Red
)
# --- 6. Generate the Plot with ggplot2 ---
plot_title <- "Multiscale Runner Model: Pace vs. Effort (%VO2max)"
plot_subtitle <- paste0(
"VO2max: ", vo2max_ml_kg_min, " ml/kg/min | ",
"Adj. RE: ", round(adjusted_re, 1), " ml/kg/km | ",
"LT1: ", lt1_perc_vo2max, "% | ",
"LT2: ", lt2_perc_vo2max, "%"
)
plot_caption <- paste0(
"Model incorporates physiology (VO2max, LTs), running economy (adjusted by biomechanics factor: ",
biomechanics_factor, ", equipment factor: ", equipment_factor,
"), and nutrition factor: ", nutrition_factor, ".\nPace above LT2 is typically sustainable for limited durations only."
)
# Function to format pace (min:sec)
format_pace <- function(pace_decimal) {
minutes <- floor(pace_decimal)
seconds <- round((pace_decimal - minutes) * 60)
sprintf("%d:%02d", minutes, seconds)
}
# Generate pace labels for y-axis
y_breaks <- pretty(performance_data$Pace_min_km, n = 8)
y_labels <- format_pace(y_breaks)
p <- ggplot(performance_data, aes(x = Effort_perc_VO2max, y = Pace_min_km)) +
# Zone Rectangles (behind other layers)
geom_rect(data = zone_rects, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = Zone),
inherit.aes = FALSE, alpha = 0.3) +
# Main Pace Curve
geom_line(aes(color = "Modeled Pace"), linewidth = 1.2) +
# Threshold Vertical Lines
geom_vline(xintercept = lt1_perc_vo2max, linetype = "dashed", color = "darkblue", linewidth = 0.8) +
geom_vline(xintercept = lt2_perc_vo2max, linetype = "dashed", color = "darkred", linewidth = 0.8) +
# Threshold Horizontal Lines
geom_hline(yintercept = lt1_pace, linetype = "dotted", color = "darkblue", linewidth = 0.8) +
geom_hline(yintercept = lt2_pace, linetype = "dotted", color = "darkred", linewidth = 0.8) +
# Points for Thresholds
geom_point(data = threshold_points, aes(color = Threshold), size = 4, shape = 18) +
# Threshold Text Labels (slightly offset)
annotate("text", x = lt1_perc_vo2max + 1, y = max(performance_data$Pace_min_km) * 0.98, # Adjust y position
label = paste("LT1\n", format_pace(lt1_pace), "/km"), hjust = 0, vjust = 1, size = 3.5, color = "darkblue") +
annotate("text", x = lt2_perc_vo2max + 1, y = max(performance_data$Pace_min_km) * 0.98, # Adjust y position
label = paste("LT2\n", format_pace(lt2_pace), "/km"), hjust = 0, vjust = 1, size = 3.5, color = "darkred") +
annotate("text", x = 100 + 1, y = max(performance_data$Pace_min_km) * 0.98, # Adjust y position
label = paste("VO2max\n", format_pace(vo2max_pace), "/km"), hjust = 0, vjust = 1, size = 3.5, color = "black") +
# --- Scales and Labels ---
scale_y_reverse(name = "Sustainable Pace (min:sec / km)", breaks = y_breaks, labels = y_labels) + # Reverse Y-axis for Pace
scale_x_continuous(name = "Effort (% VO2max)", breaks = seq(40, 110, 10), limits = c(40, 105)) +
scale_fill_manual(values = zone_colors, name = "Training Zone") +
scale_color_manual(name = "Key Points",
values = c("Modeled Pace" = "black", "LT1" = "blue", "LT2" = "red", "VO2max" = "purple"),
guide = guide_legend(override.aes = list(linetype = c("solid", "blank", "blank", "blank"),
shape = c(NA, 18, 18, 18)))) +
labs(
title = plot_title,
subtitle = plot_subtitle,
caption = plot_caption
) +
# --- Theme ---
theme_minimal(base_size = 14) +
theme(
plot.title = element_text(face = "bold", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5, size = 10),
plot.caption = element_text(hjust = 0, size = 9, face = "italic"),
legend.position = "bottom",
legend.box = "horizontal",
axis.title = element_text(face = "bold"),
panel.grid.major = element_line(color = "grey85"),
panel.grid.minor = element_line(color = "grey90")
) +
guides(fill = guide_legend(nrow = 1), color = guide_legend(nrow = 1))
# --- 7. Print the Plot ---
print(p)
ggsave("model_7_multiscale_runner/runner_1.png", width = 12, height = 8, dpi = 300)
# --- 8. Display Key Values (Optional) ---
cat("\n--- Model Parameters & Key Results ---\n")
cat("VO2max:", vo2max_ml_kg_min, "ml/kg/min\n")
cat("Base RE:", base_re_ml_kg_km, "ml/kg/km\n")
cat("Biomechanics Factor:", biomechanics_factor, "\n")
cat("Equipment Factor:", equipment_factor, "\n")
cat("Nutrition Factor:", nutrition_factor, "\n")
cat("Adjusted RE:", round(adjusted_re, 2), "ml/kg/km\n")
cat("\nThresholds:\n")
cat("LT1:", lt1_perc_vo2max, "% VO2max -> Pace:", format_pace(lt1_pace), "/km\n")
cat("LT2:", lt2_perc_vo2max, "% VO2max -> Pace:", format_pace(lt2_pace), "/km\n")
cat("VO2max (100%): Pace:", format_pace(vo2max_pace), "/km\n")
# 2. Gemini
# Install necessary packages if you don't have them
# install.packages("tidyverse") # Includes ggplot2 and dplyr
library(tidyverse)
# --- Model Parameters (Representing Runner's State) ---
# Base parameters for a hypothetical runner (Scenario 1)
runner_params_1 <- list(
VO2max = 50, # ml/min/kg (Higher = better aerobic capacity) - Used conceptually, less directly in simple model equations
Lactate_Threshold_Pace_min_km = 5.5, # min/km (Sustainable pace before rapid fatigue)
Running_Economy_kJ_per_km_kg = 4, # kJ/km/kg (Lower = more efficient) - Influenced by Biomechanics, Equipment
Initial_Energy_kJ_per_kg = 25, # kJ/kg (Starting glycogen stores) - Influenced by Nutrition
Fatigue_Resistance = 1.0, # Unitless factor (Higher = resists fatigue better) - Influenced by Training Zones
Energy_Storage_Capacity_kJ_per_kg = 30 # kJ/kg (Max energy storage) - Influenced by Training, Genetics
)
# Parameters for a potentially improved runner (Scenario 2)
# Example: Better Nutrition (higher initial energy), Better Equipment (better economy), Better Training (higher LT pace, better resistance)
runner_params_2 <- list(
VO2max = 55, # Improved VO2max
Lactate_Threshold_Pace_min_km = 5.0, # Faster LT Pace
Running_Economy_kJ_per_km_kg = 3.8, # Better Economy
Initial_Energy_kJ_per_kg = 35, # Higher Initial Energy (Nutrition)
Fatigue_Resistance = 1.2, # Improved Fatigue Resistance (Training)
Energy_Storage_Capacity_kJ_per_kg = 35 # Slightly improved capacity
)
# --- Simulation Settings ---
body_weight_kg <- 70 # kg
total_duration_minutes <- 120 # minutes (Simulate a 2-hour run)
time_step_seconds <- 5 # seconds
total_steps <- (total_duration_minutes * 60) / time_step_seconds
# Model Constants (Simplified relationships)
# These translate the theoretical concepts into simulation effects
FATIGUE_ACCUM_RATE_BASE = 0.0005 # Base fatigue increase per second at threshold pace
FATIGUE_EFFORT_POWER = 3 # Power to which (Pace / LT_Pace) is raised for fatigue accumulation (non-linear increase)
FATIGUE_REDUCTION_FACTOR = 0.8 # How much max fatigue can reduce pace (0 = no effect, 1 = pace -> 0)
ENERGY_REDUCTION_FACTOR = 1.0 # How much full depletion can reduce pace (0 = no effect, 1 = pace -> 0)
ENERGY_CONVERSION_FACTOR = 1 # Placeholder - links kJ/kg expenditure to pace/economy
# --- Helper Function to convert pace ---
min_km_to_m_per_s <- function(pace_min_km) {
return(1000 / (pace_min_km * 60))
}
m_per_s_to_min_km <- function(pace_m_per_s) {
if (pace_m_per_s <= 0) return(NA) # Avoid division by zero or negative pace
return((1000 / pace_m_per_s) / 60)
}
# --- Simulation Function ---
run_simulation <- function(params, body_weight_kg, total_steps, time_step_seconds) {
# Convert key paces to internal units (m/s)
lt_pace_mps <- min_km_to_m_per_s(params$Lactate_Threshold_Pace_min_km)
# Let's set the base target pace equal to LT pace for simplicity, deviations occur from here
base_target_pace_mps <- lt_pace_mps
# Initialize state variables
current_fatigue <- 0 # Scaled 0 to 1, or conceptually 0 to Max_Possible_Fatigue
# Let's make max fatigue accumulation reach 1 over a very long time at high effort
MAX_POSSIBLE_FATIGUE = 1 # Represents complete exhaustion
current_energy_kJ <- params$Initial_Energy_kJ_per_kg * body_weight_kg
max_energy_kJ <- params$Energy_Storage_Capacity_kJ_per_kg * body_weight_kg
current_distance_m <- 0
current_time_seconds <- 0
current_pace_mps <- base_target_pace_mps # Start at target pace
# Store results
results <- data.frame(
Time_s = 0,
Distance_m = 0,
Pace_min_km = m_per_s_to_min_km(current_pace_mps),
Fatigue = current_fatigue,
Energy_kJ = current_energy_kJ
)
# Simulation loop
for (i in 1:total_steps) {
current_time_seconds <- current_time_seconds + time_step_seconds
# --- Update State Variables ---
# 1. Calculate Energy Depletion
# Energy cost per meter: (kJ/km/kg * kg / 1000 m/km) * ConversionFactor
energy_cost_per_meter <- (params$Running_Economy_kJ_per_km_kg * body_weight_kg / 1000) * ENERGY_CONVERSION_FACTOR
energy_expended_in_step_kJ <- current_pace_mps * time_step_seconds * energy_cost_per_meter
current_energy_kJ <- current_energy_kJ - energy_expended_in_step_kJ
# Cap energy at 0
current_energy_kJ <- max(0, current_energy_kJ)
# 2. Calculate Fatigue Accumulation
# Fatigue increases faster the harder the effort relative to LT pace
# Effort factor: How much harder than LT pace? (Current_Pace / LT_Pace)
effort_factor <- max(1, current_pace_mps / lt_pace_mps) # Effort is >= 1 relative to LT pace
fatigue_increase_rate <- FATIGUE_ACCUM_RATE_BASE * (effort_factor ^ FATIGUE_EFFORT_POWER) / params$Fatigue_Resistance
fatigue_increase_in_step <- fatigue_increase_rate * time_step_seconds
current_fatigue <- current_fatigue + fatigue_increase_in_step
# Cap fatigue at max possible
current_fatigue <- min(MAX_POSSIBLE_FATIGUE, current_fatigue)
# --- Calculate Pace for Next Step (Based on current state) ---
# Scaled fatigue (0 to 1)
scaled_fatigue <- current_fatigue / MAX_POSSIBLE_FATIGUE
# Scaled energy depletion (0 to 1) - 0 means full, 1 means empty
scaled_energy_depletion <- 1 - (current_energy_kJ / max_energy_kJ) # Use max_energy_kJ here
# Calculate pace reduction factors
fatigue_reduction <- FATIGUE_REDUCTION_FACTOR * scaled_fatigue
energy_reduction <- ENERGY_REDUCTION_FACTOR * scaled_energy_depletion
# Ensure reductions don't cause negative pace conceptually, apply multiplicatively
# Pace = Base * (1 - reduction_fatigue) * (1 - reduction_energy)
# Add a small minimum multiplier to avoid exactly 0 pace early on
pace_multiplier <- max(0.1, (1 - fatigue_reduction) * (1 - energy_reduction))
current_pace_mps <- base_target_pace_mps * pace_multiplier
# --- Update Distance ---
# Use the pace from the *previous* step to calculate distance covered *in* the current step
# This is standard practice in discrete simulations
distance_covered_in_step_m <- current_pace_mps * time_step_seconds
current_distance_m <- current_distance_m + distance_covered_in_step_m
# Store results for this step
results <- bind_rows(results, data.frame(
Time_s = current_time_seconds,
Distance_m = current_distance_m,
Pace_min_km = m_per_s_to_min_km(current_pace_mps),
Fatigue = current_fatigue,
Energy_kJ = current_energy_kJ
))
}
return(results)
}
# --- Run Simulations ---
# Run simulation for Scenario 1
sim_results_1 <- run_simulation(runner_params_1, body_weight_kg, total_steps, time_step_seconds) %>%
mutate(Scenario = "Scenario 1: Baseline Runner")
# Run simulation for Scenario 2
sim_results_2 <- run_simulation(runner_params_2, body_weight_kg, total_steps, time_step_seconds) %>%
mutate(Scenario = "Scenario 2: Improved Runner (Training, Nutrition, Equipment)")
# Combine results for plotting
all_sim_results <- bind_rows(sim_results_1, sim_results_2)
# --- Plotting with ggplot2 ---
# Convert Distance_m to km for better plot axis
all_sim_results <- all_sim_results %>%
mutate(Distance_km = Distance_m / 1000)
# Plot Pace vs. Distance
pace_plot <- ggplot(all_sim_results, aes(x = Distance_km, y = Pace_min_km, color = Scenario)) +
geom_line(linewidth = 1) +
labs(
title = "Simulated Runner Pace Over Distance",
subtitle = "Comparing Baseline vs. Improved Runner Scenario",
x = "Distance (km)",
y = "Pace (min/km)",
color = "Runner Type"
) +
scale_y_reverse() + # Pace is lower (faster) at the bottom
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "bottom"
)
print(pace_plot)
ggsave("model_7_multiscale_runner/runner_2a.png", width = 12, height = 8, dpi = 300)
# Optional: Plot Fatigue vs. Distance
fatigue_plot <- ggplot(all_sim_results, aes(x = Distance_km, y = Fatigue, color = Scenario)) +
geom_line(linewidth = 1) +
labs(
title = "Simulated Fatigue Level Over Distance",
subtitle = "Comparing Baseline vs. Improved Runner Scenario",
x = "Distance (km)",
y = "Fatigue Level (Scaled 0-1)",
color = "Runner Type"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "bottom"
)
print(fatigue_plot)
ggsave("model_7_multiscale_runner/runner_2b.png", width = 12, height = 8, dpi = 300)
# Optional: Plot Energy vs. Distance
energy_plot <- ggplot(all_sim_results, aes(x = Distance_km, y = Energy_kJ, color = Scenario)) +
geom_line(linewidth = 1) +
labs(
title = "Simulated Energy (Glycogen) Stores Over Distance",
subtitle = "Comparing Baseline vs. Improved Runner Scenario",
x = "Distance (km)",
y = "Energy Remaining (kJ)",
color = "Runner Type"
) +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "bottom"
)
print(energy_plot)
ggsave("model_7_multiscale_runner/runner_2c.png", width = 12, height = 8, dpi = 300)
# 3. DeepSeek
# Load required libraries
library(ggplot2)
library(deSolve)
library(tidyverse)
library(patchwork)
library(viridis)
# Set seed for reproducibility
set.seed(123)
# ---- Multiscale Runner Model ----
# Model parameters
runner_parameters <- list(
# Physiological parameters
VO2max = 65, # ml/kg/min - maximal oxygen uptake
LT1 = 0.6, # Lactate threshold 1 (%VO2max)
LT2 = 0.85, # Lactate threshold 2 (%VO2max)
running_economy = 200, # ml/kg/km - lower is better
body_mass = 68, # kg
fat_percentage = 0.15, # body fat percentage
muscle_mass = 30, # kg
# Biomechanical parameters
stride_length = 1.5, # m
cadence = 180, # steps/min
ground_contact_time = 220, # ms
vertical_oscillation = 8.5, # cm
# Training parameters
training_load = 60, # arbitrary units
recovery_status = 0.9, # 0-1 scale (1 = fully recovered)
# Equipment parameters
shoe_efficiency = 0.97, # energy return (0-1)
apparel_drag = 0.95, # drag coefficient (lower is better)
# Nutritional parameters
glycogen_stores = 0.85, # 0-1 scale
hydration_status = 0.92, # 0-1 scale
carb_intake = 6 # g/kg/day
)
# Time parameters
simulation_time <- seq(0, 120, by = 1) # 120 minutes of running
# Differential equation model of runner physiology
runner_model <- function(time, state, parameters) {
with(as.list(c(state, parameters)), {
# Calculate current intensity (%VO2max)
if (time < 10) {
intensity <- 0.5 # warm-up
} else if (time < 30) {
intensity <- 0.7 # steady
} else if (time < 50) {
intensity <- 0.85 # threshold
} else if (time < 70) {
intensity <- 0.95 # VO2max
} else if (time < 90) {
intensity <- 0.65 # recovery
} else {
intensity <- 0.8 # final push
}
# Adjust for glycogen depletion
intensity_adj <- intensity * glycogen_stores * hydration_status * recovery_status
# Energy expenditure calculations
vo2 <- VO2max * intensity_adj
energy_expenditure <- vo2 * body_mass * 0.005 * (1 + (1 - shoe_efficiency))
# Fatigue calculations
fatigue_rate <- ifelse(intensity_adj > LT2, 0.015,
ifelse(intensity_adj > LT1, 0.008, 0.003))
# Glycogen depletion
glycogen_depletion <- ifelse(intensity_adj > LT1,
0.002 * intensity_adj^2,
0.001 * intensity_adj)
# Hydration loss
hydration_loss <- 0.0015 * intensity_adj * (1 - hydration_status)
# Lactate accumulation
lactate_prod <- ifelse(intensity_adj > LT2, 0.02,
ifelse(intensity_adj > LT1, 0.01, -0.005))
# Biomechanical efficiency
biomech_eff <- 0.9 + 0.1 * (1 - (ground_contact_time - 200)/100) *
(1 - (vertical_oscillation - 6)/10)
# Differential equations
dglycogen <- -glycogen_depletion
dhydration <- -hydration_loss
dfatigue <- fatigue_rate
dlactate <- lactate_prod - 0.01 * lactate # lactate clearance
# Performance metric
performance <- (vo2 / running_economy) * biomech_eff *
(1 - 0.5 * fatigue) * shoe_efficiency
# Return the rates of change
list(c(dglycogen, dhydration, dfatigue, dlactate),
performance = performance,
intensity = intensity_adj,
vo2_utilization = vo2,
biomech_eff = biomech_eff)
})
}
# Initial state
initial_state <- c(
glycogen = runner_parameters$glycogen_stores,
hydration = runner_parameters$hydration_status,
fatigue = 0,
lactate = 0.5
)
# Run simulation
output <- ode(y = initial_state, times = simulation_time, func = runner_model,
parms = runner_parameters) %>%
as.data.frame()
# Convert to tidy format for plotting
output_long <- output %>%
as_tibble() %>%
pivot_longer(cols = -time, names_to = "variable", values_to = "value") %>%
mutate(variable_type = case_when(
variable %in% c("glycogen", "hydration", "fatigue", "lactate") ~ "Physiological State",
variable %in% c("performance", "biomech_eff") ~ "Performance Metrics",
variable %in% c("intensity", "vo2_utilization") ~ "Exercise Intensity",
TRUE ~ "Other"
))
# ---- Create Plots ----
# Color palette
perf_palette <- viridis(6)
# 1. Physiological State Plot
physio_plot <- output_long %>%
filter(variable_type == "Physiological State") %>%
ggplot(aes(x = time, y = value, color = variable)) +
geom_line(size = 1.2) +
scale_color_manual(values = perf_palette[1:4],
labels = c("Fatigue", "Glycogen", "Hydration", "Lactate")) +
labs(title = "Physiological State During Run",
x = "Time (minutes)",
y = "Relative Level",
color = "Variable") +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom",
plot.background = element_rect(fill = "white", color = NA),
panel.grid.minor = element_blank())
# 2. Performance Metrics Plot
perf_plot <- output_long %>%
filter(variable_type == "Performance Metrics") %>%
ggplot(aes(x = time, y = value, color = variable)) +
geom_line(size = 1.2) +
scale_color_manual(values = perf_palette[5:6],
labels = c("Biomechanical Efficiency", "Performance")) +
labs(title = "Performance Metrics",
x = "Time (minutes)",
y = "Relative Performance",
color = "Metric") +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom",
plot.background = element_rect(fill = "white", color = NA),
panel.grid.minor = element_blank())
# 3. Exercise Intensity Plot
intensity_plot <- output_long %>%
filter(variable_type == "Exercise Intensity") %>%
ggplot(aes(x = time, y = value, color = variable)) +
geom_line(size = 1.2) +
geom_hline(yintercept = runner_parameters$LT1 * runner_parameters$VO2max,
linetype = "dashed", color = "gray50") +
geom_hline(yintercept = runner_parameters$LT2 * runner_parameters$VO2max,
linetype = "dashed", color = "gray50") +
annotate("text", x = 10, y = runner_parameters$LT1 * runner_parameters$VO2max + 2,
label = "LT1", color = "gray30") +
annotate("text", x = 10, y = runner_parameters$LT2 * runner_parameters$VO2max + 2,
label = "LT2", color = "gray30") +
scale_color_manual(values = perf_palette[c(2,4)],
labels = c("Exercise Intensity", "VO2 Utilization")) +
labs(title = "Exercise Intensity and VO2 Utilization",
x = "Time (minutes)",
y = "VO2 (ml/kg/min)",
color = "Variable") +
theme_minimal(base_size = 12) +
theme(legend.position = "bottom",
plot.background = element_rect(fill = "white", color = NA),
panel.grid.minor = element_blank())
# Combine plots
combined_plot <- (physio_plot / perf_plot / intensity_plot) +
plot_annotation(title = "Multiscale Runner Performance Model",
subtitle = "Integration of Biomechanical, Physiological, and Training Factors",
theme = theme(plot.title = element_text(size = 16, face = "bold"),
plot.subtitle = element_text(size = 12)))
# Display the plot
print(combined_plot)
# Save the plot
ggsave("model_7_multiscale_runner/runner_3.png", width = 12, height = 12, dpi = 300)
# 4. Perplexity
# Load libraries
library(ggplot2)
library(dplyr)
# Define runner parameters
runner <- list(
VO2max = 70, # ml/kg/min
lactate_threshold_pct = 0.85, # Fraction of VO2max at LT
running_economy = 200, # ml O2/kg/km
stride_length = 1.2, # meters
ground_contact_time = 0.22, # seconds
shoe_mass = 250, # grams
carb_availability = 1.0 # 0-1 scale
)
# Model equations
# Sustainable speed at LT (km/h)
speed_LT <- function(VO2max, LT_pct, RE) {
# VO2 at LT (ml/kg/min)
VO2_LT <- VO2max * LT_pct
# Speed at LT (km/h)
speed <- (VO2_LT / RE) * 60
return(speed)
}
# Equipment effect on economy (simplified): +1% O2 cost per 100g extra shoe mass
adjusted_economy <- function(RE, shoe_mass) {
RE * (1 + 0.01 * ((shoe_mass - 200) / 100))
}
# Nutrition effect: Reduces sustainable speed if carbs <1
nutrition_factor <- function(speed, carb_avail) {
speed * carb_avail
}
# Calculate adjusted running economy
RE_adj <- adjusted_economy(runner$running_economy, runner$shoe_mass)
# Calculate sustainable speed
v_LT <- speed_LT(runner$VO2max, runner$lactate_threshold_pct, RE_adj)
v_LT_nutrition <- nutrition_factor(v_LT, runner$carb_availability)
# Predict marathon time (km/h to hours for 42.195 km)
marathon_time_hr <- 42.195 / v_LT_nutrition
# Simulate effect of varying one parameter (e.g., VO2max)
VO2_seq <- seq(55, 85, by=2)
sim_data <- data.frame(
VO2max = VO2_seq,
Speed = sapply(VO2_seq, function(vo2) {
v <- speed_LT(vo2, runner$lactate_threshold_pct, RE_adj)
nutrition_factor(v, runner$carb_availability)
})
)
sim_data$MarathonTime <- 42.195 / sim_data$Speed
# Plot
ggplot(sim_data, aes(x=VO2max, y=MarathonTime)) +
geom_line(size=1.2, color="#2C3E50") +
geom_point(size=2, color="#E74C3C") +
labs(
title="Predicted Marathon Time vs VO2max",
x="VO2max (ml/kg/min)",
y="Marathon Time (hours)"
) +
theme_minimal(base_size = 15)
ggsave("model_7_multiscale_runner/runner_4.png", width = 9, height = 6, dpi = 300)
# 5. ChatGPT
# a. Setup
# Install packages if not already installed
#install.packages(c("ggplot2", "dplyr"))
# Load libraries
library(ggplot2)
library(dplyr)
# b. Define the Vₘₐₓ Prediction Function
# Function to predict Vmax
predict_vmax <- function(fat_pct, hr_2vt, tmc) {
vmax <- 58.632 - 0.183 * fat_pct - 0.507 * hr_2vt + 7.959 * tmc
return(vmax)
}
# c. Simulate Runner Data
# Sample data for 10 runners
set.seed(123) # For reproducibility
runner_data <- data.frame(
Runner = paste0("Runner_", 1:10),
Fat_Percentage = runif(10, 10, 25), # Fat percentage between 10% and 25%
HR_2VT = runif(10, 70, 85), # HR at 2VT between 70% and 85% of HRmax
TMC = runif(10, 0.9, 1.2) # Theoretical metabolic cost between 0.9 and 1.2
)
# Calculate Vmax for each runner
runner_data <- runner_data %>%
rowwise() %>%
mutate(Vmax = predict_vmax(Fat_Percentage, HR_2VT, TMC)) %>%
ungroup()
# d. Visualize the Results
# Plotting
ggplot(runner_data, aes(x = Fat_Percentage, y = Vmax, color = HR_2VT, size = TMC)) +
geom_point(alpha = 0.7) +
scale_color_gradient(low = "blue", high = "red") +
labs(
title = "Predicted Vmax Based on Runner Characteristics",
x = "Fat Percentage (%)",
y = "Vmax (km/h)",
color = "HR at 2VT (%)",
size = "Theoretical Metabolic Cost"
) +
theme_minimal()
ggsave("model_7_multiscale_runner/runner_5.png", width = 9, height = 6, dpi = 300)
# 6. Claude
# Runner Multiscale Model
# Incorporating biomechanics, physiology, training zones, VO2max, lactate thresholds,
# running economy, nutrition, and equipment effects
# Load required libraries
library(tidyverse)
library(patchwork) # For combining plots
# Set random seed for reproducibility
set.seed(42)
#=============================================
# 1. Model Parameters and Simulation Settings
#=============================================
# Simulation duration (in weeks)
weeks <- 24
days <- weeks * 7
# Create date sequence - making sure the length matches days
dates <- seq.Date(from = Sys.Date() - days + 1, to = Sys.Date(), by = "day")
# Verify that the length of dates matches days
stopifnot(length(dates) == days)
# Base runner characteristics
base_params <- list(
# Physiological parameters
vo2max_base = 45, # Starting VO2max (ml/kg/min)
vo2max_potential = 60, # Maximum potential VO2max
lactate_threshold_pct = 0.7, # Initial lactate threshold (% of VO2max)
running_economy_base = 210, # Initial running economy (ml/kg/km)
# Biomechanical parameters
stride_length_base = 1.2, # Initial stride length (m)
ground_contact_time = 0.25, # Ground contact time (s)
vertical_oscillation = 8, # Vertical oscillation (cm)
# Training adaptation parameters
recovery_rate = 0.92, # Recovery rate after training
adaptation_rate = 0.03, # Rate of positive adaptation
detraining_rate = 0.005, # Rate of fitness loss during rest
# Nutrition parameters
carb_loading = 1, # Carbohydrate loading effect (multiplier)
hydration = 1, # Hydration effect (multiplier)
# Equipment parameters
shoe_efficiency = 1, # Shoe efficiency multiplier
body_weight = 70, # Runner's body weight (kg)
# Environmental parameters
temperature = 20, # Default temperature (Celsius)
altitude = 0 # Default altitude (meters)
)
#=============================================
# 2. Training Zones Definition
#=============================================
training_zones <- data.frame(
zone = c(1, 2, 3, 4, 5),
name = c("Recovery", "Endurance", "Tempo", "Threshold", "VO2max"),
intensity_low = c(0.60, 0.70, 0.80, 0.87, 0.95),
intensity_high = c(0.70, 0.80, 0.87, 0.95, 1.0),
stimulus_factor = c(0.5, 1.0, 1.5, 2.0, 2.5),
fatigue_factor = c(0.4, 0.8, 1.3, 1.8, 2.3)
)
#=============================================
# 3. Training Plan Generation
#=============================================
# Function to generate a somewhat realistic training plan
generate_training_plan <- function(days, dates) {
# Create data frame for each day
training_plan <- data.frame(
day = 1:days,
date = dates,
weekday = weekdays(dates),
week = ceiling(1:days / 7)
)
# Base distance distribution
training_plan$distance_base <- 0
# Generate three mesocycles with different focuses
for (w in 1:weeks) {
week_idx <- which(training_plan$week == w)
if (w <= 8) {
# Base building phase
phase_multiplier <- 0.8 + (w / 8) * 0.2
} else if (w <= 16) {
# Intensity phase
phase_multiplier <- 1.0 + ((w - 8) / 8) * 0.2
} else {
# Taper and peak phase
phase_multiplier <- 1.2 * (1 - (w - 16) / 8 * 0.3)
}
# Apply weekly structure
for (d in week_idx) {
weekday <- training_plan$weekday[d]
if (weekday == "Monday") {
# Recovery
training_plan$distance_base[d] <- 5 * phase_multiplier
training_plan$zone[d] <- 1
} else if (weekday == "Tuesday") {
# Workout
training_plan$distance_base[d] <- 8 * phase_multiplier
training_plan$zone[d] <- ifelse(w <= 8, 3, ifelse(w <= 16, 4, 3))
} else if (weekday == "Wednesday") {
# Easy
training_plan$distance_base[d] <- 6 * phase_multiplier
training_plan$zone[d] <- 2
} else if (weekday == "Thursday") {
# Workout
training_plan$distance_base[d] <- 8 * phase_multiplier
training_plan$zone[d] <- ifelse(w <= 8, 2, ifelse(w <= 16, 5, 4))
} else if (weekday == "Friday") {
# Recovery or rest
training_plan$distance_base[d] <- 4 * phase_multiplier
training_plan$zone[d] <- 1
} else if (weekday == "Saturday") {
# Long run
training_plan$distance_base[d] <- 12 * phase_multiplier
training_plan$zone[d] <- 2
} else if (weekday == "Sunday") {
# Rest or easy
if (w %% 4 == 0) { # Rest every 4th week
training_plan$distance_base[d] <- 0
training_plan$zone[d] <- 0
} else {
training_plan$distance_base[d] <- 6 * phase_multiplier
training_plan$zone[d] <- 1
}
}
}
}
# Add random variations to distances (+-10%)
training_plan$distance <- pmax(0, training_plan$distance_base *
(1 + rnorm(days, 0, 0.1)))
# Set zone intensity and names
training_plan$zone_name <- "Rest"
training_plan$intensity <- 0
training_plan$stimulus <- 0
training_plan$fatigue <- 0
for (i in 1:nrow(training_zones)) {
zone_idx <- which(training_plan$zone == training_zones$zone[i])
training_plan$zone_name[zone_idx] <- as.character(training_zones$name[i])
# Randomize intensity within zone boundaries
for (d in zone_idx) {
low <- training_zones$intensity_low[i]
high <- training_zones$intensity_high[i]
training_plan$intensity[d] <- runif(1, low, high)
training_plan$stimulus[d] <- training_plan$distance[d] *
training_zones$stimulus_factor[i]
training_plan$fatigue[d] <- training_plan$distance[d] *
training_zones$fatigue_factor[i]
}
}
# Adding workout descriptions
training_plan$workout_description <- ""
for (d in 1:nrow(training_plan)) {
if (training_plan$zone[d] == 0) {
training_plan$workout_description[d] <- "Rest day"
} else if (training_plan$zone[d] == 1) {
training_plan$workout_description[d] <- paste0(round(training_plan$distance[d], 1),
"km easy recovery run")
} else if (training_plan$zone[d] == 2) {
if (training_plan$weekday[d] == "Saturday") {
training_plan$workout_description[d] <- paste0(round(training_plan$distance[d], 1),
"km long run")
} else {
training_plan$workout_description[d] <- paste0(round(training_plan$distance[d], 1),
"km endurance run")
}
} else if (training_plan$zone[d] == 3) {
training_plan$workout_description[d] <- paste0(round(training_plan$distance[d] * 0.3, 1),
"km warmup + ",
round(training_plan$distance[d] * 0.5, 1),
"km tempo + ",
round(training_plan$distance[d] * 0.2, 1),
"km cooldown")
} else if (training_plan$zone[d] == 4) {
training_plan$workout_description[d] <- paste0(round(training_plan$distance[d] * 0.25, 1),
"km warmup + 6x1km @threshold w/2min rest + ",
round(training_plan$distance[d] * 0.25, 1),
"km cooldown")
} else if (training_plan$zone[d] == 5) {
training_plan$workout_description[d] <- paste0(round(training_plan$distance[d] * 0.25, 1),
"km warmup + 8x400m @VO2max w/90sec rest + ",
round(training_plan$distance[d] * 0.25, 1),
"km cooldown")
}
}
return(training_plan)
}
# Generate the training plan
training_plan <- generate_training_plan(days, dates)
#=============================================
# 4. Simulation Functions
#=============================================
# Function to calculate fitness response
calculate_fitness_response <- function(params, plan) {
# Initialize fitness parameters to track
plan$vo2max <- params$vo2max_base
plan$lactate_threshold <- params$vo2max_base * params$lactate_threshold_pct