Skip to content

Commit

Permalink
2025-1-3 update
Browse files Browse the repository at this point in the history
- Refactored flux calculation module (src/flux.jl)
- Optimized main module structure (src/HydroModels.jl)
- Improved code organization and structure
- Enhanced type stability in core calculations
- Standardized function signatures across modules
  • Loading branch information
chooron committed Jan 3, 2025
1 parent c6feac7 commit 73e9f74
Show file tree
Hide file tree
Showing 48 changed files with 1,473 additions and 1,703 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.vscode
*.cursorrules
project
.gitignore
.cursorrules
Expand Down
58 changes: 29 additions & 29 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.11.2"
manifest_format = "2.0"
project_hash = "05cc91d0bfc3ab421194b7ebc7005fe7eb3149e7"
project_hash = "64ed56e32e429c69389c36fa4a17e26c1c589ea5"

[[deps.ADTypes]]
git-tree-sha1 = "72af59f5b8f09faee36b4ec48e014a79210f2f4f"
Expand Down Expand Up @@ -252,9 +252,9 @@ version = "1.1.1+0"

[[deps.ComponentArrays]]
deps = ["Adapt", "ArrayInterface", "ChainRulesCore", "ConstructionBase", "ForwardDiff", "Functors", "LinearAlgebra", "StaticArrayInterface", "StaticArraysCore"]
git-tree-sha1 = "e9b355215c05eeaaa7bf7d2ae0d329074e3cb045"
git-tree-sha1 = "9921db9efec9f395210f02b56b87b93dbb6cfd2b"
uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
version = "0.15.19"
version = "0.15.20"

[deps.ComponentArrays.extensions]
ComponentArraysGPUArraysExt = "GPUArrays"
Expand Down Expand Up @@ -423,9 +423,9 @@ version = "1.15.1"

[[deps.DifferentiationInterface]]
deps = ["ADTypes", "LinearAlgebra"]
git-tree-sha1 = "7ffe68edc2a4ad0ff2eeb5a32cc99ab45746fe3d"
git-tree-sha1 = "5df172ce4e9da710603591f48c7af74eef288892"
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
version = "0.6.27"
version = "0.6.28"

[deps.DifferentiationInterface.extensions]
DifferentiationInterfaceChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -467,9 +467,9 @@ version = "0.6.27"

[[deps.DispatchDoctor]]
deps = ["MacroTools", "Preferences"]
git-tree-sha1 = "453df2ce2aef7de59c69a56d31dcd2ec3384dd77"
git-tree-sha1 = "c204e42f63e0013d4446b9fe60fdad6edde7d8cf"
uuid = "8d63f2c5-f18a-4cf2-ba9d-b3f60fc568c8"
version = "0.4.17"
version = "0.4.18"
weakdeps = ["ChainRulesCore", "EnzymeCore"]

[deps.DispatchDoctor.extensions]
Expand Down Expand Up @@ -720,7 +720,7 @@ weakdeps = ["AbstractTrees"]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "50aedf345a709ab75872f80a2779568dc0bb461b"
uuid = "e33a78d0-f292-5ffc-b300-72abe9b543c8"
version = "2.11.2+1"
version = "2.11.2+3"

[[deps.HypergeometricFunctions]]
deps = ["LinearAlgebra", "OpenLibm_jll", "SpecialFunctions"]
Expand Down Expand Up @@ -787,9 +787,9 @@ version = "1.0.0"

[[deps.JLLWrappers]]
deps = ["Artifacts", "Preferences"]
git-tree-sha1 = "be3dc50a92e5a386872a493a10050136d4703f9b"
git-tree-sha1 = "a007feb38b422fbdab534406aeca1b86823cb4d6"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.6.1"
version = "1.7.0"

[[deps.JuliaFormatter]]
deps = ["CSTParser", "CommonMark", "DataStructures", "Glob", "PrecompileTools", "TOML", "Tokenize"]
Expand Down Expand Up @@ -997,9 +997,9 @@ weakdeps = ["ChainRulesCore", "ForwardDiff", "SpecialFunctions"]

