Friday, November 16, 2012

Evaporation Terminologies

Pan Evaporation (Pan loss)
http://en.wikipedia.org/wiki/Pan_evaporation
>=Free surface water evaporation (EVCAN in PRMS)

Lake Evaporation
Evaporation from a natural body of water is usually at a lower rate because the body of water does not have metal sides that get hot with the sun, and while light penetration in a pan is essentially uniform, light penetration in natural bodies of water will decrease as depth increases. Most textbooks suggest multiplying the pan evaporation by 0.75 to correct for this.



Potential Evaporation / Evaportranspiration (PET)
Potential Evaporation or Evaportranspiration with unlimited water supply (well-watered crop)
http://en.wikipedia.org/wiki/Potential_evaporation   
Also famous Penman and Penman-Monteith
Penman equation (1948) 
Penman-Monteith equation (1965)

In FAO webpage below, a definition for PET:
http://www.fao.org/docrep/W3094E/w3094e06.htm
Potential evapotranspiration (PET) has been defined as the volume of water per unit area of field evaporated and transpired by a dense stand of actively growing short grass that is well endowed with (never short of) water.

Different crops have their own PET
PET = Kc * RET

PET- Potential evapotranspiration (PET), is the evaporation and transpiration that potentially could occur if a field of the crop had an ideal unlimited water supply.
RET is the reference ET often denoted as ETo. RET usually represents the PET of the reference crops most active growth. Kc, then becomes a function or series of values, specific to the crop of interest through its growing season. These can be quite elaborate in the case of certain corn varieties, but tend to use a trapezoidal or a leaf area index (LAI) curve for common crop or vegetation canopies.

http://en.wikipedia.org/wiki/Crop_coefficient

Thursday, August 30, 2012

ArcGIS Advanced Labeling

To create complex label in ArcGIS, go to
Layer Properties, click Labels
In Label field, click Expression button.
In Label Expression window, in Fields, double click each field you need to use in the label expression.
In the Expression, check Advanced box. It will give you a piece of code. By editing this code, you can customize your own label.



Below is an example of the customized label.  Where when [Sheet2$.N_GYearly1995] field is less than 0, using red font, when greater than 0, using blue font.

---------------------------------------------------------------

---------------------------------------------------------------



For other advanced VBScripting for labels, refer to:

http://63.227.83.34/files/ArcMap_VBScript_Labeling.pdf
This gives a good introduction on ArcMap VBScript labeling.

http://webhelp.esri.com/arcgisdesktop/9.2/index.cfm?TopicName=About_building_label_expressions
This is a good reference sheet for looking up Scripts.

Monday, May 7, 2012

Parallel Programming with Fortran 101 - continued 2

My second OpenMP program:

This is an application I built based on my day-to-day work.  We were often required to run a model with different input parameters. This process if done in serial, cost a processor a very long processing time.  In the past, I often use batch file to do this.  Now all our computers are at least two cores, I would like to run these models in parallel. 

My first attempt failed but the second successed with help from the OpenMP forum members. Failing is a frustrating and time consuming process of learning. This link contains my issues and solution.
http://www.openmp.org/forum/viewtopic.php?f=3&t=1418

The success program is copied below. Anyone may slightly alter it for your Monte Carlo Type of simulations.

program ompHelloWorld
use omp_lib
integer NTHREADS, TID, I, TID1

!$OMP PARALLEL PRIVATE(TID,TID1), SHARED(NTHREADS)
!$OMP MASTER  
    TID1 = OMP_GET_THREAD_NUM()
    PRINT *, 'Master computer id is ', TID1
    NTHREADS = OMP_GET_NUM_THREADS()
    PRINT *, 'Number of threads = ', NTHREADS
!$OMP END MASTER
    TID = OMP_GET_THREAD_NUM()
    PRINT *, 'Hello World from thread = ', TID   
!    CALL POOH(TID)   
    CALL RUNMODEL(TID)
    pause

!$OMP END PARALLEL

end program ompHelloWorld

!Run a model
!Note that under home directory there has to be a cdandrun.bat
!contains the following two lines
!cd %1
!MODHMS1 >lst

SUBROUTINE RUNMODEL(ID)
   INTEGER ID
   CHARACTER*200 CPATH, cpath2  
   write(*,*) 'Run model using thread#',ID
  
!  Change current path  
   WRITE(CPATH,100) ID+1
100 FORMAT(I2)  
   CPATH='cdandrun.bat gvtest'//ADJUSTL(CPATH)
   WRITE(*,*) TRIM(CPATH)
   call getcwd(cpath2)
   WRITE(*,*) 'The current path is ', TRIM(CPATH2)
  CALL SYSTEM(cpath)

