File: example.c

package info (click to toggle)
libctl 3.2.2-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 2,304 kB
  • ctags: 1,178
  • sloc: sh: 11,466; ansic: 5,903; lisp: 2,311; makefile: 123
file content (178 lines) | stat: -rw-r--r-- 6,250 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* libctl: flexible Guile-based control files for scientific software 
 * Copyright (C) 1998-2014 Massachusetts Institute of Technology and Steven G. Johnson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 * Steven G. Johnson can be contacted at stevenj@alum.mit.edu.
 */

/**************************************************************************/

/* Example routines, callable from Guile as defined in the example.scm
   specifications file. Read in the input variables and writes some
   data into the output variables. */

/**************************************************************************/

#include <stdio.h>
#include <stdlib.h>

#include "ctl-io.h"
#include <ctlgeom.h>

/**************************************************************************/

/* function to display a little information about a geometric object to
   prove that we've read it in correctly. */
static void display_object_info(geometric_object obj)
{
  printf("     center = (%g,%g,%g), epsilon = %g\n",
	 obj.center.x, obj.center.y,
	 obj.center.z, obj.material.epsilon);
  
  switch (obj.which_subclass) {
  case CYLINDER:
    printf("          cylinder with height %g, axis (%g, %g, %g)\n",
	   obj.subclass.cylinder_data->height,
	   obj.subclass.cylinder_data->axis.x,
	   obj.subclass.cylinder_data->axis.y,
	   obj.subclass.cylinder_data->axis.z);
    break;
  case SPHERE:
    printf("          sphere with radius %g\n",
	   obj.subclass.sphere_data->radius);
    break;
  case BLOCK:
    printf("          block with size (%g,%g,%g)\n",
	   obj.subclass.block_data->size.x,
	   obj.subclass.block_data->size.y,
	   obj.subclass.block_data->size.z);
    printf("          projection matrix: %10.6f%10.6f%10.6f\n"
	   "                             %10.6f%10.6f%10.6f\n"
	   "                             %10.6f%10.6f%10.6f\n",
	   obj.subclass.block_data->projection_matrix.c0.x,
	   obj.subclass.block_data->projection_matrix.c1.x,
	   obj.subclass.block_data->projection_matrix.c2.x,
	   obj.subclass.block_data->projection_matrix.c0.y,
	   obj.subclass.block_data->projection_matrix.c1.y,
	   obj.subclass.block_data->projection_matrix.c2.y,
	   obj.subclass.block_data->projection_matrix.c0.z,
	   obj.subclass.block_data->projection_matrix.c1.z,
	   obj.subclass.block_data->projection_matrix.c2.z);
    break;
  case GEOMETRIC_OBJECT_SELF:
    printf("          generic geometric object\n");
    break;
  default:
    printf("          UNKNOWN OBJECT TYPE!\n");
  }
}

/* run function.  This function is callable from Scheme.  When it
   is called, the input variables are already assigned.   After
   it is called, the values assigned to the output variables are
   automatically exported to scheme. */
void run_program(void)
{
  int i, depth, nobjects;
  vector3 p;
  geom_box_tree t;

  /* Just print out some data to prove that we have read the
     input variables: */

  printf("Working in %d dimensions.\n", dimensions);

  printf("\nk-points are:\n");
  for (i = 0; i < k_points.num_items; ++i)
    printf("     (%g,%g,%g)\n",
	   k_points.items[i].x, k_points.items[i].y, k_points.items[i].z);
  
  printf("\nsome geometry info:\n");
  for (i = 0; i < geometry.num_items; ++i)
    display_object_info(geometry.items[i]);

  t = create_geom_box_tree();
  printf("\ngeometry box tree:\n");
  display_geom_box_tree(2, t);
  geom_box_tree_stats(t, &depth, &nobjects);
  printf("\ntree has depth %d and %d object nodes (vs. %d objects)\n",
	 depth, nobjects, geometry.num_items);

  p.x = 1; p.y = 0; p.z = 0;
  printf("Epsilon of (%g, %g) is %g (tree) or %g (non-tree)\n",
	 p.x, p.y,
	 material_of_point_in_tree(p, t).epsilon,
	 material_of_point(p).epsilon);

  destroy_geom_box_tree(t);

  printf("\nDone writing input.  Sending data to output vars.\n");

  /* Write out some data to the output variables.  Note that we
     MUST do this.  If we leave any output variables uninitialized,
     the result is undefined. */

  if (num_write_output_vars > 1)
    destroy_output_vars(); /* we are responsible for calling this */
  
  printf("dummy = (%g+%gi, %g+%gi, %g+%gi)\n", dummy.x.re, dummy.x.im,
	 dummy.y.re, dummy.y.im, dummy.z.re, dummy.z.im);
  dummy = make_cvector3(vector3_scale(2, cvector3_re(dummy)),
			vector3_scale(3, cvector3_im(dummy)));
  mean_dielectric = 1.23456789;
  gaps.num_items = 2;
  gaps.items = (number *) malloc(gaps.num_items * sizeof(number));
  gaps.items[0] = 3.14159;
  gaps.items[1] = 1.41421;
}

/* Another function callable from Scheme. This function does not
   use the input/output variables, but passes information in
   explicitely through its parameter and out through its return
   value.

   In a real program, this might return the fraction of the field
   energy in the given object. */
number energy_in_object(geometric_object obj)
{
  printf("Computing power in object.\n");
  display_object_info(obj);
  printf("Returning 0.123456.\n");
  return 0.123456;
}

/* A function to test passing and returning list parameters
   to/from Scheme: */
vector3_list list_func_test(number x, integer_list s, vector3 v)
{
     vector3_list vout;
     int i;

     vout.num_items = s.num_items;
     vout.items = (vector3*) malloc(sizeof(vector3) * vout.num_items);
     for (i = 0; i < vout.num_items; ++i)
	  vout.items[i] = vector3_scale(s.items[i] * x, v);
     return vout;
}

/* return func(arg), where func is a Scheme function returning a number. */
number function_func(function func, number arg)
{
     return 
	  ctl_convert_number_to_c(
	       gh_call1(func, ctl_convert_number_to_scm(arg)));
}