[[deps.Lux]]
deps = ["ADTypes", "Adapt", "ArgCheck", "ArrayInterface", "ChainRulesCore", "Compat", "ConcreteStructs", "DispatchDoctor", "EnzymeCore", "FastClosures", "ForwardDiff", "Functors", "GPUArraysCore", "LinearAlgebra", "LuxCore", "LuxLib", "MLDataDevices", "MacroTools", "Markdown", "NNlib", "Optimisers", "Preferences", "Random", "Reexport", "SIMDTypes", "Setfield", "Static", "StaticArraysCore", "Statistics", "WeightInitializers"]
git-tree-sha1 = "48f87b2211d2315d8cb36d749600b7ccc229270f"
git-tree-sha1 = "6d23b9976515a1d10f78d16f1e5f8fb544292fc2"
uuid = "b2108857-7c20-44ae-9111-449ecde12c47"
version = "1.4.1"
version = "1.4.4"

[deps.Lux.extensions]
LuxComponentArraysExt = "ComponentArrays"
Expand Down Expand Up @@ -1061,9 +1061,9 @@ version = "1.2.1"

[[deps.LuxLib]]
deps = ["ArrayInterface", "ChainRulesCore", "Compat", "CpuId", "DispatchDoctor", "EnzymeCore", "FastClosures", "ForwardDiff", "Hwloc", "KernelAbstractions", "LinearAlgebra", "LuxCore", "MLDataDevices", "Markdown", "NNlib", "Polyester", "Preferences", "Random", "Reexport", "Static", "StaticArraysCore", "Statistics"]
git-tree-sha1 = "534a18fb11aa9c564c78cddaa61981634a5e02ef"
git-tree-sha1 = "86578d42ce26e8cccbf5297881f8cf43b81df0a8"
uuid = "82251201-b29d-42c6-8e01-566dec8acb11"
version = "1.3.10"
version = "1.4.0"

[deps.LuxLib.extensions]
LuxLibAppleAccelerateExt = "AppleAccelerate"
Expand Down Expand Up @@ -1101,9 +1101,9 @@ version = "2024.2.0+0"

[[deps.MLDataDevices]]
deps = ["Adapt", "Compat", "Functors", "Preferences", "Random"]
git-tree-sha1 = "80eb04ae663507d9303473d26710a4c62efa0f3c"
git-tree-sha1 = "c2581519bdea44ff65a74d763f0d122ef3c0434d"
uuid = "7e8f7934-dd98-4c1a-8fe8-92b47a384d40"
version = "1.6.5"
version = "1.6.6"

[deps.MLDataDevices.extensions]
MLDataDevicesAMDGPUExt = "AMDGPU"
Expand Down Expand Up @@ -1193,9 +1193,9 @@ version = "1.11.0"

[[deps.ModelingToolkit]]
deps = ["AbstractTrees", "ArrayInterface", "BlockArrays", "Combinatorics", "CommonSolve", "Compat", "ConstructionBase", "DataStructures", "DiffEqBase", "DiffEqCallbacks", "DiffEqNoiseProcess", "DiffRules", "Distributed", "Distributions", "DocStringExtensions", "DomainSets", "DynamicQuantities", "EnumX", "ExprTools", "Expronicon", "FindFirstFunctions", "ForwardDiff", "FunctionWrappers", "FunctionWrappersWrappers", "Graphs", "InteractiveUtils", "JuliaFormatter", "JumpProcesses", "Latexify", "Libdl", "LinearAlgebra", "MLStyle", "NaNMath", "NonlinearSolve", "OffsetArrays", "OrderedCollections", "PrecompileTools", "RecursiveArrayTools", "Reexport", "RuntimeGeneratedFunctions", "SCCNonlinearSolve", "SciMLBase", "SciMLStructures", "Serialization", "Setfield", "SimpleNonlinearSolve", "SparseArrays", "SpecialFunctions", "StaticArrays", "SymbolicIndexingInterface", "SymbolicUtils", "Symbolics", "URIs", "UnPack", "Unitful"]
git-tree-sha1 = "afb3f4346dda57caf44cdf3ff8faf9e1afc7f1fd"
git-tree-sha1 = "c497ca1ba15722a5009cd6571a7a618c60b98dc3"
uuid = "961ee093-0014-501f-94e3-6117800e7a78"
version = "9.57.0"
version = "9.59.0"