END SUBROUTINE RUNMODEL

Thursday, April 19, 2012

Exploring FORTRAN DLL

Environment Intel Fortran 11.1

1. How to create a FORTRAN DLL in visual studio with INTEL FORTRAN compiler:

http://sukhbinder.wordpress.com/2011/04/14/how-to-create-fortran-dll-in-visual-studio-with-intel-fortran-compiler/

Simple to follow, worked.

2. How to call FORTRAN DLL in visual studio with INTEL FORTRAN compiler:
http://www.eng-tips.com/viewthread.cfm?qid=117348


Another method:
http://www.tek-tips.com/viewthread.cfm?qid=1572697

Calling DLL from C#
http://software.intel.com/en-us/articles/calling-fortran-function-or-subroutine-in-dll-from-c-code/

Parallel Programming 101 - Fortran OpenMP for dummies

These content are obtained from the internet, I only extracted the most useful information and tested them.  I do not claim any authorship.

1. Compiler Settings
Compaq Visual Fortran compiler does not support parallel programming feature.
If you are using Intel Visual Fortran to compile your parallel code, you have to do the following in order to have it run correctly:
Right click on project
Choose properties -> Fortran -> Preprocesor -> OpenMP->Conditional Compilation -> Yes
Choose properties -> Fortran -> Language -> Process->OpenMP Directives -> Generate Parallell Code(/Qopenmp)

(- I have to do these in my intel fortran compiler to compile! Also it is best to use free format Fortran)


2. To convert a serial code into a parallel code, you will need to use OpenMP directives
Basic format of OpenMP directives:
A typical OpenMP directive looks like this (Fortran is not case-sensitive):
!$OMP PARALLEL DO
OpenMP directives must start with a sentinel !$OMP
!$OMP can appear in any column, but must be preceded by white space only
Continuation lines must have an ampersand as the last non-blank character in a line.
!$OMP PARALLEL PRIVATE(TID)can be written as
!$OMP PARALLEL &
!$OMP PRIVATE (TID)
(Although directives start with an ! sign, it is not a comment, as in Fortran)


3. Parallel Region Construct
Parallel region: a block of code that will be executed by multiple threads.
When a thread reaches a parallel region, it creates a team of threads. The code within the parallel region is duplicated and all threads will execute the code.
The format of the parallel construct is
!$OMP PARALLEL
[Code Block]
!$OMP END PARALLEL

Example Code 1

PROGRAM Hello
!$OMP PARALLEL
print*, 'Hello World!'
!$OMP END PARALLEL
END PROGRAM

If the above example is running on a duo core computer, then
the output will be:
Hello World!
Hello World!

(but this is not useful at all so far, as we do not want to repeat the same task on all threads)

Here is my first OpenMP program, it runs, but when I wanted to loop through the threads, error occurred. So I went to study a bit more. Note that you need to include omp_lib.h in this program. This has not touched so far.
  
!----------------------------
program ompHelloWorld
#ifdef _OPENMP
   include 'omp_lib.h'  !needed for OMP_GET_NUM_THREADS()
#endif

integer TID

!$omp parallel    
   TID = OMP_GET_THREAD_NUM()
   PRINT *, 'Hello World from thread = ', TID  
!$omp end parallel
continue
end program ompHelloWorld
!--------------------

Increasing the complexity
!--------------------
 program ompHelloWorld

#ifdef _OPENMP
   include 'omp_lib.h'  !needed for OMP_GET_NUM_THREADS()
#endif

integer nthr
integer NTHREADS, TID

TID=-999
NTHREADS=-999

!$omp parallel  
  
   TID = OMP_GET_THREAD_NUM()
   PRINT *, 'Hello World from thread = ', TID

   IF (TID .EQ. 0) THEN
       NTHREADS = OMP_GET_NUM_THREADS()
       PRINT *, 'Master computer report:'
       PRINT *, 'Number of threads = ', NTHREADS
   ELSE
       PRINT *, 'Slave computer report:'
       PRINT *, 'I am on vacation.'
   END IF     
!$omp end parallel

continue

end program ompHelloWorld
!--------------------

(Note that there is an issue with using F10 to step through the code or using F5 to debug the code. It appears that F10 stepping results are not correct. Also, inserting breakpoints into the code seems to cause issue too. I have to visit this issue!!!)

4. Work-Sharing Construct
A work-sharing construct does not create new threads. It divides the execution of the enclosed code among the existing threads.
Three types of work-sharing construct
  1. DO directive:
  2. SECTIONS directive
  3. SINGLE directive
A work-sharing construct assumes a parallel region has already been initiated, otherwise it executes in serial on a single processor.
(Now it is getting interesting!)

