Archive

Archive for the ‘Code’ Category

C++ strcpy, etc.

April 28, 2009 proopnarine 2 comments

In making some minor modifications to the main simulation program, CEG-1.4.8, I encountered a compile error using gcc 4.3.2-7. The system is 64-bit AMD based, running Fedora 10. gcc was installed from the rpm package gcc-4.3.2-7.x86_64. The simple compile command g++ -o CEG-1.4.8-SMP CEG-1.4.8-SMP.cpp yielded the following output:
CEG-1.4.8-SMP.cpp: In function ‘int main(int, char**)’:
CEG-1.4.8-SMP.cpp:75: error: ‘strcpy’ was not declared in this scope
CEG-1.4.8-SMP.cpp:114: error: ‘strcpy’ was not declared in this scope
CEG-1.4.8-SMP.cpp:281: error: ‘strcpy’ was not declared in this scope

Given that these lines refer to fairly old parts of the code, and that compilation has been successful for years, I suspected that either there is a bug in this version of gcc, this latest RH update to its gcc package, or that my code is no longer legal. It seems to be the latter, and using strcpy is not really a good idea anyway. Here’s an example of the offending code (all uses referred to file reads from command line arguments or opening files for output):
//open existing matrix file specified by user
do
{
strcpy(matrix_name,argv[2]);
std::cout << "\nName of network matrix file: " << matrix_name;
matrix_file.open(matrix_name);
}

Note the use of strcpy in the fourth line. Simply replacing the routine worked. Here’s a correction:
//open existing matrix file specified by user
do
{
const char *matrix_name = argv[2];
std::cout << "\nName of network matrix file: " << matrix_name;
matrix_file.open(matrix_name);
}

Categories: Code Tags: , ,

Generating beta variates

March 19, 2009 proopnarine Leave a comment
100 Beta variates from example in text.

100 Beta variates from example in text.

Our problems are to (1) draw random variates from various beta distributions, (2) embed the code in the “parallel” SMP version of CEG, and (3) pass the beta variates to CEG simulations. After exploring a number of different ways to generate the variates in C++, notably using the GNU Scientific Library, I’ve decided to simply use an Octave script. The syntax is very easy, and because the C++ simulations are embedded in a Bash script, it’s really easy to simply generate the variates and have C++ load the output files. Here’s the basic Octave script:

#! /bin/octave -qf
#Generate beta variates;
arg_list = argv ();
a = str2double(arg_list{1});
b = str2double(arg_list{2});
c = str2double(arg_list{3});
x = betarnd(a,b,c,1);
disp (x)

This script is run by the following Bash script:
#! /bin/bash
beta_file1=$4
echo $beta_file1
octave -q $1 $2 $3 >$beta_file1

The Bash script is called with command line parameters, namely the parameters for Octave’s betarnd function (a, b, and number of variates; the last parameter is 1, since output is a vector). A call would therefore look like this:
./beta.sh 2 5 100 beta_output1.dat
where beta.sh is the Bash script, the distribution being called is \beta(2,5), and 100 random variates will be generated. The output is re-directed to output file beta_output1.dat. Don’t forget to make your script executable with chmod +x beta.sh

Categories: Code Tags: ,