[deps.ModelingToolkit.extensions]
MTKBifurcationKitExt = "BifurcationKit"
Expand Down Expand Up @@ -1374,10 +1374,10 @@ uuid = "05823500-19ac-5b8b-9628-191a04bc5112"
version = "0.8.1+2"

[[deps.OpenSpecFun_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "13652491f6856acfd2db29360e1bbcd4565d04f1"
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl"]
git-tree-sha1 = "1346c9208249809840c91b26703912dff463d335"
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
version = "0.5.5+0"
version = "0.5.6+0"

[[deps.Optim]]
deps = ["Compat", "FillArrays", "ForwardDiff", "LineSearches", "LinearAlgebra", "NLSolversBase", "NaNMath", "Parameters", "PositiveFactorizations", "Printf", "SparseArrays", "StatsBase"]
Expand Down Expand Up @@ -1627,9 +1627,9 @@ version = "0.6.43"

[[deps.SciMLBase]]
deps = ["ADTypes", "Accessors", "ArrayInterface", "CommonSolve", "ConstructionBase", "Distributed", "DocStringExtensions", "EnumX", "Expronicon", "FunctionWrappersWrappers", "IteratorInterfaceExtensions", "LinearAlgebra", "Logging", "Markdown", "PrecompileTools", "Preferences", "Printf", "RecipesBase", "RecursiveArrayTools", "Reexport", "RuntimeGeneratedFunctions", "SciMLOperators", "SciMLStructures", "StaticArraysCore", "Statistics", "SymbolicIndexingInterface"]
git-tree-sha1 = "213408a448e27170e4fca428838b8d11c5bbf1ab"
git-tree-sha1 = "3e5a9c5d6432b77a271646b4ada2573f239ac5c4"
uuid = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
version = "2.68.1"
version = "2.70.0"

[deps.SciMLBase.extensions]
SciMLBaseChainRulesCoreExt = "ChainRulesCore"
Expand Down Expand Up @@ -1780,9 +1780,9 @@ weakdeps = ["OffsetArrays", "StaticArrays"]

[[deps.StaticArrays]]
deps = ["LinearAlgebra", "PrecompileTools", "Random", "StaticArraysCore"]
git-tree-sha1 = "7c01731da8ab6d3094c4d44c9057b00932459255"
git-tree-sha1 = "47091a0340a675c738b1304b58161f3b0839d454"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.9.9"
version = "1.9.10"
weakdeps = ["ChainRulesCore", "Statistics"]

[deps.StaticArrays.extensions]
Expand Down Expand Up @@ -1876,9 +1876,9 @@ version = "3.7.2"

[[deps.Symbolics]]
deps = ["ADTypes", "ArrayInterface", "Bijections", "CommonWorldInvalidations", "ConstructionBase", "DataStructures", "DiffRules", "Distributions", "DocStringExtensions", "DomainSets", "DynamicPolynomials", "IfElse", "LaTeXStrings", "Latexify", "Libdl", "LinearAlgebra", "LogExpFunctions", "MacroTools", "Markdown", "NaNMath", "PrecompileTools", "Primes", "RecipesBase", "Reexport", "RuntimeGeneratedFunctions", "SciMLBase", "Setfield", "SparseArrays", "SpecialFunctions", "StaticArraysCore", "SymbolicIndexingInterface", "SymbolicLimits", "SymbolicUtils", "TermInterface"]
git-tree-sha1 = "ce9c95fc859007747a4faf10166201e0b10d4313"
git-tree-sha1 = "493c70c2a94a96dff97523afd2fb4ea7fd346b93"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
version = "6.22.0"
version = "6.22.1"

[deps.Symbolics.extensions]
SymbolicsForwardDiffExt = "ForwardDiff"
Expand Down Expand Up @@ -1988,9 +1988,9 @@ version = "1.11.0"

[[deps.Unitful]]
deps = ["Dates", "LinearAlgebra", "Random"]
git-tree-sha1 = "01915bfcd62be15329c9a07235447a89d588327c"
git-tree-sha1 = "c0667a8e676c53d390a09dc6870b3d8d6650e2bf"
uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "1.21.1"
version = "1.22.0"
weakdeps = ["ConstructionBase", "InverseFunctions"]

[deps.Unitful.extensions]
Expand Down
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HydroModels"
uuid = "7e3cde01-c141-467b-bff6-5350a0af19fc"
authors = ["jingx <[email protected]>"]
version = "0.1.4"
version = "0.1.5"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand All @@ -26,6 +26,7 @@ TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Accessors = "0.1"
Aqua = "0.8"
ComponentArrays = "0.15"
CSV = "0.10"
DataFrames = "1"
DataInterpolations = "6"
Graphs = "1"
Expand All @@ -41,6 +42,7 @@ SparseArrays = "1"
StableRNGs = "1"
SymbolicUtils = "3"
Symbolics = "6"
Statistics = "1"
TOML = "1"
Test = "1"
julia = "1"
Expand All @@ -53,4 +55,4 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Aqua", "CSV", "DataFrames", "Statistics"]
test = ["Test", "Aqua", "CSV", "DataFrames", "Statistics"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# HydroModels.jl

[![version](https://docs.juliahub.com/HydroModels/version.svg)](https://juliahub.com/ui/Packages/General/HydroModels)
[![英文文档](https://img.shields.io/badge/docs-dev-blue.svg)](https://chooron.github.io/HydroModels.jl/dev/)
[![中文文档](https://img.shields.io/badge/docs-zh-red.svg)](https://chooron.github.io/HydroModels.jl/dev-zh/)
[![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
Expand Down
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
- [ ] Flux output need constraint
- [ ] Add NoneMeanFlux
- [ ] **GPU support (limited by scalar index)**
- [ ] DataInterpolations.jl is not support for now
- [ ] meta should store variables and parameters
- [ ] compact problem
- [X] compact problem
- [ ] ensemble model
- [ ] base merge for component meta
- [ ] Neural Flux make the gradient computation slow a lot
Expand Down
63 changes: 21 additions & 42 deletions dev/test_bucket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ using ModelingToolkit
using Lux
using BenchmarkTools
using DataInterpolations
using HydroModelTools
using CUDA
using Zygote

include("../src/HydroModels.jl")

Expand All @@ -18,56 +19,34 @@ include("../models/exphydro.jl")

ele = bucket_1
f, Smax, Qmax, Df, Tmax, Tmin = 0.01674478, 1709.461015, 18.46996175, 2.674548848, 0.175739196, -2.092959084
# 0.0167, 1709.46, 18.47, 2.6745, 0.1757, -2.093
params = ComponentVector(f=f, Smax=Smax, Qmax=Qmax, Df=Df, Tmax=Tmax, Tmin=Tmin)
init_states = ComponentVector(snowpack=0.0, soilwater=1303.004248)
pas = ComponentVector(params=params, initstates=init_states)

file_path = "data/exphydro/01013500.csv"
data = CSV.File(file_path);
df = DataFrame(data);
ts = collect(1:10000)

# single node input
input = (lday=df[ts, "dayl(day)"], temp=df[ts, "tmean(C)"], prcp=df[ts, "prcp(mm/day)"])
input_arr = Matrix(reduce(hcat, collect(input[ele.meta.inputs])))
input_arr = Matrix(reduce(hcat, collect(input[HydroModels.get_input_names(ele)]))')
results = ele(input_arr, pas)

# # multi node input
# node_num = 10
# node_names = [Symbol(:node, i) for i in 1:node_num]
# node_params = ComponentVector(
# f=fill(f, node_num), Smax=fill(Smax, node_num), Qmax=fill(Qmax, node_num),
# Df=fill(Df, node_num), Tmax=fill(Tmax, node_num), Tmin=fill(Tmin, node_num)
# )
# node_states = ComponentVector(
# snowpack=fill(0.0, node_num), soilwater=fill(1303.004248, node_num)
# )

# node_pas = ComponentVector(params=node_params[HydroModels.get_param_names(ele)], initstates=node_states[HydroModels.get_state_names(ele)])
# input_arr = reduce(hcat, collect(input[HydroModels.get_input_names(ele)]))
# node_input = reduce((m1, m2) -> cat(m1, m2, dims=3), repeat([input_arr], length(node_names)))
# node_input = permutedims(node_input, (2, 3, 1))
# config = (ptyidx=1:10, styidx=1:10, timeidx=ts)
# result = ele(node_input, node_pas, config=config)


# # share parameters
# node_num = 3
# node_names = [Symbol(:node, i) for i in 1:node_num]
# node_params = ComponentVector(
# f=fill(f, node_num), Smax=fill(Smax, node_num), Qmax=fill(Qmax, node_num),
# Df=fill(Df, node_num), Tmax=fill(Tmax, node_num), Tmin=fill(Tmin, node_num)
# )
# node_states = ComponentVector(
# snowpack=fill(0.0, node_num), soilwater=fill(1303.004248, node_num)
# )

# node_pas = ComponentVector(params=node_params[HydroModels.get_param_names(ele)], initstates=node_states[HydroModels.get_state_names(ele)])
# input_arr = reduce(hcat, collect(input[HydroModels.get_input_names(ele)]))
# node_input = reduce((m1, m2) -> cat(m1, m2, dims=3), repeat([input_arr], 10))
# node_input = permutedims(node_input, (2, 3, 1))
# config = (ptyidx=[1,2,2,2,1,3,3,2,3,2], styidx=[1,2,2,2,1,3,3,2,3,2], timeidx=ts)
# result = ele(node_input, node_pas, config=config)

# test_arr = ones(3, 3, 100)
# # node_input = cat(node_input, result, dims=1)
# multi node input
node_num = 10
node_names = [Symbol(:node, i) for i in 1:node_num]
node_params = ComponentVector(
f=fill(f, node_num), Smax=fill(Smax, node_num), Qmax=fill(Qmax, node_num),
Df=fill(Df, node_num), Tmax=fill(Tmax, node_num), Tmin=fill(Tmin, node_num)
)
node_states = ComponentVector(
snowpack=fill(0.0, node_num), soilwater=fill(1303.004248, node_num)
)

node_pas = ComponentVector(params=node_params[HydroModels.get_param_names(ele)], initstates=node_states[HydroModels.get_state_names(ele)])
input_arr = reduce(hcat, collect(input[HydroModels.get_input_names(ele)]))
node_input = reduce((m1, m2) -> cat(m1, m2, dims=3), repeat([input_arr], length(node_names)))
node_input = permutedims(node_input, (2, 3, 1))
config = (ptyidx=1:10, styidx=1:10, timeidx=ts)
result = ele(node_input, node_pas, config=config)
16 changes: 16 additions & 0 deletions dev/test_nn_model.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 导入模块
using CSV
using DataFrames
using ComponentArrays
using ModelingToolkit
using Plots
using Lux
using StableRNGs

include("../src/HydroModels.jl")
HydroFlux = HydroModels.HydroFlux
StateFlux = HydroModels.StateFlux
NeuralFlux = HydroModels.NeuralFlux
HydroBucket = HydroModels.HydroBucket
HydroModel = HydroModels.HydroModel
include("../models/m50.jl")
Loading

0 comments on commit 73e9f74

Please sign in to comment.