Octopus
unit.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19!
36
37#include "global.h"
38
39module unit_oct_m
40 use global_oct_m
41 implicit none
42
43 private
44 public :: &
45 unit_t, &
49 operator(*), &
50 operator(/), &
51 operator(**), &
52 sqrt
53
54 type unit_t
55 ! Components are public by default
56 real(real64) :: factor
57 character(len=20) :: abbrev
58 character(len=50) :: name
59 end type unit_t
60
61 interface operator (*)
62 module procedure units_multiply
63 end interface operator (*)
64
65 interface operator (/)
66 module procedure units_divide
67 end interface operator (/)
68
69 interface operator (**)
70 module procedure units_pow
71 end interface operator (**)
72
73 interface units_to_atomic
74 module procedure dunits_to_atomic, zunits_to_atomic
75 end interface units_to_atomic
76
77 interface units_from_atomic
79 end interface units_from_atomic
80
81 interface sqrt
82 module procedure units_sqrt
83 end interface sqrt
84
85contains
86
87 !-----------------------------------------------
88
89 real(real64) elemental pure function dunits_to_atomic(this, val) result(res)
90 type(unit_t), intent(in) :: this
91 real(real64), intent(in) :: val
92
93 res = val*this%factor
94
95 end function dunits_to_atomic
96
97 !-----------------------------------------------
98
99 complex(real64) elemental pure function zunits_to_atomic(this, val) result(res)
100 type(unit_t), intent(in) :: this
101 complex(real64), intent(in) :: val
102
103 res = val*this%factor
104
105 end function zunits_to_atomic
106
107 !-----------------------------------------------
108
109 real(real64) elemental pure function dunits_from_atomic(this, val) result(res)
110 type(unit_t), intent(in) :: this
111 real(real64), intent(in) :: val
112
113 res = val/this%factor
114
115 end function dunits_from_atomic
116
117 !-----------------------------------------------
118
119 complex(real64) elemental pure function zunits_from_atomic(this, val) result(res)
120 type(unit_t), intent(in) :: this
121 complex(real64), intent(in) :: val
122
123 res = val/this%factor
124
125 end function zunits_from_atomic
126
127 !-----------------------------------------------
128
129 character(len=20) pure function units_abbrev(this) result(abbrev)
130 type(unit_t), intent(in) :: this
131
132 abbrev = this%abbrev
133 end function units_abbrev
134
135 !-----------------------------------------------
136
137 type(unit_t) pure function units_multiply(aa, bb) result(cc)
138 type(unit_t), intent(in) :: aa
139 type(unit_t), intent(in) :: bb
140
141 cc%factor = aa%factor*bb%factor
142 cc%abbrev = trim(aa%abbrev)//'*'//trim(bb%abbrev)
143
144 end function units_multiply
145
146 !-----------------------------------------------
148 type(unit_t) pure function units_divide(aa, bb) result(cc)
149 type(unit_t), intent(in) :: aa
150 type(unit_t), intent(in) :: bb
152 cc%factor = aa%factor/bb%factor
153 cc%abbrev = trim(aa%abbrev)//'/'//trim(bb%abbrev)
155 end function units_divide
156 !-----------------------------------------------
157
158 type(unit_t) pure function units_pow(aa, nn) result(cc)
159 type(unit_t), intent(in) :: aa
160 integer, intent(in) :: nn
161
162 cc%factor = aa%factor**nn
163
164 ! We have to do the conversion by hand. This is ugly, but we
165 ! cannot use write here since this function might be called inside
166 ! another write (stupid Fortran).
167
168 select case (nn)
169 case (-3)
170 cc%abbrev = trim(aa%abbrev)//'^-3'
171 case (-2)
172 cc%abbrev = trim(aa%abbrev)//'^-2'
173 case (-1)
174 cc%abbrev = trim(aa%abbrev)//'^-1'
175 case (0)
176 cc%abbrev = '1'
177 case (1)
178 cc%abbrev = trim(aa%abbrev)
179 case (2)
180 cc%abbrev = trim(aa%abbrev)//'^2'
181 case (3)
182 cc%abbrev = trim(aa%abbrev)//'^3'
183 case (4)
184 cc%abbrev = trim(aa%abbrev)//'^4'
185 case (5)
186 cc%abbrev = trim(aa%abbrev)//'^5'
187 case default
188 cc%abbrev = trim(aa%abbrev)//'^n'
189 end select
190
191 end function units_pow
193
194 !-----------------------------------------------
195
196 type(unit_t) pure function units_sqrt(aa) result(cc)
197 type(unit_t), intent(in) :: aa
198
199 cc%factor = sqrt(aa%factor)
200 cc%abbrev = 'sqrt('//trim(aa%abbrev)//')'
201
202 end function units_sqrt
203
204end module unit_oct_m
205
206!! Local Variables:
207!! mode: f90
208!! coding: utf-8
209!! End:
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:132
type(unit_t) pure function units_sqrt(aa)
Definition: unit.F90:290
real(real64) elemental pure function dunits_to_atomic(this, val)
Definition: unit.F90:183
complex(real64) elemental pure function zunits_to_atomic(this, val)
Definition: unit.F90:193
type(unit_t) pure function units_multiply(aa, bb)
Definition: unit.F90:231
type(unit_t) pure function units_divide(aa, bb)
Definition: unit.F90:242
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:223
real(real64) elemental pure function dunits_from_atomic(this, val)
Definition: unit.F90:203
complex(real64) elemental pure function zunits_from_atomic(this, val)
Definition: unit.F90:213
type(unit_t) pure function units_pow(aa, nn)
Definition: unit.F90:252