Path: blob/devel/elmerice/examples/Inverse_Methods/MassConservation/src/Random.F90
3206 views
!/*****************************************************************************/1! *2! * Elmer/Ice, a glaciological add-on to Elmer3! * http://elmerice.elmerfem.org4! *5! *6! * This program is free software; you can redistribute it and/or7! * modify it under the terms of the GNU General Public License8! * as published by the Free Software Foundation; either version 29! * of the License, or (at your option) any later version.10! *11! * This program is distributed in the hope that it will be useful,12! * but WITHOUT ANY WARRANTY; without even the implied warranty of13! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14! * GNU General Public License for more details.15! *16! * You should have received a copy of the GNU General Public License17! * along with this program (in file fem/GPL-2); if not, write to the18! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,19! * Boston, MA 02110-1301, USA.20! *21! *****************************************************************************/22! ******************************************************************************23! *24! * Authors: F. Gillet-Chaulet25! * Web: http://elmerice.elmerfem.org26! *27! * Original Date: Dec. 202028! *29! * A user function to initialise variables with a random number30! * using an even or normal distribution31! *32! * INPUTS:33! * - Argument: x(2) real values for a linear transformation of the34! random number: out = r*x(1)+x(2)35! * - Constants:36! * - Random Seed = Integer [OPTIONAL] => give a seed for repetability37! * - Random Function = String "even" for an even distribution between 0 138! * or "normal" for a normal distribution of 0 mean and unit variance39! *****************************************************************************40FUNCTION Random(Model,nodenumber,x) RESULT(r)41USE DefUtils42implicit none43!-----------------44TYPE(Model_t) :: Model45INTEGER :: nodenumber46REAL(kind=dp),INTENT(IN) :: x(2)47REAL(kind=dp) :: r4849INTEGER :: ssize50INTEGER,ALLOCATABLE :: seed(:)51LOGICAL, SAVE :: Firsttime=.TRUE.52LOGICAL :: Found5354CHARACTER(LEN=MAX_NAME_LEN),SAVE :: Rtype5556IF (Firsttime) THEN57CALL random_seed(size=ssize)58allocate(seed(ssize))5960seed = ListGetInteger( Model % Constants, 'Random Seed',Found )61IF (Found) call random_seed( put=seed )62CALL random_seed(get=seed)6364Rtype = ListGetString( Model % Constants, 'Random Function',UnFoundFatal=.TRUE.)65deallocate(seed)66Firsttime=.FALSE.67ENDIF686970SELECT CASE(Rtype)71CASE('even')72r = EvenRandom()73CASE('normal')74r = NormalRandom()75CASE DEFAULT76CALL FATAL('Random Function','Random Function should be <even> or <normal>')77END SELECT7879r = r*x(1)+x(2)8081End FUNCTION Random8283848586