Trisurf Monte Carlo simulator
Samo Penic
2019-03-08 6dab1ac322cfbad8390861456c674922073911b3
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* This is a first attempt in implementation of plugins to Trisurf-ng. */
 
/*
 
Options for tape:
 
extra_plugin_path = "..." (OPTIONAL)
 
vesicle bipyramid {
    nshell=23    
}
 
vesicle vtufile {
    filename="filename"
}
 
vesicle vtudatabase {
    run_id=123
    timestep=999
}
 
vesicle properties {
    xk0=20
    dmax=1.7
    dmin_interspecies=1.2
    stepsize=0.15
}
 
space properties {
    nxmax=60
    nymax=60
    nzmax=60
}
 
loop control {
    inner=1000
    outer=1000
}
 
plugin pressure {
    pressure=10
}
 
plugin constant_volume {
    precision=1e14
}
 
plugin constant_area {
 
}
 
plugin confinement_plate {
    distance=5
    force=10
    coordinate=2 // 0=x,1=y,2=z
}
 
plugin spherical_harmonics {
    lmax=21
    filename="abc.csv"
}
 
plugin spontaneous_curvature {
    c0=2
    N=100
    w=10
    force=3
}
 
plugin filament {
    monomers=100
    xi=10
}
 
plugin polymer {
    polymers = 100
    monomers = 10
    elastic_coefficient=100
    is_internal = true
}
 
plugin nucleus {
    R_nucleus=10
    R_nucleusX=0
    R_nucleusY=0
    R_nucleusZ=0
}
 
*/
#include "plugins.h"
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include "general.h"
 
ts_plugin *init_plugin(ts_char *filename){
    ts_plugin *plugin = (ts_plugin *)calloc(1, sizeof(ts_plugin));
    plugin->filename=(ts_char *)calloc(strlen(filename)+1,sizeof(ts_char));
    strcpy(plugin->filename,filename);
    plugin->libhandle = dlopen(plugin->filename, RTLD_NOW);
    if(!plugin->libhandle){
        ts_fprintf(stderr,"Error loading plugin: %s\n", plugin->filename);
        fatal("Exiting",235);
    }
    ts_plugin_details* (*get_plugin_details)(void) = dlsym(plugin->libhandle, "init");
    if(get_plugin_details==NULL){
        ts_fprintf(stderr,"Plugin %s invalid. No init() function.\n", plugin->filename);
        fatal("Exiting", 236);
    }
    plugin->details = get_plugin_details();
    plugin->function = (ts_plugin_function *)calloc(1,sizeof(ts_plugin_function));
    
    plugin->function->cleanup = dlsym(plugin->libhandle, "cleanup");
    plugin->function->at_start = dlsym(plugin->libhandle, "at_start");
    plugin->function->after_vesicle_init = dlsym(plugin->libhandle, "after_vesicle_init");
    plugin->function->vm_hard_constraint = dlsym(plugin->libhandle, "vm_hard_constraint");
    plugin->function->vm_energy_before_prepare = dlsym(plugin->libhandle, "vm_energy_before_prepare");
    plugin->function->vm_energy_before_execute = dlsym(plugin->libhandle, "vm_energy_before_execute");
    plugin->function->vm_energy_after_prepare = dlsym(plugin->libhandle, "vm_energy_after_prepare");
    plugin->function->vm_energy_after_execute = dlsym(plugin->libhandle, "vm_energy_after_execute");
    plugin->function->vm_before_montecarlo_constraint = dlsym(plugin->libhandle, "vm_before_montecarlo_constraint");
    plugin->function->vm_new_state_rejected = dlsym(plugin->libhandle, "vm_new_state_rejected");
    plugin->function->vm_new_state_accepted = dlsym(plugin->libhandle, "vm_new_state_accepted");
 
    return plugin;
}
 
 
ts_plugin_chain *plugin_to_chain(ts_plugin_chain *chain, ts_plugin *plugin){
    ts_plugin_chain *retchain;
    if(chain!=NULL){
        retchain=chain;
        while(chain->next!=NULL){
            chain=chain->next;
        }
        chain->next=(ts_plugin_chain *)calloc(1,sizeof(ts_plugin_chain));
        chain->next->plugin=plugin;
        return retchain;
    } else {
        chain=(ts_plugin_chain *)calloc(1,sizeof(ts_plugin_chain));
        chain->plugin=plugin;
        return chain;
    }
}
 
 
ts_plugin_list *init_plugin_list(ts_char **plugin_filenames, ts_uint number_of_plugins){
    ts_plugin_list *plist=(ts_plugin_list *)calloc(1, sizeof(ts_plugin_list));
    plist->plugin=(ts_plugin **)calloc(number_of_plugins, sizeof(ts_plugin *));
    plist->chain=(ts_plugin_chains *)calloc(1, sizeof(ts_plugin_chains));
    for(int i=0;i<number_of_plugins;i++){
        plist->plugin[i]=init_plugin(plugin_filenames[i]);
        if(plist->plugin[i]->function->cleanup!=NULL){
            plist->chain->cleanup=plugin_to_chain(plist->chain->cleanup, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->at_start!=NULL){
            plist->chain->at_start=plugin_to_chain(plist->chain->at_start, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->after_vesicle_init!=NULL){
            plist->chain->after_vesicle_init=plugin_to_chain(plist->chain->after_vesicle_init, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->vm_hard_constraint!=NULL){
            plist->chain->vm_hard_constraint=plugin_to_chain(plist->chain->vm_hard_constraint, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->vm_energy_before_prepare!=NULL){
            plist->chain->vm_energy_before_prepare=plugin_to_chain(plist->chain->vm_energy_before_prepare, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->vm_energy_after_execute!=NULL){
            plist->chain->vm_energy_after_execute=plugin_to_chain(plist->chain->vm_energy_after_execute, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->vm_before_montecarlo_constraint!=NULL){
            plist->chain->vm_before_montecarlo_constraint=plugin_to_chain(plist->chain->vm_before_montecarlo_constraint, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->vm_new_state_accepted!=NULL){
            plist->chain->vm_new_state_accepted=plugin_to_chain(plist->chain->vm_new_state_accepted, plist->plugin[i]);
        }
        if(plist->plugin[i]->function->vm_new_state_rejected!=NULL){
            plist->chain->vm_new_state_rejected=plugin_to_chain(plist->chain->vm_new_state_rejected, plist->plugin[i]);
        }
    }
    plist->n=number_of_plugins;
    return plist;
}    
 
void free_plugin(ts_plugin *plugin){
    plugin->function->cleanup();
    free(plugin->function);
    free(plugin->details);
    free(plugin->filename);
    dlclose(plugin->libhandle);
    free(plugin);
}
 
void plugin_list_free(ts_plugin_list *plist){
    for(int i=0;i<plist->n;i++){
        free_plugin(plist->plugin[i]);
    }
    free(plist->chain);
    free(plist);
}