# hw5s - COMPUTER SCIENCE 50100 Computing for Science and...

This preview shows pages 1–3. Sign up to view the full content.

COMPUTER SCIENCE 50100 Computing for Science and Engineering FALL 2014 Solution of ASSIGNMENT # 5 (35 points) November 24 Due Wednesday, November 19 at 11:30 am This assignment covers Sections 4.6, 4.7, 5.1 of the class notes. Given in the Appendix is a program, comprising main.c and heat.c , that solves the heat equation at specified points in the unit cube, where the top and bottom face are kept at 100 C and the sides at 0 C. It uses a random walk method (based on the Feynman-Kac formula). This method is chosen because it is simple to program and computationally expensive. Func- tion main calculates the solution on a grid covering an octant of the cross-section z = 1 / 2. It prints a subset of these values and calculates the average temperature (assuming a piecewise bilinear interpolant). Here is a script for compilation and execution: #!/usr/bin/env python # run0.py from subprocess import call if call("gcc -std=c99 main.c heat.c -lm -o main.exe", shell=True): exit(-1) call("./main.exe", shell=True) 1. (7 points) The following script creates a dynamically loadable library for the functions in heat.c : #!/usr/bin/env python # run1.py from subprocess import call if call("gcc -std=c99 heat.c -c", shell=True): exit(-1) if call("gcc -dynamiclib heat.o -lm -o heat.dll", shell=True): exit(-1) if call("gcc -std=c99 main.c wrapDL.c -ldl -o main.exe", shell=True): exit(-1) call("./main.exe", shell=True) You are to write a wrapper wrapDL.c that defines a function temperature having the same prototype as in main.c but containing no code except what is needed to call the function temperature in the file heat.dll . Solution: // wrapDL.c #include <dlfcn.h> void temperature(int n, double xyz[n][3], double u[n]){ void *dl = dlopen("heat.dll", RTLD_LAZY); typedef void (*temperature_t)(int n, double xyz[n][3], double u[n]); temperature_t temperature_ = (temperature_t)dlsym(dl, "temperature"); temperature_(n, xyz, u); dlclose(dl);} 2. (14 points) 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
(a) (7 points) Translate main.c into Python with the following changes: (i) the function main takes as a single argument the temperature function to be called, (ii) the temperature function takes as a single argument the array of x, y, z coordinates and returns the array of u values, and (iii) a contour plot is saved to a file temperature.png instead of a table being printed.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern