Cleaned up the page tag usage

This commit is contained in:
Rasmus Rasmussen 2025-02-25 12:13:03 +01:00
parent 663ac1cff6
commit 688531adfc
6 changed files with 279 additions and 295 deletions

View File

@ -158,7 +158,7 @@ $ ./main
</code></pre>
</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>

View File

@ -1,50 +1,48 @@
<script setup lang="ts">
import CodeTag from "../../../tags/CodeTag.vue";
import Paragraph from "../../../tags/Paragraph.vue";
</script>
<template>
<div>
<h1 class="text-3xl">Data Types</h1>
<hr class="mb-6">
<paragraph>Data Types</paragraph>
<p class="mb-6">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
even Python, you will feel the difference pretty fast.<br><br>
<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
even Python, you will feel the difference pretty fast.<br><br>
Let's look at a small list of some common types.
</p>
Let's look at a small list of some common types.</paragraph>
<ul class="mb-6">
<li>&gt; <code>integer</code></li>
<li>&gt; <code>real</code></li>
<li>&gt; <code>double precision</code></li>
<li>&gt; <code>logical</code></li>
<li>&gt; <code>character</code></li>
</ul>
<ul class="mb-6">
<li>&gt; <code>integer</code></li>
<li>&gt; <code>real</code></li>
<li>&gt; <code>double precision</code></li>
<li>&gt; <code>logical</code></li>
<li>&gt; <code>character</code></li>
</ul>
<p class="mb-6"><code class="text-red-500">integer</code> 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 class="text-red-500">float</code>. 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>
<paragraph><code-tag>integer</code-tag> is, if you hadn't guessed, a whole number. Like most languages,
<code-tag>int</code-tag>. The <code-tag>real</code-tag> type is like a
<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>
The <code class="text-red-500">double precision</code> 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>
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>
The <code-tag>double precision</code-tag> is a double, obviously. And as you have probably guessed at
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
like a string. Or like in SQL: <code-tag>VARCHAR(256)</code-tag>.</paragraph>
<h2 class="text-2xl">integer</h2>
<hr class="mb-6">
<h2 class="text-2xl">integer</h2>
<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>.
So if you want a 16 bit integer, you do <code class="text-red-500">integer(kind = 16)</code>. Observe the following code example.
As an exercise for the reader, try to imagine what would be printed to the terminal.<br><br>
<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-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>
*Do note that we're using Fortran 90, and not the newer versions that supports unsigned integers.<a href="#footnote-1">[1]</a>
</p>
*Do note that we're using Fortran 90, and not the newer versions that supports unsigned integers.<a href="#footnote-1">[1]</a>
</paragraph>
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
@ -72,11 +70,11 @@ program hello
end program hello
</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -95,26 +93,25 @@ end program hello
kind = 16
170141183460469231731687303715884105727
</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,
real or array type of any kind.<br><br>
<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>
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>
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>
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.
</p>
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.</paragraph>
<h2 class="text-2xl">real</h2>
<hr class="mb-6">
<h2 class="text-2xl">real</h2>
<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
integers, real can also be specified with a <code-tag>kind(n)</code-tag> modifier. Observe this example:</p>
<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:</paragraph>
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
@ -142,11 +139,11 @@ program hello
end program hello
</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -166,19 +163,19 @@ end program hello
1.18973149535723176508575932662800702E+4932
3.36210314311209350626267781732175260E-4932
</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
the smallest positive number of a real kind. That function is not available for integers.</p>
<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.</paragraph>
<h2 class="text-2xl">double precision</h2>
<hr class="mb-6">
<h2 class="text-2xl">double precision</h2>
<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
to double precision as the real type.</p>
<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.</paragraph>
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
@ -191,11 +188,11 @@ program hello
end program hello
</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -203,24 +200,23 @@ end program hello
1.7976931348623157E+308
2.2250738585072014E-308
</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
also the same as an 8 byte real.</p>
<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.</paragraph>
<h2 class="text-2xl">logical</h2>
<hr class="mb-6">
<h2 class="text-2xl">logical</h2>
<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
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>
<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
<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,
or some systems handles smaller objects better. It all depends on the architecture.
</p>
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.</paragraph>
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
@ -253,13 +249,13 @@ program hello
end program hello
</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>
without specifying an optimization level. What do you think the output would look like?</p>
<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?</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -282,18 +278,17 @@ end program hello
value: T
size: 16
</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
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,
check the footnote number 2. <a href="#footnote-1">[2]</a><br><br>
<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
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>
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>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
logical :: bool = .true.;
logical(kind = 1) :: bool1 = .true.;
@ -302,11 +297,11 @@ logical(kind = 4) :: bool4 = .true.;
logical(kind = 8) :: bool8 = .true.;
logical(kind = 16) :: bool16 = .true.;
</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -329,18 +324,18 @@ logical(kind = 16) :: bool16 = .true.;
value: T
size: 16
</code></pre>
</div>
</div>
<h2 class="text-2xl">character</h2>
<hr class="mb-6">
<h2 class="text-2xl">character</h2>
<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
a <code-tag>string</code-tag> in other languages, depending on the length you give it.<br><br>
<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>
Observe the example below.</p>
Observe the example below.</paragraph>
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
@ -360,11 +355,11 @@ program hello
end program hello
</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -378,24 +373,24 @@ end program hello
value: a lol
size: 10
</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,
the trailing indexes in the character will be filled with whitespace.<br><br>
<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>
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>
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.</paragraph>
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>main.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
trim(char2)//'lol';
</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">
&gt; gfortran main.f90
&gt; ./a.out
@ -413,16 +408,11 @@ trim(char2)//'lol';
value: alol
size: 10
</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>
</template>
<style scoped>
</style>
<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>
</template>

