Trisurf Monte Carlo simulator
Samo Penic
2018-12-25 eb7d7ef09dce79ec0c95f8e8779abe963c4043f2
First working version of plugins
3 files modified
3 files added
156 ■■■■■ changed files
src/Makefile.am 8 ●●●●● patch | view | raw | blame | history
src/demo_plugin.c 18 ●●●●● patch | view | raw | blame | history
src/general.h 32 ●●●●● patch | view | raw | blame | history
src/main.c 10 ●●●● patch | view | raw | blame | history
src/plugins.c 82 ●●●●● patch | view | raw | blame | history
src/plugins.h 6 ●●●●● patch | view | raw | blame | history
src/Makefile.am
@@ -13,8 +13,10 @@
#------------- LIBS ----------
lib_LTLIBRARIES= libtrisurf.la
libtrisurf_la_SOURCES= general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c dumpstate.c frame.c energy.c timestep.c vertexmove.c bondflip.c poly.c stats.c sh.c shcomplex.c constvol.c snapshot.c restore.c cluster.c
lib_LTLIBRARIES= libtrisurf.la demoplugin.la
libtrisurf_la_SOURCES= general.c vertex.c bond.c triangle.c cell.c vesicle.c initial_distribution.c io.c dumpstate.c frame.c energy.c timestep.c vertexmove.c bondflip.c poly.c stats.c sh.c shcomplex.c constvol.c snapshot.c restore.c cluster.c plugins.c
#libtrisurf_la_CPPFLAGS = ${libxml2_CFLAGS}
libtrisurf_la_LIBADD=${libxml2_LIBS}
pkginclude_HEADERS=general.h vertex.h bond.h triangle.h cell.h vesicle.h initial_distribution.h io.h dumpstate.h frame.h energy.h timestep.h vertexmove.h bondflip.h poly.h stats.h sh.h shcomplex.h constvol.h snapshot.h restore.h cluster.h
pkginclude_HEADERS=general.h vertex.h bond.h triangle.h cell.h vesicle.h initial_distribution.h io.h dumpstate.h frame.h energy.h timestep.h vertexmove.h bondflip.h poly.h stats.h sh.h shcomplex.h constvol.h snapshot.h restore.h cluster.h plugins.h
demoplugin_la_SOURCES= demo_plugin.c
demoplugin_la_LDFLAGS = -module -avoid-version -export-dynamic
src/demo_plugin.c
New file
@@ -0,0 +1,18 @@
#include "plugins.h"
#include <stdlib.h>
#include "general.h"
char plugin_name[] = "First plugin";
char plugin_description[]= "Plugin that shows how plugin should be made";
char plugin_author[] = "SAMO PENIC";
ts_plugin_details *init (){
    ts_fprintf(stdout,"Hello. Plugin %s is initiating.\n\nThis will load the details section of the plugin\n", plugin_name);
    ts_plugin_details *details=(ts_plugin_details *)calloc(1,sizeof(ts_plugin_details));
    details->name = plugin_name;
    return details;
}
void cleanup(){
    ts_fprintf(stdout,"Goodbye from plugin %s. This functions clears what would be created in init...\n",plugin_name);
}
src/general.h
@@ -300,6 +300,37 @@
} ts_tape;
/* plugins */
typedef struct {
    void (*at_start)(void);
    void (*after_vesicle_init)(void *vesicle);
    ts_bool (*vm_hard_constraint)(void *vesicle, ts_vertex *vtx, ts_vertex *odl_vtx);
    ts_double (*vm_energy_before)(void *vesicle, ts_vertex *vtx);
    ts_double (*vm_energy_after)(void *vesicle, ts_vertex *vtx);
    ts_double (*vm_new_state_accepted)(void *vesicle, ts_vertex *vtx);
    ts_double (*vm_new_state_rejected)(void *vesicle, ts_vertex *vtx);
    void (*cleanup)(void);
} ts_plugin_function;
typedef struct {
    ts_char *name;
    ts_char *description;
    ts_char *author;
} ts_plugin_details;
typedef struct {
    ts_plugin_details *details;
    ts_plugin_function *function;
    ts_char *filename;
    void *libhandle;
} ts_plugin;
typedef struct {
    ts_uint n;
    ts_plugin **plugin;
} ts_plugin_list;
/* end plugins */
typedef struct {
@@ -330,6 +361,7 @@
    ts_double nucleus_center[3];
    ts_double area;
    ts_confinement_plane confinement_plane;
    ts_plugin_list plist;
} ts_vesicle;
src/main.c
@@ -17,15 +17,21 @@
#include "shcomplex.h"
#include "dumpstate.h"
#include "restore.h"
#include "plugins.h"
#include <fcntl.h>
/** Entrance function to the program
  * @param argv is a number of parameters used in program call (including the program name
  * @param argc is a pointer to strings (character arrays) which holds the arguments
  * @returns returns 0 on success, any other number on fail.
*/
#include <string.h>
int main(int argv, char *argc[]){
    ts_char *plugin0 = (ts_char *)calloc(255,sizeof(ts_char));
    strcpy(plugin0,"/home/samo/programiranje/trisurf-ng/src/first.so");
    ts_char **plugins=(ts_char **)calloc(1,sizeof(ts_char *));
    *plugins=plugin0;
    ts_plugin_list *plist=init_plugin_list(plugins,1);
    ts_fprintf(stdout, "TRISURF in PRVI PLUGIN %s\n", plist->plugin[0]->details->name);
    ts_vesicle *vesicle;
    ts_tape *tape;
    ts_uint start_iteration=0;
src/plugins.c
New file
@@ -0,0 +1,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);
}
src/plugins.h
New file
@@ -0,0 +1,6 @@
#ifndef _H_PLUGINS
#define _H_PLUGINS
#include "general.h"
ts_plugin *init_plugin(ts_char *filename);
ts_plugin_list *init_plugin_list(ts_char **plugin_filenames, ts_uint number_of_filenames);
#endif