Moving to C++ create_ppm function. Need to workout output formatting.

This commit is contained in:
Patrick Shriwise 2018-10-09 15:15:41 -05:00
parent 05192cc2d7
commit 562b8daf85
3 changed files with 92 additions and 77 deletions

View file

@ -67,6 +67,12 @@ namespace openmc {
};
extern "C" void output_ppm(ObjectPlot* pl,
const std::vector< std::vector< std::vector<int> > > &data);
extern "C" void create_ppm(ObjectPlot* pl);
extern "C" void position_rgb(Particle* p, ObjectPlot* pl, int rgb[3], int &id);
extern "C" void voxel_init(hid_t file_id, const hsize_t* dims, hid_t* dspace,

View file

@ -33,8 +33,14 @@ module plot
integer(C_INT), intent(out) :: rgb(3)
integer(C_INT), intent(out) :: id
end subroutine position_rgb
end interface
subroutine create_ppm(pl) bind(C)
import ObjectPlot
type(ObjectPlot), intent(in) :: pl
end subroutine create_ppm
end interface
contains
!===============================================================================
@ -70,80 +76,80 @@ contains
! specification in the portable pixmap format (PPM)
!===============================================================================
subroutine create_ppm(pl)
type(ObjectPlot), intent(in) :: pl
! subroutine create_ppm(pl)
! type(ObjectPlot), intent(in) :: pl
integer :: in_i
integer :: out_i
integer :: x, y ! pixel location
integer :: rgb(3) ! colors (red, green, blue) from 0-255
integer :: id
integer :: height, width
real(8) :: in_pixel
real(8) :: out_pixel
real(8) :: xyz(3)
integer, allocatable :: data(:,:,:)
type(Particle) :: p
! integer :: in_i
! integer :: out_i
! integer :: x, y ! pixel location
! integer :: rgb(3) ! colors (red, green, blue) from 0-255
! integer :: id
! integer :: height, width
! real(8) :: in_pixel
! real(8) :: out_pixel
! real(8) :: xyz(3)
! integer, allocatable :: data(:,:,:)
! type(Particle) :: p
width = pl % pixels(1)
height = pl % pixels(2)
! width = pl % pixels(1)
! height = pl % pixels(2)
in_pixel = pl % width(1)/dble(width)
out_pixel = pl % width(2)/dble(height)
! in_pixel = pl % width(1)/dble(width)
! out_pixel = pl % width(2)/dble(height)
! Allocate and initialize results array
allocate(data(3, width, height))
data(:,:,:) = 0
! ! Allocate and initialize results array
! allocate(data(3, width, height))
! data(:,:,:) = 0
if (pl % basis == PLOT_BASIS_XY) then
in_i = 1
out_i = 2
xyz(1) = pl % origin(1) - pl % width(1) / TWO
xyz(2) = pl % origin(2) + pl % width(2) / TWO
xyz(3) = pl % origin(3)
else if (pl % basis == PLOT_BASIS_XZ) then
in_i = 1
out_i = 3
xyz(1) = pl % origin(1) - pl % width(1) / TWO
xyz(2) = pl % origin(2)
xyz(3) = pl % origin(3) + pl % width(2) / TWO
else if (pl % basis == PLOT_BASIS_YZ) then
in_i = 2
out_i = 3
xyz(1) = pl % origin(1)
xyz(2) = pl % origin(2) - pl % width(1) / TWO
xyz(3) = pl % origin(3) + pl % width(2) / TWO
end if
! if (pl % basis == PLOT_BASIS_XY) then
! in_i = 1
! out_i = 2
! xyz(1) = pl % origin(1) - pl % width(1) / TWO
! xyz(2) = pl % origin(2) + pl % width(2) / TWO
! xyz(3) = pl % origin(3)
! else if (pl % basis == PLOT_BASIS_XZ) then
! in_i = 1
! out_i = 3
! xyz(1) = pl % origin(1) - pl % width(1) / TWO
! xyz(2) = pl % origin(2)
! xyz(3) = pl % origin(3) + pl % width(2) / TWO
! else if (pl % basis == PLOT_BASIS_YZ) then
! in_i = 2
! out_i = 3
! xyz(1) = pl % origin(1)
! xyz(2) = pl % origin(2) - pl % width(1) / TWO
! xyz(3) = pl % origin(3) + pl % width(2) / TWO
! end if
! allocate and initialize particle
call particle_initialize(p)
p % coord(1) % xyz = xyz
p % coord(1) % uvw = [ HALF, HALF, HALF ]
p % coord(1) % universe = root_universe
! ! allocate and initialize particle
! call particle_initialize(p)
! p % coord(1) % xyz = xyz
! p % coord(1) % uvw = [ HALF, HALF, HALF ]
! p % coord(1) % universe = root_universe
!$omp parallel do firstprivate(p) private(x, rgb, id) reduction(+ : data)
do y = 1, height
! Set y coordinate
p % coord(1) % xyz(out_i) = xyz(out_i) - out_pixel*(y - 1)
do x = 1, width
! Set x coordinate
p % coord(1) % xyz(in_i) = xyz(in_i) + in_pixel*(x - 1)
! !$omp parallel do firstprivate(p) private(x, rgb, id) reduction(+ : data)
! do y = 1, height
! ! Set y coordinate
! p % coord(1) % xyz(out_i) = xyz(out_i) - out_pixel*(y - 1)
! do x = 1, width
! ! Set x coordinate
! p % coord(1) % xyz(in_i) = xyz(in_i) + in_pixel*(x - 1)
! get pixel color
call position_rgb(p, pl, rgb, id)
! Create a pixel at (x,y) with color (r,g,b)
data(:, x, y) = rgb
end do
end do
!$omp end parallel do
! ! get pixel color
! call position_rgb(p, pl, rgb, id)
! ! Create a pixel at (x,y) with color (r,g,b)
! data(:, x, y) = rgb
! end do
! end do
! !$omp end parallel do
! Draw tally mesh boundaries on the image if requested
if (pl % index_meshlines_mesh >= 0) call draw_mesh_lines(pl, data)
! ! Draw tally mesh boundaries on the image if requested
! if (pl % index_meshlines_mesh >= 0) call draw_mesh_lines(pl, data)
! Write out the ppm to a file
call output_ppm(pl, data)
! ! Write out the ppm to a file
! call output_ppm(pl, data)
end subroutine create_ppm
! end subroutine create_ppm
!===============================================================================
! DRAW_MESH_LINES draws mesh line boundaries on an image

View file

@ -1,4 +1,6 @@
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include "openmc/plot.h"
#include "openmc/constants.h"
@ -8,6 +10,7 @@
#include "openmc/geometry.h"
#include "openmc/cell.h"
#include "openmc/material.h"
#include "openmc/string_functions.h"
namespace openmc {
@ -64,9 +67,9 @@ void create_ppm(ObjectPlot* pl) {
std::vector< std::vector< std::vector<int>>> data;
data.resize(width);
for (auto i : data) {
for (auto & i : data) {
i.resize(height);
for (auto j : i) {
for (auto & j : i) {
j.resize(3);
}
}
@ -108,11 +111,13 @@ void create_ppm(ObjectPlot* pl) {
for (int x = 0; x < width; x++) {
p->coord[0].xyz[in_i] = xyz[in_i] + in_pixel*(x);
// position_rgb(p, pl, rgb, id);
std::copy(rgb, rgb+3, &(data[x][y][0]));
data[x][y][0] = rgb[0];
data[x][y][1] = rgb[1];
data[x][y][2] = rgb[2];
}
}
//output_ppm(pl, data);
output_ppm(pl, data);
}
@ -182,27 +187,25 @@ void position_rgb(Particle* p, ObjectPlot* pl, int rgb[3], int &id) {
// OUTPUT_PPM writes out a previously generated image to a PPM file
//===============================================================================
void output_ppm(ObjectPlot* pl, int* data, int data_size)
void output_ppm(ObjectPlot* pl,
const std::vector< std::vector< std::vector<int> > > &data)
{
// check incoming data size
assert(data_size % 3 == 0);
// Open PPM file for writing
std::ofstream of(pl->path_plot);
std::string fname = std::string(pl->path_plot);
fname = strtrim(fname);
std::ofstream of(fname);
// Write header
of << "P6" << std::endl;
of << pl->pixels[0] << " " << pl->pixels[1] << std::endl;
of << "255" << std::endl;
int* rgb;
// Write color for each pixel
for (int y = 0; y < pl->pixels[1]; y++) {
for (int x = 0; x < pl->pixels[0]; x++) {
int idx = 3*(x + y*width);
rgb = &data[idx];
of << itoa(rgb[0]) << itoa(rgb[1]) << itoa(rgb[2]);
std::vector<int> rgb = data[x][y];
of << (char)rgb[0] << (char)rgb[1] << (char)rgb[2];
}
}