View File

@ -1,47 +1,48 @@
<script setup lang="ts">
import Paragraph from "../../../tags/Paragraph.vue";
import CodeTag from "../../../tags/CodeTag.vue";
import PageStart from "../../../tags/PageStart.vue";
</script>
<template>
<div>
<h1 class="text-3xl">Hello World In Fortran 90</h1>
<hr class="mb-6">
<page-start>Hello World In Fortran 90</page-start>
<p class="mb-6">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
can be simple or complex, depending on the size of the project.</p>
<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
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">
program hello
implicit none
print *, 'Hello world!'
end program hello
</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>,
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>
<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: <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>
and <code class="text-red-500">print *, 'Hello world!'</code></span>
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>
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>
in this example.</p>
With modern compilers, running the newest Fortran standards, <code-tag>write(*,*) 'Hello world!'</code-tag>
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 <code-tag>print *</code-tag>
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.</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">
$ gfortran -o hello main.f90
$ ./hello
Hello world!
</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
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>
<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
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">
program hello
implicit none
@ -58,31 +59,30 @@ program hello
end program hello
</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>
and <code class="text-red-500">ios</code></span>.
<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 <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
whim.<br><br>
<paragraph>So what we're seeing here, is that we have created two variables: <code-tag>usertxt</code-tag>
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
have an explicit string type, doesn't mean we can't read or write text, as the <code-tag>character</code-tag>
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>
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.
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>
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
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
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
has triggered yet.<br><br>
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
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>
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
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
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>
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.
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
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">
$ gfortran -o hello main.f90
$ ./hello
@ -90,6 +90,5 @@ $ ./hello
Hello world :D
Hello world :D
</code></pre>
</div>
</div>
</template>

View File

