Source code for pySD.initsuperdropsbinary_src.rgens
"""Copyright (c) 2024 MPI-M, Clara Bayley----- CLEO -----File: rgens.pyProject: initsuperdropsbinary_srcCreated Date: Friday 13th October 2023Author: Clara Bayley (CB)Additional Contributors:-----Last Modified: Tuesday 7th May 2024Modified By: CB-----License: BSD 3-Clause "New" or "Revised" Licensehttps://opensource.org/licenses/BSD-3-Clause-----File Description:various ways of generatring radii of superdroplets for their initial conditions"""importnumpyasnp
[docs]classMonoAttrGen:"""method to generate superdroplets with an attribute all equal to attr0"""def__init__(self,attr0):self.attr0=attr0def__call__(self,nsupers):"""Returns attribute for nsupers all with the value of attr0"""iftype(nsupers)==np.ndarray:nsupers=np.shape(nsupers)[0]attrs=np.full(nsupers,self.attr0)returnattrs
[docs]classSampleLog10RadiiGen:"""method to generate superdroplet radii by randomly sampling from bins that are linearly spaced in log10(r) between rspan[0] and rspan[1]"""def__init__(self,rspan):self.rspan=rspandef__call__(self,nsupers):"""Returns radii for nsupers sampled from rspan [m]"""returnself.generate_radiisample(nsupers)# units [m]
[docs]defgenerate_radiisample(self,nbins):"""Divide rspan [m] into evenly spaced bins in log10(r). If edges=True, return values of radii at edges of bins. Else sample each bin randomly to obtain the radius of 'nsupers' no. of superdroplets"""ifnbins:log10redgs=np.linspace(np.log10(self.rspan[0]),np.log10(self.rspan[1]),nbins+1)# log10(r) bin edgesradii=self.randomlysample_log10rbins(nbins,log10redgs)returnradii# [m]else:returnnp.array([])
[docs]defrandomlysample_log10rbins(self,nbins,log10redgs):"""given the bin edges, randomly sample each bin of log10(radius /m) and return the resultant radii [m]"""log10r_binwidth=(log10redgs[-1]-log10redgs[0])/nbinsrandlog10deltar=np.random.uniform(low=0.0,high=log10r_binwidth,size=nbins)randlog10r=log10redgs[:-1]+randlog10deltarradii=10**(randlog10r)returnradii# [m]