"""Subside a `SequenceModelGrid`."""from__future__importannotationsimportosfromcollections.abcimportCallableimportnumpyasnpfromlandlabimportComponentfromnumpy.typingimportNDArrayfromscipyimportinterpolatefromsequence.gridimportSequenceModelGrid
[docs]classSubsidenceTimeSeries(Component):"""A *Landlab* component that subsides a grid."""_name="Subsider"_time_units="y"_info={"bedrock_surface__increment_of_elevation":{"dtype":"float","intent":"inout","optional":False,"units":"m","mapping":"node","doc":"Change in elevation due to subsidence",}}
[docs]def__init__(self,grid:SequenceModelGrid,filepath:os.PathLike,kind:str="linear"):"""Create a grid subsider from a time-series file. Parameters ---------- grid: SequenceModelGrid A landlab grid. filepath: os.PathLike Name of csv-formatted subsidence file. kind: str, optional Kind of interpolation as a string (one of 'linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic'). Default is 'linear'. """if"bedrock_surface__increment_of_elevation"notingrid.at_node:grid.add_zeros("bedrock_surface__increment_of_elevation",at="node")super().__init__(grid)self._filepath=filepathself._kind=kinddata=np.loadtxt(filepath,delimiter=",",comments="#")self._subsidence=SubsidenceTimeSeries._subsidence_interpolator(data,kind=self._kind)self._dz_dt=self._calc_subsidence_rate()self._time=0.0
@propertydefsubsidence_rate(self)->NDArray:"""Return the current subsidence rate."""returnself._dz_dt@staticmethoddef_subsidence_interpolator(data:NDArray,kind:str="linear")->Callable[[float|NDArray],NDArray]:returninterpolate.interp1d(data[:,0],data[:,1],kind=kind,copy=True,assume_sorted=True,bounds_error=True,)def_calc_subsidence_rate(self)->NDArray:returnself._subsidence(self.grid.x_of_node[self.grid.nodes_at_bottom_edge])@propertydeftime(self)->float:"""Return the current component time."""returnself._time@propertydeffilepath(self)->str:"""Return the path to the current subsidence file."""returnstr(self._filepath)@filepath.setterdeffilepath(self,new_path:os.PathLike)->None:self._filepath=new_pathself._subsidence=SubsidenceTimeSeries._subsidence_interpolator(np.loadtxt(self._filepath,delimiter=",",comments="#"),kind=self._kind)self._dz_dt=self._calc_subsidence_rate()
[docs]defrun_one_step(self,dt:float)->None:"""Update the component by a time step. Parameters ---------- dt : float The time step to update the component by. """self.grid.get_profile("bedrock_surface__increment_of_elevation")[:]+=(self.subsidence_rate*dt)self._time+=dt