@ -1,31 +1,28 @@
<script setup lang="ts">
import PageStart from "../../../tags/PageStart.vue";
import Paragraph from "../../../tags/Paragraph.vue";
</script>
<template>
<div>
<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>
<page-start>Fortran</page-start>
<p>
Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.</p>
<br>
<paragraph>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.<br><br>
<p>Table of contents:</p>
<ul>
<li><RouterLink to="/Fortran/SetUp">&gt; Set Up</RouterLink></li>
<li><RouterLink to="/Fortran/HelloWorld">&gt; Hello World</RouterLink></li>
<li><RouterLink to="/Fortran/DataTypes">&gt; Data Types</RouterLink></li>
<li><RouterLink to="/Fortran/Methods">&gt; Method, Functions, Sub Routines and Calls</RouterLink></li>
<li><RouterLink to="/Fortran/ReadingAFile">&gt; Reading A File</RouterLink></li>
</ul>
</div>
Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.</paragraph>
<p>Table of contents:</p>
<ul>
<li><RouterLink to="/Fortran/SetUp">&gt; Set Up</RouterLink></li>
<li><RouterLink to="/Fortran/HelloWorld">&gt; Hello World</RouterLink></li>
<li><RouterLink to="/Fortran/DataTypes">&gt; Data Types</RouterLink></li>
<li><RouterLink to="/Fortran/Methods">&gt; Method, Functions, Sub Routines and Calls</RouterLink></li>
<li><RouterLink to="/Fortran/ReadingAFile">&gt; Reading A File</RouterLink></li>
</ul>
</template>

View File

@ -2,18 +2,17 @@
import PageStart from "../../../tags/PageStart.vue";
import CodeTag from "../../../tags/CodeTag.vue";
import Paragraph from "../../../tags/Paragraph.vue";
</script>
<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
<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
read the whole file. Easy peasy.<br><br>
<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,
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>
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>
</div>
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>
</template>

View File

