Skip to content

Commit

Permalink
Update examples.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
SAYANTANDE authored Jul 22, 2024
1 parent 015ed64 commit 744e758
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions docs/src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,67 @@ ax.set_xlabel("Time")
ax.set_ylabel("Stress")
ax.grid("on")
#!nb fig #hide

# ## Example 4

# This example demonstrates generating a timeline and stress data, fitting multiple models to the data,
# calling the `extractfitdata` function, listing the errors, and determining which model fits the best.

using RHEOS

This comment has been minimized.

Copy link
@akabla

akabla Jul 22, 2024

Member

This is not needed, as already included at the top of the file.


# Generate Timeline
datat = timeline(t_start = 0, t_end = 20.0, step = 0.02) # Create a timeline from 0 to 20 seconds with a step size of 0.02 seconds

# Generate Stress Data (Ramp & hold)
dramp_stress = stressfunction(datat, ramp(offset = 4.0, gradient = 0.8)) # Generate a ramp stress function with offset 4.0 and gradient 0.8
dhold_stress = dramp_stress - stressfunction(datat, ramp(offset = 5.0, gradient = 0.8)) # Generate a hold stress function by subtracting a shifted ramp

# Define the rheological model and predict
model = RheoModel(SLS_Zener, (η = 1, kᵦ = 1, kᵧ = 1))
SLS_predict = modelpredict(dhold_stress, model)
data = SLS_predict

# Fit three models to the data
SLS_Zener_model = modelfit(data, SLS_Zener, strain_imposed)
Maxwell_model = modelfit(data, Maxwell, strain_imposed)
BurgersLiquid_model = modelfit(data, BurgersLiquid, strain_imposed)

# Call the extractfitdata function to extract fitting data
extracted_data = extractfitdata(data)

# Determine which model fits best by comparing errors
best_model = ""
min_error = Inf

for (model_name, entries) in extracted_data
total_error = sum(entry.info.error for entry in entries)

println("Model: $model_name, Total Error: $total_error")

if total_error < min_error
min_error = total_error
best_model = model_name
end
end

println("Best fitting model: $best_model with total error: $min_error")

# Create strain-only data for model predictions
strain_only_data = onlystrain(data)

# Get model predictions for plotting
SLS_Zener_predict = modelpredict(strain_only_data, SLS_Zener_model)
Maxwell_predict = modelpredict(strain_only_data, Maxwell_model)
BurgersLiquid_predict = modelpredict(strain_only_data, BurgersLiquid_model)

# Plot data and fitted models
fig, ax = subplots(1, 1, figsize = (7, 5))
ax.plot(data.t, data.σ, ".", color = "green", label = "Original Data")
ax.plot(SLS_Zener_predict.t, SLS_Zener_predict.σ, "-", color = "red", label = "SLS_Zener Model")
ax.plot(Maxwell_predict.t, Maxwell_predict.σ, "--", color = "blue", label = "Maxwell Model")
ax.plot(BurgersLiquid_predict.t, BurgersLiquid_predict.σ, ":", color = "purple", label = "BurgersLiquid Model")
ax.set_xlabel("Time")
ax.set_ylabel("Stress")
ax.legend()
ax.grid("on")
#!nb fig #hide

0 comments on commit 744e758

Please sign in to comment.