4.1 SINGLE Directive
The SINGLE directive speci…es that the enclosed code is to be executed by only one thread in the team.
Useful when dealing with sections of code that are not thread safe (such as I/O)
Example
!$OMP PARALLEL
    !$OMP SINGLE

    !$OMP END SINGLE
!$OMP END PARALLEL


4.2 DO directives
The DO directive specifi…es that the iterations of the loop immediately following it must be executed in parallel by the team.
The format of the DO directive is
!$OMP DO
[Code Block]
!$OMP END DO
Example Code
!$OMP PARALLEL
   !$OMP DO
   DO i = 1,n
      z(i) = a*x(i) + y
   END DO
   !$OMP END DO
!$OMP END PARALLEL

For convenience, there is
PARALLEL DO
We can replace the above code with
!$OMP PARALLEL DO
DO i = 1,n
z(i) = a*x(i) + y
END DO
Note that PARALLEL DO does not need an END PARALLEL DO.



4.3 SECTION directives
The SECTIONS directive divides the enclosed sections of code among the existing threads.
The format of the SECTIONS directive is
!$OMP SECTIONS
   !$OMP SECTION
     [Code Block 1]
   !$OMP SECTION
     [Code Block 2]
   !$OMP SECTION
     [Code Block 3]
!$OMP END SECTIONS
Example Code
!$OMP PARALLEL SHARED(A,B,C,D), PRIVATE(I)
   !$OMP SECTIONS
   !$OMP SECTION
     DO I = 1, N
        C(I) = A(I) + B(I)
     ENDDO
   !$OMP SECTION
     DO I = 1, N
        D(I) = A(I) * B(I)
     ENDDO
   !$OMP END SECTIONS
!$OMP END PARALLEL

For convenience, there is
PARALLEL SECTIONS
We can replace the above code with
!$OMP PARALLEL SECTIONS
!$OMP SECTION
[Code Block 1]
!$OMP SECTION
[Code Block 2]
!$OMP END PARALLEL SECTIONS

With the above work-share construct, I can construct some simple yet useful program.
----------------------------------------------------------------------------
Trouble 1: 
When I compile the program and sent my executable to a lab computer with 8 cores to run, it failed with an error message: 
libiomp5md.dll is missing

This is the most useful answer I found on the internet

Even when static libraries are selected, the OpenMP library is linked dynamically - this can be overridden by adding /Qopenmp-link:static under Fortran > Command Line > Additional Options.

OMP libary
!$omp parallel num_threads(3)   !use three threads for this program


Directives for Sychronization



CRITICAL contains the codes that will be executed in serial, no longer parallel, this is to avoid data comfliction


!$OMP CRITICAL
...
!$OMP END CRITICAL 

Example (I did not test this, probably should)
! Look for the largest element in an array
currentMax = -1.E10
!$OMP PARALLEL DO
    DO i = 1, n
        IF( a(i) < currentMax) THEN
           !$OMP CRITICAL
           
              IF( a(i) < currentMax) THEN 
                 currentMax = a(i)
            ENDIF
           !$OMP END CRITICAL 
        END IF
END DO

!$OMP END PARALLEL DO


--------------------------

I am running out of time for another project. This will be put on hold.

References:

  • A comprehensive Book:

http://www.amazon.com/Parallel-Programming-OpenMP-Rohit-Chandra/dp/1558606718#reader_1558606718

  • A simple to follow yet in-depth ppt (in C)

http://openmp.org/mp-documents/omp-hands-on-SC08.pdf

  • Useful table summaries of directives

http://www.cita.utoronto.ca/MISC/computing_guide/docs/Intel_Fortran_v8.1/f_ug2/index.htm#par_dirs.htm

http://www.cita.utoronto.ca/MISC/computing_guide/docs/Intel_Fortran_v8.1/f_ug2/par_dirs.htm

  • OpenMP training workshop of Llnl

https://computing.llnl.gov/tutorials/openMP/#RunTimeLibrary

https://computing.llnl.gov/tutorials/openMP/exercise.html

https://computing.llnl.gov/?set=training&page=index#training_materials

https://computing.llnl.gov/tutorials/openMP/exercise.html

Tuesday, April 17, 2012

Map a program modules

dependency walker
http://www.dependencywalker.com/

Wednesday, April 27, 2011

Work with Path

http://stackoverflow.com/questions/428085/how-do-i-set-a-path-in-visual-studio
For example, to prepend C:\Windows\Temp to the PATH:
PATH=C:\WINDOWS\Temp;%PATH%
Similarly, to append $(TargetDir)\DLLS to the PATH:
PATH=%PATH%;$(TargetDir)\DLLS
Project | Properties | Select Configuration | Configuration Properties | Debugging | Working directory
Repeat for each project configuration.