Skip to content

Start tutorials #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions _data/nav.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- title: Tutorial
url: /tutorial/
- title: Learn
url: /learn/
- title: Compilers
url: /compilers/
- title: Development
Expand Down
5 changes: 5 additions & 0 deletions _includes/important.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="aside-important">
<b> Important: </b>
{{ include.content }}

</div>
5 changes: 5 additions & 0 deletions _includes/note.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="aside-note">
<b> Note: </b>
{{ include.content }}

</div>
5 changes: 5 additions & 0 deletions _includes/tip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="aside-tip">
<b> Tip: </b>
{{ include.content }}

</div>
1 change: 1 addition & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.0/normalize.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link href="/assets/css/main.css" rel="stylesheet">
<link href="/assets/css/syntax.css" rel="stylesheet">
<link rel="canonical" href="{{ site.url }}{% if page.canonical_url %}{{ page.canonical_url }}{% else %}{{ page.url }}{% endif %}" />
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
Expand Down
25 changes: 25 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,28 @@ footer a {
.newsletter h1 {
margin-bottom: 0px;
}

.aside-note {
color: gray;
/* border-left: 5px solid #734f96; */
border-left: 5px solid gray;
font-size: 16px;
padding-left: 10px;
margin: 20px 0;
}

.aside-tip {
color: gray;
border-left: 5px solid #3c92d1;
font-size: 16px;
padding-left: 10px;
margin: 20px 0;
}

.aside-important {
border-left: 5px solid #c7254e;
font-size: 16px;
padding-left: 10px;
margin: 20px 0;
background-color: #f9f2f4;
}
73 changes: 73 additions & 0 deletions assets/css/syntax.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
friendly.css
source: https://github.com/richleland/pygments-css (Unlicense/PD)
*/
.highlight .hll { background-color: #ffffcc }
.highlight { background: #f0f0f0; }
.highlight .c { color: #60a0b0; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #007020; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .ch { color: #60a0b0; font-style: italic } /* Comment.Hashbang */
.highlight .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #007020 } /* Comment.Preproc */
.highlight .cpf { color: #60a0b0; font-style: italic } /* Comment.PreprocFile */
.highlight .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */
.highlight .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0044DD } /* Generic.Traceback */
.highlight .kc { color: #007020; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #007020 } /* Keyword.Pseudo */
.highlight .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #902000 } /* Keyword.Type */
.highlight .m { color: #40a070 } /* Literal.Number */
.highlight .s { color: #4070a0 } /* Literal.String */
.highlight .na { color: #4070a0 } /* Name.Attribute */
.highlight .nb { color: #007020 } /* Name.Builtin */
.highlight .nc { color: #0e84b5; font-weight: bold } /* Name.Class */
.highlight .no { color: #60add5 } /* Name.Constant */
.highlight .nd { color: #555555; font-weight: bold } /* Name.Decorator */
.highlight .ni { color: #d55537; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #007020 } /* Name.Exception */
.highlight .nf { color: #06287e } /* Name.Function */
.highlight .nl { color: #002070; font-weight: bold } /* Name.Label */
.highlight .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #062873; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #bb60d5 } /* Name.Variable */
.highlight .ow { color: #007020; font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #40a070 } /* Literal.Number.Bin */
.highlight .mf { color: #40a070 } /* Literal.Number.Float */
.highlight .mh { color: #40a070 } /* Literal.Number.Hex */
.highlight .mi { color: #40a070 } /* Literal.Number.Integer */
.highlight .mo { color: #40a070 } /* Literal.Number.Oct */
.highlight .sa { color: #4070a0 } /* Literal.String.Affix */
.highlight .sb { color: #4070a0 } /* Literal.String.Backtick */
.highlight .sc { color: #4070a0 } /* Literal.String.Char */
.highlight .dl { color: #4070a0 } /* Literal.String.Delimiter */
.highlight .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #4070a0 } /* Literal.String.Double */
.highlight .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #4070a0 } /* Literal.String.Heredoc */
.highlight .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */
.highlight .sx { color: #c65d09 } /* Literal.String.Other */
.highlight .sr { color: #235388 } /* Literal.String.Regex */
.highlight .s1 { color: #4070a0 } /* Literal.String.Single */
.highlight .ss { color: #517918 } /* Literal.String.Symbol */
.highlight .bp { color: #007020 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #06287e } /* Name.Function.Magic */
.highlight .vc { color: #bb60d5 } /* Name.Variable.Class */
.highlight .vg { color: #bb60d5 } /* Name.Variable.Global */
.highlight .vi { color: #bb60d5 } /* Name.Variable.Instance */
.highlight .vm { color: #bb60d5 } /* Name.Variable.Magic */
.highlight .il { color: #40a070 } /* Literal.Number.Integer.Long */
166 changes: 166 additions & 0 deletions learn/beginner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
---
layout: page
title: Beginner tutorial
permalink: /learn/beginner
---

This tutorial is aimed at those with little or no programming experience or those
wishing to refresh their knowledge of the basics.

Jump to:

- [Hello world first program](#hello-world)
- [Variables](#variables)
- [Controlling program flow (if,loops)](#controlling-program-flow)
- [Procedures (functions,subroutines)](#procedures)

## Hello world

In this tutorial series we will use the free and open source [GNU Fortran compiler (gfortran)](https://gcc.gnu.org/fortran/).
If you haven't already setup a Fortan compiler on your system, [see here](/learn/setup) for how to do so.

{% include note.html content="Fortran is a <b>compiled language</b> which means that when written it must be passed through a
compiler to produce a machine executable that can be run." %}

Once you have setup up your compiler, open a new file in your favourite code editor and enter the following:

```fortran
program hello
print *, 'Hello, World!'
end program hello
```

save the file in an empty directory with the name `hello.f90`.

{% include note.html content=".f90 is the standard file extension for modern fortran source files.
The 90 refers to the first modern fortran standard in 1990." %}

Now to compile this program, first open your terminal and use the `cd` command to change directory to
where you saved your `hello.f90` file:

```shell
$> cd /path/to/hello
```

Now type the following to compile your program:

```shell
$> gfortran hello.f90 -o hello
```

Here we have told the compiler to output the compiled program to an executable file called `hello`.
To run this compiled program:

```shell
$> ./hello
Hello, World!
```

In this first example we have used the `program` keyword followed by our program name to signify
the start of our executable program. This is followed by the code we wish to execute and subsequently
_closed_ with a matching `end program` to signify where the program ends.

## Variables

Computer programs perform operations on data - this data is stored in _variables_ which each have
a name and a type.

Fortran comes with 5 built-in data types:

* `integer` -- for data that represent whole numbers, positive or negative
* `real` -- for floating-point data (not a whole number)
* `complex` -- pair consisting of a real part and an imaginary part
* `character` -- for text data
* `logical` -- for data that represent boolean (true or false) values

### Declaring variables

Before we can use a variable, we must first __declare__ the variable - this tells the compiler what
the variable is called, it's data-type and its properties.

{% include note.html content="Fortran is a <b>statically typed</b> language which means the type of each
variable is fixed when the program is compiled - variable types cannot change while the program is running." %}

In the following example program we declare variables of each built-in type:

```fortran
program variables
implicit none

integer :: count
real :: pi
complex :: frequency
character :: initial
logical :: isOkay

end program variables
```

{% include tip.html content="Make sure to give your variables meaningful names and stick
to a consistent naming style; this will save a lot of time for yourself and others
when reading the code in the future." %}

Note the additional line at the beginning of the program: `implicit none`.
This statement tells the compiler that all variables will be explicitly declared; without
this statement variables will be implicitly typed according to the letter they begin with.

{% include important.html content="Always use the <b>implicit none</b> statement at
the beginning of each program and procedure. Implicit typing is considered bad practice in
modern programming since it hides information leading to more program errors." %}

### Assigning values

We can assign and reassign values to our variables using the _assignment_ operator `=`:

```fortran
count = 10
pi = 4.141592
frequency = (1.0,-0.5)
initial = 'A'
isOkay = .false.
```

Characters are surrounded by either single (`'`) or double quotes (`"`). In the above example, our
character variable only contains space for a single letter; later on we will see how to store multiple
characters (known as a _string_).

Logical or boolean values can be either `.true.` or `.false.`.

### Printing values

We can use the `print` statement introduced earlier to print variable values to screen:

```fortran
print *, 'The value of count (integer) is: ',count
print *, 'The value of pi (real) is: ',pi
print *, 'The value of frequency (complex) is: ',frequency
print *, 'The value of initial (character) is: ',initial
print *, 'The value of isOkay (logical) is: ',isOkay
```


## Arrays

## Controlling program flow

### Conditional construct (`if`)


### Loop constructs (`do`)

## Procedures

### Functions

Here's a function that returns a cube-root of a real number:

```fortran
real function cbrt(x)
real, intent(in) :: x
cbrt = x**(1. / 3)
end function cbrt
```

### Subroutines


5 changes: 5 additions & 0 deletions learn/best_practices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
layout: page
title: Fortran best practices
permalink: /learn/best_practices
---
37 changes: 37 additions & 0 deletions learn/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: page
title: Learn Fortran
permalink: /learn/
---


## Why learn Fortran?



## Tutorials

- [__Compiler setup__](/learn/setup): _setting up a fortran compiler for the first time._

- [__Beginner__](/learn/beginner): _for those with little or no programming experience_.
- Basic programming concepts
- Declaring variables and arrays of different data types
- Conditional and looping constructs

- __Intermediate__ (_under development_): _for those with some programming experience
- Syntax for variable declaration, conditional and looping constructs
- Dynamic arrays
- Writing functions and subroutines
- Using modules to group functions, subroutines and data

- __Advanced__ (_under development_): _for those familiar with advanced programming concepts_
- Fortran-specific concepts compared to other languages
- Fortran syntax for variable declaration, conditional and looping constructs
- Dynamic arrays and pointers
- Functions, subroutines, modules and submodules
- Derived types, methods and type-extension
- Generic programming

## Other articles

- [__Best practices__](/learn/best_practices): _recommendations and guidelines for writing good Fortran code._
38 changes: 38 additions & 0 deletions learn/setup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: page
title: Compiler setup
permalink: /learn/setup
---


Fortran is a compiled language which means that when written it must be passed through a
compiler to produce a machine executable that can be run.

This is in contrast to interpreted languages which can be run directly from the source file
using a special _interpreter_ program.

In this tutorial, we'll work with the free and open source
[GNU Fortran compiler (gfortran)](https://gcc.gnu.org/fortran/),
which is part of the
[GNU Compiler Colection (GCC)](https://gcc.gnu.org/).

To install gfortran on Linux, use your system package manager.
Otherwise, for macOS or Windows, refer to gfortran binaries from
[this page](https://gcc.gnu.org/install/binaries.html).

To check if you have _gfortran_ setup correctly, open a terminal and run the following command :

```shell
$> gfortran --version
```

this should output something like:

```
GNU Fortran 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

If this command works then you are ready to [write and compile your first Fortran program](/learn/beginner).
Loading