Skip to content

Commit

Permalink
Merge pull request #166 from JuliaRheology/dev
Browse files Browse the repository at this point in the history
Split of definitions.jl and new example
  • Loading branch information
akabla authored Jul 24, 2024
2 parents f66a00a + f0257fb commit a93e865
Show file tree
Hide file tree
Showing 7 changed files with 933 additions and 761 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.


# 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))
data = modelpredict(dhold_stress, model)

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

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

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

for (model_name, fitdata) in extracted_data
error = fitdata.info.error

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

if error < min_error
min_error = 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
stress_only_data = onlystress(data)

# Get model predictions for plotting
SLS_Zener_predict = modelpredict(stress_only_data, SLS_Zener_model)
Maxwell_predict = modelpredict(stress_only_data, Maxwell_model)
BurgersLiquid_predict = modelpredict(stress_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("Strain")
ax.legend()
ax.grid("on")
#!nb fig #hide
4 changes: 3 additions & 1 deletion src/RHEOS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const RheovecOrNone = Union{Vector{RheoFloat}, Nothing}
######################################################################
export namedtuple, dict
export RheoFloat
export extractfitdata

# definitions.jl
export RheoLogItem, RheoLog, rheologrun, showlog
Expand Down Expand Up @@ -88,7 +89,8 @@ export AFM, Tweezers

include("base.jl")
include("symbols.jl")
include("definitions.jl")
include("rheodata.jl")
include("rheomodel.jl")
include("IO.jl")
include("modeldatabase.jl")
include("datagen.jl")
Expand Down
Loading

0 comments on commit a93e865

Please sign in to comment.