Guidelines for the CMM coding project 5 October 2006 (or, “How to make your life easier in the long run”)

Slides:



Advertisements
Similar presentations
Modular Design Using Subroutines (functions later)
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advanced Programming 15 Feb The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing.
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.
FORTRAN Short Course Week 1 Kate T-C February 17, 2008.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Debugging Introduction to Computing Science and Programming I.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
FORTRAN Short Course Week 2 Kate Thayer-Calder February 23, 2009.
Program Design and Development
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Computer Science 1620 Programming & Problem Solving.
CS 201 Functions Debzani Deb.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
29-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Chapter 6: Functions.
The Project AH Computing. Functional Requirements  What the product must do!  Examples attractive welcome screen all options available as clickable.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Introduction to FORTRAN
Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
General Programming Introduction to Computing Science and Programming I.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Understanding the TigerSHARC ALU pipeline Determining the speed of one stage of IIR filter – Part 3 Understanding the memory pipeline issues.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Model Task 0A: Programming the 1D upstream scheme ATM 562 Fall 2015 Fovell 1.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Understanding the TigerSHARC ALU pipeline Determining the speed of one stage of IIR filter – Part 2 Understanding the pipeline.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
Model Task 4: Time stepping and the leapfrog scheme ATM 562 Fall 2015 Fovell (see updated course notes, Chapter 12) 1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
HKOI Programming HKOI Training Team (Intermediate) Alan, Tam Siu Lung Unu, Tse Chi Yung.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
Ten Commandments of Word Processing. I. Thou shall not use spaces n Put no more than two spaces together. n Use the key to line things up. n Better yet,
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
The Hashemite University Computer Engineering Department
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Introduction to Python
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming – Touch Sensors
Understanding the TigerSHARC ALU pipeline
Tonga Institute of Higher Education IT 141: Information Systems
CMSC 345 Programming.
Tonga Institute of Higher Education IT 141: Information Systems
Unit 3: Variables in Java
Arrays.
Presentation transcript:

Guidelines for the CMM coding project 5 October 2006 (or, “How to make your life easier in the long run”)

Guiding philosophies of this advice… I. A good model code is easy to read and trouble shoot… you can find what you’re looking for quickly and you can see what each line of code is supposed to be doing II. A good model code preserves forward compatibility, so that as we add new features, we don’t have to reinvent the wheel

1.Start each and every program and subroutine with… IMPLICIT NONE

2. Define physical constants and model configurations in parameter statements, not in the body of the code! INTEGER NT PARAMETER(NT=1000) This prevents accidental overwriting of a variable somewhere else in the code

3. Determine whether a variable needs to be an integer, a single precision real, or a double precision real, and declare it properly INTEGER NT PARAMETER(NT=1000) REAL DT PARAMETER(DT=2.0) DOUBLE PRECISION PI PARAMETER(PI= D+00)

4. Along with this, remember that for many compilers, the product of two kinds of variables is restricted by the less precise variable INTEGER I REAL A, B I=2 A=2.2 B=A*I B=A*REAL(I) B may be = 4 on some compilers (On many compilers, IMPLICIT NONE alleviates this problem)

5. Comment your code heavily! Our goal in this class is to understand every line of code in our model… believe it or not, in a week or a month, you will forget why you coded something in a particular way, or you will forget what a particular line of code is meant to do.

6. Call your variables and subroutines by names that bring to mind what they represent! EPS ASSELINCOEF We all like to save keystrokes and keep our code compact, but which of these is unambiguous? EPSILON

7. Similarly, make your calculations look like the equations you’re trying to represent Often it’s possible to really combine and condense a lot of coefficients into one compact variable… Models do this to save on the number of calculations… but, when you are trying to modify that line (or debug it, or base another line off of it) you will have to go back and figure out where the compact variable is defined, whether it is defined correctly, whether it needs to be modified for a new application, etc. GAMMA = K * DT / DX**2.

8. Continuing the theme… don’t use a bunch of temporary/dummy variables for multiple things at various points in the code Some models do this to save on RAM… but, when you are trying to modify a line (or debug it, or base another line off of it) you will have the problems previously mentioned. In addition, when you look at a random line of code, you can’t be sure of what the dummy variable means! ! Check for parcel buoyancy DUM=PTPAR-PTENV IF(DUM.GE.0.0) CAPE = CAPE + G*DZ*DUM/PTENV And later… ! Condense excess vapor from parcel DUM = QVS*LV*17.27*237.0/(CP * (TEMP-36.0)**2) DUM = (QVPAR-QVS)/(1+DUM) QVPAR = QVPAR - MAX(DUM,0.0)

