/[ascend]/branches/jacob/models/johnpye/fprops/mixtures/mixture_generics.c
ViewVC logotype

Contents of /branches/jacob/models/johnpye/fprops/mixtures/mixture_generics.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2988 - (show annotations) (download) (as text)
Tue Jun 30 04:00:24 2015 UTC (3 years, 4 months ago) by jacob
File MIME type: text/x-csrc
File size: 3473 byte(s)
expanded new library file to export mixture functions, and test .a4c file to check that the functions are exported correctly, and receive correct data.

minor changes to <ascend/compiler/instance_enum.h>, adding a few macro definitions of ASCEND data types for library file to be able to mention by name.

1 /* ASCEND modelling environment
2 Copyright (C) Carnegie Mellon University
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation --
17
18 Free Software Foundation, Inc.
19 59 Temple Place - Suite 330
20 Boston, MA 02111-1307, USA.
21 *//*
22 by Jacob Shealy, June 25-, 2015
23
24 Generic functions that are used in modeling mixtures, but that do not have
25 anything specifically to do with mixtures.
26 */
27
28 #include "mixture_generics.h"
29 #include "mixture_struct.h"
30
31 double my_min(unsigned nelems, double *nums){
32 unsigned i;
33 double min=nums[0];
34 for(i=1;i<nelems;i++){
35 if(nums[i]<min){
36 min = nums[i];
37 }
38 }
39 return min;
40 }
41
42 double my_max(unsigned nelems, double *nums){
43 unsigned i;
44 double max=nums[0];
45 for(i=1;i<nelems;i++){
46 if(nums[i]>max){
47 max = nums[i];
48 }
49 }
50 return max;
51 }
52
53 double my_sum(unsigned nelems, double *nums){
54 unsigned i;
55 double sum=0.0;
56 for(i=0;i<nelems;i++){
57 sum += nums[i];
58 }
59 return sum;
60 }
61
62 /*
63 Find index of the minimum value in the array `nums', with maximum index
64 `nelems'
65 */
66 unsigned index_of_min(unsigned nelems, double *nums){
67 unsigned i;
68 unsigned min_ix=0; /* the index of the minimum element */
69 double min=nums[0]; /* the minimum element */
70
71 for(i=1;i<nelems;i++){
72 if(nums[i]<min){
73 min_ix = i; /* update both `min' and `min_ix' */
74 min = nums[i];
75 }
76 }
77 return min_ix;
78 }
79
80 /*
81 Find index of the maximum value in the array `nums', with maximum index
82 `nelems'
83 */
84 unsigned index_of_max(unsigned nelems, double *nums){
85 unsigned i;
86 unsigned max_ix=0; /* the index of the minimum element */
87 double max=nums[0]; /* the minimum element */
88
89 for(i=1;i<nelems;i++){
90 if(nums[i]>max){
91 max_ix = i; /* update both `min' and `min_ix' */
92 max = nums[i];
93 }
94 }
95 return max_ix;
96 }
97
98 /*
99 Generic root-finding function that uses the secant method, starting from
100 the positions in `x' and setting the first element of `x' to the position
101 at which `func' equals zero within the given tolerance `tol'
102 */
103 void secant_solve(SecantSubjectFunction *func, void *user_data, double x[2], double tol){
104 #define MAX_ITER 30
105 unsigned i;
106 double y[2];
107 double delta_x;
108
109 y[1] = (*func)(x[1], user_data);
110
111 for(i=0;i<MAX_ITER;i++){
112 y[0] = (*func)(x[0], user_data);
113 if(fabs(y[0])<tol){
114 printf("\n\n\tRoot-finding SUCCEEDED after %u iterations;"
115 "\n\t zeroed function has value %.6g at postion %.6g\n", i, y[0], x[0]);
116 break;
117 }
118 if(x[0]==x[1]){
119 printf("\n\n\tRoot-finding FAILED after %u iterations;"
120 "\n\t independent variables equal at %.6g,"
121 "\n\t function is not zero, but %.6g",
122 i, x[0], y[0]);
123 break;
124 }
125
126 /* update independent variable x[0] */
127 delta_x = -y[0] * (x[0] - x[1])/(y[0] - y[1]);
128 x[1] = x[0]; /* reassign second position to first position */
129 y[1] = y[0];
130 x[0] += delta_x; /* shift first position to more accurate value */
131 }
132 /* puts("\n\tLeaving secant root-finding routine now"); */
133 #undef MAX_ITER
134 }
135

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22