Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Main purpose of FC2K is to embed native code inside Kepler workflow. This way, you are able to connect various, physics based, calculations into chain of execution. There are few rules you have to follow while embedding native code inside Kepler:

  • your routine signature must be exactly the same as one suggested by FC2K
  • in order to access IDSes, you have to use IMAS based modules inside your Fortran code
  • you can pass data into and outside of the code in the form of:
    • primitive types
    • IDSes
    • code parameters
  • you should avoid accessing external files unless it's really impossible to run the simulation without external source of data
  • all physics related information should be exchanged only via IDSes

In this tutorial, I will show show you how to build simple native code and plug it inside Kepler using FC2K.

Sample, shown below, will access IDS data and print it on console.

Warning
titlesource code

Source code for this sample can be found at following location

/afs/gw/swimas/resources/tutorials/2019-10-PSNC/FC2K/simple_IMAS_code 

Note that workflow is running such way it reads data from pulse file of user g2michal - it means you don't have to copy input data into your public/imasdb  folder. However, you are still obliged to use imasdb  in order to set location for temporary databases.


Info

Make sure to initialise your environment. Follow the steps here: 00. Initial setup or here: 05.3. IMAS - basic topics - environment set-up(POZ'19Oct)

...

Info

Make sure to initialise west test database - it will be used for temporary files

Code Block
> imasdb test



Preparing the

...

FC2K project

First of all, we need simple code that will read data. In this sample, we will use very simple code that reads distribution IDS, shows some info, and output scalar value

...

When you talk about actor, most important is the interface. It's API defines how actor interacts with other actors in the workflow.

What interface should I provide

Info

You can always start with FC2K in order to make sure how native interface should be implemented like.

...

Defining interface to user code

First of all, let's add two ports into actor. Input port - IDS, and output port - integer.

Now, let's take a look how the native code should look like in order to provide correct implementation of the actor's API.

Image Removed

Now, once we know how our interface looks like, we can start developing the code

Source code of the actor

In our case, we can start with providing very basic implementation

Code Block
subroutine distributiondisp(distributionin, output)

  use ids_schemas

  implicit none

  type (ids_distribution_sources) :: distributionin
  integer, intent(out)            :: output

  output = 1

  return

end subroutine

As you can see, we don't even access input data, yet. All we have, so far, is the API of the native code.


Creating library with native code

As you recall from previous session (dedicated to FC2K) we have to have library that contains native code we are supposed to run.

Image Removed

Let's prepare it. We will do it, by creating Makefile  project - library will be called libdistribution_disp.

...

...

In

...

Once we run make we can use library inside the project

...

this example we will use library  libdistribution_disp.a  prepared  previously


Describing the actor

When we fill information regarding actor, we make it possible to create a bridge between Kepler (actor) and native code (in this case Fortran based library). I will fill the table following way

...

Now it's time to put all the things together. Start your Kepler and create following workflow.

Now we can run it.

Image Removed

Image Added

As you can see, the result is exactly the same as we have expected. Now, let's try to get some check data coming from the IDS.

Getting value from the IDS and passing it back to Kepler

...


Code Block

...

First of all, let's take a look at the data we are working with

Code Block
> idsdumpidsdumppath g2michal westtest 3 92436 1 distribution_sources
class distribution_sources
Attribute ids_properties
 	class ids_properties
	Attribute comment:
	Attribute homogeneous_time: 1
	Attribute source:
	Attribute provider:
	Attribute creation_date:
	Attribute version_put
 		class version_put
		Attribute data_dictionary: 3.24.0
		Attribute access_layer: 4.2.0
		Attribute access_layer_language: fortran
Attribute source
 	class source
Attribute vacuum_toroidal_field
 	class vacuum_toroidal_field
	Attribute r0: -9e+40
	Attribute r0_error_upper: -9e+40
	Attribute r0_error_lower: -9e+40
	Attribute b0
	[]
	Attribute b0_error_upper
	[]
	Attribute b0_error_lower
	[]
Attribute magnetic_axis
 	class magnetic_axis
	Attribute r
	[]
	Attribute r_error_upper
	[]
	Attribute r_error_lower
	[]
	Attribute z
	[]
	Attribute z_error_upper
	[]
	Attribute z_error_lower
	[]
Attribute code
 	class code
	Attribute name: coreprofiles2distsourcesF
	Attribute commit:
	Attribute version: 1.1
	Attribute repository:
	Attribute parameters: my_code_specific_parameters
	Attribute output_flag
	[]
Attribute time
[49.00500107 49.02500153 49.04499817 49.06499863 49.08499908 49.10499954
 49.125      49.14500046 49.16500092 49.18500137 49.20500183 49.22499847
 49.24499893 49.26499939 49.28499985 49.30500031 49.32500076 49.34500122
 49.36499786 49.38500214 49.40499878 49.42499924 49.44499969 49.46500015
 49.48500061 49.50500107 49.52500153 49.54499817 49.56499863 49.58499908
 49.60499954 49.625      49.64500046 49.66500092 49.68500137 49.70500183
 49.72499847 49.74499893 49.76499939 49.78499985 49.80500031 49.82500076
 49.84500122 49.86499786 49.88500214 49.90499878 49.92499924 49.94499969
 49.96500015 49.98500061 50.00500107 50.02500153 50.04499817 50.06499863
 50.08499908 50.10499954 50.125      50.14500046 50.16500092 50.18500137
 50.20500183 50.22499847 50.24499893 50.26499939 50.28499985 50.30500031
 50.32500076 50.34500122 50.36499786 50.38500214 50.40499878 50.42499924
 50.44499969 50.46500015 50.48500061 50.50500107 50.52500153 50.54499817
 50.56499863 50.58499908 50.60499954 50.625      50.64500046 50.66500092
 50.68500137 50.70500183 50.72499847 50.74499893 50.76499939 50.78499985
 50.80500031 50.82500076 50.84500122 50.86499786 50.88500214 50.90499878
 50.92499924 50.94499969 50.96500015 50.98500061 51.

Let's say we will retrieve the first time index (as integer) from the array of all times. Remember, this is just an exercise where we want to make all things work together. Don't pay attention to whether it's something meaningful or not. Let's say our actor is supposed to retrieve first time index in the IDS.

We need the code that will perform this action. Let's change our source code a little bit.

Code Block
subroutine distributiondisp(distributionin, output)
  use ids_schemas

  implicit none

  type (ids_distribution_sources) :: distributionin
  integer, intent(out)            :: output

  output = int( distributionin%time(1) )  ! we are reading data from the IDS and return it back to Kepler

  return
end subroutine

Once we compile the code and regenerate actor again

Code Block
> make clean
> make second
> fc2k distribution_disp_fc2k.xml
> kepler

we can run workflow again. This time, the value is coming from the IDS.

...

 "time(0)"
Type: <class 'numpy.float64'>
----------------------------------------------
----------------------------------------------
49.005001068115234