Alberto Fassò Endre Futó

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
11 Introduction to Object Oriented Programming (Continued) Cats.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Arrays. C++ Style Data Structures: Arrays(1) An ordered set (sequence) with a fixed number of elements, all of the same type, where the basic operation.
Basic concepts of C++ Presented by Prof. Satyajit De
Functions + Overloading + Scope
Chapter 8 Arrays, Strings and Pointers
Chapter 7 User-Defined Methods.
Chapter 7 Pointers and C-Strings
Computer Skills2 for Scientific Colleges
APPENDIX a WRITING SUBROUTINES IN C
A bit of C programming Lecture 3 Uli Raich.
Lecture-5 Arrays.
Chapter 1. Introduction to Computers and Programming
Pointers and Pointer-Based Strings
Student Book An Introduction
C Basics.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Programmazione I a.a. 2017/2018.
Object-Oriented Programming Using C++
C++ Arrays.
DATA HANDLING.
Pass by Reference, const, readonly, struct
CS 2308 Exam I Review.
7 Arrays.
Introduction to Programming
Pointers, Dynamic Data, and Reference Types
More About Data Types & Functions
Data type List Definition:
Programming Funamental slides
C++ Compilation Model C++ is a compiled language
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
7 Arrays.
C programming Language
CS150 Introduction to Computer Science 1
Pointers Pointers point to memory locations
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
NAG Workshop and Surgery
Review of C++ Language Basics
Fundamental Programming
Arrays Arrays A few types Structures of related data items
Fundamental Programming
CS250 Introduction to Computer Science II
Outline Announcements Differences between FORTRAN and C
CS150 Introduction to Computer Science 1
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
C++ Programming Basics
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
Structure (i.e. struct) An structure creates a user defined data type
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Class rational part2.
ENERGY 211 / CME 211 Lecture 28 December 1, 2008.
Programming fundamentals 2 Chapter 3:Pointer
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Alberto Fassò Endre Futó FLUKA in ROOT Alberto Fassò Endre Futó

Introduction FLUKA is written in FORTRAN77 (~900 routines) It is excluded to rewrite it in C++ Present use of FLUKA: - FORTRAN user routines having access to the information in the COMMONs Solution: - Rewrite COMMONs to C++ header files - Rewrite only user routines to C++ (better: call from FORTRAN user routines corresponding C++ procedures)

Main differences between FORTRAN and C++ (1) Arrays: FORTRAN: columnwise (leftmost subscripts vary fastest) C++: rowwise (rightmost subscipts vary fastest) Subroutine arguments: FORTRAN: to be passed by reference C++: non-array arguments passed by value, therefore declare them to be passed by ref or const ref C++: array args are declared arrays, but actually passed by ref, because C++ converts use of array name to a pointer to the array's first element C++: args not modified by FORTRAN to be declared const

Main differences between FORTRAN and C++ (2) Be aware of storing vars in memory LOGICAL variables exponent should be e (not d) If in C++ number starts with 0 -> octal number Character variables (no string in FORTRAN) No blanks in C++ variable names Problem of EQUIVALENCE -> union Input/Ouput: avoid doing input or output from both languages with the same file or device There may be implementation-specific behaviours, but generally the output of the two will be intermingled

Rewrite COMMONs to C++ header files Perl script c2h.pl far from being perfect, question of time and effort to write a better one, therefore always check: Multidimensional arrays (swap dimensions) Arrays dimensioned X(n:m) -> X[n-m+1] CHARACTER* vars -> swap dim and char size LOGICAL vars -> all should be integers All comment lines All continuation lines All unions created from EQUIVALENCEs All double constants -> exponent should be e All exp constants -> exp cannot start with 0

Rewrite COMMONs to C++ header files extern “C” { typedef struct { Double_t whasou[12]; Double_t tkesum; Int_t lussrc; } episorCommon; #define EPISOR COMMON_BLOCK(EPISOR,episor) COMMON_BLOCK_DEF(episorCommon, EPISOR); typedef struct { Char_t sdsou[8]; } chepsrCommon; #define CHEPSR COMMON_BLOCK(CHEPSR,chepsr) COMMON_BLOCK_DEF(chepsrCommon, CHEPSR); } * /EPISOR/ LOGICAL LUSSRC CHARACTER SDUSOU*8 COMMON /EPISOR/ WHASOU(12),TKESUM,LUSSRC COMMON /CHEPSR/ SDUSOU

Mixing FORTRAN and C++ header.h #ifndef __CFORTRAN_LOADED #include “....../root/include/cfortran.h” #endif #ifndef WIN32 #ifndef WIN32 #define flukam flukam_ #else #define flukam FLUKAM #endif extern “C” { void flukam (const int& iflgeo); // COMMON /AAAAAA/ AA, BB, II, JJ typedef {double aa; double bb; int ii; int jj;} aaaaaaCommon; #define AAAAAA COMMON_BLOCK(AAAAAA,aaaaaa) COMMON_BLOCK_DEF(aaaaaaCommon,AAAAAA); }

Mixing FORTRAN and C++ main.cxx + flukam.f SUBROUTINE FLUKAM (IFLGEO) REAL *8 AA, BB COMMON /AAAAAA/ AA,BB,II,JJ AA = 1. BB = 2. II = 10 JJ = 20 ICODE = 100 MREG = 17 CALL MGDRAW (ICODE,MREG) RETURN END #include “header.h” int main () { flukam (0); return (0); }

Mixing FORTRAN and C++ mgdraw.cxx #include <iostream.h> #include <iomanip.h> #include “header.h” #ifndef WIN32 #define flukam flukam_ #else #define flukam FLUKAM #endif extern “C” { void mgdraw (int& icode, int& mreg) { cout << “ ICODE=” << icode << “ MREG=” << mreg << endl; cout << “ AA=” << AAAAAA.aa << “ BB=” << AAAAAA.bb << endl; cout << “ II=” << AAAAAA.ii << “ JJ=” << AAAAAA.jj << endl; } }

Mixing FORTRAN and C++ compiling and linking g++ -c main.cxx g++ -c mgdraw.cxx g77 -c flukam.f g++ -o mixedprog main.o mgdraw.o flukam.o mixedprog ========================== Results ======================= ICODE=100 MREG=17 AA=1. BB=2. II=10 JJ=20

Mixing FLUKA and ROOT compiling and linking In the C++ user routine include all necessary header files of ROOT, e.g. #include “...../root/include/TObject.h” all header files containing variables of COMMONs to be used, e.g. #include “......./fluka/flukacppheaders/Ftrackr.h” Modify the lfluka linker file replace g77 by g++ Add library g2c $F77 $FFLAGS ....... -l${LIBF} ${LIBA} -lg2c