Trisurf Monte Carlo simulator
Samo Penic
2018-12-25 eb7d7ef09dce79ec0c95f8e8779abe963c4043f2
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
/* This is a first attempt in implementation of plugins to Trisurf-ng. */
 
/*
1. Capabilities of Plugin
-------------------------
 
Every plugin should list it's capabilities -- that is what subrutines are available in it. Capabilities list for the moment are:
 
* PLUGIN_PROGRAM_START (requires subrutine void at_start(void)): runs at startup
* PLUGIN_VESICLE_INITIALIZED (requires subrutine void after_vesicle_init(vesicle *vesicle):
    runs after vesicle gets initialized.
 
* PLUGIN_VERTEX_MOVE (requires subrutines:
 
    ts_bool vm_hard_constraint(vesicle *vesicle, vtx *vtx, vtx *old_vtx)
    ts_double vm_energy_before(vesicle *vesicle, vtx *vtx)
    ts_double vm_energy_after(vesicle *vesicle, vtx *vtx)
    ts_double vm_new_state_accepted(vesicle *vesicle, vtx *vtx)
    ts_double vm_new_state_rejected(vesicle *vesicle, vtx *vtx)
 
    )
 
    these subrutines are added into the vertex move into right places
 
* PLUGIN_BOND_FLIP ( N/A yet)
*/
#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),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", filename);
        fatal("Exiting",235);
    }
    ts_plugin_details* (*get_plugin_details)(void) = dlsym(plugin->libhandle, "init");
    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 = dlsym(plugin->libhandle, "vm_energy_before");
    plugin->function->vm_energy_after = dlsym(plugin->libhandle, "vm_energy_after");
    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_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 *));
    for(int i=0;i<number_of_plugins;i++){
        plist->plugin[i]=init_plugin(plugin_filenames[i]);
    }
    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 free_plugin_list(ts_plugin_list *plist){
    for(int i=0;i<plist->n;i++){
        free_plugin(plist->plugin[i]);
    }
    free(plist);
}