fieldslice.cpp 1.92 KB
#include "fieldslice.h"
#include "dataTypes.h"

#include "cufft.h"

#include <iostream>
#include <stdlib.h>
using namespace std;

fieldslice::fieldslice(unsigned int x_size, unsigned int y_size)
{
	//save the slice resolution
	R[0] = x_size;
	R[1] = x_size;

	scalarField = true;

	//init_gpu();


}

void fieldslice::toAngularSpectrum()
{
    cufftHandle plan;

    //create a CUFFT plan handle
    if(cufftPlan2d(&plan, R[0], R[1], CUFFT_C2C) != CUFFT_SUCCESS)
    {
        cout<<"Error creating CUFFT plan for computing the angular spectrum."<<endl;
        exit(1);
    }

#ifdef PRECISION_SINGLE
    if(cufftExecC2C(plan, (cufftComplex*)x_hat, (cufftComplex*)x_hat, CUFFT_FORWARD) != CUFFT_SUCCESS)
#elif defined PRECISION_DOUBLE
    if(cufftExecZ2Z(plan, (cufftDoubleComplex*)x_hat, (cufftDoubleComplex*)x_hat, CUFFT_FORWARD) != CUFFT_SUCCESS)
#endif
    {
        cout<<"Error executing the CUFFT forward FFT to compute the angular spectrum."<<endl;
        exit(1);

    }

    cufftDestroy(plan);

}

void fieldslice::fromAngularSpectrum()
{
    cufftHandle plan;

    //create a CUFFT plan handle
    if(cufftPlan2d(&plan, R[0], R[1], CUFFT_C2C) != CUFFT_SUCCESS)
    {
        cout<<"Error creating CUFFT plan for computing the angular spectrum."<<endl;
        exit(1);
    }

#ifdef PRECISION_SINGLE
    if(cufftExecC2C(plan, (cufftComplex*)x_hat, (cufftComplex*)x_hat, CUFFT_INVERSE) != CUFFT_SUCCESS)
#elif defined PRECISION_DOUBLE
    if(cufftExecZ2Z(plan, (cufftDoubleComplex*)x_hat, (cufftDoubleComplex*)x_hat, CUFFT_INVERSE) != CUFFT_SUCCESS)
#endif
    {
        cout<<"Error executing the CUFFT forward FFT to compute the angular spectrum."<<endl;
        exit(1);

    }

    //divide the field by the number of values in the field
    ScaleField( 1.0 / (R[0] * R[1]));

    cufftDestroy(plan);

}

fieldslice::fieldslice()
{
	R[0] = R[1] = 0;
	x_hat = y_hat = z_hat = NULL;
}



fieldslice::~fieldslice()
{
	//kill_gpu();
}