Example #1
0
void example_2d()
{

	const double Ly = 2*M_PI;
	const double Lx = 2*M_PI;

	const ptrdiff_t N0 = 100;
	const ptrdiff_t N1 = 100;

	const double dy = Ly/N0;
	const double dx = Lx/N1;

	FFTWPoisson2DMPI solver(N0, Ly, N1, Lx);

	const int nx = solver.get_nx();
	const int ny = solver.get_ny();
	const int y0 = solver.get_y0();

	double *x = new double[nx*ny];
	double *s = new double[nx*ny];
	build_problem(x, y0, ny, dy, 0, nx, dx, 10);
	build_solution(s, y0, ny, dy, 0, nx, dx, 10);

	solver.solve(x);

	double err = 0;
	for(int i=0; i<nx*ny; i++)
		err = std::max(err, std::abs(x[i]-s[i]));
	std::cout << err << std::endl;

	delete x, s;

}
Example #2
0
void hypre_example_2d()
{

	const double Ly = 2*M_PI;
	const double Lx = 2*M_PI;

	const ptrdiff_t N0 = 100;
	const ptrdiff_t N1 = 100;

	const double dy = Ly/(N0+1);
	const double dx = Lx/(N1+1);

	HypreSolver2D solver(N0, Ly, N1, Lx);

	const int nx = solver.get_nx();
	const int ny = solver.get_ny();
	const int y0 = solver.get_y0();

	double *x = new double[nx*ny];
	double *s = new double[nx*ny];
	build_problem(x, y0+1, ny, dy, 1, nx, dx, 10);
	build_solution(s, y0+1, ny, dy, 1, nx, dx, 10);


	solver.solve(&x[0]);

	// I want to make sure that it works multiple times.
	// It is not always clear from the hypre function calls
	// that this will work as expected
	build_problem(x, y0+1, ny, dy, 1, nx, dx, 10);
	solver.solve(&x[0]);

	int num_iterations = solver.get_num_iterations();
	double final_res_norm = solver.get_final_res_norm();

	double err = 0;
	for(int i=0; i<nx*ny; i++)
		err = std::max(err, std::abs(x[i]-s[i]));
	std::cout <<  err << ' '
			  << num_iterations << ' '
			  << final_res_norm << std::endl;

	delete x, s;

}
Example #3
0
int mpl_generate(MPL *mpl, char *file)
{     if (!(mpl->phase == 1 || mpl->phase == 2))
         fault("mpl_generate: invalid call sequence");
      /* set up error handler */
      if (setjmp(mpl->jump)) goto done;
      /* generate model */
      mpl->phase = 3;
      open_output(mpl, file, "w");
      generate_model(mpl);
      close_output(mpl);
      /* build problem instance */
      build_problem(mpl);
      /* generation phase has been finished */
      print("Model has been successfully generated");
done: /* return to the calling program */
      return mpl->phase;
}
Example #4
0
void hypre_example_3d()
{

	const double Lz = 2*M_PI;
	const double Ly = 2*M_PI;
	const double Lx = 2*M_PI;

	const ptrdiff_t N0 = 100;
	const ptrdiff_t N1 = 100;
	const ptrdiff_t N2 = 100;

	const double dz = Lz/(N0+1);
	const double dy = Ly/(N1+1);
	const double dx = Lx/(N2+1);

	HypreSolver3D solver(N0, Lz, N1, Ly, N2, Lx);

	const int nz = solver.get_nz();
	const int nx = solver.get_nx();
	const int ny = solver.get_ny();
	const int z0 = solver.get_z0();

	double *x = new double[nx*ny*nz];
	double *s = new double[nx*ny*nz];
	build_problem(x, z0+1, nz, dz, 1, ny, dy, 1, nx, dx, 10);
	build_solution(s, z0+1, nz, dz, 1, ny, dy, 1, nx, dx, 10);

	solver.solve(x);

	int num_iterations = solver.get_num_iterations();
	double final_res_norm = solver.get_final_res_norm();

	double err = 0;
	for(int i=0; i<nx*ny*nz; i++)
		err = std::max(err, std::abs(x[i]-s[i]));
	std::cout <<  err << ' '
			  << num_iterations << ' '
			  << final_res_norm << std::endl;

	delete x, s;

}
Example #5
0
void example_3d()
{

	const double Lz = 2*M_PI;
	const double Ly = 2*M_PI;
	const double Lx = 2*M_PI;

	const ptrdiff_t N0 = 100;
	const ptrdiff_t N1 = 100;
	const ptrdiff_t N2 = 100;

	const double dz = Lz/N0;
	const double dy = Ly/N1;
	const double dx = Lx/N2;

	FFTWPoisson3DMPI solver(N0, Lz, N1, Ly, N2, Lx);

	const int nz = solver.get_nz();
	const int nx = solver.get_nx();
	const int ny = solver.get_ny();
	const int z0 = solver.get_z0();

	double *x = new double[nx*ny*nz];
	double *s = new double[nx*ny*nz];
	build_problem(x, z0, nz, dz, 0, ny, dy, 0, nx, dx, 10);
	build_solution(s, z0, nz, dz, 0, ny, dy, 0, nx, dx, 10);

	solver.solve(x);

	double err = 0;
	for(int i=0; i<nx*ny*nz; i++)
		err = std::max(err, std::abs(x[i]-s[i]));
	std::cout << err << std::endl;

	delete x, s;

}
Example #6
0
int mpl_read_data(MPL *mpl, char *file)
#if 0 /* 02/X-2008 */
{     if (mpl->phase != 1)
#else
{     if (!(mpl->phase == 1 || mpl->phase == 2))
#endif
         xfault("mpl_read_data: invalid call sequence\n");
      if (file == NULL)
         xfault("mpl_read_data: no input filename specified\n");
      /* set up error handler */
      if (setjmp(mpl->jump)) goto done;
      /* process data section */
      mpl->phase = 2;
      xprintf("Reading data section from %s...\n", file);
      mpl->flag_d = 1;
      open_input(mpl, file);
      /* in this case the keyword 'data' is optional */
      if (is_literal(mpl, "data"))
      {  get_token(mpl /* data */);
         if (mpl->token != T_SEMICOLON)
            error(mpl, "semicolon missing where expected");
         get_token(mpl /* ; */);
      }
      data_section(mpl);
      /* process end statement */
      end_statement(mpl);
      xprintf("%d line%s were read\n",
         mpl->line, mpl->line == 1 ? "" : "s");
      close_input(mpl);
done: /* return to the calling program */
      return mpl->phase;
}

/*----------------------------------------------------------------------
-- mpl_generate - generate model.
--
-- *Synopsis*
--
-- #include "glpmpl.h"
-- int mpl_generate(MPL *mpl, char *file);
--
-- *Description*
--
-- The routine mpl_generate generates the model using its description
-- stored in the translator database. This phase means generating all
-- variables, constraints, and objectives, executing check and display
-- statements, which precede the solve statement (if it is presented),
-- and building the problem instance.
--
-- The character string file specifies the name of output text file, to
-- which output produced by display statements should be written. It is
-- allowed to specify NULL, in which case the output goes to stdout via
-- the routine print.
--
-- This routine should be called once after the routine mpl_read_model
-- or mpl_read_data and if one of the latters returned the code 2.
--
-- *Returns*
--
-- The routine mpl_generate returns one of the following codes:
--
-- 3 - model has been successfully generated. In this case the calling
--     program may call other api routines to obtain components of the
--     problem instance from the translator database.
-- 4 - processing failed due to some errors. In this case the calling
--     program should call the routine mpl_terminate to terminate model
--     processing. */

int mpl_generate(MPL *mpl, char *file)
{     if (!(mpl->phase == 1 || mpl->phase == 2))
         xfault("mpl_generate: invalid call sequence\n");
      /* set up error handler */
      if (setjmp(mpl->jump)) goto done;
      /* generate model */
      mpl->phase = 3;
      open_output(mpl, file);
      generate_model(mpl);
      flush_output(mpl);
      /* build problem instance */
      build_problem(mpl);
      /* generation phase has been finished */
      xprintf("Model has been successfully generated\n");
done: /* return to the calling program */
      return mpl->phase;
}

/*----------------------------------------------------------------------
-- mpl_get_prob_name - obtain problem (model) name.
--
-- *Synopsis*
--
-- #include "glpmpl.h"
-- char *mpl_get_prob_name(MPL *mpl);
--
-- *Returns*
--
-- The routine mpl_get_prob_name returns a pointer to internal buffer,
-- which contains symbolic name of the problem (model).
--
-- *Note*
--
-- Currently MathProg has no feature to assign a symbolic name to the
-- model. Therefore the routine mpl_get_prob_name tries to construct
-- such name using the name of input text file containing model section,
-- although this is not a good idea (due to portability problems). */

char *mpl_get_prob_name(MPL *mpl)
{     char *name = mpl->mpl_buf;
      char *file = mpl->mod_file;
      int k;
      if (mpl->phase != 3)
         xfault("mpl_get_prob_name: invalid call sequence\n");
      for (;;)
      {  if (strchr(file, '/') != NULL)
            file = strchr(file, '/') + 1;
         else if (strchr(file, '\\') != NULL)
            file = strchr(file, '\\') + 1;
         else if (strchr(file, ':') != NULL)
            file = strchr(file, ':') + 1;
         else
            break;
      }
      for (k = 0; ; k++)
      {  if (k == 255) break;
         if (!(isalnum((unsigned char)*file) || *file == '_')) break;
         name[k] = *file++;
      }
      if (k == 0)
         strcpy(name, "Unknown");
      else
         name[k] = '\0';
      xassert(strlen(name) <= 255);
      return name;
}