root/trunk/matml/src/ternary/Ternary.c

Revision 508, 12.4 kB (checked in by powell, 2 years ago)

Stubs for missing callbacks.

  • Property svn:eol-style set to native
Line 
1/***************************************
2  $Header$
3
4  This is a Glade front end to Ternary GUI.
5
6  Copyright 2009 Adam C. Powell, IV
7  ***************************************/
8
9
10#include "gibbs.h"
11#include <glade/glade.h>
12#include <libgnomeui/libgnomeui.h>
13
14GladeXML *xml;
15FILE *isotherm_geomview_pipe=NULL, *phdiag_geomview_pipe=NULL;
16
17/* eparams: name, R,T0, G1@T0,G2,G3, C1,C2,C3, S1,S2,S3,S4,S5,
18   O12,O13,O23, O123,O234,O235,O245,O345,O2345 gauss ptr, gauss number */
19energy_params allparams[2];
20double T_iso=298., P=1., T_min=273., T_max=373.;
21int P1_res=20, P2_res=20, T_steps=50, numpoints=462, refine=2, *verts=NULL;
22int max_boundaries=0;
23gboolean spinodal_toggle [2];
24
25char P1_name[100], P2_name[100], gv_version [100], *entry_names[] = {
26  "T_entry", "R_entry", "P_entry", "Tmin_entry", "Tmax_entry",
27  "P1_T_entry", "P1_G1_entry", "P1_G2_entry", "P1_G3_entry",
28  "P1_C1_entry", "P1_C2_entry", "P1_C3_entry",
29  "P1_S1_entry", "P1_S2_entry", "P1_S3_entry",
30  "P1_O12_entry", "P1_O13_entry", "P1_O23_entry", "P1_O123_entry",
31  "P2_T_entry", "P2_G1_entry", "P2_G2_entry", "P2_G3_entry",
32  "P2_C1_entry", "P2_C2_entry", "P2_C3_entry",
33  "P2_S1_entry", "P2_S2_entry", "P2_S3_entry",
34  "P2_O12_entry", "P2_O13_entry", "P2_O23_entry", "P2_O123_entry" };
35
36char *hscale_names[] = {
37  "T_hscale", "R_hscale", "P_hscale", "Tmin_hscale", "Tmax_hscale",
38  "P1_T_hscale", "P1_G1_hscale", "P1_G2_hscale", "P1_G3_hscale",
39  "P1_C1_hscale", "P1_C2_hscale", "P1_C3_hscale",
40  "P1_S1_hscale", "P1_S2_hscale", "P1_S3_hscale",
41  "P1_O12_hscale", "P1_O13_hscale", "P1_O23_hscale", "P1_O123_hscale",
42  "P2_T_hscale", "P2_G1_hscale", "P2_G2_hscale", "P2_G3_hscale",
43  "P2_C1_hscale", "P2_C2_hscale", "P2_C3_hscale",
44  "P2_S1_hscale", "P2_S2_hscale", "P2_S3_hscale",
45  "P2_O12_hscale", "P2_O13_hscale", "P2_O23_hscale", "P2_O123_hscale" };
46
47GtkWidget *entry_widgets [33], *hscale_widgets [33];
48double *pointers[] =
49  { &T_iso, &(allparams[0].R), &P, &T_min, &T_max,
50    &(allparams[0].T0), &(allparams[0].G1_T0), &(allparams[0].G2_T0), &(allparams[0].G3_T0),
51    &(allparams[0].G1_C), &(allparams[0].G2_C), &(allparams[0].G3_C),
52    &(allparams[0].S1), &(allparams[0].S2), &(allparams[0].S3),
53    &(allparams[0].Omega12), &(allparams[0].Omega13), &(allparams[0].Omega23),
54    &(allparams[0].Omega123),
55    &(allparams[1].T0), &(allparams[1].G1_T0), &(allparams[1].G2_T0), &(allparams[1].G3_T0),
56    &(allparams[1].G1_C), &(allparams[1].G2_C), &(allparams[1].G3_C),
57    &(allparams[1].S1), &(allparams[1].S2), &(allparams[1].S3),
58    &(allparams[1].Omega12), &(allparams[1].Omega13), &(allparams[1].Omega23),
59    &(allparams[1].Omega123) };
60
61void update_pointer (int i, double content)
62{
63  *pointers [i] = content;
64
65  if (i==1)
66    allparams[1].R = content;
67}
68
69void recalculate_and_display ()
70{
71  int i, numbounds, hullnumfacets;
72  energy_point *points=NULL;
73  hull_facet *hullfacets = NULL;
74  phase_boundary spinodals [2], *allbounds=NULL;
75  visual_point *vpoints=NULL;
76
77  // Reallocate points array, and set up triangles
78  numpoints = (P1_res+1)*(P1_res+2)/2 + (P2_res+1)*(P2_res+2)/2;
79  if (!(points = (energy_point *) realloc
80        (points, numpoints * sizeof (energy_point))))
81    { printf ("recalculate_and_display: "
82              "Cannot (re-)allocate point coordinate array\n"); exit (1); }
83  if (!(verts = (int *) realloc
84        (verts, (P1_res*P1_res*3 + P2_res*P2_res*3) * sizeof (int))))
85    { printf ("recalculate_and_display: "
86              "Cannot (re-)allocate triangle vertex array\n"); exit (1); }
87  if (i=init_triangle_array (P1_res, points, 0, 0.,0., 1.,0., 0.,1.))
88    { printf ("recalculate_and_display: Error %d in init_triangle_array\n", i);
89      exit (i); }
90  if (i=init_triangle_array (P2_res, points+(P1_res+1)*(P1_res+2)/2,
91                             0, 0.,0., 1.,0., 0.,1.))
92    { printf ("recalculate_and_display: Error %d in init_triangle_array\n", i);
93      exit (i); }
94  if (i=init_triangle_vertices (P1_res, verts, 0))
95    { printf ("recalculate_and_display: "
96              "Error %d in init_triangle_vertices\n", i); exit (i); }
97  if (i=init_triangle_vertices (P2_res, verts+P1_res*P1_res*3,
98                                (P1_res+1)*(P1_res+2)/2))
99    { printf ("recalculate_and_display: "
100              "Error %d in init_triangle_vertices\n", i); exit (i); }
101
102  // Calculate free energies and convex hull, and refine
103  if (i=free_energies (points, numpoints/2, T_iso,P, allparams,0,
104                       WITH_DERIVATIVES_NO_INFINITY))
105    { printf ("recalculate_and_display: Error %d in free_energies\n", i);
106      exit (i); }
107  if (i=free_energies (points+numpoints/2, numpoints/2, T_iso,P, allparams,1,
108                       WITH_DERIVATIVES_NO_INFINITY))
109    { printf ("recalculate_and_display: Error %d in free_energies\n", i);
110      exit (i); }
111
112  if (i=hullCalculate (3, points, numpoints, allparams, 2, T_iso, P,
113                       &hullfacets, &hullnumfacets))
114    { printf ("recalculate_and_display: Error %d in hullCalculate\n", i);
115      exit (i); }
116  for (i=0; i<refine; i++)
117    hullRefine (&points, &numpoints, &hullfacets, &hullnumfacets,
118                allparams, 2, T_iso, P, 1e-10, 1e-7, 0);
119
120  // Calculate spinodals if appropriate
121  if (spinodal_toggle [0])
122    if (i=calc_spinodal
123        (&points, &numpoints, verts, P1_res*P1_res, T_iso,P, allparams,0,
124         spinodals))
125      { printf ("recalculate_and_display: Error %d in calc_spinodal\n", i);
126        exit (i); }
127  if (spinodal_toggle [1])
128    if (i=calc_spinodal
129        (&points, &numpoints, verts+P1_res*P1_res, P2_res*P2_res, T_iso,P,
130         allparams,1, spinodals+1))
131      { printf ("recalculate_and_display: Error %d in calc_spinodal\n", i);
132        exit (i); }
133
134  // Calculate phase boundaries
135  if (i=hullReturnPhaseBoundaries (points,numpoints, hullfacets,hullnumfacets,
136                                   allparams, T_iso, P, &allbounds, &numbounds))
137    { printf ("recalculate_and_display: "
138              "error %d in hullReturnPhaseBoundaries\n", i); exit (i); }
139
140  // Display free energy surface, spinodals and boundaries using geomview
141  if (isotherm_geomview_pipe)
142    {
143      if (i=energy_to_visual_array (numpoints, points, &vpoints, 0.,-1500.,
144                                    1.,500., NULL,NULL, 0))
145        { printf ("recalculate_and_display: "
146                  "Error %d in energy_to_visual_array\n", i); exit (i); }
147
148      if (i=GeomviewDisplayTriangleCOFF
149          (isotherm_geomview_pipe, "Free Energy Function", "tfe", "shading smooth",
150           numpoints, vpoints, P1_res*P1_res + P2_res*P2_res, verts))
151        { printf ("recalculate_and_display: Error %d in Geomview Display\n", i);
152          exit (i); }
153
154      if (i=GeomviewDisplayPhaseBoundary
155          (isotherm_geomview_pipe, "Spinodal 1", "spin1", "-face +edge",
156           numpoints, vpoints, spinodal_toggle[0] ? spinodals : NULL))
157        { printf ("recalculate_and_display: Error %d in Geomview Display\n", i);
158          exit (i); }
159      if (i=GeomviewDisplayPhaseBoundary
160          (isotherm_geomview_pipe, "Spinodal 2", "spin2", "-face +edge",
161           numpoints, vpoints, spinodal_toggle[1] ? spinodals+1 : NULL))
162        { printf ("recalculate_and_display: Error %d in Geomview Display\n", i);
163          exit (i); }
164
165      max_boundaries = (numbounds>max_boundaries) ? numbounds : max_boundaries;
166      for (i=0; i<max_boundaries; i++)
167        {
168          char name [20], label [10];
169          int ret;
170
171          snprintf (name, 18, "Boundary %d", i);
172          snprintf (label, 8, "bd%d", i);
173          if (ret=GeomviewDisplayPhaseBoundary
174              (isotherm_geomview_pipe, name, label, "-face +edge",
175               numpoints, vpoints, (i<numbounds) ? allbounds+i : NULL))
176            { printf ("recalculate_and_display: "
177                      "Error %d in Geomview Display\n", ret); exit (ret); }
178        }
179    }
180
181  free (vpoints);
182  free (allbounds);
183  free (hullfacets);
184  free (points);
185
186  //printf ("New parameters: \n");
187  //for (i=0; i<33; i++)
188  //  printf (" %s %g", entry_names[i], *(pointers[i]));
189  //printf ("\n");
190}
191
192void get_entry_update_hscale_and_pointer (int i)
193{
194  GtkWidget *entry = entry_widgets [i], *hscale = hscale_widgets [i];
195  G_CONST_RETURN char *entrytext;
196  gdouble content;
197
198  entrytext = gtk_entry_get_text (GTK_ENTRY(entry));
199  sscanf (entrytext, "%lf", &content);
200
201  gtk_range_set_value (GTK_RANGE (hscale), content);
202  update_pointer (i, content);
203  if (isotherm_geomview_pipe || phdiag_geomview_pipe)
204    recalculate_and_display ();
205}
206
207void on_entry_activate (GtkWidget *widget, gpointer user_data)
208{
209  int i;
210
211  for (i=0; i<33 && widget != entry_widgets [i]; i++)
212    ;
213  if (i<33)
214    get_entry_update_hscale_and_pointer (i);
215  else
216    printf ("Internal error, on_entry_activate got widget not in table\n");
217}
218
219void on_hscale_value_changed (GtkWidget *widget, gpointer user_data)
220{
221  int i;
222
223  for (i=0; i<33 && widget != hscale_widgets [i]; i++)
224    ;
225  if (i<33)
226    {
227      GtkWidget *entry = entry_widgets [i];
228      const gchar entrytext [100];
229      gdouble content;
230
231      content = gtk_range_get_value (GTK_RANGE (hscale_widgets [i]));
232      snprintf (entrytext, 98, "%g", content);
233      gtk_entry_set_text (GTK_ENTRY(entry), entrytext);
234
235      update_pointer (i, content);
236      if (isotherm_geomview_pipe || phdiag_geomview_pipe)
237        recalculate_and_display ();
238    }
239  else
240    printf ("Internal error, on_hscale_value_changed got widget 0x%lx not in table\n", widget);
241}
242
243void on_spinodal_togglebutton_toggled (GtkWidget *widget, gpointer user_data)
244{
245  spinodal_toggle [0] = gtk_toggle_button_get_active
246    (GTK_TOGGLE_BUTTON(glade_xml_get_widget(xml, "P1_spinodal_togglebutton")));
247  spinodal_toggle [1] = gtk_toggle_button_get_active
248    (GTK_TOGGLE_BUTTON(glade_xml_get_widget(xml, "P2_spinodal_togglebutton")));
249  if (isotherm_geomview_pipe || phdiag_geomview_pipe)
250    recalculate_and_display ();
251}
252
253void on_spinbutton_value_changed (GtkWidget *widget, gpointer user_data)
254{
255  T_steps = (int) gtk_spin_button_get_value
256    (GTK_SPIN_BUTTON (glade_xml_get_widget(xml, "phase_steps_spinbutton")));
257  refine = (int) gtk_spin_button_get_value
258    (GTK_SPIN_BUTTON (glade_xml_get_widget(xml, "refine_spinbutton")));
259  P1_res = (int) gtk_spin_button_get_value
260    (GTK_SPIN_BUTTON (glade_xml_get_widget(xml, "P1_res_spinbutton")));
261  P2_res = (int) gtk_spin_button_get_value
262    (GTK_SPIN_BUTTON (glade_xml_get_widget(xml, "P2_res_spinbutton")));
263  numpoints = (P1_res+1)*(P1_res+2)/2 + (P2_res+1)*(P2_res+2)/2;
264  recalculate_and_display ();
265}
266
267void on_name_entry_activate (GtkWidget *widget, gpointer user_data)
268{
269  strncpy (allparams[0].name, (char *) gtk_entry_get_text
270           (GTK_ENTRY (glade_xml_get_widget (xml, "P1_name_entry"))), 99);
271  strncpy (allparams[1].name, (char *) gtk_entry_get_text
272           (GTK_ENTRY (glade_xml_get_widget (xml, "P2_name_entry"))), 99);
273}
274
275void show_isotherm (GtkWidget *widget, gpointer user_data)
276{
277  int ierr;
278  gboolean active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(widget));
279
280  if (active && isotherm_geomview_pipe == NULL)
281    {
282      ierr = GeomviewBegin (&isotherm_geomview_pipe, gv_version);
283      recalculate_and_display ();
284    }
285  else if ((!active) && isotherm_geomview_pipe != NULL)
286    GeomviewEnd (&isotherm_geomview_pipe);
287  else
288    printf ("Error: toggle is %d but geomview was 0x%lx\n", active,
289            isotherm_geomview_pipe);
290}
291
292void save_isotherm_snapshot (GtkWidget *widget, gpointer user_data)
293{
294  G_CONST_RETURN gchar *entrytext;
295
296  entrytext = gtk_entry_get_text (GTK_ENTRY (widget));
297
298  if (isotherm_geomview_pipe)
299    {
300      printf ("Saving snapshot %s...\n", entrytext);
301      fprintf (isotherm_geomview_pipe, "(snapshot c0 %s)\n", entrytext);
302    }
303}
304
305void show_phasediag (GtkWidget *widget, gpointer user_data)
306{} // Not yet implemented
307
308void save_phase_snapshot (GtkWidget *widget, gpointer user_data)
309{} // Not yet implemented
310
311int main (int argc, char *argv[])
312{
313  int i;
314  char filename [200];
315
316  // Initialize the program, and load and connect the Glade XML interface
317  gnome_program_init ("Ternary", VERSION, LIBGNOMEUI_MODULE, argc, argv, NULL);
318  strncpy (filename, GLADE_DIRECTORY, 186);
319  strcat (filename, "/Ternary.glade");
320  printf ("Loading Glade XML user interface file %s\n", filename);
321  xml = glade_xml_new (filename, NULL, NULL);
322  glade_xml_signal_autoconnect (xml);
323  // Fill the entry_widgets and hscale_widgets tables
324  allparams[0].name = P1_name;
325  allparams[1].name = P2_name;
326  for (i=32; i>=0; i--)
327    {
328      entry_widgets [i] = glade_xml_get_widget (xml, entry_names [i]);
329      hscale_widgets [i] = glade_xml_get_widget (xml, hscale_names [i]);
330      get_entry_update_hscale_and_pointer (i);
331    }
332  on_spinbutton_value_changed (NULL, NULL);
333  on_name_entry_activate (NULL, NULL);
334  on_spinodal_togglebutton_toggled (NULL, NULL);
335
336  // Run main loop
337  if (i=GeomviewBegin (&isotherm_geomview_pipe, gv_version))
338    { printf ("main: Error %d in GeomviewBegin\n", i); exit (i); }
339  gtk_widget_show (glade_xml_get_widget (xml, "main_window"));
340  recalculate_and_display();
341  gtk_main();
342  if (isotherm_geomview_pipe)
343    GeomviewEnd (&isotherm_geomview_pipe);
344
345  return 0;
346}
Note: See TracBrowser for help on using the browser.