Skip to content

Commit

Permalink
Added a scale parameter to state variable (default=1) to scale absolu…
Browse files Browse the repository at this point in the history
…te tolerances. Another take on #2.
  • Loading branch information
philippkraft committed Dec 12, 2017
1 parent 9500277 commit 4ed21fe
Show file tree
Hide file tree
Showing 9 changed files with 12,667 additions and 1,161 deletions.
8,248 changes: 7,599 additions & 649 deletions cmf/cmf_core.py

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions cmf/cmf_core_src/cmf.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(SolutionDir)math\integrators\sundials_cvode\include;$(BOOSTDIR)\boost\tr1;$(BOOSTDIR);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(SolutionDir)math\integrators\sundials_cvode\include;$(BOOSTDIR)\boost\tr1;$(BOOSTDIR);C:\Apps\WinPython-64bit-3.6.1.0Qt5\python-3.6.1.amd64\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;CMF_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
Expand Down Expand Up @@ -456,7 +456,6 @@
<ClCompile Include="atmosphere\Weather.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="ClassDiagram.cd" />
<None Include="math\array.i" />
<None Include="upslope\cell.i" />
<CustomBuild Include="cmf.i">
Expand Down
5,541 changes: 5,044 additions & 497 deletions cmf/cmf_core_src/cmf_wrap.cpp

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions cmf/cmf_core_src/math/statevariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,17 @@ namespace cmf {
bool m_StateIsNew;
/// Holds the value of the Statevariable
real m_State;

protected:
virtual void StateChangeAction() {}
/// Sets the updated flag (m_StateIsNew) to false
void MarkStateChangeHandled() {m_StateIsNew=false;}
/// Returns if the state was currently updated
bool StateIsChanged() {return m_StateIsNew;}
// A scale to handle abstol issues with the CVode Solver and with the is_empty function
// By default, the scale is 1. Absolute tolerance is scale times rel tolerance
real m_Scale;

public:
typedef std::shared_ptr<StateVariable> ptr;
/// Returns the derivate of the state variable at time @c time
Expand All @@ -118,11 +123,14 @@ namespace cmf {
void set_state(real newState);

virtual real get_abs_errtol(real rel_errtol) const{
return rel_errtol;
return rel_errtol * m_Scale;
}
virtual std::string to_string() const=0;
/// ctor
StateVariable(real InitialState=0) : m_State(InitialState),m_StateIsNew(true) {}
StateVariable(real InitialState=0, real scale=1)
: m_State(InitialState),m_StateIsNew(true), m_Scale(scale)
{}

virtual ~StateVariable() {}
};

Expand Down
4 changes: 3 additions & 1 deletion cmf/cmf_core_src/upslope/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ cmf::water::WaterStorage::ptr Cell::add_storage( std::string Name,char storage_r
if (isopenwater)
ws = cmf::river::OpenWaterStorage::create(get_project(),get_area());
else
ws = WaterStorage::create(get_project());
// Create a water storage with a "standard storage" of 1mm for tolerances
ws = WaterStorage::create(get_project(), 0.0, /*scale=*/ get_area()/1000.);

ws->position=get_position();
ws->Name=Name;
if (storage_role=='C')
Expand Down
2 changes: 1 addition & 1 deletion cmf/cmf_core_src/upslope/connections/surfacefluxes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ real cmf::upslope::connections::SimpleTindexSnowMelt::calc_q( cmf::math::Time t
// limit snow meltrate to current snow/(10 min)
return std::min(Snow->get_volume() * 24 * 6, potential_meltrate_m3);
}
else
else // too cold, no melting
return 0.0;
}

Expand Down
5 changes: 3 additions & 2 deletions cmf/cmf_core_src/water/WaterStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ void WaterStorage::initializeSoluteStorages(const solute_vector& solutes) {
}


WaterStorage::WaterStorage(cmf::project& _project, const std::string& _Name,double InitialState/*=0*/ )
: cmf::math::StateVariable(InitialState),flux_node(_project) ,m_Concentrations(), m_state_variable_content('V')
WaterStorage::WaterStorage(cmf::project& _project, const std::string& _Name,
double InitialState/*=0*/, double scale/*=1*/ )
: cmf::math::StateVariable(InitialState, scale),flux_node(_project) ,m_Concentrations(), m_state_variable_content('V')
{
initializeSoluteStorages(_project.solutes);
this->Name = _Name;
Expand Down
11 changes: 5 additions & 6 deletions cmf/cmf_core_src/water/WaterStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ namespace cmf {
virtual real volume_to_head(real volume) const;

public:
real get_abs_errtol(real rel_errtol) const {
return rel_errtol;
}
/// @brief A character indicating the integrated variable (either 'V' for Volume or 'h' for head)
inline char get_state_variable_content() const {return m_state_variable_content;}
/// @brief A character indicating the integrated variable (either 'V' for Volume or 'h' for head)
Expand All @@ -82,7 +79,9 @@ namespace cmf {
/// @param project The project the waterstorage belongs to
/// @param Name Name of the water storage
/// @param InitialState Initial water content in m<sup>3</sup>
WaterStorage(cmf::project& project,const std::string & Name="", double InitialState=0);
/// @param A kind of "standard size" in m3 of the water storage to scale tolerances, default 1m3
WaterStorage(cmf::project& project,const std::string & Name="",
double InitialState=0, double scale=1);

static std::shared_ptr<WaterStorage> from_node(cmf::water::flux_node::ptr node);
/// @brief Returns the water quality of the water storage.
Expand Down Expand Up @@ -153,9 +152,9 @@ namespace cmf {
{
return std::dynamic_pointer_cast<cmf::water::WaterStorage>(node);
}
static std::shared_ptr<cmf::water::WaterStorage> create(cmf::project& _project, real initial_state=0.0)
static std::shared_ptr<cmf::water::WaterStorage> create(cmf::project& _project, real initial_state=0.0, real scale=1.0)
{
return std::shared_ptr<cmf::water::WaterStorage>(new WaterStorage(_project,"unknown",initial_state));
return std::shared_ptr<cmf::water::WaterStorage>(new WaterStorage(_project,"unknown",initial_state, scale));
}

};
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io
import datetime

version = '1.0.4a'
version = '1.0.4'

# Try to import numpy, if it fails we have a problem
try:
Expand Down

0 comments on commit 4ed21fe

Please sign in to comment.