@ -1,57 +1,58 @@
<script setup lang="ts">
import CodeTag from "../../../tags/CodeTag.vue";
import PageStart from "../../../tags/PageStart.vue";
import Paragraph from "../../../tags/Paragraph.vue";
</script>
<template>
<div>
<h1 class="text-3xl">Setting Up The Environment</h1>
<hr class="mb-6">
<page-start>Setting Up The Environment</page-start>
<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
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>
<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
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
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>
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
has some rather interesting qualities to them.</paragraph>
<p>Compilers:</p>
<ul class="mb-6">
<li>&gt; <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>
<li>&gt; <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>
</ul>
<p>Compilers:</p>
<ul class="mb-6">
<li>&gt; <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>
<li>&gt; <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>
</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>
First, we install the compiler.</p>
<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.</paragraph>
<p>For Fedora.</p>
<code class="text-red-500 mb-6">$ sudo dnf install gcc-gfortran</code>
<p>For Fedora.</p>
<code-tag>$ sudo dnf install gcc-gfortran</code-tag>
<p>For ubuntu.</p>
<code class="text-red-500 mb-6">$ sudo apt install gfortran</code>
<p>For ubuntu.</p>
<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>
<hr class="mb-6">
<h2 class="text-2xl">Project Structure</h2>
<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
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
source code. Like <code class="text-red-500">*.f90, *.f95</code> files.<br><br>
<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-tag>src</code-tag> folder. That's where we keep all of out
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
<code class="text-red-500">library</code>, <code class="text-red-500">test</code>
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
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>
But other than that, most projects include a <code-tag>Makefile</code-tag> file, a
<code-tag>library</code-tag>, <code-tag>test</code-tag>
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
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,
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>
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,
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">
project/
src/ # Source files (.f90, .f95, etc.)
@ -65,33 +66,31 @@ project/
</code></pre>
</div>
<p>Notes:</p>
<ul class="mb-6">
<li>&gt; <code class="text-red-500">src</code> (This is where we keep all of out source code).</li>
<li>&gt; <code class="text-red-500">modules</code> (This is where we keep our classes. Or in Fortran-speak, modules).</li>
<li>&gt; <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>&gt; <code class="text-red-500">bin</code> (This is where the compiled program should end up in).</li>
</ul>
<p>Notes:</p>
<ul class="mb-6">
<li>&gt; <code-tag>src</code-tag> (This is where we keep all of out source code).</li>
<li>&gt; <code-tag>modules</code-tag> (This is where we keep our classes. Or in Fortran-speak, modules).</li>
<li>&gt; <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>&gt; <code-tag>bin</code-tag> (This is where the compiled program should end up in).</li>
</ul>
<h2 class="text-2xl">Compiling</h2>
<hr class="mb-6">
<h2 class="text-2xl">Compiling</h2>
<hr class="mb-6">
<paragraph>Makefiles are very useful, and becomes increasingly useful, the bigger the project gets. A makefile simplifies the
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
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>
And with a bigger project, comes a big count of source files. That's where a makefile helps a lot, as it handles
and tracks all changes to each file, ensuring that it's only the changed files that gets re-compiled.<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
and tracks all changes to each file, ensuring that it's only the changed files that gets re-compiled.<br><br>
So let's see a makefile in action. Below code is a simple Hello World application, that we will use a makefile to
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
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.</p>
<code class="text-red-500">hello.f90</code>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<code-tag>hello.f90</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
program hello
implicit none
@ -100,34 +99,9 @@ end program hello
</code></pre>
</div>
<p class="mb-6">The makefile, in the eyes of a modern programmer (C#, Python, JS, Etc) might look rather repulsive. But be not
afraid. It will all make sense in a bit.</p>
<ul>
<li>&gt; <code class="text-red-500">hello: hello.o</code></li>
<li class="pb-2">&gt; &gt; Here, the program "hello" depends on the object file "hello.o".</li>
<li>&gt; <code class="text-red-500">$(FC) $(FFLAGS) -o $@ $^</code></li>
<li class="pb-2">&gt; &gt; This one compiles the object files into the program.</li>
<li>&gt; <code class="text-red-500">hello.o: hello.f90</code></li>
<li class="pb-2">&gt; &gt; Here, the object file "hello.o" depends on the source file "hello.f90".</li>
<li>&gt; <code class="text-red-500">$(FC) $(FFLAGS) -c $<</code></li>
<li class="pb-2">&gt; &gt; This one compiles the source files into the object files.</li>
<li>&gt; <code class="text-red-500">run: ./hello</code></li>
<li class="pb-2">&gt; &gt; This runs the program if you execute <code class="text-red-500">make run</code></li>
<li>&gt; <code class="text-red-500">clean: rm -f *.o hello</code></li>
<li class="pb-2">&gt; &gt; 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">
<code-tag>makefile</code-tag>
<div class="font-mono shadow-lg shadow-slate-500 p-2 mb-6">
<pre><code class="text-sm">
Makefile for hello.f90
FC = gfortran # Fortran compiler
FFLAGS = -O2 # Compiler flags (optimization level)
@ -145,34 +119,59 @@ clean:
</code></pre>
</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>&gt; <code-tag>hello: hello.o</code-tag></li>
<li class="pb-2">&gt; &gt; Here, the program "hello" depends on the object file "hello.o".</li>
<li>&gt; <code-tag>$(FC) $(FFLAGS) -o $@ $^</code-tag></li>
<li class="pb-2">&gt; &gt; This one compiles the object files into the program.</li>
<li>&gt; <code-tag>hello.o: hello.f90</code-tag></li>
<li class="pb-2">&gt; &gt; Here, the object file "hello.o" depends on the source file "hello.f90".</li>
<li>&gt; <code-tag>$(FC) $(FFLAGS) -c $<</code-tag></li>
<li class="pb-2">&gt; &gt; This one compiles the source files into the object files.</li>
<li>&gt; <code-tag>run: ./hello</code-tag></li>
<li class="pb-2">&gt; &gt; This runs the program if you execute <code-tag>make run</code-tag></li>
<li>&gt; <code-tag>clean: rm -f *.o hello</code-tag></li>
<li class="pb-2">&gt; &gt; 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">
$ make
gfortran -O2 -c hello.f90
gfortran -O2 -o hello hello.o
</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
see the output.</p>
<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.</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">
$ ./hello
Hello, World!
</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">
$ make run
./hello
Hello, World!
</code></pre>
</div>
</div>
</template>