9. Cosmetics: avoid IF statements when possible (they run slowly and look ugly!) ! Upstream scheme depending upon flow direction IF(C.GE.0.0) THEN PHI(I,N+1)=PHI(I,N)-C*DT*(PHI(I,N)-PHI(I-1,N))/DX ELSE PHI(I,N+1)=PHI(I,N)-C*DT*(PHI(I+1,N)-PHI(I,N))/DX ENDIF ! Upstream scheme depending upon flow direction PHI(I,N+1)=PHI(I,N) & -0.5*(C+ABS(C))*DT*(PHI(I,N)-PHI(I-1,N))/DX & -0.5*(C-ABS(C))*DT*(PHI(I+1,N)-PHI(I,N))/DX

10. A three-level time scheme (e.g. leapfrog) only requires the time dimension of the arrays to be 3 (not NT) INTEGER NX, NT PARAMETER(NX=100, NT=10000) REAL PSI(NX,NT) INTEGER I, N ! Omit the initial condition statements for now DO N=2,NT DO I=2,NX PSI(I,N+1)=PSI(I,N-1) + forcing[PSI(I,N)] ENDDO

10. A three-level time scheme (e.g. leapfrog) only requires the time dimension of the arrays to be 3 (not NT) INTEGER NX, NT PARAMETER(NX=100, NT=10000) REAL PSIP(NX), PSIC(NX), PSIF(NX) ! Past, current, future INTEGER I, N ! Omit the initial condition statements for now DO N=2,NT PSIP=PSIC PSIC=PSIF DO I=2,NX PSIF(I)=PSIP(I) + forcing[PSIC(I)] ENDDO Only practical approach for long simulations! (RAM)

11. Obviously, this requires writing output during the run (not waiting until the very end) ! Omit the declaration statements for now ! Omit the initial condition statements for now DO N=2,NT TIMENOW=REAL(N-1)*DT PSIP=PSIC PSIC=PSIF DO I=2,NX PSIF(I)=PSIP(I) + forcing[PSIC(I)] ENDDO ! if it's time, dump output IF(TIMENOW.GE.THISDUMP) THEN CALL GRADSHISTORYDUMP ! set next time for output THISDUMP = THISDUMP + DTOUTPUT ENDIF ENDDO

12. In the atmosphere, all processes happen simultaneously… in the model, they must happen in a certain order. In other words, you can’t just put things anywhere you want! Example… what is the correct order for these processes in a model? Predict “dry” change in temperature Compute saturation mixing ratio Update water vapor for evap/cond Apply boundary conditions Update temperature for evap/cond

13. Use modular code… A. Creating a “driver” code and using subroutines B. Breaking your code up into meaningful files i) Using INCLUDE ii) Declaring everything in one place iii) Using COMMON blocks iii) Compiling a group of files

Breaking your code up into meaningful files… -Using INCLUDE -Declaring everything in one place constants.inc:

Breaking your code up into meaningful files… -Using INCLUDE -Declaring everything in one place

Using COMMON blocks USE: The preamble to each subroutine is a series of INCLUDE statements that define all the variables. The final thing after all the included definitions is then the INCLUDE statement for the file that contains the COMMON block Declare the variables once (only), in one place Never have to worry about whether a variable is being passed to subroutines Saves RAM, because each subroutine looks at the same place in the memory (don’t use extra memory to declare a redundant copy of variables passed to the subroutine) When a variable is updated in a subroutine, it is updated everywhere in the code

Recall: creating a “driver” code and using subroutines… So, each subroutine also begins with the INCLUDE statements!

Breaking your code up into meaningful files… -Using INCLUDE -Declaring everything in one place

Breaking your code up into meaningful files… -Compiling a group of files Your model code can become quite long…(the last time I did this project, the 2D model driver with simple microphysics was 1431 lines, excluding all declarations) Why not break the subroutines up, too? All subroutines related to initial conditions: init.F All subroutines related to boundary conditions: boundaries.F All subroutines related to writing out data: writeout.F All subroutines related to main integration: integration.F All subroutines related to main driver: modeldriver.F

Breaking your code up into meaningful files… -Compiling a group of files Then, to compile, create a shell script: compile.sh #!/bin/csh rm cmm.exe cat modeldriver.F init.F boundaries.F writeout.F integration.F > onefile.F f90 –o cmm.exe onefile.F Compiling the model is now simple, and you know right where to look if you want to work on the boundary conditions!