-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
771 lines (620 loc) · 15.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<meta name="author" content="Stefan Parviainen">
<meta name="keywords" content="fortran,programming">
<meta name="description" content="Introduction to modern Fortran through uncommented examples">
<title>Introduction to modern Fortran through (mostly) uncommented examples</title>
<style>
body { counter-reset: chapter; }
h2:before { content: counter(chapter) ". "; counter-increment: chapter;}
h2 { counter-reset: section; }
h3:before { content: counter(chapter) "." counter(section) " "; counter-increment: section; }
.typename { color: red; }
.attribute { color: orangered; }
.typeparam { color: orangered; }
.scalarvar { color: blue; font-style: italic;}
.arrvar { color: blue; font-style:italic; font-weight: bold; }
.funcname { color: purple; }
.subname { color: purple; }
.intrinsic { color: slateblue; }
.literal { color: gray; }
.loop { color: darkgoldenrod; }
.control { color: crimson; }
.other { color: limegreen; }
.comment {background: lightblue; }
.operator {color: brown; }
.special {color: darkgreen; }
</style>
</head>
<body>
<article>
<h1>Introduction to modern Fortran through (mostly) uncommented examples</h1>
<p>This documents presents some of the features of modern Fortran (mostly up to Fortran 2003), with an emphasis on things useful in scientific computing. In particular, less used features, such as object oriented code, is not presented. Due to complexity, presently interoperability between Fortran and C is also omitted. The <code>forall</code> construct is omitted on purpose since it can mostly be replaced by the superior <code>do concurrent</code>. Most Fortan intrinsic procedures are also left out, with the focus instead on Fortran the language. Coarrays will probably be included in the future. For an excellent (although much lengthier) description of the features of Fortran, see Modern Fortran Explained, by Metcalf et al. The same author also provides a nice overview of the development of the language in his paper, <a href='http://journal.info.unlp.edu.ar/journal/journal30/papers/JCST-Apr11-1.pdf'>The Seven Ages of Fortran</a>.</p>
<p>The reader is assumed to be familiar with programming. The examples presented here are mostly uncommented. This is to encourage the reader to read and actually think about what is going on. It is easy to find more information about a confusing topic by using a simple web search.</p>
<p>Due to historical baggage there are many ways to do the same things in Fortran, some better than others. This documents tries to err on the safe side as to not teach any bad habits to new Fortraneers. In particular, implicit types are disabled and procedures are placed inside modules so that they obtain an implicit interface and the programmer gets to enjoy the benefits of type safety. Additionally, intent is always specified for procedure arguments.</p>
<section id='toc'/>
<section id='minimal'>
<h2><a href='#minimal'>Minimal Fortran program</a></h2>
<pre><code>
program program_example
implicit none
print *, "Hello, world"
end program
</code></pre>
</section>
<section id='types'>
<h2><a href='#types'>Types and variables</a></h2>
<section id='basic_types'>
<h3><a href='#basic_types'>Basic types</a></h3>
<pre><code>
program variables_example
implicit none
integer :: a, b
integer, dimension(10) :: array_with_10_elements
integer, dimension(-5:4) :: another_array_with_10_elements
real :: c
double precision :: d
complex :: e
logical :: f
character(len=80) :: string_with_max_80_chars
integer, parameter :: g = 10
integer(kind=selected_int_kind(10)) :: int_with_max_value_1e10
real(kind=selected_real_kind(5, 10)) :: real_with_precision_5_max_value_1e10
end program
</code></pre>
</section>
<section id='derived_types'>
<h3><a href='#derived_types'>Derived types</a></h3>
<pre><code>
program derivedtype_example
implicit none
type point2d
real :: x
real :: y
end type
type, extends(point2d) :: point3d
real :: z
end type
type(point3d) :: pt
pt%x = 0
pt%point2d%y = 1
pt%z = 1
print *, pt
end program
</code></pre>
</section>
<section id='pointer'>
<h3><a href='#pointer'>Pointer</a></h3>
<pre><code>
program pointer_example
implicit none
integer, target :: i
integer, pointer :: j
j => i
j = 1
print *, i
end program
</code></pre>
</section>
<section id='associate'>
<h3><a href='#associate'>Associate</a></h3>
<pre><code>
program associate_example
implicit none
integer :: i
associate(j => i)
j = 1
end associate
print *, i
end program
</code></pre>
</section>
<section id='block'>
<h3><a href='#block'>Block</a></h3>
<pre><code>
program block_example
implicit none
integer :: i
i = 1
block
integer :: j
j = 2
end block
print *, j ! Error, undefined outside block
end program
</code></pre>
</section>
</section>
<section id='arrays'>
<h2><a href='#arrays'>Arrays and matrices</a></h2>
<section id='array_elements'>
<h3><a href='#array_elements'>Array elements</a></h3>
<pre><code>
program array_example
implicit none
integer, dimension(-2:2) :: a
a = 0
a = [1, 2, 3, 4, 5]
a(-2) = 1
a(-1:0) = 3
a(1:) = [1, 2]
a([-2, 0, 2]) = [7, 8, 9]
a(-2:2:2) = [7, 8, 9]
end program
</code></pre>
</section>
<section id='multidimensional_arrays'>
<h3><a href='#multidimensional_arrays'>Multidimensional arrays</a></h3>
<pre><code>
program multidim_example
implicit none
integer, dimension(3, 3) :: a
a = 0
a = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3])
a(3, 3) = 1
a(1, :) = [2, 3, 4]
a(:, 1:2) = reshape([1, 2, 3, 4, 5, 6], [3, 2])
end program
</code></pre>
</section>
<section id='advanced_constuctors'>
<h3><a href='#advanced_constuctors'>Advanced array constructors</a></h3>
<pre><code>
program arrayconstructor_example
implicit none
integer :: i, j
print *, [((i*j, i=1, 3), j=1, 3)]
end program
</code></pre>
</section>
<section id='dynamic_arrays'>
<h3><a href='#dynamic_arrays'>Dynamic array allocation</a></h3>
<pre><code>
program allocatable_example
implicit none
integer, allocatable, dimension(:) :: a
allocate(a(10))
print *, shape(a)
end program
</code></pre>
</section>
<section id='automatic_allocation'>
<h3><a href='automatic_allocation'>Automatic array allocation</a></h3>
<pre><code>
program autoallocate_example
implicit none
integer, allocatable, dimension(:) :: a
a = [1, 2, 3]
print *, shape(a)
a = [1, 2, 3, 4, 5]
print *, shape(a)
end program
</code></pre>
</section>
<section id='array_math'>
<h3><a href='#array_math'>Array mathematics</a></h3>
<pre><code>
program arraymath_example
implicit none
integer, parameter, dimension(2) :: a1 = [1, 2]
integer, parameter, dimension(2) :: a2 = [3, 4]
print *, 2*a1
print *, 2+a1
print *, a1+a2
print *, a1*a2
end program
</code></pre>
</section>
</section>
<section id='control_structures'>
<h2><a href='#control_structures'>Control structures</a></h2>
<section id='if'>
<h3><a href='#if'>If</a></h3>
<pre><code>
program if_example
implicit none
integer, parameter :: i = 4
if (i < 0 .and. i /= -2) then
print *, "Case 1"
else if (i == 0 .or. i == 2) then
print *, "Case 2"
else
print *, "Case 3"
end if
end program
</code></pre>
</section>
<section id='select'>
<h3><a href='#select'>Select case</a></h3>
<pre><code>
program case_example
implicit none
integer, parameter :: i = 4
select case(i)
case (:-1)
print *, "i < 0"
case (0)
print *, "i = 0"
case (1:)
print *, "i > 0"
case default
print *, "An error occurred"
stop
end select
end program
</code></pre>
</section>
<section id='do'>
<h3><a href='#do'>Do</a></h3>
<pre><code>
program do_example
implicit none
integer :: i
do i=1, 10
print *, "Loop iteration", i
end do
end program
</code></pre>
</section>
<section id='do_while'>
<h3><a href='#do_while'>Do-while</a></h3>
<pre><code>
program dowhile_example
implicit none
integer :: i
i = 0
do while (i < 10)
print *, "Loop iteration", i
i = i + 1
end do
end program
</code></pre>
</section>
<section id='concurrent'>
<h3><a href='#concurrent'>Do-concurrent</a></h3>
<pre><code>
program doconcurrent_example
implicit none
integer :: i, j
real, dimension(10, 10) :: a
do concurrent (i=lbound(a, 1):ubound(a, 1), j=lbound(a, 2):ubound(a, 2))
a(i, j) = i + j
end do
print *, a
end program
</code></pre>
</section>
<section id='where'>
<h3><a href='#where'>Where</a></h3>
<pre><code>
program where_example
implicit none
integer, dimension(4) :: a
a = [1, 2, 3, 4]
where (a > 2)
a = 1
elsewhere
a = 0
end where
print *, a
end program
</code></pre>
</section>
</section>
<section id='procedures'>
<h2><a href='#procedures'>Procedures</a></h2>
<section id='subroutines'>
<h3><a href='#subroutines'>Subroutines</a></h3>
<pre><code>
module example_module
implicit none
contains
subroutine square(x)
integer, intent(inout) :: x
x = x**2
return
end subroutine
end module
program subroutine_example
use example_module, only: square
implicit none
integer :: x
x = 2
call square(x)
print *, x
end program
</code></pre>
</section>
<section id='functions'>
<h3><a href='#functions'>Functions</a></h3>
<pre><code>
module example_module
implicit none
contains
integer function square(x)
integer, intent(in) :: x
square = x**2
return
end function
end module
program function_example
use example_module, only: square
implicit none
integer :: x
x = 2
print *, square(x)
end program
</code></pre>
</section>
<section id='intent'>
<h3><a href='#intent'>Intent</a></h3>
<pre><code>
module example_module
implicit none
contains
subroutine f(x, y, z)
integer, intent(in) :: x
integer, intent(inout) :: y
integer, intent(out) :: z
x = 4 ! Error, not allowed due to intent
z = 4
y = z ! Error, not allowed due to intent
y = x*y
end subroutine
end module
program intent_example
use example_module, only: f
implicit none
integer :: x, y, z
x = 1
y = 2
z = 3
print *, f(x, y, z), x, y, z
end program
</code></pre>
</section>
<section id='recursion'>
<h3><a href='#recursion'>Recursive functions</a></h3>
<pre><code>
module example_module
implicit none
contains
recursive integer function factorial(n) result(res)
integer, intent(in) :: n
if(n <= 1) then
res = 1
else
res = n*factorial(n-1)
end if
end function
end module
program recursive_example
use example_module, only: factorial
implicit none
print *, factorial(5)
end program
</code></pre>
</section>
<section id='save'>
<h3><a href='#save'>Save attribute</a></h3>
<pre><code>
module example_module
implicit none
contains
integer function foo()
integer, save :: times_called = 0
times_called = times_called + 1
foo = times_called
end function
end module
program save_example
use example_module, only: foo
implicit none
print *, foo(), foo()
end program
</code></pre>
</section>
<section id='optional'>
<h3><a href='#optional'>Optional arguments</a></h3>
<pre><code>
module example_module
implicit none
contains
integer function foo(x)
integer, intent(in), optional :: x
if(present(x)) then
foo = x
else
foo = -1
end if
end function
end module
program optional_example
use example_module, only: foo
implicit none
print *, foo(), foo(1)
end program
</code></pre>
</section>
<section id='pure'>
<h3><a href='#pure'>Pure functions</a></h3>
<pre><code>
module example_module
implicit none
integer :: a
contains
pure integer function f(x) ! Error, function can't be pure due to the content below
integer :: x ! Error, must have intent(in)
integer, save :: file_unit ! Error, can't save state
a = 1 ! Error, can't assign to global or module variables
open(newunit=file_unit, file="example") ! Error, can't use I/O
end function
end module
program pure_example
use example_module, only: f
implicit none
print *, f(1)
end program
</code></pre>
</section>
<section id='elemental'>
<h3><a href='#elemental'>Elemental functions</a></h3>
<pre><code>
module example_module
implicit none
contains
pure elemental integer function square(x)
integer, intent(in) :: x
square = x**2
end function
end module
program elemental_example
use example_module, only: square
implicit none
integer, dimension(4) :: a
a = [1, 2, 3, 4]
print *, square(a)
end program
</code></pre>
</section>
<section id='array_arguments'>
<h3><a href='#array_argunmets'>Array arguments</a></h3>
<pre><code>
module example_module
implicit none
contains
subroutine foo(a1, a2)
integer, dimension(:) :: a1
integer, dimension(:, :) :: a2
print *, a1
print *, a2
end subroutine
end module
program arrayarg_example
use example_module, only: foo
implicit none
call foo([1, 2], reshape([1, 2, 3, 4], [2, 2]))
end program
</code></pre>
</section>
<section id='functions_as_arguments'>
<h3><a href='#functions_as_arguments'>Functions as arguments</a></h3>
<pre><code>
module example_module
implicit none
contains
integer function value1(f)
interface
integer function f(x)
integer, intent(in) :: x
end function
end interface
value1 = f(1)
end function
integer function foo(x)
integer, intent(in) :: x
foo = 2 * x + 3
end function
end module
program funcarg_example
use example_module, only: value1, foo
implicit none
print *, value1(foo)
end program
</code></pre>
</section>
<section id='overloading'>
<h3><a href='#overloading'>Overloading</a></h3>
<pre><code>
module example_module
implicit none
interface square
module procedure int_square, real_square
end interface
contains
integer function int_square(x)
integer, intent(in) :: x
int_square = x**2
end function
real function real_square(x)
real, intent(in) :: x
real_square = x**2
end function
end module
program overload_example
use example_module, only: square
implicit none
print *, square(2), square(2.0)
end program
</code></pre>
</section>
</section>
<section id='io'>
<h2><a href='#io'>Input/Output</a></h2>
<section id='output'>
<h3><a href='#output'>Output</a></h3>
<pre><code>
program output_example
implicit none
print *, "A string", 1, 1.0, [1, 2, 3]
end program
</code></pre>
</section>
<section id='format'>
<h3><a href='#format'>Format strings</a></h3>
<pre><code>
program format_example
implicit none
print "(2F6.3, F4.1)", 1.0, 2.0, 3.0
end program
</code></pre>
</section>
<section id='input'>
<h3><a href='#input'>Input</a></h3>
<pre><code>
program input_example
implicit none
integer :: i
double precision :: j
read (*, *) i, j
print *, "You entered the numbers", i, j
end program
</code></pre>
</section>
<section id='files'>
<h3><a href='#files'>Simple file access</a></h3>
<pre><code>
program file_example
implicit none
integer :: file_unit
character(len=80) :: text
open(newunit=file_unit, file="file_name")
write (file_unit, *) "Hello, world"
read (file_unit, *) text
print *, text
end program
</code></pre>
</section>
<section id='stringio'>
<h3><a href='#stringio'>String IO</a></h3>
<pre><code>
program stringio_example
implicit none
character(2) :: text = "42"
character(len=80) :: text2
integer :: number
read (text, *) number
write (text2, *) number
print *, number, text2
end program
</code></pre>
</section>
</section>
<footer>
<a href='https://creativecommons.org/publicdomain/zero/1.0/'>No rights reserved</a>. <a href='https://github.com/pafcu/FortranByExamples'>Source on Github.</a>
</footer>
</article>
</body>
</html>