Fortran

Guide To Learn

Advanced Fortran use

Synchronizing parallel images on assignment

The steps for implementing a custom assignment are the same as those for an operator, with a few minor differences. Like other arithmetic operators, assignment is an operation with two operands, one on the left and one on the right. Unlike other arithmetic operators that take two input arguments and return a new value as […]

Implementing the arithmetic for the Field class

In section 10.3, we wrote a function that takes the difference between two datetime instances, which we then used to redefine the arithmetic operator -. Here, we take the exact same approach, except that we need to put some more leg work in. Take a look back at listing 10.7–we need to implement procedures to define each operator […]

A refresher on the Field class

Before we jump into the implementation of type-bound methods operators for the Field class, let’s refresh our memory on what this class is made of. In a nutshell, the Field class is a wrapper around the two-dimensional data array that stores the values of the field itself, and a few more components needed for the internal workings of the data structure, […]

Overriding operators in the tsunami simulator

You now have a good idea of how defining an arithmetic operator for use with derived types works. Let’s apply this knowledge to implement the full arithmetic for the Field type in the tsunami simulator. We implemented this derived type back in chapter 8, where we used it to model the physical properties of the water flow, such […]

Time difference algorithm

The simplest algorithm for calculating a difference between two times is to recast the times into a single, common axis, such as number of days since some point in the past. (Recall UNIX time from earlier in the chapter?) Once there, it’s straightforward to calculate the difference–it’s just a matter of subtracting one numerical value […]

Implementing a custom subtraction operator

We have our datetime and timedelta classes. The question remains: How can we take the difference between two datetimes using nothing but an arithmetic subtraction operator? Doing just td = birthday – now won’t work out of the box because a datetime is an arbitrary data structure. To the compiler, it has no intrinsic meaning and is just a sequence of numbers in memory. If we try this, we […]

Calculating the difference between two times

Finally, we get to the meat of the countdown app: calculating the difference between the two times. There are three necessary ingredients for this to work: At the end of this section, we’ll be able to initialize two datetimes and take their difference, which should look something like this: ❶ Two datetime instances ❷ One timedelta instance […]

Getting current date and time

We got the date from the user input, and now we need to get the current date and time. Fortran provides a subroutine date_and_time that gives you access to the current local machine time. Here’s how it works, wrapped in a function to return a datetime instance: ❶ Array of eight integers ❷ Gets the date and time values and stores […]

Scroll to top