Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/examples/Inverse_Methods/MassConservation/src/Random.F90
3206 views
1
!/*****************************************************************************/
2
! *
3
! * Elmer/Ice, a glaciological add-on to Elmer
4
! * http://elmerice.elmerfem.org
5
! *
6
! *
7
! * This program is free software; you can redistribute it and/or
8
! * modify it under the terms of the GNU General Public License
9
! * as published by the Free Software Foundation; either version 2
10
! * of the License, or (at your option) any later version.
11
! *
12
! * This program is distributed in the hope that it will be useful,
13
! * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
! * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
! * GNU General Public License for more details.
16
! *
17
! * You should have received a copy of the GNU General Public License
18
! * along with this program (in file fem/GPL-2); if not, write to the
19
! * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20
! * Boston, MA 02110-1301, USA.
21
! *
22
! *****************************************************************************/
23
! ******************************************************************************
24
! *
25
! * Authors: F. Gillet-Chaulet
26
! * Web: http://elmerice.elmerfem.org
27
! *
28
! * Original Date: Dec. 2020
29
! *
30
! * A user function to initialise variables with a random number
31
! * using an even or normal distribution
32
! *
33
! * INPUTS:
34
! * - Argument: x(2) real values for a linear transformation of the
35
! random number: out = r*x(1)+x(2)
36
! * - Constants:
37
! * - Random Seed = Integer [OPTIONAL] => give a seed for repetability
38
! * - Random Function = String "even" for an even distribution between 0 1
39
! * or "normal" for a normal distribution of 0 mean and unit variance
40
! *****************************************************************************
41
FUNCTION Random(Model,nodenumber,x) RESULT(r)
42
USE DefUtils
43
implicit none
44
!-----------------
45
TYPE(Model_t) :: Model
46
INTEGER :: nodenumber
47
REAL(kind=dp),INTENT(IN) :: x(2)
48
REAL(kind=dp) :: r
49
50
INTEGER :: ssize
51
INTEGER,ALLOCATABLE :: seed(:)
52
LOGICAL, SAVE :: Firsttime=.TRUE.
53
LOGICAL :: Found
54
55
CHARACTER(LEN=MAX_NAME_LEN),SAVE :: Rtype
56
57
IF (Firsttime) THEN
58
CALL random_seed(size=ssize)
59
allocate(seed(ssize))
60
61
seed = ListGetInteger( Model % Constants, 'Random Seed',Found )
62
IF (Found) call random_seed( put=seed )
63
CALL random_seed(get=seed)
64
65
Rtype = ListGetString( Model % Constants, 'Random Function',UnFoundFatal=.TRUE.)
66
deallocate(seed)
67
Firsttime=.FALSE.
68
ENDIF
69
70
71
SELECT CASE(Rtype)
72
CASE('even')
73
r = EvenRandom()
74
CASE('normal')
75
r = NormalRandom()
76
CASE DEFAULT
77
CALL FATAL('Random Function','Random Function should be <even> or <normal>')
78
END SELECT
79
80
r = r*x(1)+x(2)
81
82
End FUNCTION Random
83
84
85
86