Cleaned up the page tag usage
This commit is contained in:
parent
663ac1cff6
commit
688531adfc
@ -158,7 +158,7 @@ $ ./main
|
|||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<paragraph>TLDR: Functions are assigned as variables, and subroutines are imported and called with the <code-tag>call</code-tag> keyword.</paragraph>
|
<paragraph class="font-bold">TLDR: Functions are assigned as variables, and subroutines are imported and called with the <code-tag>call</code-tag> keyword.</paragraph>
|
||||||
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -1,50 +1,48 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
|
||||||
import CodeTag from "../../../tags/CodeTag.vue";
|
import CodeTag from "../../../tags/CodeTag.vue";
|
||||||
|
import Paragraph from "../../../tags/Paragraph.vue";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<paragraph>Data Types</paragraph>
|
||||||
<h1 class="text-3xl">Data Types</h1>
|
|
||||||
<hr class="mb-6">
|
|
||||||
|
|
||||||
<p class="mb-6">Just like any other programming language that has datatypes, Fortran has datatypes as well. The big difference, from
|
<paragraph>Just like any other programming language that has datatypes, Fortran has datatypes as well. The big difference, from
|
||||||
other languages, of cause, is how they're created and assigned. If you're a developer of, for example, C#, Java or
|
other languages, of cause, is how they're created and assigned. If you're a developer of, for example, C#, Java or
|
||||||
even Python, you will feel the difference pretty fast.<br><br>
|
even Python, you will feel the difference pretty fast.<br><br>
|
||||||
|
|
||||||
Let's look at a small list of some common types.
|
Let's look at a small list of some common types.</paragraph>
|
||||||
</p>
|
|
||||||
|
|
||||||
<ul class="mb-6">
|
<ul class="mb-6">
|
||||||
<li>> <code>integer</code></li>
|
<li>> <code>integer</code></li>
|
||||||
<li>> <code>real</code></li>
|
<li>> <code>real</code></li>
|
||||||
<li>> <code>double precision</code></li>
|
<li>> <code>double precision</code></li>
|
||||||
<li>> <code>logical</code></li>
|
<li>> <code>logical</code></li>
|
||||||
<li>> <code>character</code></li>
|
<li>> <code>character</code></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p class="mb-6"><code class="text-red-500">integer</code> is, if you hadn't guessed, a whole number. Like most languages,
|
<paragraph><code-tag>integer</code-tag> is, if you hadn't guessed, a whole number. Like most languages,
|
||||||
<code class="text-red-500">int</code>. The <code class="text-red-500">real</code> type is like a
|
<code-tag>int</code-tag>. The <code-tag>real</code-tag> type is like a
|
||||||
<code class="text-red-500">float</code>. But the difference with Fortran types, in general, is that you have "less" types,
|
<code-tag>float</code-tag>. But the difference with Fortran types, in general, is that you have "less" types,
|
||||||
but you have more control over those types, making it essentially the same as if you had more types. More on that later.<br><br>
|
but you have more control over those types, making it essentially the same as if you had more types. More on that later.<br><br>
|
||||||
|
|
||||||
The <code class="text-red-500">double precision</code> is a double, obviously. And as you have probably guessed at
|
The <code-tag>double precision</code-tag> is a double, obviously. And as you have probably guessed at
|
||||||
this point, <code class="text-red-500">logical</code> is a boolean. <code class="text-red-500">character</code>
|
this point, <code-tag>logical</code-tag> is a boolean. <code-tag>character</code-tag>
|
||||||
however, is a little bit different. In "new" programming terms, it would be an array of characters that behaves
|
however, is a little bit different. In "new" programming terms, it would be an array of characters that behaves
|
||||||
like a string. Or like in SQL: <code class="text-red-500">VARCHAR(256)</code>.</p>
|
like a string. Or like in SQL: <code-tag>VARCHAR(256)</code-tag>.</paragraph>
|
||||||
|
|
||||||
<h2 class="text-2xl">integer</h2>
|
<h2 class="text-2xl">integer</h2>
|
||||||
<hr class="mb-6">
|
<hr class="mb-6">
|
||||||
|
|
||||||
<p class="mb-6">In Fortran, you can specify the size of an integer at declaration. You do that with <code class="text-red-500">(kind = n)</code>.
|
<paragraph>In Fortran, you can specify the size of an integer at declaration. You do that with <code-tag>(kind = n)</code-tag>.
|
||||||
So if you want a 16 bit integer, you do <code class="text-red-500">integer(kind = 16)</code>. Observe the following code example.
|
So if you want a 16 bit integer, you do <code-tag>integer(kind = 16)</code-tag>. Observe the following code example.
|
||||||
As an exercise for the reader, try to imagine what would be printed to the terminal.<br><br>
|
As an exercise for the reader, try to imagine what would be printed to the terminal.<br><br>
|
||||||
|
|
||||||
*Do note that we're using Fortran 90, and not the newer versions that supports unsigned integers.<a href="#footnote-1">[1]</a>
|
*Do note that we're using Fortran 90, and not the newer versions that supports unsigned integers.<a href="#footnote-1">[1]</a>
|
||||||
</p>
|
</paragraph>
|
||||||
|
|
||||||
<code-tag>main.f90</code-tag>
|
<code-tag>main.f90</code-tag>
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -72,11 +70,11 @@ program hello
|
|||||||
|
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Now, as the astute reader might have already figured, the output would look like this:</p>
|
<paragraph>Now, as the astute reader might have already figured, the output would look like this:</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -95,26 +93,25 @@ end program hello
|
|||||||
kind = 16
|
kind = 16
|
||||||
170141183460469231731687303715884105727
|
170141183460469231731687303715884105727
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">The <code-tag>huge(n)</code-tag> function simply assigns the highest largest number of any integer,
|
<paragraph>The <code-tag>huge(n)</code-tag> function simply assigns the highest largest number of any integer,
|
||||||
real or array type of any kind.<br><br>
|
real or array type of any kind.<br><br>
|
||||||
|
|
||||||
Any integer without a kind, is automatically a 4 byte integer. Integers with kind 2, is 2 bytes big. Integers with
|
Any integer without a kind, is automatically a 4 byte integer. Integers with kind 2, is 2 bytes big. Integers with
|
||||||
kind 4 is 4 bytes big. And so on and so forth.<br><br>
|
kind 4 is 4 bytes big. And so on and so forth.<br><br>
|
||||||
|
|
||||||
Usually, unless your working with huge datasets, <code-tag>integer</code-tag> or if you want to be more semantic,
|
Usually, unless your working with huge datasets, <code-tag>integer</code-tag> or if you want to be more semantic,
|
||||||
<code-tag>integer(kind = 4)</code-tag> would be more than sufficient to work with.
|
<code-tag>integer(kind = 4)</code-tag> would be more than sufficient to work with.</paragraph>
|
||||||
</p>
|
|
||||||
|
|
||||||
<h2 class="text-2xl">real</h2>
|
<h2 class="text-2xl">real</h2>
|
||||||
<hr class="mb-6">
|
<hr class="mb-6">
|
||||||
|
|
||||||
<p class="mb-6">The <code-tag>real</code-tag> type is, in most other languages, a <code-tag>float</code-tag>. And with
|
<paragraph>The <code-tag>real</code-tag> type is, in most other languages, a <code-tag>float</code-tag>. And with
|
||||||
integers, real can also be specified with a <code-tag>kind(n)</code-tag> modifier. Observe this example:</p>
|
integers, real can also be specified with a <code-tag>kind(n)</code-tag> modifier. Observe this example:</paragraph>
|
||||||
|
|
||||||
<code-tag>main.f90</code-tag>
|
<code-tag>main.f90</code-tag>
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -142,11 +139,11 @@ program hello
|
|||||||
|
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">The output:</p>
|
<paragraph>The output:</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -166,19 +163,19 @@ end program hello
|
|||||||
1.18973149535723176508575932662800702E+4932
|
1.18973149535723176508575932662800702E+4932
|
||||||
3.36210314311209350626267781732175260E-4932
|
3.36210314311209350626267781732175260E-4932
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">As you can see here, we have included a new print statement. <code-tag>tiny(n)</code-tag> returns
|
<paragraph>As you can see here, we have included a new print statement. <code-tag>tiny(n)</code-tag> returns
|
||||||
the smallest positive number of a real kind. That function is not available for integers.</p>
|
the smallest positive number of a real kind. That function is not available for integers.</paragraph>
|
||||||
|
|
||||||
<h2 class="text-2xl">double precision</h2>
|
<h2 class="text-2xl">double precision</h2>
|
||||||
<hr class="mb-6">
|
<hr class="mb-6">
|
||||||
|
|
||||||
<p class="mb-6"><code-tag>double precision</code-tag> is pretty much just a bigger version of real. All of the same rules apply
|
<paragraph><code-tag>double precision</code-tag> is pretty much just a bigger version of real. All of the same rules apply
|
||||||
to double precision as the real type.</p>
|
to double precision as the real type.</paragraph>
|
||||||
|
|
||||||
<code-tag>main.f90</code-tag>
|
<code-tag>main.f90</code-tag>
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -191,11 +188,11 @@ program hello
|
|||||||
|
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">And the output looks like this:</p>
|
<paragraph>And the output looks like this:</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -203,24 +200,23 @@ end program hello
|
|||||||
1.7976931348623157E+308
|
1.7976931348623157E+308
|
||||||
2.2250738585072014E-308
|
2.2250738585072014E-308
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6"><code-tag>double precision</code-tag> does not support the kind modifier. A double precision number is
|
<paragraph><code-tag>double precision</code-tag> does not support the kind modifier. A double precision number is
|
||||||
also the same as an 8 byte real.</p>
|
also the same as an 8 byte real.</paragraph>
|
||||||
|
|
||||||
<h2 class="text-2xl">logical</h2>
|
<h2 class="text-2xl">logical</h2>
|
||||||
<hr class="mb-6">
|
<hr class="mb-6">
|
||||||
|
|
||||||
<p class="mb-6"><code-tag>logical</code-tag> is a boolean. It's either true or false. But a funny thing with the logical type in
|
<paragraph><code-tag>logical</code-tag> is a boolean. It's either true or false. But a funny thing with the logical type in
|
||||||
Fortran, compared to other languages, is that you can define the size of it. The logical type supports the
|
Fortran, compared to other languages, is that you can define the size of it. The logical type supports the
|
||||||
<code-tag>kind(n)</code-tag> modifier.<br><br>
|
<code-tag>kind(n)</code-tag> modifier.<br><br>
|
||||||
|
|
||||||
A good reason for the different sized logical, is optimization. Some systems handles larger object better than smaller,
|
A good reason for the different sized logical, is optimization. Some systems handles larger object better than smaller,
|
||||||
or some systems handles smaller objects better. It all depends on the architecture.
|
or some systems handles smaller objects better. It all depends on the architecture.</paragraph>
|
||||||
</p>
|
|
||||||
|
|
||||||
<code-tag>main.f90</code-tag>
|
<code-tag>main.f90</code-tag>
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -253,13 +249,13 @@ program hello
|
|||||||
|
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Now I have a small exercise for the reader again. Let's assume that we are using <code-tag>gfortran</code-tag>
|
<paragraph>Now I have a small exercise for the reader again. Let's assume that we are using <code-tag>gfortran</code-tag>
|
||||||
without specifying an optimization level. What do you think the output would look like?</p>
|
without specifying an optimization level. What do you think the output would look like?</paragraph>
|
||||||
|
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -282,18 +278,17 @@ end program hello
|
|||||||
value: T
|
value: T
|
||||||
size: 16
|
size: 16
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Huh, that was weird. The 8 bit logical is false, while the others are true. The reason for this behavior
|
<paragraph>Huh, that was weird. The 8 bit logical is false, while the others are true. The reason for this behavior
|
||||||
could be a multitude of things. One could be optimization, as the compiler tries to make the program faster. And
|
could be a multitude of things. One could be optimization, as the compiler tries to make the program faster. And
|
||||||
by setting the 8 bit logical to false. It could also be how an unassigned logical is represented. For more information,
|
by setting the 8 bit logical to false. It could also be how an unassigned logical is represented. For more information,
|
||||||
check the footnote number 2. <a href="#footnote-1">[2]</a><br><br>
|
check the footnote number 2. <a href="#footnote-1">[2]</a><br><br>
|
||||||
|
|
||||||
A good programming practise is to always assign your logical when creating them.</p>
|
A good programming practise is to always assign your logical when creating them.</paragraph>
|
||||||
|
|
||||||
|
<code-tag>main.f90</code-tag>
|
||||||
<code-tag>main.f90</code-tag>
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
logical :: bool = .true.;
|
logical :: bool = .true.;
|
||||||
logical(kind = 1) :: bool1 = .true.;
|
logical(kind = 1) :: bool1 = .true.;
|
||||||
@ -302,11 +297,11 @@ logical(kind = 4) :: bool4 = .true.;
|
|||||||
logical(kind = 8) :: bool8 = .true.;
|
logical(kind = 8) :: bool8 = .true.;
|
||||||
logical(kind = 16) :: bool16 = .true.;
|
logical(kind = 16) :: bool16 = .true.;
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Now, the output looks as expected.</p>
|
<paragraph>Now, the output looks as expected.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -329,18 +324,18 @@ logical(kind = 16) :: bool16 = .true.;
|
|||||||
value: T
|
value: T
|
||||||
size: 16
|
size: 16
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<h2 class="text-2xl">character</h2>
|
<h2 class="text-2xl">character</h2>
|
||||||
<hr class="mb-6">
|
<hr class="mb-6">
|
||||||
|
|
||||||
<p class="mb-6"><code-tag>character</code-tag> in fortran can be seen as either a <code-tag>char</code-tag> or
|
<paragraph><code-tag>character</code-tag> in fortran can be seen as either a <code-tag>char</code-tag> or
|
||||||
a <code-tag>string</code-tag> in other languages, depending on the length you give it.<br><br>
|
a <code-tag>string</code-tag> in other languages, depending on the length you give it.<br><br>
|
||||||
|
|
||||||
Observe the example below.</p>
|
Observe the example below.</paragraph>
|
||||||
|
|
||||||
<code-tag>main.f90</code-tag>
|
<code-tag>main.f90</code-tag>
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -360,11 +355,11 @@ program hello
|
|||||||
|
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Output looks like this:</p>
|
<paragraph>Output looks like this:</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -378,24 +373,24 @@ end program hello
|
|||||||
value: a lol
|
value: a lol
|
||||||
size: 10
|
size: 10
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">lol. With Fortran, if you create a character, with a certain size, but only assign it a smaller vallue,
|
<paragraph>lol. With Fortran, if you create a character, with a certain size, but only assign it a smaller vallue,
|
||||||
the trailing indexes in the character will be filled with whitespace.<br><br>
|
the trailing indexes in the character will be filled with whitespace.<br><br>
|
||||||
|
|
||||||
To mitigate this, we could either set the correct length when creating the character, or we could use the <code-tag>trim(value)</code-tag>
|
To mitigate this, we could either set the correct length when creating the character, or we could use the <code-tag>trim(value)</code-tag>
|
||||||
function. It removes trailing whitespaces from strings.</p>
|
function. It removes trailing whitespaces from strings.</paragraph>
|
||||||
|
|
||||||
<code-tag>main.f90</code-tag>
|
<code-tag>main.f90</code-tag>
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
trim(char2)//'lol';
|
trim(char2)//'lol';
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Now, the output should be fixed:</p>
|
<paragraph>Now, the output should be fixed:</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
> gfortran main.f90
|
> gfortran main.f90
|
||||||
> ./a.out
|
> ./a.out
|
||||||
@ -413,16 +408,11 @@ trim(char2)//'lol';
|
|||||||
value: alol
|
value: alol
|
||||||
size: 10
|
size: 10
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
|
||||||
|
|
||||||
<p>Footnotes:</p>
|
|
||||||
<ul>
|
|
||||||
<li id="footnote-1">[1] <a href="https://gcc.gnu.org/onlinedocs/gfortran/Unsigned-integers.html" class="underline">unsigned integers with gfortran</a></li>
|
|
||||||
<li id="footnote-2">[2] <a href="https://stackoverflow.com/a/71190625" class="underline">Why would using uninitialized variables in Fortran ever work? [duplicate]</a></li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
|
||||||
|
|
||||||
<style scoped>
|
<p>Footnotes:</p>
|
||||||
|
<ul>
|
||||||
</style>
|
<li id="footnote-1">[1] <a href="https://gcc.gnu.org/onlinedocs/gfortran/Unsigned-integers.html" class="underline">unsigned integers with gfortran</a></li>
|
||||||
|
<li id="footnote-2">[2] <a href="https://stackoverflow.com/a/71190625" class="underline">Why would using uninitialized variables in Fortran ever work? [duplicate]</a></li>
|
||||||
|
</ul>
|
||||||
|
</template>
|
@ -1,47 +1,48 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
import Paragraph from "../../../tags/Paragraph.vue";
|
||||||
|
import CodeTag from "../../../tags/CodeTag.vue";
|
||||||
|
import PageStart from "../../../tags/PageStart.vue";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<page-start>Hello World In Fortran 90</page-start>
|
||||||
<h1 class="text-3xl">Hello World In Fortran 90</h1>
|
|
||||||
<hr class="mb-6">
|
|
||||||
|
|
||||||
<p class="mb-6">A Hello World program is always a great step into a new programming language. It gives a simple oversight into
|
<paragraph>A Hello World program is always a great step into a new programming language. It gives a simple oversight into
|
||||||
the build process of said language. And with a simple project come a simple intro (mostly). But fret not, Fortran
|
the build process of said language. And with a simple project come a simple intro (mostly). But fret not, Fortran
|
||||||
can be simple or complex, depending on the size of the project.</p>
|
can be simple or complex, depending on the size of the project.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
print *, 'Hello world!'
|
print *, 'Hello world!'
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">In the good ol' days (before FORTRAN 77), when printing to console, you would write <span><code class="text-red-500">write(*,*)</code></span>,
|
<paragraph>In the good ol' days (before FORTRAN 77), when printing to console, you would write <code-tag>write(*,*)</code-tag>,
|
||||||
but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword: <span><code class="text-red-500">print</code></span>. <br><br>
|
but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword: <code-tag>print</code-tag>. <br><br>
|
||||||
|
|
||||||
With modern compilers, running the newest Fortran standards, <span><code class="text-red-500">write(*,*) 'Hello world!'</code>
|
With modern compilers, running the newest Fortran standards, <code-tag>write(*,*) 'Hello world!'</code-tag>
|
||||||
and <code class="text-red-500">print *, 'Hello world!'</code></span>
|
and <code-tag>print *, 'Hello world!'</code-tag>
|
||||||
will compile to the exact same assembly. So if you only need to print something to stdout, just use <span><code class="text-red-500">print *</code></span>
|
will compile to the exact same assembly. So if you only need to print something to stdout, just use <code-tag>print *</code-tag>
|
||||||
as it conveys the meaning of the code better. Plus we don't need the full functionality of <span><code class="text-red-500">write(*,*)</code></span>
|
as it conveys the meaning of the code better. Plus we don't need the full functionality of <code-tag>write(*,*)</code-tag>
|
||||||
in this example.</p>
|
in this example.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
$ gfortran -o hello main.f90
|
$ gfortran -o hello main.f90
|
||||||
$ ./hello
|
$ ./hello
|
||||||
Hello world!
|
Hello world!
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Now, hear me out, why not spice up the program a little bit more? What if we want to read user input? Well, fret
|
<paragraph>Now, hear me out, why not spice up the program a little bit more? What if we want to read user input? Well, fret
|
||||||
not my friend, because I have just the solution for you then. All we gotta do is to read from the terminal, and
|
not my friend, because I have just the solution for you then. All we gotta do is to read from the terminal, and
|
||||||
then print out the value.</p>
|
then print out the value.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -58,31 +59,30 @@ program hello
|
|||||||
|
|
||||||
end program hello
|
end program hello
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">So what we're seeing here, is that we have created two variables: <span><code class="text-red-500">usertxt</code>
|
<paragraph>So what we're seeing here, is that we have created two variables: <code-tag>usertxt</code-tag>
|
||||||
and <code class="text-red-500">ios</code></span>.
|
and <code-tag>ios</code-tag>.<code-tag>usertxt</code-tag> is the input we're reading from the terminal. Although since Fortran 90 doesn't
|
||||||
<span><code class="text-red-500">usertxt</code></span> is the input we're reading from the terminal. Although since Fortran 90 doesn't
|
have an explicit string type, doesn't mean we can't read or write text, as the <code-tag>character</code-tag>
|
||||||
have an explicit string type, doesn't mean we can't read or write text, as the <span><code class="text-red-500">character</code></span>
|
type is just an array of 1000 characters long. It doesn't have to be 1000 characters, but it's what I chose on a
|
||||||
type is just an array of 1000 characters long. It doesn't have to be 1000 characters, but it's what I chose on a
|
whim.<br><br>
|
||||||
whim.<br><br>
|
|
||||||
|
|
||||||
The other variable, <span><code class="text-red-500">ios</code></span>, I probably don't need to introduce, as it's just an integer.
|
The other variable, <code-tag>ios</code-tag>, I probably don't need to introduce, as it's just an integer.
|
||||||
But what control-flow this value holds, I feel is quite important, as a lot of file-based business logic makes
|
But what control-flow this value holds, I feel is quite important, as a lot of file-based business logic makes
|
||||||
use of it.<br><br>
|
use of it.<br><br>
|
||||||
|
|
||||||
Introducing, <span><code class="text-red-500">iostat</code></span>. AKA, Input/Output Status Specifier. It indicates the status of the
|
Introducing, <code-tag>iostat</code-tag>. AKA, Input/Output Status Specifier. It indicates the status of the
|
||||||
I/O operation. If the integer is positive, it indicates an error code. If it's negative, it indicates an end of file
|
I/O operation. If the integer is positive, it indicates an error code. If it's negative, it indicates an end of file
|
||||||
condition. If it's zero, then it does not indicate anything yet. No error, no End Of File, no particular condition
|
condition. If it's zero, then it does not indicate anything yet. No error, no End Of File, no particular condition
|
||||||
has triggered yet.<br><br>
|
has triggered yet.<br><br>
|
||||||
|
|
||||||
So in this case, where we check if <span><code class="text-red-500">ios</code></span> is End Of File, then that means if we input an
|
So in this case, where we check if <code-tag>ios</code-tag> is End Of File, then that means if we input an
|
||||||
End Of File key combination (Ctrl+D on Unix/Linux, and Ctrl+Z on Windows), then the program would stop immediately.
|
End Of File key combination (Ctrl+D on Unix/Linux, and Ctrl+Z on Windows), then the program would stop immediately.
|
||||||
We use the <span><code class="text-red-500">trim()</code></span> function to remove any trailing whitespaces. That's because, if you
|
We use the <code-tag>trim()</code-tag> function to remove any trailing whitespaces. That's because, if you
|
||||||
make a character array longer than the actual text, it will be filled with whitespace after the text. So now the output
|
make a character array longer than the actual text, it will be filled with whitespace after the text. So now the output
|
||||||
looks like this:</p>
|
looks like this:</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
$ gfortran -o hello main.f90
|
$ gfortran -o hello main.f90
|
||||||
$ ./hello
|
$ ./hello
|
||||||
@ -90,6 +90,5 @@ $ ./hello
|
|||||||
Hello world :D
|
Hello world :D
|
||||||
Hello world :D
|
Hello world :D
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
@ -1,31 +1,28 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
import PageStart from "../../../tags/PageStart.vue";
|
||||||
|
import Paragraph from "../../../tags/Paragraph.vue";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<page-start>Fortran</page-start>
|
||||||
<h1 class="text-3xl">Fortran</h1>
|
|
||||||
<hr>
|
|
||||||
<p>Fortran (Formula Translation), is one of the ancient languages somehow still in use as of today. First introduced
|
|
||||||
in the 1950s by a team at IBM, Fortran was designed specifically for scientific and engineering applications,
|
|
||||||
where its ability to efficiently handle complex mathematical operations far outperformed rivaling languages at the
|
|
||||||
time.</p>
|
|
||||||
<br>
|
|
||||||
|
|
||||||
<p>
|
<paragraph>Fortran (Formula Translation), is one of the ancient languages somehow still in use as of today. First introduced
|
||||||
Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
|
in the 1950s by a team at IBM, Fortran was designed specifically for scientific and engineering applications,
|
||||||
2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
|
where its ability to efficiently handle complex mathematical operations far outperformed rivaling languages at the
|
||||||
in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
|
time.<br><br>
|
||||||
and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.</p>
|
|
||||||
<br>
|
|
||||||
|
|
||||||
<p>Table of contents:</p>
|
Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
|
||||||
<ul>
|
2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
|
||||||
<li><RouterLink to="/Fortran/SetUp">> Set Up</RouterLink></li>
|
in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
|
||||||
<li><RouterLink to="/Fortran/HelloWorld">> Hello World</RouterLink></li>
|
and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.</paragraph>
|
||||||
<li><RouterLink to="/Fortran/DataTypes">> Data Types</RouterLink></li>
|
|
||||||
<li><RouterLink to="/Fortran/Methods">> Method, Functions, Sub Routines and Calls</RouterLink></li>
|
<p>Table of contents:</p>
|
||||||
<li><RouterLink to="/Fortran/ReadingAFile">> Reading A File</RouterLink></li>
|
<ul>
|
||||||
</ul>
|
<li><RouterLink to="/Fortran/SetUp">> Set Up</RouterLink></li>
|
||||||
</div>
|
<li><RouterLink to="/Fortran/HelloWorld">> Hello World</RouterLink></li>
|
||||||
|
<li><RouterLink to="/Fortran/DataTypes">> Data Types</RouterLink></li>
|
||||||
|
<li><RouterLink to="/Fortran/Methods">> Method, Functions, Sub Routines and Calls</RouterLink></li>
|
||||||
|
<li><RouterLink to="/Fortran/ReadingAFile">> Reading A File</RouterLink></li>
|
||||||
|
</ul>
|
||||||
</template>
|
</template>
|
@ -2,18 +2,17 @@
|
|||||||
|
|
||||||
import PageStart from "../../../tags/PageStart.vue";
|
import PageStart from "../../../tags/PageStart.vue";
|
||||||
import CodeTag from "../../../tags/CodeTag.vue";
|
import CodeTag from "../../../tags/CodeTag.vue";
|
||||||
|
import Paragraph from "../../../tags/Paragraph.vue";
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<page-start>Reading A File In Fortran 90</page-start>
|
||||||
<page-start>Reading A File In Fortran 90</page-start>
|
|
||||||
|
|
||||||
<p class="mb-6">Reading files in high level programming languages is rather easy. I C#, you would just create a
|
<paragraph>Reading files in high level programming languages is rather easy. I C#, you would just create a
|
||||||
<code-tag>StreamReader</code-tag> object, and execute: <code-tag>streamReader.ReadLine()</code-tag> and bam,
|
<code-tag>StreamReader</code-tag> object, and execute: <code-tag>streamReader.ReadLine()</code-tag> and bam,
|
||||||
you've read the first line of the file. Now just check if the string returned is null, in a while loop, and you've
|
you've read the first line of the file. Now just check if the string returned is null, in a while loop, and you've
|
||||||
read the whole file. Easy peasy.<br><br>
|
read the whole file. Easy peasy.<br><br>
|
||||||
|
|
||||||
In Fortran 90, however, there is a little more than that. Not a lot more, to be honest, but there is a little more.</p>
|
In Fortran 90, however, there is a little more than that. Not a lot more, to be honest, but there is a little more.</paragraph>
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
@ -1,57 +1,58 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
|
||||||
|
import CodeTag from "../../../tags/CodeTag.vue";
|
||||||
|
import PageStart from "../../../tags/PageStart.vue";
|
||||||
|
import Paragraph from "../../../tags/Paragraph.vue";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<page-start>Setting Up The Environment</page-start>
|
||||||
<h1 class="text-3xl">Setting Up The Environment</h1>
|
|
||||||
<hr class="mb-6">
|
|
||||||
|
|
||||||
<p class="mb-6">If you're familiar with C, then you would feel right at home with Fortran's build environment. There isn't really
|
<paragraph>If you're familiar with C, then you would feel right at home with Fortran's build environment. There isn't really
|
||||||
an "official" compiler for the language, only a standard, that has to be implemented by the compilers. But there
|
an "official" compiler for the language, only a standard, that has to be implemented by the compilers. But there
|
||||||
are a lot of great compilers, that might as well be "official."<br><br>
|
are a lot of great compilers, that might as well be "official."<br><br>
|
||||||
|
|
||||||
The top listed compiler on Fortran's website, is the GNU Fortran compiler (gfortran). It's the one I will be using
|
The top listed compiler on Fortran's website, is the GNU Fortran compiler (gfortran). It's the one I will be using
|
||||||
throughout the examples on the site. Unless stated otherwise. There are also some other notable compilers, that
|
throughout the examples on the site. Unless stated otherwise. There are also some other notable compilers, that
|
||||||
has some rather interesting qualities to them.</p>
|
has some rather interesting qualities to them.</paragraph>
|
||||||
|
|
||||||
<p>Compilers:</p>
|
<p>Compilers:</p>
|
||||||
<ul class="mb-6">
|
<ul class="mb-6">
|
||||||
<li>> <a class="underline" href="https://lfortran.org/">lfortran</a> (This one can be used as an interactive
|
<li>> <a class="underline" href="https://lfortran.org/">lfortran</a> (This one can be used as an interactive
|
||||||
compiler, supports parsing all of the 2018 standard syntax and also compile Fortran to WebAssemply).</li>
|
compiler, supports parsing all of the 2018 standard syntax and also compile Fortran to WebAssemply).</li>
|
||||||
<li>> <a class="underline" href="https://developer.nvidia.com/hpc-sdk">NVIDIA HPC SDK</a> (This one comes with a whole lot of GPU
|
<li>> <a class="underline" href="https://developer.nvidia.com/hpc-sdk">NVIDIA HPC SDK</a> (This one comes with a whole lot of GPU
|
||||||
accelerated libraries).</li>
|
accelerated libraries).</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p class="mb-6">But we will be using gfortran. Through the examples on this site, I will mainly be focusing on developing on Linux. <br>
|
<paragraph>But we will be using gfortran. Through the examples on this site, I will mainly be focusing on developing on Linux. <br>
|
||||||
First, we install the compiler.</p>
|
First, we install the compiler.</paragraph>
|
||||||
|
|
||||||
<p>For Fedora.</p>
|
<p>For Fedora.</p>
|
||||||
<code class="text-red-500 mb-6">$ sudo dnf install gcc-gfortran</code>
|
<code-tag>$ sudo dnf install gcc-gfortran</code-tag>
|
||||||
|
|
||||||
<p>For ubuntu.</p>
|
<p>For ubuntu.</p>
|
||||||
<code class="text-red-500 mb-6">$ sudo apt install gfortran</code>
|
<code-tag>$ sudo apt install gfortran</code-tag>
|
||||||
|
|
||||||
<p class="mb-6">And that's it. Pretty easy.</p>
|
<paragraph>And that's it. Pretty easy.</paragraph>
|
||||||
|
|
||||||
<h2 class="text-2xl">Project Structure</h2>
|
<h2 class="text-2xl">Project Structure</h2>
|
||||||
<hr class="mb-6">
|
<hr class="mb-6">
|
||||||
|
|
||||||
<p class="mb-6">When creating a Fortran project, or any project in general, you want a structure. There is a lot of different
|
<paragraph>When creating a Fortran project, or any project in general, you want a structure. There is a lot of different
|
||||||
layouts for the structure, but equal amongst them, is the <code class="text-red-500">src</code> folder. That's where we keep all of out
|
layouts for the structure, but equal amongst them, is the <code-tag>src</code-tag> folder. That's where we keep all of out
|
||||||
source code. Like <code class="text-red-500">*.f90, *.f95</code> files.<br><br>
|
source code. Like <code-tag>*.f90, *.f95</code-tag> files.<br><br>
|
||||||
|
|
||||||
But other than that, most projects include a <code class="text-red-500">Makefile</code> file, a
|
But other than that, most projects include a <code-tag>Makefile</code-tag> file, a
|
||||||
<code class="text-red-500">library</code>, <code class="text-red-500">test</code>
|
<code-tag>library</code-tag>, <code-tag>test</code-tag>
|
||||||
and <code class="text-red-500">bin</code> folder. Your layout doesn't need to look like this specifically, nor does it have to contain
|
and <code-tag>bin</code-tag> folder. Your layout doesn't need to look like this specifically, nor does it have to contain
|
||||||
the same folders. Each project is different, and so are the requirements. But this layout is simple, and great
|
the same folders. Each project is different, and so are the requirements. But this layout is simple, and great
|
||||||
for medium to large projects.<br><br>
|
for medium to large projects.<br><br>
|
||||||
|
|
||||||
But if we're doing micro-projects, as in like, a hello world application, a test application, or a small tool,
|
But if we're doing micro-projects, as in like, a hello world application, a test application, or a small tool,
|
||||||
this layout is rather redundant, and in the examples on this site, we will only use it on bigger projects,
|
this layout is rather redundant, and in the examples on this site, we will only use it on bigger projects,
|
||||||
and we will let you know, when or how we set up the project.</p>
|
and we will let you know, when or how we set up the project.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
project/
|
project/
|
||||||
├── src/ # Source files (.f90, .f95, etc.)
|
├── src/ # Source files (.f90, .f95, etc.)
|
||||||
@ -65,33 +66,31 @@ project/
|
|||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p>Notes:</p>
|
<p>Notes:</p>
|
||||||
<ul class="mb-6">
|
<ul class="mb-6">
|
||||||
<li>> <code class="text-red-500">src</code> (This is where we keep all of out source code).</li>
|
<li>> <code-tag>src</code-tag> (This is where we keep all of out source code).</li>
|
||||||
<li>> <code class="text-red-500">modules</code> (This is where we keep our classes. Or in Fortran-speak, modules).</li>
|
<li>> <code-tag>modules</code-tag> (This is where we keep our classes. Or in Fortran-speak, modules).</li>
|
||||||
<li>> <code class="text-red-500">include</code> (This is where we keep our interfaces, that out modules will inherit from, if we're using Fortran 90 or above).</li>
|
<li>> <code-tag>include</code-tag> (This is where we keep our interfaces, that out modules will inherit from, if we're using Fortran 90 or above).</li>
|
||||||
<li>> <code class="text-red-500">bin</code> (This is where the compiled program should end up in).</li>
|
<li>> <code-tag>bin</code-tag> (This is where the compiled program should end up in).</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
<h2 class="text-2xl">Compiling</h2>
|
||||||
|
<hr class="mb-6">
|
||||||
|
|
||||||
<h2 class="text-2xl">Compiling</h2>
|
<paragraph>Makefiles are very useful, and becomes increasingly useful, the bigger the project gets. A makefile simplifies the
|
||||||
<hr class="mb-6">
|
compilation process, as all of the compile flags can be gathered inside a single file. It makes it possible to
|
||||||
|
dynamically change the compilation depending on the system architecture or config. It also removes the need to
|
||||||
|
manually compile and link your objects together, every time you make a change.<br><br>
|
||||||
|
|
||||||
<p class="mb-6">Makefiles are very useful, and becomes increasingly useful, the bigger the project gets. A makefile simplifies the
|
And with a bigger project, comes a big count of source files. That's where a makefile helps a lot, as it handles
|
||||||
compilation process, as all of the compile flags can be gathered inside a single file. It makes it possible to
|
and tracks all changes to each file, ensuring that it's only the changed files that gets re-compiled.<br><br>
|
||||||
dynamically change the compilation depending on the system architecture or config. It also removes the need to
|
|
||||||
manually compile and link your objects together, every time you make a change.<br><br>
|
|
||||||
|
|
||||||
And with a bigger project, comes a big count of source files. That's where a makefile helps a lot, as it handles
|
So let's see a makefile in action. Below code is a simple Hello World application, that we will use a makefile to
|
||||||
and tracks all changes to each file, ensuring that it's only the changed files that gets re-compiled.<br><br>
|
compile. Although it's worth noting, that a program this small, a makefile is wildly redundant. But it does show
|
||||||
|
how it's set up in a simple environment.</paragraph>
|
||||||
|
|
||||||
So let's see a makefile in action. Below code is a simple Hello World application, that we will use a makefile to
|
<code-tag>hello.f90</code-tag>
|
||||||
compile. Although it's worth noting, that a program this small, a makefile is wildly redundant. But it does show
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
how it's set up in a simple environment.</p>
|
|
||||||
|
|
||||||
|
|
||||||
<code class="text-red-500">hello.f90</code>
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
program hello
|
program hello
|
||||||
implicit none
|
implicit none
|
||||||
@ -100,34 +99,9 @@ end program hello
|
|||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<code-tag>makefile</code-tag>
|
||||||
<p class="mb-6">The makefile, in the eyes of a modern programmer (C#, Python, JS, Etc) might look rather repulsive. But be not
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
afraid. It will all make sense in a bit.</p>
|
|
||||||
|
|
||||||
<ul>
|
|
||||||
<li>> <code class="text-red-500">hello: hello.o</code></li>
|
|
||||||
<li class="pb-2">> > Here, the program "hello" depends on the object file "hello.o".</li>
|
|
||||||
<li>> <code class="text-red-500">$(FC) $(FFLAGS) -o $@ $^</code></li>
|
|
||||||
<li class="pb-2">> > This one compiles the object files into the program.</li>
|
|
||||||
<li>> <code class="text-red-500">hello.o: hello.f90</code></li>
|
|
||||||
<li class="pb-2">> > Here, the object file "hello.o" depends on the source file "hello.f90".</li>
|
|
||||||
<li>> <code class="text-red-500">$(FC) $(FFLAGS) -c $<</code></li>
|
|
||||||
<li class="pb-2">> > This one compiles the source files into the object files.</li>
|
|
||||||
<li>> <code class="text-red-500">run: ./hello</code></li>
|
|
||||||
<li class="pb-2">> > This runs the program if you execute <code class="text-red-500">make run</code></li>
|
|
||||||
<li>> <code class="text-red-500">clean: rm -f *.o hello</code></li>
|
|
||||||
<li class="pb-2">> > Here, the object files gets removed when running <code class="text-red-500">make clean</code></li>
|
|
||||||
</ul>
|
|
||||||
<br>
|
|
||||||
|
|
||||||
<p class="mb-6">A quick note. It's very important that whenever something is indented in a makefile, that it's indented with a tab,
|
|
||||||
and not 4 spaces. It needs to be a tab character.</p>
|
|
||||||
|
|
||||||
<code class="text-red-500">makefile</code>
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
Makefile for hello.f90
|
|
||||||
|
|
||||||
FC = gfortran # Fortran compiler
|
FC = gfortran # Fortran compiler
|
||||||
FFLAGS = -O2 # Compiler flags (optimization level)
|
FFLAGS = -O2 # Compiler flags (optimization level)
|
||||||
|
|
||||||
@ -145,34 +119,59 @@ clean:
|
|||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">Now simply run the makefile with the following command.</p>
|
<paragraph>The makefile, in the eyes of a modern programmer (C#, Python, JS, Etc) might look rather repulsive. But be not
|
||||||
|
afraid, because at the end of the day, it's simply just some rules, that the compiler follows to compile your program.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<ul class="mb-6">
|
||||||
|
<li>> <code-tag>hello: hello.o</code-tag></li>
|
||||||
|
<li class="pb-2">> > Here, the program "hello" depends on the object file "hello.o".</li>
|
||||||
|
|
||||||
|
<li>> <code-tag>$(FC) $(FFLAGS) -o $@ $^</code-tag></li>
|
||||||
|
<li class="pb-2">> > This one compiles the object files into the program.</li>
|
||||||
|
|
||||||
|
<li>> <code-tag>hello.o: hello.f90</code-tag></li>
|
||||||
|
<li class="pb-2">> > Here, the object file "hello.o" depends on the source file "hello.f90".</li>
|
||||||
|
|
||||||
|
<li>> <code-tag>$(FC) $(FFLAGS) -c $<</code-tag></li>
|
||||||
|
<li class="pb-2">> > This one compiles the source files into the object files.</li>
|
||||||
|
|
||||||
|
<li>> <code-tag>run: ./hello</code-tag></li>
|
||||||
|
<li class="pb-2">> > This runs the program if you execute <code-tag>make run</code-tag></li>
|
||||||
|
|
||||||
|
<li>> <code-tag>clean: rm -f *.o hello</code-tag></li>
|
||||||
|
<li class="pb-2">> > Here, the object files gets removed when running <code-tag>make clean</code-tag></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<paragraph>A quick note. It's very important that whenever something is indented in a makefile, that it's indented with a tab,
|
||||||
|
and not 4 spaces. It needs to be a tab character.</paragraph>
|
||||||
|
|
||||||
|
<paragraph>Now simply run the makefile with the following command.</paragraph>
|
||||||
|
|
||||||
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
$ make
|
$ make
|
||||||
gfortran -O2 -c hello.f90
|
gfortran -O2 -c hello.f90
|
||||||
gfortran -O2 -o hello hello.o
|
gfortran -O2 -o hello hello.o
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">We have now successfully compiled the program with the help of a makefile. Now simply run the program and we'll
|
<paragraph>We have now successfully compiled the program with the help of a makefile. Now simply run the program and we'll
|
||||||
see the output.</p>
|
see the output.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
$ ./hello
|
$ ./hello
|
||||||
Hello, World!
|
Hello, World!
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="mb-6">The exact same should happen, if we run the next command instead.</p>
|
<paragraph>The exact same should happen, if we run the next command instead.</paragraph>
|
||||||
|
|
||||||
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
|
||||||
<pre><code class="text-sm">
|
<pre><code class="text-sm">
|
||||||
$ make run
|
$ make run
|
||||||
./hello
|
./hello
|
||||||
Hello, World!
|
Hello, World!
|
||||||
</code></pre>
|
</code></pre>
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
Loading…
Reference in New Issue
Block a user